Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/DefaultFileSystem.cpp

Issue 29449592: Issue 5183 - Provide async interface for FileSystem (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Created May 26, 2017, 12:43 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/DefaultFileSystem.cpp
===================================================================
--- a/src/DefaultFileSystem.cpp
+++ b/src/DefaultFileSystem.cpp
@@ -70,36 +70,84 @@
DefaultFileSystem::Read(const std::string& path) const
{
std::shared_ptr<std::istream> result(new std::ifstream(NormalizePath(path).c_str()));
if (result->fail())
throw RuntimeErrorWithErrno("Failed to open " + path);
return result;
}
+void DefaultFileSystem::Read(const std::string& path,
+ const ReadCallback& callback) const
+{
+ try
+ {
+ auto result = Read(path);
+ callback(result);
+ }
+ catch (...)
+ {
+ }
+}
+
void DefaultFileSystem::Write(const std::string& path,
std::istream& data)
{
std::ofstream file(NormalizePath(path).c_str(), std::ios_base::out | std::ios_base::binary);
file << Utils::Slurp(data);
}
+void DefaultFileSystem::Write(const std::string& path,
+ std::istream& data,
+ const Callback& callback)
+{
+ Write(path, data);
+ callback();
+}
+
void DefaultFileSystem::Move(const std::string& fromPath,
const std::string& toPath)
{
if (rename(NormalizePath(fromPath).c_str(), NormalizePath(toPath).c_str()))
throw RuntimeErrorWithErrno("Failed to move " + fromPath + " to " + toPath);
}
+void DefaultFileSystem::Move(const std::string& fromPath,
+ const std::string& toPath,
+ const Callback& callback)
+{
+ try
+ {
+ Move(fromPath, toPath);
+ callback();
+ }
+ catch (...)
+ {
+ }
+}
+
void DefaultFileSystem::Remove(const std::string& path)
{
if (remove(NormalizePath(path).c_str()))
throw RuntimeErrorWithErrno("Failed to remove " + path);
}
+void DefaultFileSystem::Remove(const std::string& path,
+ const Callback& callback)
+{
+ try
+ {
+ Remove(path);
+ callback();
+ }
+ catch (...)
+ {
+ }
+}
+
FileSystem::StatResult DefaultFileSystem::Stat(const std::string& path) const
{
FileSystem::StatResult result;
#ifdef WIN32
WIN32_FILE_ATTRIBUTE_DATA data;
if (!GetFileAttributesExW(NormalizePath(path).c_str(), GetFileExInfoStandard, &data))
{
DWORD err = GetLastError();
@@ -156,16 +204,29 @@
+ static_cast<int64_t>(nativeStat.st_mtim.tv_nsec) / NSEC_IN_MSEC;
#else
result.lastModified = static_cast<int64_t>(nativeStat.st_mtime) * MSEC_IN_SEC;
#endif
return result;
#endif
}
+void DefaultFileSystem::Stat(const std::string& path,
+ const StatCallback& callback) const
+{
+ try
+ {
+ auto stats = Stat(path);
+ callback(stats);
+ }
+ catch (...)
+ {
+ }
+}
+
std::string DefaultFileSystem::Resolve(const std::string& path) const
{
if (basePath == "")
{
return path;
}
else
{
@@ -178,16 +239,21 @@
return basePath + PATH_SEPARATOR + path;
}
else
{
return path;
}
}
}
+void DefaultFileSystem::Resolve(const std::string& path,
+ const ResolveCallback& callback) const
+{
+ callback(Resolve(path));
+}
void DefaultFileSystem::SetBasePath(const std::string& path)
{
basePath = path;
if (*basePath.rbegin() == PATH_SEPARATOR)
{
basePath.resize(basePath.size() - 1);

Powered by Google App Engine
This is Rietveld