Index: src/DefaultFileSystem.cpp |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/src/DefaultFileSystem.cpp |
@@ -0,0 +1,62 @@ |
+#include <AdblockPlus/DefaultFileSystem.h> |
Wladimir Palant
2013/04/16 13:03:41
A general concern with this API: what about non-AS
Felix Dahlke
2013/04/16 14:00:09
Which functions in particular? rename and remove b
Wladimir Palant
2013/04/16 14:10:13
I suspect that all of cstdio and fstream maps to A
|
+#include <cstdio> |
+#include <fstream> |
+#include <stdexcept> |
+ |
+#ifndef WIN32 |
+#include <cerrno> |
+#include <sys/stat.h> |
+#endif |
+ |
+#include "../src/Utils.h" |
+ |
+using namespace AdblockPlus; |
+ |
+std::tr1::shared_ptr<std::istream> |
+DefaultFileSystem::Read(const std::string& path) const |
Wladimir Palant
2013/04/16 13:03:41
Is this expecting an absolute path? I'm not sure w
Felix Dahlke
2013/04/16 14:00:09
This supports both relative and absolute paths, I'
|
+{ |
+ std::ifstream* file = new std::ifstream; |
+ file->open(path.c_str()); |
Wladimir Palant
2013/04/16 13:03:41
Why not open the file in the constructor already?
Felix Dahlke
2013/04/16 14:00:09
Done.
|
+ return std::tr1::shared_ptr<std::istream>(file); |
+} |
+ |
+void DefaultFileSystem::Write(const std::string& path, |
+ std::tr1::shared_ptr<std::ostream> data) |
+{ |
+ std::ofstream file; |
+ file.open(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 std::runtime_error("Failed to move " + fromPath + " to " + toPath); |
Wladimir Palant
2013/04/16 13:03:41
How about adding strerror(errno) to the error stri
Felix Dahlke
2013/04/16 14:00:09
Done.
|
+} |
+ |
+void DefaultFileSystem::Remove(const std::string& path) |
+{ |
+ if (remove(path.c_str())) |
+ throw std::runtime_error("Failed to remove " + path); |
Wladimir Palant
2013/04/16 13:03:41
Same here, how about adding strerror(errno) to the
Felix Dahlke
2013/04/16 14:00:09
Done.
|
+} |
+ |
+FileSystem::StatResult DefaultFileSystem::Stat(const std::string& path) const |
+{ |
+#ifdef WIN32 |
+ throw std::runtime_error("Stat is not implemented on Windows"); |
Wladimir Palant
2013/04/16 13:03:41
Why not use _stat64 on Windows? You have the right
Felix Dahlke
2013/04/16 14:00:09
I really didn't expect Windows to support POSIX st
|
+#else |
+ struct stat nativeStat; |
+ if (stat(path.c_str(), &nativeStat)) { |
+ if (errno == ENOENT) |
+ return FileSystem::StatResult(); |
+ throw std::runtime_error("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; |
+#endif |
+} |