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

Delta Between Two Patch Sets: src/DefaultFileSystem.cpp

Issue 29449592: Issue 5183 - Provide async interface for FileSystem (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Left Patch Set: Updated patch after review. Created June 16, 2017, 9:52 p.m.
Right Patch Set: Rebase on master. Last changes. Created July 7, 2017, 1:36 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 | « libadblockplus.gyp ('k') | src/FileSystemJsObject.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 /* 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
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
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("", 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 const std::string& 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 std::stringstream stream; 248 impl->Write(path, data);
244 stream << data;
245 impl->Write(path, stream);
246 } 249 }
247 catch (std::exception& e) 250 catch (std::exception& e)
248 { 251 {
249 error = e.what(); 252 error = e.what();
250 } 253 }
251 catch (...) 254 catch (...)
252 { 255 {
253 error = "Unknown error while writing to " + path; 256 error = "Unknown error while writing to " + path;
254 } 257 }
255 callback(error); 258 callback(error);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 error = "Unknown error while calling stat on " + path; 328 error = "Unknown error while calling stat on " + path;
326 } 329 }
327 callback(StatResult(), error); 330 callback(StatResult(), error);
328 }).detach(); 331 }).detach();
329 } 332 }
330 333
331 std::string DefaultFileSystem::Resolve(const std::string& path) const 334 std::string DefaultFileSystem::Resolve(const std::string& path) const
332 { 335 {
333 return syncImpl->Resolve(path); 336 return syncImpl->Resolve(path);
334 } 337 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld