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