| LEFT | RIGHT |
| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 #else | 61 #else |
| 62 // POSIX systems: assume that file system encoding is UTF-8 and just use the | 62 // POSIX systems: assume that file system encoding is UTF-8 and just use the |
| 63 // file paths as they are. | 63 // file paths as they are. |
| 64 std::string NormalizePath(const std::string& path) | 64 std::string NormalizePath(const std::string& path) |
| 65 { | 65 { |
| 66 return path; | 66 return path; |
| 67 } | 67 } |
| 68 #endif | 68 #endif |
| 69 } | 69 } |
| 70 | 70 |
| 71 std::shared_ptr<std::istream> | 71 IFileSystem::IOBuffer |
| 72 DefaultFileSystemSync::Read(const std::string& path) const | 72 DefaultFileSystemSync::Read(const std::string& path) const |
| 73 { | 73 { |
| 74 std::shared_ptr<std::istream> result(new std::ifstream(NormalizePath(path).c_s
tr())); | 74 std::ifstream file(NormalizePath(path).c_str(), std::ios_base::binary); |
| 75 if (result->fail()) | 75 if (file.fail()) |
| 76 throw RuntimeErrorWithErrno("Failed to open " + path); | 76 throw RuntimeErrorWithErrno("Failed to open " + path); |
| 77 return result; | 77 |
| 78 } | 78 file.seekg(0, std::ios_base::end); |
| 79 | 79 auto dataSize = file.tellg(); |
| 80 file.seekg(0, std::ios_base::beg); |
| 81 |
| 82 IFileSystem::IOBuffer data(dataSize); |
| 83 file.read(reinterpret_cast<std::ifstream::char_type*>(data.data()), |
| 84 data.size()); |
| 85 return data; |
| 86 } |
| 80 | 87 |
| 81 void DefaultFileSystemSync::Write(const std::string& path, | 88 void DefaultFileSystemSync::Write(const std::string& path, |
| 82 std::istream& data) | 89 const IFileSystem::IOBuffer& data) |
| 83 { | 90 { |
| 84 std::ofstream file(NormalizePath(path).c_str(), std::ios_base::out | std::ios_
base::binary); | 91 std::ofstream file(NormalizePath(path).c_str(), std::ios_base::out | std::ios_
base::binary); |
| 85 file << Utils::Slurp(data); | 92 file.write(reinterpret_cast<const std::ofstream::char_type*>(data.data()), |
| 93 data.size()); |
| 86 } | 94 } |
| 87 | 95 |
| 88 void DefaultFileSystemSync::Move(const std::string& fromPath, | 96 void DefaultFileSystemSync::Move(const std::string& fromPath, |
| 89 const std::string& toPath) | 97 const std::string& toPath) |
| 90 { | 98 { |
| 91 if (rename(NormalizePath(fromPath).c_str(), NormalizePath(toPath).c_str())) | 99 if (rename(NormalizePath(fromPath).c_str(), NormalizePath(toPath).c_str())) |
| 92 throw RuntimeErrorWithErrno("Failed to move " + fromPath + " to " + toPath); | 100 throw RuntimeErrorWithErrno("Failed to move " + fromPath + " to " + toPath); |
| 93 } | 101 } |
| 94 | 102 |
| 95 void DefaultFileSystemSync::Remove(const std::string& path) | 103 void DefaultFileSystemSync::Remove(const std::string& path) |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 | 212 |
| 205 void DefaultFileSystem::Read(const std::string& path, | 213 void DefaultFileSystem::Read(const std::string& path, |
| 206 const ReadCallback& callback) const | 214 const ReadCallback& callback) const |
| 207 { | 215 { |
| 208 auto impl = syncImpl; | 216 auto impl = syncImpl; |
| 209 std::thread([impl, path, callback] | 217 std::thread([impl, path, callback] |
| 210 { | 218 { |
| 211 std::string error; | 219 std::string error; |
| 212 try | 220 try |
| 213 { | 221 { |
| 214 auto result = impl->Read(path); | 222 auto data = impl->Read(path); |
| 215 std::stringstream output; | 223 callback(std::move(data), error); |
| 216 output << result->rdbuf(); | |
| 217 std::string buffer = output.str(); | |
| 218 callback(std::move(buffer), error); | |
| 219 return; | 224 return; |
| 220 } | 225 } |
| 221 catch (std::exception& e) | 226 catch (std::exception& e) |
| 222 { | 227 { |
| 223 error = e.what(); | 228 error = e.what(); |
| 224 } | 229 } |
| 225 catch (...) | 230 catch (...) |
| 226 { | 231 { |
| 227 error = "Unknown error while reading from " + path; | 232 error = "Unknown error while reading from " + path; |
| 228 } | 233 } |
| 229 callback(std::string(), error); | 234 callback(IOBuffer(), error); |
| 230 }).detach(); | 235 }).detach(); |
| 231 } | 236 } |
| 232 | 237 |
| 233 void DefaultFileSystem::Write(const std::string& path, | 238 void DefaultFileSystem::Write(const std::string& path, |
| 234 std::shared_ptr<std::istream> data, | 239 const IOBuffer& data, |
| 235 const Callback& callback) | 240 const Callback& callback) |
| 236 { | 241 { |
| 237 auto impl = syncImpl; | 242 auto impl = syncImpl; |
| 238 std::thread([impl, path, data, callback] | 243 std::thread([impl, path, data, callback] |
| 239 { | 244 { |
| 240 std::string error; | 245 std::string error; |
| 241 try | 246 try |
| 242 { | 247 { |
| 243 impl->Write(path, *data); | 248 impl->Write(path, data); |
| 244 } | 249 } |
| 245 catch (std::exception& e) | 250 catch (std::exception& e) |
| 246 { | 251 { |
| 247 error = e.what(); | 252 error = e.what(); |
| 248 } | 253 } |
| 249 catch (...) | 254 catch (...) |
| 250 { | 255 { |
| 251 error = "Unknown error while writing to " + path; | 256 error = "Unknown error while writing to " + path; |
| 252 } | 257 } |
| 253 callback(error); | 258 callback(error); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 error = "Unknown error while calling stat on " + path; | 328 error = "Unknown error while calling stat on " + path; |
| 324 } | 329 } |
| 325 callback(StatResult(), error); | 330 callback(StatResult(), error); |
| 326 }).detach(); | 331 }).detach(); |
| 327 } | 332 } |
| 328 | 333 |
| 329 std::string DefaultFileSystem::Resolve(const std::string& path) const | 334 std::string DefaultFileSystem::Resolve(const std::string& path) const |
| 330 { | 335 { |
| 331 return syncImpl->Resolve(path); | 336 return syncImpl->Resolve(path); |
| 332 } | 337 } |
| LEFT | RIGHT |