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

Delta Between Two Patch Sets: src/DefaultFileSystem.cpp

Issue 10369004: File system path resolving (Closed)
Left Patch Set: Comments addressed Created April 30, 2013, 8:09 a.m.
Right Patch Set: Addressing comments Created May 5, 2013, 10:49 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « include/AdblockPlus/FileSystem.h ('k') | test/BaseJsTest.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #include <AdblockPlus/DefaultFileSystem.h> 1 #include <AdblockPlus/DefaultFileSystem.h>
2 #include <cstdio> 2 #include <cstdio>
3 #include <cstring> 3 #include <cstring>
4 #include <fstream> 4 #include <fstream>
5 #include <stdexcept> 5 #include <stdexcept>
6 6
7 #include <cerrno> 7 #include <cerrno>
8 #include <sys/types.h> 8 #include <sys/types.h>
9 #include <sys/stat.h> 9 #include <sys/stat.h>
10 10
11 #ifdef _WIN32 11 #ifdef _WIN32
12 #ifndef S_ISDIR 12 #ifndef S_ISDIR
13 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) 13 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
14 #include <Shlobj.h> 14 #include <Shlobj.h>
15 #include <Shlwapi.h>
15 #endif 16 #endif
16 17
17 #ifndef S_ISREG 18 #ifndef S_ISREG
18 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) 19 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
19 #endif 20 #endif
20 #endif 21 #endif
21 22
22 #include "../src/Utils.h" 23 #include "../src/Utils.h"
23 24
24 using namespace AdblockPlus; 25 using namespace AdblockPlus;
(...skipping 29 matching lines...) Expand all
54 if (rename(fromPath.c_str(), toPath.c_str())) 55 if (rename(fromPath.c_str(), toPath.c_str()))
55 throw RuntimeErrorWithErrno("Failed to move " + fromPath + " to " + toPath); 56 throw RuntimeErrorWithErrno("Failed to move " + fromPath + " to " + toPath);
56 } 57 }
57 58
58 void DefaultFileSystem::Remove(const std::string& path) 59 void DefaultFileSystem::Remove(const std::string& path)
59 { 60 {
60 if (remove(path.c_str())) 61 if (remove(path.c_str()))
61 throw RuntimeErrorWithErrno("Failed to remove " + path); 62 throw RuntimeErrorWithErrno("Failed to remove " + path);
62 } 63 }
63 64
65 /*
66 * In order to get millisecond resolution for modification times, it's necessary to use the 'stat' structure defined in
67 * POSIX 2008, which has 'struct timespec st_mtim' instead of 'time_t st_mtime'. Use "#define _POSIX_C_SOURCE 200809L"
68 * before the headers to invoke. The trouble is that not all systems may have th is available, a category that includes
69 * MS Windows, and so we'll need multiple implementations.
70 */
64 FileSystem::StatResult DefaultFileSystem::Stat(const std::string& path) const 71 FileSystem::StatResult DefaultFileSystem::Stat(const std::string& path) const
65 { 72 {
66 #ifdef WIN32 73 #ifdef WIN32
67 struct _stat nativeStat; 74 struct _stat nativeStat;
68 const int failure = _stat(path.c_str(), &nativeStat); 75 const int failure = _stat(path.c_str(), &nativeStat);
69 #else 76 #else
70 struct stat nativeStat; 77 struct stat nativeStat;
71 const int failure = stat(path.c_str(), &nativeStat); 78 const int failure = stat(path.c_str(), &nativeStat);
72 #endif 79 #endif
73 if (failure) { 80 if (failure) {
74 if (errno == ENOENT) 81 if (errno == ENOENT)
75 return FileSystem::StatResult(); 82 return FileSystem::StatResult();
76 throw RuntimeErrorWithErrno("Unable to stat " + path); 83 throw RuntimeErrorWithErrno("Unable to stat " + path);
77 } 84 }
78 FileSystem::StatResult result; 85 FileSystem::StatResult result;
79 result.exists = true; 86 result.exists = true;
80 result.isFile = S_ISREG(nativeStat.st_mode); 87 result.isFile = S_ISREG(nativeStat.st_mode);
81 result.isDirectory = S_ISDIR(nativeStat.st_mode); 88 result.isDirectory = S_ISDIR(nativeStat.st_mode);
82 result.lastModified = static_cast<int64_t>(nativeStat.st_mtime) * 1000; 89 result.lastModified = static_cast<int64_t>(nativeStat.st_mtime) * 1000;
83 return result; 90 return result;
84 } 91 }
85 92
86 std::string DefaultFileSystem::Resolve(const std::string& path) const 93 std::string DefaultFileSystem::Resolve(const std::string& path) const
87 { 94 {
88 return basePath + "\\" + path; 95 if (basePath == "")
Felix Dahlke 2013/04/30 09:14:52 \\ won't work on UNIX, but I think / should work e
Wladimir Palant 2013/04/30 09:29:50 1) This path separator is Windows-specific. Please
96 {
97 return path;
98 }
99 else
100 {
101 if (PathIsRelative(Utils::ToUTF16String(path, path.length()).c_str()))
Wladimir Palant 2013/05/06 06:18:30 This is Windows-specific and won't compile on othe
Wladimir Palant 2013/05/06 06:19:51 Oops, exactly the other way round of course: i
102 {
103 return basePath + PATH_SEPARATOR + path;
104 }
105 else
106 {
107 return path;
108 }
109 }
89 } 110 }
90 111
91 void DefaultFileSystem::SetBasePath(const std::string& path) 112 void DefaultFileSystem::SetBasePath(const std::string& path)
92 { 113 {
114 basePath = path;
93 115
94 if (path.rfind('\\') == (path.length() - 1)) 116 if ((*basePath.rbegin() == PATH_SEPARATOR))
Felix Dahlke 2013/04/30 09:14:52 How about if (*path.rbegin() == '\\')? This also
Wladimir Palant 2013/04/30 09:29:50 Please search for PATH_SEPARATOR here as well, not
Wladimir Palant 2013/05/06 06:18:30 Nit: You seem to have an extra pair parentheses he
95 { 117 {
96 basePath = path.substr(0, path.length() - 1); 118 basePath.pop_back();
Felix Dahlke 2013/04/30 09:14:52 Never used this, but basePath.pop_back(); seems to
97 }
98 else
99 {
100 basePath = path;
101 } 119 }
102 } 120 }
103 121
LEFTRIGHT

Powered by Google App Engine
This is Rietveld