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

Unified Diff: src/DefaultFileSystem.cpp

Issue 10296001: Implement File API (Closed)
Patch Set: Addressed the new issues Created April 16, 2013, 1:37 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 | « shell/src/Main.cpp ('k') | src/FileReader.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/DefaultFileSystem.cpp
===================================================================
new file mode 100644
--- /dev/null
+++ b/src/DefaultFileSystem.cpp
@@ -0,0 +1,73 @@
+#include <AdblockPlus/DefaultFileSystem.h>
+#include <cstdio>
+#include <fstream>
+#include <stdexcept>
+
+#ifndef WIN32
+#include <cerrno>
+#include <sys/stat.h>
+#endif
+
+#include "../src/Utils.h"
+
+using namespace AdblockPlus;
+
+namespace
+{
+ class RuntimeErrorWithErrno : public std::runtime_error
+ {
+ public:
+ explicit RuntimeErrorWithErrno(const std::string& message)
+ : std::runtime_error(message + " (" + strerror(errno) + ")")
+ {
+ }
+ };
+}
+
+std::tr1::shared_ptr<std::istream>
+DefaultFileSystem::Read(const std::string& path) const
+{
+ return std::tr1::shared_ptr<std::istream>(new std::ifstream(path.c_str()));
+}
+
+void DefaultFileSystem::Write(const std::string& path,
+ std::tr1::shared_ptr<std::ostream> data)
+{
+ std::ofstream file(path.c_str());
+ file << Utils::Slurp(*data);
+}
+
+void DefaultFileSystem::Move(const std::string& fromPath,
+ const std::string& toPath)
+{
+ if (rename(fromPath.c_str(), toPath.c_str()))
+ throw RuntimeErrorWithErrno("Failed to move " + fromPath + " to " + toPath);
+}
+
+void DefaultFileSystem::Remove(const std::string& path)
+{
+ if (remove(path.c_str()))
+ throw RuntimeErrorWithErrno("Failed to remove " + path);
+}
+
+FileSystem::StatResult DefaultFileSystem::Stat(const std::string& path) const
+{
+#ifdef WIN32
+ struct _stat64 nativeStat;
+ const int failure = _stat64(path.c_str(), &nativeStat);
+#else
+ struct stat64 nativeStat;
+ const int failure = stat64(path.c_str(), &nativeStat);
+#endif
+ if (failure) {
+ if (errno == ENOENT)
+ return FileSystem::StatResult();
+ throw RuntimeErrorWithErrno("Unable to stat " + path);
+ }
+ FileSystem::StatResult result;
+ result.exists = true;
+ result.isFile = S_ISREG(nativeStat.st_mode);
+ result.isDirectory = S_ISDIR(nativeStat.st_mode);
+ result.lastModified = nativeStat.st_mtime;
+ return result;
+}
« no previous file with comments | « shell/src/Main.cpp ('k') | src/FileReader.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld