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: Created April 24, 2013, 2:10 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 #ifdef WIN32 95 if (basePath == "")
89 // Resolve to LocalLow folder 96 {
90 wchar_t* resolvedPath; 97 return path;
91 HRESULT hr = SHGetKnownFolderPath(FOLDERID_LocalAppDataLow, 0, NULL, &resolved Path); 98 }
92 if (FAILED(hr)) 99 else
93 return std::string(path); 100 {
Wladimir Palant 2013/04/24 09:17:35 Why not just return path; here?
94 std::wstring resolvedW(resolvedPath); 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
95 CoTaskMemFree(resolvedPath); 102 {
96 103 return basePath + PATH_SEPARATOR + path;
97 // TODO: Better conversion here 104 }
98 std::string resolved(resolvedW.begin(), resolvedW.end()); 105 else
Wladimir Palant 2013/04/24 09:17:35 Please use Utils::ToUTF8String() here (and we are
99 resolved.append("\\AdblockPlus\\"); 106 {
100 resolved.append(path); 107 return path;
101 return resolved; 108 }
102 #else 109 }
103 return std::string(path);
Wladimir Palant 2013/04/24 09:17:35 Why not just return path; here?
104 #endif
105 } 110 }
106 111
112 void DefaultFileSystem::SetBasePath(const std::string& path)
113 {
114 basePath = path;
115
116 if ((*basePath.rbegin() == PATH_SEPARATOR))
Wladimir Palant 2013/05/06 06:18:30 Nit: You seem to have an extra pair parentheses he
117 {
118 basePath.pop_back();
119 }
120 }
121
LEFTRIGHT

Powered by Google App Engine
This is Rietveld