| OLD | NEW | 
|---|
| 1 /* | 1 /* | 
| 2  * This file is part of Adblock Plus <https://adblockplus.org/>, | 2  * This file is part of Adblock Plus <https://adblockplus.org/>, | 
| 3  * Copyright (C) 2006-2017 eyeo GmbH | 3  * Copyright (C) 2006-2017 eyeo GmbH | 
| 4  * | 4  * | 
| 5  * Adblock Plus is free software: you can redistribute it and/or modify | 5  * Adblock Plus is free software: you can redistribute it and/or modify | 
| 6  * it under the terms of the GNU General Public License version 3 as | 6  * it under the terms of the GNU General Public License version 3 as | 
| 7  * published by the Free Software Foundation. | 7  * published by the Free Software Foundation. | 
| 8  * | 8  * | 
| 9  * Adblock Plus is distributed in the hope that it will be useful, | 9  * Adblock Plus is distributed in the hope that it will be useful, | 
| 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 117     if (err == ERROR_FILE_NOT_FOUND || | 117     if (err == ERROR_FILE_NOT_FOUND || | 
| 118         err == ERROR_PATH_NOT_FOUND || | 118         err == ERROR_PATH_NOT_FOUND || | 
| 119         err == ERROR_INVALID_DRIVE) | 119         err == ERROR_INVALID_DRIVE) | 
| 120     { | 120     { | 
| 121       return result; | 121       return result; | 
| 122     } | 122     } | 
| 123     throw RuntimeErrorWithErrno("Unable to stat " + path); | 123     throw RuntimeErrorWithErrno("Unable to stat " + path); | 
| 124   } | 124   } | 
| 125 | 125 | 
| 126   result.exists = true; | 126   result.exists = true; | 
| 127   if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |  | 
| 128   { |  | 
| 129     result.isFile = false; |  | 
| 130     result.isDirectory = true; |  | 
| 131   } |  | 
| 132   else |  | 
| 133   { |  | 
| 134     result.isFile = true; |  | 
| 135     result.isDirectory = false; |  | 
| 136   } |  | 
| 137 | 127 | 
| 138   // See http://support.microsoft.com/kb/167296 on this conversion | 128   // See http://support.microsoft.com/kb/167296 on this conversion | 
| 139   #define FILE_TIME_TO_UNIX_EPOCH_OFFSET 116444736000000000LL | 129   #define FILE_TIME_TO_UNIX_EPOCH_OFFSET 116444736000000000LL | 
| 140   #define FILE_TIME_TO_MILLISECONDS_FACTOR 10000 | 130   #define FILE_TIME_TO_MILLISECONDS_FACTOR 10000 | 
| 141   ULARGE_INTEGER time; | 131   ULARGE_INTEGER time; | 
| 142   time.LowPart = data.ftLastWriteTime.dwLowDateTime; | 132   time.LowPart = data.ftLastWriteTime.dwLowDateTime; | 
| 143   time.HighPart = data.ftLastWriteTime.dwHighDateTime; | 133   time.HighPart = data.ftLastWriteTime.dwHighDateTime; | 
| 144   result.lastModified = (time.QuadPart - FILE_TIME_TO_UNIX_EPOCH_OFFSET) / | 134   result.lastModified = (time.QuadPart - FILE_TIME_TO_UNIX_EPOCH_OFFSET) / | 
| 145       FILE_TIME_TO_MILLISECONDS_FACTOR; | 135       FILE_TIME_TO_MILLISECONDS_FACTOR; | 
| 146   return result; | 136   return result; | 
| 147 #else | 137 #else | 
| 148   struct stat nativeStat; | 138   struct stat nativeStat; | 
| 149   const int failure = stat(NormalizePath(path).c_str(), &nativeStat); | 139   const int failure = stat(NormalizePath(path).c_str(), &nativeStat); | 
| 150   if (failure) | 140   if (failure) | 
| 151   { | 141   { | 
| 152     if (errno == ENOENT) | 142     if (errno == ENOENT) | 
| 153       return result; | 143       return result; | 
| 154     throw RuntimeErrorWithErrno("Unable to stat " + path); | 144     throw RuntimeErrorWithErrno("Unable to stat " + path); | 
| 155   } | 145   } | 
| 156   result.exists = true; | 146   result.exists = true; | 
| 157   result.isFile = S_ISREG(nativeStat.st_mode); |  | 
| 158   result.isDirectory = S_ISDIR(nativeStat.st_mode); |  | 
| 159 | 147 | 
| 160   #define MSEC_IN_SEC 1000 | 148   #define MSEC_IN_SEC 1000 | 
| 161   #define NSEC_IN_MSEC 1000000 | 149   #define NSEC_IN_MSEC 1000000 | 
| 162   // Note: _POSIX_C_SOURCE macro is defined automatically on Linux due to g++ | 150   // Note: _POSIX_C_SOURCE macro is defined automatically on Linux due to g++ | 
| 163   // defining _GNU_SOURCE macro. On OS X we still fall back to the "no | 151   // defining _GNU_SOURCE macro. On OS X we still fall back to the "no | 
| 164   // milliseconds" branch, it has st_mtimespec instead of st_mtim. | 152   // milliseconds" branch, it has st_mtimespec instead of st_mtim. | 
| 165 #if _POSIX_C_SOURCE >= 200809L | 153 #if _POSIX_C_SOURCE >= 200809L | 
| 166   result.lastModified = static_cast<int64_t>(nativeStat.st_mtim.tv_sec) * MSEC_I
     N_SEC | 154   result.lastModified = static_cast<int64_t>(nativeStat.st_mtim.tv_sec) * MSEC_I
     N_SEC | 
| 167                       +  static_cast<int64_t>(nativeStat.st_mtim.tv_nsec) / NSEC
     _IN_MSEC; | 155                       +  static_cast<int64_t>(nativeStat.st_mtim.tv_nsec) / NSEC
     _IN_MSEC; | 
| 168 #else | 156 #else | 
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 203   { | 191   { | 
| 204     basePath.resize(basePath.size() - 1); | 192     basePath.resize(basePath.size() - 1); | 
| 205   } | 193   } | 
| 206 } | 194 } | 
| 207 | 195 | 
| 208 DefaultFileSystem::DefaultFileSystem(const Scheduler& scheduler, std::unique_ptr
     <DefaultFileSystemSync> syncImpl) | 196 DefaultFileSystem::DefaultFileSystem(const Scheduler& scheduler, std::unique_ptr
     <DefaultFileSystemSync> syncImpl) | 
| 209   : scheduler(scheduler), syncImpl(std::move(syncImpl)) | 197   : scheduler(scheduler), syncImpl(std::move(syncImpl)) | 
| 210 { | 198 { | 
| 211 } | 199 } | 
| 212 | 200 | 
| 213 void DefaultFileSystem::Read(const std::string& path, | 201 void DefaultFileSystem::Read(const std::string& fileName, | 
| 214                              const ReadCallback& callback) const | 202                              const ReadCallback& callback) const | 
| 215 { | 203 { | 
| 216   scheduler([this, path, callback] | 204   scheduler([this, fileName, callback] | 
| 217   { | 205   { | 
| 218     std::string error; | 206     std::string error; | 
| 219     try | 207     try | 
| 220     { | 208     { | 
| 221       auto data = syncImpl->Read(path); | 209       auto data = syncImpl->Read(Resolve(fileName)); | 
| 222       callback(std::move(data), error); | 210       callback(std::move(data), error); | 
| 223       return; | 211       return; | 
| 224     } | 212     } | 
| 225     catch (std::exception& e) | 213     catch (std::exception& e) | 
| 226     { | 214     { | 
| 227       error = e.what(); | 215       error = e.what(); | 
| 228     } | 216     } | 
| 229     catch (...) | 217     catch (...) | 
| 230     { | 218     { | 
| 231       error =  "Unknown error while reading from " + path; | 219       error =  "Unknown error while reading from " + fileName + " as " + Resolve
     (fileName); | 
| 232     } | 220     } | 
| 233     callback(IOBuffer(), error); | 221     callback(IOBuffer(), error); | 
| 234   }); | 222   }); | 
| 235 } | 223 } | 
| 236 | 224 | 
| 237 void DefaultFileSystem::Write(const std::string& path, | 225 void DefaultFileSystem::Write(const std::string& fileName, | 
| 238                               const IOBuffer& data, | 226                               const IOBuffer& data, | 
| 239                               const Callback& callback) | 227                               const Callback& callback) | 
| 240 { | 228 { | 
| 241   scheduler([this, path, data, callback] | 229   scheduler([this, fileName, data, callback] | 
| 242   { | 230   { | 
| 243     std::string error; | 231     std::string error; | 
| 244     try | 232     try | 
| 245     { | 233     { | 
| 246       syncImpl->Write(path, data); | 234       syncImpl->Write(Resolve(fileName), data); | 
| 247     } | 235     } | 
| 248     catch (std::exception& e) | 236     catch (std::exception& e) | 
| 249     { | 237     { | 
| 250       error = e.what(); | 238       error = e.what(); | 
| 251     } | 239     } | 
| 252     catch (...) | 240     catch (...) | 
| 253     { | 241     { | 
| 254       error = "Unknown error while writing to " + path; | 242       error = "Unknown error while writing to " + fileName + " as " + Resolve(fi
     leName); | 
| 255     } | 243     } | 
| 256     callback(error); | 244     callback(error); | 
| 257   }); | 245   }); | 
| 258 } | 246 } | 
| 259 | 247 | 
| 260 void DefaultFileSystem::Move(const std::string& fromPath, | 248 void DefaultFileSystem::Move(const std::string& fromFileName, | 
| 261                              const std::string& toPath, | 249                              const std::string& toFileName, | 
| 262                              const Callback& callback) | 250                              const Callback& callback) | 
| 263 { | 251 { | 
| 264   scheduler([this, fromPath, toPath, callback] | 252   scheduler([this, fromFileName, toFileName, callback] | 
| 265   { | 253   { | 
| 266     std::string error; | 254     std::string error; | 
| 267     try | 255     try | 
| 268     { | 256     { | 
| 269       syncImpl->Move(fromPath, toPath); | 257       syncImpl->Move(Resolve(fromFileName), Resolve(toFileName)); | 
| 270     } | 258     } | 
| 271     catch (std::exception& e) | 259     catch (std::exception& e) | 
| 272     { | 260     { | 
| 273       error = e.what(); | 261       error = e.what(); | 
| 274     } | 262     } | 
| 275     catch (...) | 263     catch (...) | 
| 276     { | 264     { | 
| 277       error = "Unknown error while moving " + fromPath + " to " + toPath; | 265       error = "Unknown error while moving " + fromFileName + " to " + toFileName
     ; | 
| 278     } | 266     } | 
| 279     callback(error); | 267     callback(error); | 
| 280   }); | 268   }); | 
| 281 } | 269 } | 
| 282 | 270 | 
| 283 void DefaultFileSystem::Remove(const std::string& path, | 271 void DefaultFileSystem::Remove(const std::string& fileName, | 
| 284                                const Callback& callback) | 272                                const Callback& callback) | 
| 285 { | 273 { | 
| 286   scheduler([this, path, callback] | 274   scheduler([this, fileName, callback] | 
| 287   { | 275   { | 
| 288     std::string error; | 276     std::string error; | 
| 289     try | 277     try | 
| 290     { | 278     { | 
| 291       syncImpl->Remove(path); | 279       syncImpl->Remove(Resolve(fileName)); | 
| 292     } | 280     } | 
| 293     catch (std::exception& e) | 281     catch (std::exception& e) | 
| 294     { | 282     { | 
| 295       error = e.what(); | 283       error = e.what(); | 
| 296     } | 284     } | 
| 297     catch (...) | 285     catch (...) | 
| 298     { | 286     { | 
| 299       error = "Unknown error while removing " + path; | 287       error = "Unknown error while removing " + fileName + " as " + Resolve(file
     Name); | 
| 300     } | 288     } | 
| 301     callback(error); | 289     callback(error); | 
| 302   }); | 290   }); | 
| 303 } | 291 } | 
| 304 | 292 | 
| 305 void DefaultFileSystem::Stat(const std::string& path, | 293 void DefaultFileSystem::Stat(const std::string& fileName, | 
| 306                              const StatCallback& callback) const | 294                              const StatCallback& callback) const | 
| 307 { | 295 { | 
| 308   scheduler([this, path, callback] | 296   scheduler([this, fileName, callback] | 
| 309   { | 297   { | 
| 310     std::string error; | 298     std::string error; | 
| 311     try | 299     try | 
| 312     { | 300     { | 
| 313       auto result = syncImpl->Stat(path); | 301       auto result = syncImpl->Stat(Resolve(fileName)); | 
| 314       callback(result, error); | 302       callback(result, error); | 
| 315       return; | 303       return; | 
| 316     } | 304     } | 
| 317     catch (std::exception& e) | 305     catch (std::exception& e) | 
| 318     { | 306     { | 
| 319       error = e.what(); | 307       error = e.what(); | 
| 320     } | 308     } | 
| 321     catch (...) | 309     catch (...) | 
| 322     { | 310     { | 
| 323       error = "Unknown error while calling stat on " + path; | 311       error = "Unknown error while calling stat on " + fileName + " as " + Resol
     ve(fileName); | 
| 324     } | 312     } | 
| 325     callback(StatResult(), error); | 313     callback(StatResult(), error); | 
| 326   }); | 314   }); | 
| 327 } | 315 } | 
| 328 | 316 | 
| 329 std::string DefaultFileSystem::Resolve(const std::string& path) const | 317 std::string DefaultFileSystem::Resolve(const std::string& fileName) const | 
| 330 { | 318 { | 
| 331   return syncImpl->Resolve(path); | 319   return syncImpl->Resolve(fileName); | 
| 332 } | 320 } | 
| OLD | NEW | 
|---|