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: Rebase on master. Last changes. Created July 7, 2017, 1:36 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
« no previous file with comments | « libadblockplus.gyp ('k') | src/FileSystemJsObject.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/DefaultFileSystem.cpp
===================================================================
--- a/src/DefaultFileSystem.cpp
+++ b/src/DefaultFileSystem.cpp
@@ -14,17 +14,19 @@
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
#include <AdblockPlus/DefaultFileSystem.h>
#include <cstdio>
#include <cstring>
#include <fstream>
+#include <sstream>
#include <stdexcept>
+#include <thread>
#include <sys/types.h>
#ifdef _WIN32
#include <windows.h>
#include <Shlobj.h>
#include <Shlwapi.h>
#else
@@ -61,57 +63,57 @@
// file paths as they are.
std::string NormalizePath(const std::string& path)
{
return path;
}
#endif
}
-FileSystem::IOBuffer
-DefaultFileSystem::Read(const std::string& path) const
+IFileSystem::IOBuffer
+DefaultFileSystemSync::Read(const std::string& path) const
{
std::ifstream file(NormalizePath(path).c_str(), std::ios_base::binary);
if (file.fail())
throw RuntimeErrorWithErrno("Failed to open " + path);
file.seekg(0, std::ios_base::end);
auto dataSize = file.tellg();
file.seekg(0, std::ios_base::beg);
- IOBuffer data(dataSize);
+ IFileSystem::IOBuffer data(dataSize);
file.read(reinterpret_cast<std::ifstream::char_type*>(data.data()),
data.size());
return data;
}
-void DefaultFileSystem::Write(const std::string& path,
- const IOBuffer& data)
+void DefaultFileSystemSync::Write(const std::string& path,
+ const IFileSystem::IOBuffer& data)
{
std::ofstream file(NormalizePath(path).c_str(), std::ios_base::out | std::ios_base::binary);
file.write(reinterpret_cast<const std::ofstream::char_type*>(data.data()),
data.size());
}
-void DefaultFileSystem::Move(const std::string& fromPath,
- const std::string& toPath)
+void DefaultFileSystemSync::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::Remove(const std::string& path)
+void DefaultFileSystemSync::Remove(const std::string& path)
{
if (remove(NormalizePath(path).c_str()))
throw RuntimeErrorWithErrno("Failed to remove " + path);
}
-FileSystem::StatResult DefaultFileSystem::Stat(const std::string& path) const
+IFileSystem::StatResult DefaultFileSystemSync::Stat(const std::string& path) const
{
- FileSystem::StatResult result;
+ IFileSystem::StatResult result;
#ifdef WIN32
WIN32_FILE_ATTRIBUTE_DATA data;
if (!GetFileAttributesExW(NormalizePath(path).c_str(), GetFileExInfoStandard, &data))
{
DWORD err = GetLastError();
if (err == ERROR_FILE_NOT_FOUND ||
err == ERROR_PATH_NOT_FOUND ||
err == ERROR_INVALID_DRIVE)
@@ -165,17 +167,17 @@
+ 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
}
-std::string DefaultFileSystem::Resolve(const std::string& path) const
+std::string DefaultFileSystemSync::Resolve(const std::string& path) const
{
if (basePath == "")
{
return path;
}
else
{
#ifdef _WIN32
@@ -188,18 +190,148 @@
}
else
{
return path;
}
}
}
-void DefaultFileSystem::SetBasePath(const std::string& path)
+void DefaultFileSystemSync::SetBasePath(const std::string& path)
{
basePath = path;
if (*basePath.rbegin() == PATH_SEPARATOR)
{
basePath.resize(basePath.size() - 1);
}
}
+DefaultFileSystem::DefaultFileSystem(const FileSystemSyncPtr& syncImpl)
+ : syncImpl(syncImpl)
+{
+}
+
+void DefaultFileSystem::Read(const std::string& path,
+ const ReadCallback& callback) const
+{
+ auto impl = syncImpl;
+ std::thread([impl, path, callback]
+ {
+ std::string error;
+ try
+ {
+ auto data = impl->Read(path);
+ callback(std::move(data), error);
+ return;
+ }
+ catch (std::exception& e)
+ {
+ error = e.what();
+ }
+ catch (...)
+ {
+ error = "Unknown error while reading from " + path;
+ }
+ callback(IOBuffer(), error);
+ }).detach();
+}
+
+void DefaultFileSystem::Write(const std::string& path,
+ const IOBuffer& data,
+ const Callback& callback)
+{
+ auto impl = syncImpl;
+ std::thread([impl, path, data, callback]
+ {
+ std::string error;
+ try
+ {
+ impl->Write(path, data);
+ }
+ catch (std::exception& e)
+ {
+ error = e.what();
+ }
+ catch (...)
+ {
+ error = "Unknown error while writing to " + path;
+ }
+ callback(error);
+ }).detach();
+}
+
+void DefaultFileSystem::Move(const std::string& fromPath,
+ const std::string& toPath,
+ const Callback& callback)
+{
+ auto impl = syncImpl;
+ std::thread([impl, fromPath, toPath, callback]
+ {
+ std::string error;
+ try
+ {
+ impl->Move(fromPath, toPath);
+ }
+ catch (std::exception& e)
+ {
+ error = e.what();
+ }
+ catch (...)
+ {
+ error = "Unknown error while moving " + fromPath + " to " + toPath;
+ }
+ callback(error);
+ }).detach();
+}
+
+void DefaultFileSystem::Remove(const std::string& path,
+ const Callback& callback)
+{
+ auto impl = syncImpl;
+ std::thread([impl, path, callback]
+ {
+ std::string error;
+ try
+ {
+ impl->Remove(path);
+ }
+ catch (std::exception& e)
+ {
+ error = e.what();
+ }
+ catch (...)
+ {
+ error = "Unknown error while removing " + path;
+ }
+ callback(error);
+ }).detach();
+}
+
+void DefaultFileSystem::Stat(const std::string& path,
+ const StatCallback& callback) const
+{
+ auto impl = syncImpl;
+ std::thread([impl, path, callback]
+ {
+ std::string error;
+ try
+ {
+ auto result = impl->Stat(path);
+ callback(result, error);
+ return;
+ }
+ catch (std::exception& e)
+ {
+ error = e.what();
+ }
+ catch (...)
+ {
+ error = "Unknown error while calling stat on " + path;
+ }
+ callback(StatResult(), error);
+ }).detach();
+}
+
+std::string DefaultFileSystem::Resolve(const std::string& path) const
+{
+ return syncImpl->Resolve(path);
+}
« no previous file with comments | « libadblockplus.gyp ('k') | src/FileSystemJsObject.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld