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

Side by Side Diff: src/DefaultFileSystem.cpp

Issue 29449592: Issue 5183 - Provide async interface for FileSystem (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
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:
View unified diff | Download patch
« no previous file with comments | « libadblockplus.gyp ('k') | src/FileSystemJsObject.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <AdblockPlus/DefaultFileSystem.h> 18 #include <AdblockPlus/DefaultFileSystem.h>
19 #include <cstdio> 19 #include <cstdio>
20 #include <cstring> 20 #include <cstring>
21 #include <fstream> 21 #include <fstream>
22 #include <sstream>
22 #include <stdexcept> 23 #include <stdexcept>
24 #include <thread>
23 25
24 #include <sys/types.h> 26 #include <sys/types.h>
25 27
26 #ifdef _WIN32 28 #ifdef _WIN32
27 #include <windows.h> 29 #include <windows.h>
28 #include <Shlobj.h> 30 #include <Shlobj.h>
29 #include <Shlwapi.h> 31 #include <Shlwapi.h>
30 #else 32 #else
31 #include <sys/stat.h> 33 #include <sys/stat.h>
32 #include <cerrno> 34 #include <cerrno>
(...skipping 26 matching lines...) Expand all
59 #else 61 #else
60 // 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
61 // file paths as they are. 63 // file paths as they are.
62 std::string NormalizePath(const std::string& path) 64 std::string NormalizePath(const std::string& path)
63 { 65 {
64 return path; 66 return path;
65 } 67 }
66 #endif 68 #endif
67 } 69 }
68 70
69 FileSystem::IOBuffer 71 IFileSystem::IOBuffer
70 DefaultFileSystem::Read(const std::string& path) const 72 DefaultFileSystemSync::Read(const std::string& path) const
71 { 73 {
72 std::ifstream file(NormalizePath(path).c_str(), std::ios_base::binary); 74 std::ifstream file(NormalizePath(path).c_str(), std::ios_base::binary);
73 if (file.fail()) 75 if (file.fail())
74 throw RuntimeErrorWithErrno("Failed to open " + path); 76 throw RuntimeErrorWithErrno("Failed to open " + path);
75 77
76 file.seekg(0, std::ios_base::end); 78 file.seekg(0, std::ios_base::end);
77 auto dataSize = file.tellg(); 79 auto dataSize = file.tellg();
78 file.seekg(0, std::ios_base::beg); 80 file.seekg(0, std::ios_base::beg);
79 81
80 IOBuffer data(dataSize); 82 IFileSystem::IOBuffer data(dataSize);
81 file.read(reinterpret_cast<std::ifstream::char_type*>(data.data()), 83 file.read(reinterpret_cast<std::ifstream::char_type*>(data.data()),
82 data.size()); 84 data.size());
83 return data; 85 return data;
84 } 86 }
85 87
86 void DefaultFileSystem::Write(const std::string& path, 88 void DefaultFileSystemSync::Write(const std::string& path,
87 const IOBuffer& data) 89 const IFileSystem::IOBuffer& data)
88 { 90 {
89 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);
90 file.write(reinterpret_cast<const std::ofstream::char_type*>(data.data()), 92 file.write(reinterpret_cast<const std::ofstream::char_type*>(data.data()),
91 data.size()); 93 data.size());
92 } 94 }
93 95
94 void DefaultFileSystem::Move(const std::string& fromPath, 96 void DefaultFileSystemSync::Move(const std::string& fromPath,
95 const std::string& toPath) 97 const std::string& toPath)
96 { 98 {
97 if (rename(NormalizePath(fromPath).c_str(), NormalizePath(toPath).c_str())) 99 if (rename(NormalizePath(fromPath).c_str(), NormalizePath(toPath).c_str()))
98 throw RuntimeErrorWithErrno("Failed to move " + fromPath + " to " + toPath); 100 throw RuntimeErrorWithErrno("Failed to move " + fromPath + " to " + toPath);
99 } 101 }
100 102
101 void DefaultFileSystem::Remove(const std::string& path) 103 void DefaultFileSystemSync::Remove(const std::string& path)
102 { 104 {
103 if (remove(NormalizePath(path).c_str())) 105 if (remove(NormalizePath(path).c_str()))
104 throw RuntimeErrorWithErrno("Failed to remove " + path); 106 throw RuntimeErrorWithErrno("Failed to remove " + path);
105 } 107 }
106 108
107 FileSystem::StatResult DefaultFileSystem::Stat(const std::string& path) const 109 IFileSystem::StatResult DefaultFileSystemSync::Stat(const std::string& path) con st
108 { 110 {
109 FileSystem::StatResult result; 111 IFileSystem::StatResult result;
110 #ifdef WIN32 112 #ifdef WIN32
111 WIN32_FILE_ATTRIBUTE_DATA data; 113 WIN32_FILE_ATTRIBUTE_DATA data;
112 if (!GetFileAttributesExW(NormalizePath(path).c_str(), GetFileExInfoStandard, &data)) 114 if (!GetFileAttributesExW(NormalizePath(path).c_str(), GetFileExInfoStandard, &data))
113 { 115 {
114 DWORD err = GetLastError(); 116 DWORD err = GetLastError();
115 if (err == ERROR_FILE_NOT_FOUND || 117 if (err == ERROR_FILE_NOT_FOUND ||
116 err == ERROR_PATH_NOT_FOUND || 118 err == ERROR_PATH_NOT_FOUND ||
117 err == ERROR_INVALID_DRIVE) 119 err == ERROR_INVALID_DRIVE)
118 { 120 {
119 return result; 121 return result;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 #if _POSIX_C_SOURCE >= 200809L 165 #if _POSIX_C_SOURCE >= 200809L
164 result.lastModified = static_cast<int64_t>(nativeStat.st_mtim.tv_sec) * MSEC_I N_SEC 166 result.lastModified = static_cast<int64_t>(nativeStat.st_mtim.tv_sec) * MSEC_I N_SEC
165 + static_cast<int64_t>(nativeStat.st_mtim.tv_nsec) / NSEC _IN_MSEC; 167 + static_cast<int64_t>(nativeStat.st_mtim.tv_nsec) / NSEC _IN_MSEC;
166 #else 168 #else
167 result.lastModified = static_cast<int64_t>(nativeStat.st_mtime) * MSEC_IN_SEC; 169 result.lastModified = static_cast<int64_t>(nativeStat.st_mtime) * MSEC_IN_SEC;
168 #endif 170 #endif
169 return result; 171 return result;
170 #endif 172 #endif
171 } 173 }
172 174
173 std::string DefaultFileSystem::Resolve(const std::string& path) const 175 std::string DefaultFileSystemSync::Resolve(const std::string& path) const
174 { 176 {
175 if (basePath == "") 177 if (basePath == "")
176 { 178 {
177 return path; 179 return path;
178 } 180 }
179 else 181 else
180 { 182 {
181 #ifdef _WIN32 183 #ifdef _WIN32
182 if (PathIsRelative(NormalizePath(path).c_str())) 184 if (PathIsRelative(NormalizePath(path).c_str()))
183 #else 185 #else
184 if (path.length() && *path.begin() != PATH_SEPARATOR) 186 if (path.length() && *path.begin() != PATH_SEPARATOR)
185 #endif 187 #endif
186 { 188 {
187 return basePath + PATH_SEPARATOR + path; 189 return basePath + PATH_SEPARATOR + path;
188 } 190 }
189 else 191 else
190 { 192 {
191 return path; 193 return path;
192 } 194 }
193 } 195 }
194 } 196 }
195 197
196 void DefaultFileSystem::SetBasePath(const std::string& path) 198 void DefaultFileSystemSync::SetBasePath(const std::string& path)
197 { 199 {
198 basePath = path; 200 basePath = path;
199 201
200 if (*basePath.rbegin() == PATH_SEPARATOR) 202 if (*basePath.rbegin() == PATH_SEPARATOR)
201 { 203 {
202 basePath.resize(basePath.size() - 1); 204 basePath.resize(basePath.size() - 1);
203 } 205 }
204 } 206 }
205 207
208 DefaultFileSystem::DefaultFileSystem(const FileSystemSyncPtr& syncImpl)
209 : syncImpl(syncImpl)
210 {
211 }
212
213 void DefaultFileSystem::Read(const std::string& path,
214 const ReadCallback& callback) const
215 {
216 auto impl = syncImpl;
217 std::thread([impl, path, callback]
218 {
219 std::string error;
220 try
221 {
222 auto data = impl->Read(path);
223 callback(std::move(data), error);
224 return;
225 }
226 catch (std::exception& e)
227 {
228 error = e.what();
229 }
230 catch (...)
231 {
232 error = "Unknown error while reading from " + path;
233 }
234 callback(IOBuffer(), error);
235 }).detach();
236 }
237
238 void DefaultFileSystem::Write(const std::string& path,
239 const IOBuffer& data,
240 const Callback& callback)
241 {
242 auto impl = syncImpl;
243 std::thread([impl, path, data, callback]
244 {
245 std::string error;
246 try
247 {
248 impl->Write(path, data);
249 }
250 catch (std::exception& e)
251 {
252 error = e.what();
253 }
254 catch (...)
255 {
256 error = "Unknown error while writing to " + path;
257 }
258 callback(error);
259 }).detach();
260 }
261
262 void DefaultFileSystem::Move(const std::string& fromPath,
263 const std::string& toPath,
264 const Callback& callback)
265 {
266 auto impl = syncImpl;
267 std::thread([impl, fromPath, toPath, callback]
268 {
269 std::string error;
270 try
271 {
272 impl->Move(fromPath, toPath);
273 }
274 catch (std::exception& e)
275 {
276 error = e.what();
277 }
278 catch (...)
279 {
280 error = "Unknown error while moving " + fromPath + " to " + toPath;
281 }
282 callback(error);
283 }).detach();
284 }
285
286 void DefaultFileSystem::Remove(const std::string& path,
287 const Callback& callback)
288 {
289 auto impl = syncImpl;
290 std::thread([impl, path, callback]
291 {
292 std::string error;
293 try
294 {
295 impl->Remove(path);
296 }
297 catch (std::exception& e)
298 {
299 error = e.what();
300 }
301 catch (...)
302 {
303 error = "Unknown error while removing " + path;
304 }
305 callback(error);
306 }).detach();
307 }
308
309 void DefaultFileSystem::Stat(const std::string& path,
310 const StatCallback& callback) const
311 {
312 auto impl = syncImpl;
313 std::thread([impl, path, callback]
314 {
315 std::string error;
316 try
317 {
318 auto result = impl->Stat(path);
319 callback(result, error);
320 return;
321 }
322 catch (std::exception& e)
323 {
324 error = e.what();
325 }
326 catch (...)
327 {
328 error = "Unknown error while calling stat on " + path;
329 }
330 callback(StatResult(), error);
331 }).detach();
332 }
333
334 std::string DefaultFileSystem::Resolve(const std::string& path) const
335 {
336 return syncImpl->Resolve(path);
337 }
OLDNEW
« no previous file with comments | « libadblockplus.gyp ('k') | src/FileSystemJsObject.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld