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

Delta Between Two Patch Sets: src/DefaultFileSystem.cpp

Issue 10296001: Implement File API (Closed)
Left Patch Set: Created April 12, 2013, 10:10 a.m.
Right Patch Set: Addressed the new issues Created April 16, 2013, 1:37 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « shell/src/Main.cpp ('k') | src/FileReader.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(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 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld