LEFT | RIGHT |
(no file at all) | |
| 1 #include <AdblockPlus/DefaultFileSystem.h> |
| 2 #include <cstdio> |
| 3 #include <fstream> |
| 4 #include <stdexcept> |
| 5 |
| 6 #ifndef WIN32 |
| 7 #include <cerrno> |
| 8 #include <sys/stat.h> |
| 9 #endif |
| 10 |
| 11 #include "../src/Utils.h" |
| 12 |
| 13 using namespace AdblockPlus; |
| 14 |
| 15 namespace |
| 16 { |
| 17 class RuntimeErrorWithErrno : public std::runtime_error |
| 18 { |
| 19 public: |
| 20 explicit RuntimeErrorWithErrno(const std::string& message) |
| 21 : std::runtime_error(message + " (" + strerror(errno) + ")") |
| 22 { |
| 23 } |
| 24 }; |
| 25 } |
| 26 |
| 27 std::tr1::shared_ptr<std::istream> |
| 28 DefaultFileSystem::Read(const std::string& path) const |
| 29 { |
| 30 return std::tr1::shared_ptr<std::istream>(new std::ifstream(path.c_str())); |
| 31 } |
| 32 |
| 33 void DefaultFileSystem::Write(const std::string& path, |
| 34 std::tr1::shared_ptr<std::ostream> data) |
| 35 { |
| 36 std::ofstream file(path.c_str()); |
| 37 file << Utils::Slurp(*data); |
| 38 } |
| 39 |
| 40 void DefaultFileSystem::Move(const std::string& fromPath, |
| 41 const std::string& toPath) |
| 42 { |
| 43 if (rename(fromPath.c_str(), toPath.c_str())) |
| 44 throw RuntimeErrorWithErrno("Failed to move " + fromPath + " to " + toPath); |
| 45 } |
| 46 |
| 47 void DefaultFileSystem::Remove(const std::string& path) |
| 48 { |
| 49 if (remove(path.c_str())) |
| 50 throw RuntimeErrorWithErrno("Failed to remove " + path); |
| 51 } |
| 52 |
| 53 FileSystem::StatResult DefaultFileSystem::Stat(const std::string& path) const |
| 54 { |
| 55 #ifdef WIN32 |
| 56 struct _stat64 nativeStat; |
| 57 const int failure = _stat64(path.c_str(), &nativeStat); |
| 58 #else |
| 59 struct stat64 nativeStat; |
| 60 const int failure = stat64(path.c_str(), &nativeStat); |
| 61 #endif |
| 62 if (failure) { |
| 63 if (errno == ENOENT) |
| 64 return FileSystem::StatResult(); |
| 65 throw RuntimeErrorWithErrno("Unable to stat " + path); |
| 66 } |
| 67 FileSystem::StatResult result; |
| 68 result.exists = true; |
| 69 result.isFile = S_ISREG(nativeStat.st_mode); |
| 70 result.isDirectory = S_ISDIR(nativeStat.st_mode); |
| 71 result.lastModified = nativeStat.st_mtime; |
| 72 return result; |
| 73 } |
LEFT | RIGHT |