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

Side by Side Diff: src/DefaultFileSystem.cpp

Issue 10296001: Implement File API (Closed)
Patch Set: Addressed all remaining issues Created April 16, 2013, 11:55 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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>
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 std::tr1::shared_ptr<std::istream>
16 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 {
18 std::ifstream* file = new std::ifstream;
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 }
22
23 void DefaultFileSystem::Write(const std::string& path,
24 std::tr1::shared_ptr<std::ostream> data)
25 {
26 std::ofstream file;
27 file.open(path.c_str());
28 file << Utils::Slurp(*data);
29 }
30
31 void DefaultFileSystem::Move(const std::string& fromPath,
32 const std::string& toPath)
33 {
34 if (rename(fromPath.c_str(), toPath.c_str()))
35 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.
36 }
37
38 void DefaultFileSystem::Remove(const std::string& path)
39 {
40 if (remove(path.c_str()))
41 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.
42 }
43
44 FileSystem::StatResult DefaultFileSystem::Stat(const std::string& path) const
45 {
46 #ifdef WIN32
47 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
48 #else
49 struct stat nativeStat;
50 if (stat(path.c_str(), &nativeStat)) {
51 if (errno == ENOENT)
52 return FileSystem::StatResult();
53 throw std::runtime_error("Unable to stat " + path);
54 }
55 FileSystem::StatResult result;
56 result.exists = true;
57 result.isFile = S_ISREG(nativeStat.st_mode);
58 result.isDirectory = S_ISDIR(nativeStat.st_mode);
59 result.lastModified = nativeStat.st_mtime;
60 return result;
61 #endif
62 }
OLDNEW
« 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