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: Make read write deal with binary buffers. Created July 6, 2017, 12:19 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(), std::ios_base::binary)); 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::ostream& 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 << data.rdbuf(); 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 is = impl->Read(path); 222 auto data = impl->Read(path);
215 is->seekg(0, std::ios_base::end);
216 auto dataSize = is->tellg();
217 is->seekg(0, std::ios_base::beg);
218
219 std::vector<char> data(dataSize);
220 is->read(data.data(), data.size());
221 callback(std::move(data), error); 223 callback(std::move(data), error);
222 return; 224 return;
223 } 225 }
224 catch (std::exception& e) 226 catch (std::exception& e)
225 { 227 {
226 error = e.what(); 228 error = e.what();
227 } 229 }
228 catch (...) 230 catch (...)
229 { 231 {
230 error = "Unknown error while reading from " + path; 232 error = "Unknown error while reading from " + path;
231 } 233 }
232 callback(std::vector<char>(), error); 234 callback(IOBuffer(), error);
233 }).detach(); 235 }).detach();
234 } 236 }
235 237
236 void DefaultFileSystem::Write(const std::string& path, 238 void DefaultFileSystem::Write(const std::string& path,
237 const std::vector<char>& data, 239 const IOBuffer& data,
238 const Callback& callback) 240 const Callback& callback)
239 { 241 {
240 auto impl = syncImpl; 242 auto impl = syncImpl;
241 std::thread([impl, path, data, callback] 243 std::thread([impl, path, data, callback]
242 { 244 {
243 std::string error; 245 std::string error;
244 try 246 try
245 { 247 {
246 std::stringstream stream; 248 impl->Write(path, data);
247 stream.write(data.data(), data.size());
248 impl->Write(path, stream);
249 } 249 }
250 catch (std::exception& e) 250 catch (std::exception& e)
251 { 251 {
252 error = e.what(); 252 error = e.what();
253 } 253 }
254 catch (...) 254 catch (...)
255 { 255 {
256 error = "Unknown error while writing to " + path; 256 error = "Unknown error while writing to " + path;
257 } 257 }
258 callback(error); 258 callback(error);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 error = "Unknown error while calling stat on " + path; 328 error = "Unknown error while calling stat on " + path;
329 } 329 }
330 callback(StatResult(), error); 330 callback(StatResult(), error);
331 }).detach(); 331 }).detach();
332 } 332 }
333 333
334 std::string DefaultFileSystem::Resolve(const std::string& path) const 334 std::string DefaultFileSystem::Resolve(const std::string& path) const
335 { 335 {
336 return syncImpl->Resolve(path); 336 return syncImpl->Resolve(path);
337 } 337 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld