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 all remaining issues Created April 16, 2013, 11:55 a.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
===================================================================
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
+}
« no previous file with comments | « shell/src/Main.cpp ('k') | src/FileReader.cpp » ('j') | src/FileSystemJsObject.cpp » ('J')

Powered by Google App Engine
This is Rietveld