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

Side by Side Diff: src/DefaultFileSystem.cpp

Issue 29499592: Issue 5183 - remove synchronous FileSystem interface and stop exposing of DefaultFileSystem (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created July 27, 2017, 9:17 a.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 | « src/DefaultFileSystem.h ('k') | src/FileSystemJsObject.cpp » ('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 "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 <sstream>
23 #include <stdexcept> 23 #include <stdexcept>
24 #include <thread> 24 #include <thread>
25 25
26 #include <sys/types.h> 26 #include <sys/types.h>
27 27
28 #ifdef _WIN32 28 #ifdef _WIN32
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 void DefaultFileSystemSync::SetBasePath(const std::string& path) 198 void DefaultFileSystemSync::SetBasePath(const std::string& path)
199 { 199 {
200 basePath = path; 200 basePath = path;
201 201
202 if (*basePath.rbegin() == PATH_SEPARATOR) 202 if (*basePath.rbegin() == PATH_SEPARATOR)
203 { 203 {
204 basePath.resize(basePath.size() - 1); 204 basePath.resize(basePath.size() - 1);
205 } 205 }
206 } 206 }
207 207
208 DefaultFileSystem::DefaultFileSystem(const FileSystemSyncPtr& syncImpl) 208 DefaultFileSystem::DefaultFileSystem(std::unique_ptr<DefaultFileSystemSync> sync Impl)
209 : syncImpl(syncImpl) 209 : syncImpl(std::move(syncImpl))
210 { 210 {
211 } 211 }
212 212
213 void DefaultFileSystem::Read(const std::string& path, 213 void DefaultFileSystem::Read(const std::string& path,
214 const ReadCallback& callback) const 214 const ReadCallback& callback) const
215 { 215 {
216 auto impl = syncImpl; 216 std::thread([this, path, callback]
217 std::thread([impl, path, callback]
218 { 217 {
219 std::string error; 218 std::string error;
220 try 219 try
221 { 220 {
222 auto data = impl->Read(path); 221 auto data = syncImpl->Read(path);
223 callback(std::move(data), error); 222 callback(std::move(data), error);
224 return; 223 return;
225 } 224 }
226 catch (std::exception& e) 225 catch (std::exception& e)
227 { 226 {
228 error = e.what(); 227 error = e.what();
229 } 228 }
230 catch (...) 229 catch (...)
231 { 230 {
232 error = "Unknown error while reading from " + path; 231 error = "Unknown error while reading from " + path;
233 } 232 }
234 callback(IOBuffer(), error); 233 callback(IOBuffer(), error);
235 }).detach(); 234 }).detach();
236 } 235 }
237 236
238 void DefaultFileSystem::Write(const std::string& path, 237 void DefaultFileSystem::Write(const std::string& path,
239 const IOBuffer& data, 238 const IOBuffer& data,
240 const Callback& callback) 239 const Callback& callback)
241 { 240 {
242 auto impl = syncImpl; 241 std::thread([this, path, data, callback]
243 std::thread([impl, path, data, callback]
244 { 242 {
245 std::string error; 243 std::string error;
246 try 244 try
247 { 245 {
248 impl->Write(path, data); 246 syncImpl->Write(path, data);
249 } 247 }
250 catch (std::exception& e) 248 catch (std::exception& e)
251 { 249 {
252 error = e.what(); 250 error = e.what();
253 } 251 }
254 catch (...) 252 catch (...)
255 { 253 {
256 error = "Unknown error while writing to " + path; 254 error = "Unknown error while writing to " + path;
257 } 255 }
258 callback(error); 256 callback(error);
259 }).detach(); 257 }).detach();
260 } 258 }
261 259
262 void DefaultFileSystem::Move(const std::string& fromPath, 260 void DefaultFileSystem::Move(const std::string& fromPath,
263 const std::string& toPath, 261 const std::string& toPath,
264 const Callback& callback) 262 const Callback& callback)
265 { 263 {
266 auto impl = syncImpl; 264 std::thread([this, fromPath, toPath, callback]
267 std::thread([impl, fromPath, toPath, callback]
268 { 265 {
269 std::string error; 266 std::string error;
270 try 267 try
271 { 268 {
272 impl->Move(fromPath, toPath); 269 syncImpl->Move(fromPath, toPath);
273 } 270 }
274 catch (std::exception& e) 271 catch (std::exception& e)
275 { 272 {
276 error = e.what(); 273 error = e.what();
277 } 274 }
278 catch (...) 275 catch (...)
279 { 276 {
280 error = "Unknown error while moving " + fromPath + " to " + toPath; 277 error = "Unknown error while moving " + fromPath + " to " + toPath;
281 } 278 }
282 callback(error); 279 callback(error);
283 }).detach(); 280 }).detach();
284 } 281 }
285 282
286 void DefaultFileSystem::Remove(const std::string& path, 283 void DefaultFileSystem::Remove(const std::string& path,
287 const Callback& callback) 284 const Callback& callback)
288 { 285 {
289 auto impl = syncImpl; 286 std::thread([this, path, callback]
290 std::thread([impl, path, callback]
291 { 287 {
292 std::string error; 288 std::string error;
293 try 289 try
294 { 290 {
295 impl->Remove(path); 291 syncImpl->Remove(path);
296 } 292 }
297 catch (std::exception& e) 293 catch (std::exception& e)
298 { 294 {
299 error = e.what(); 295 error = e.what();
300 } 296 }
301 catch (...) 297 catch (...)
302 { 298 {
303 error = "Unknown error while removing " + path; 299 error = "Unknown error while removing " + path;
304 } 300 }
305 callback(error); 301 callback(error);
306 }).detach(); 302 }).detach();
307 } 303 }
308 304
309 void DefaultFileSystem::Stat(const std::string& path, 305 void DefaultFileSystem::Stat(const std::string& path,
310 const StatCallback& callback) const 306 const StatCallback& callback) const
311 { 307 {
312 auto impl = syncImpl; 308 std::thread([this, path, callback]
313 std::thread([impl, path, callback]
314 { 309 {
315 std::string error; 310 std::string error;
316 try 311 try
317 { 312 {
318 auto result = impl->Stat(path); 313 auto result = syncImpl->Stat(path);
319 callback(result, error); 314 callback(result, error);
320 return; 315 return;
321 } 316 }
322 catch (std::exception& e) 317 catch (std::exception& e)
323 { 318 {
324 error = e.what(); 319 error = e.what();
325 } 320 }
326 catch (...) 321 catch (...)
327 { 322 {
328 error = "Unknown error while calling stat on " + path; 323 error = "Unknown error while calling stat on " + path;
329 } 324 }
330 callback(StatResult(), error); 325 callback(StatResult(), error);
331 }).detach(); 326 }).detach();
332 } 327 }
333 328
334 std::string DefaultFileSystem::Resolve(const std::string& path) const 329 std::string DefaultFileSystem::Resolve(const std::string& path) const
335 { 330 {
336 return syncImpl->Resolve(path); 331 return syncImpl->Resolve(path);
337 } 332 }
OLDNEW
« no previous file with comments | « src/DefaultFileSystem.h ('k') | src/FileSystemJsObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld