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: Created May 26, 2017, 12:43 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
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
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 68
69 std::shared_ptr<std::istream> 69 std::shared_ptr<std::istream>
70 DefaultFileSystem::Read(const std::string& path) const 70 DefaultFileSystem::Read(const std::string& path) const
71 { 71 {
72 std::shared_ptr<std::istream> result(new std::ifstream(NormalizePath(path).c_s tr())); 72 std::shared_ptr<std::istream> result(new std::ifstream(NormalizePath(path).c_s tr()));
73 if (result->fail()) 73 if (result->fail())
74 throw RuntimeErrorWithErrno("Failed to open " + path); 74 throw RuntimeErrorWithErrno("Failed to open " + path);
75 return result; 75 return result;
76 } 76 }
77 77
78 void DefaultFileSystem::Read(const std::string& path,
79 const ReadCallback& callback) const
80 {
81 try
82 {
83 auto result = Read(path);
84 callback(result);
85 }
86 catch (...)
87 {
88 }
89 }
90
78 void DefaultFileSystem::Write(const std::string& path, 91 void DefaultFileSystem::Write(const std::string& path,
79 std::istream& data) 92 std::istream& data)
80 { 93 {
81 std::ofstream file(NormalizePath(path).c_str(), std::ios_base::out | std::ios_ base::binary); 94 std::ofstream file(NormalizePath(path).c_str(), std::ios_base::out | std::ios_ base::binary);
82 file << Utils::Slurp(data); 95 file << Utils::Slurp(data);
83 } 96 }
84 97
98 void DefaultFileSystem::Write(const std::string& path,
99 std::istream& data,
100 const Callback& callback)
101 {
102 Write(path, data);
103 callback();
104 }
105
85 void DefaultFileSystem::Move(const std::string& fromPath, 106 void DefaultFileSystem::Move(const std::string& fromPath,
86 const std::string& toPath) 107 const std::string& toPath)
87 { 108 {
88 if (rename(NormalizePath(fromPath).c_str(), NormalizePath(toPath).c_str())) 109 if (rename(NormalizePath(fromPath).c_str(), NormalizePath(toPath).c_str()))
89 throw RuntimeErrorWithErrno("Failed to move " + fromPath + " to " + toPath); 110 throw RuntimeErrorWithErrno("Failed to move " + fromPath + " to " + toPath);
90 } 111 }
91 112
113 void DefaultFileSystem::Move(const std::string& fromPath,
114 const std::string& toPath,
115 const Callback& callback)
116 {
117 try
118 {
119 Move(fromPath, toPath);
120 callback();
121 }
122 catch (...)
123 {
124 }
125 }
126
92 void DefaultFileSystem::Remove(const std::string& path) 127 void DefaultFileSystem::Remove(const std::string& path)
93 { 128 {
94 if (remove(NormalizePath(path).c_str())) 129 if (remove(NormalizePath(path).c_str()))
95 throw RuntimeErrorWithErrno("Failed to remove " + path); 130 throw RuntimeErrorWithErrno("Failed to remove " + path);
96 } 131 }
97 132
133 void DefaultFileSystem::Remove(const std::string& path,
134 const Callback& callback)
135 {
136 try
137 {
138 Remove(path);
139 callback();
140 }
141 catch (...)
142 {
143 }
144 }
145
98 FileSystem::StatResult DefaultFileSystem::Stat(const std::string& path) const 146 FileSystem::StatResult DefaultFileSystem::Stat(const std::string& path) const
99 { 147 {
100 FileSystem::StatResult result; 148 FileSystem::StatResult result;
101 #ifdef WIN32 149 #ifdef WIN32
102 WIN32_FILE_ATTRIBUTE_DATA data; 150 WIN32_FILE_ATTRIBUTE_DATA data;
103 if (!GetFileAttributesExW(NormalizePath(path).c_str(), GetFileExInfoStandard, &data)) 151 if (!GetFileAttributesExW(NormalizePath(path).c_str(), GetFileExInfoStandard, &data))
104 { 152 {
105 DWORD err = GetLastError(); 153 DWORD err = GetLastError();
106 if (err == ERROR_FILE_NOT_FOUND || 154 if (err == ERROR_FILE_NOT_FOUND ||
107 err == ERROR_PATH_NOT_FOUND || 155 err == ERROR_PATH_NOT_FOUND ||
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 #if _POSIX_C_SOURCE >= 200809L 202 #if _POSIX_C_SOURCE >= 200809L
155 result.lastModified = static_cast<int64_t>(nativeStat.st_mtim.tv_sec) * MSEC_I N_SEC 203 result.lastModified = static_cast<int64_t>(nativeStat.st_mtim.tv_sec) * MSEC_I N_SEC
156 + static_cast<int64_t>(nativeStat.st_mtim.tv_nsec) / NSEC _IN_MSEC; 204 + static_cast<int64_t>(nativeStat.st_mtim.tv_nsec) / NSEC _IN_MSEC;
157 #else 205 #else
158 result.lastModified = static_cast<int64_t>(nativeStat.st_mtime) * MSEC_IN_SEC; 206 result.lastModified = static_cast<int64_t>(nativeStat.st_mtime) * MSEC_IN_SEC;
159 #endif 207 #endif
160 return result; 208 return result;
161 #endif 209 #endif
162 } 210 }
163 211
212 void DefaultFileSystem::Stat(const std::string& path,
213 const StatCallback& callback) const
214 {
215 try
216 {
217 auto stats = Stat(path);
218 callback(stats);
219 }
220 catch (...)
221 {
222 }
223 }
224
164 std::string DefaultFileSystem::Resolve(const std::string& path) const 225 std::string DefaultFileSystem::Resolve(const std::string& path) const
165 { 226 {
166 if (basePath == "") 227 if (basePath == "")
167 { 228 {
168 return path; 229 return path;
169 } 230 }
170 else 231 else
171 { 232 {
172 #ifdef _WIN32 233 #ifdef _WIN32
173 if (PathIsRelative(NormalizePath(path).c_str())) 234 if (PathIsRelative(NormalizePath(path).c_str()))
174 #else 235 #else
175 if (path.length() && *path.begin() != PATH_SEPARATOR) 236 if (path.length() && *path.begin() != PATH_SEPARATOR)
176 #endif 237 #endif
177 { 238 {
178 return basePath + PATH_SEPARATOR + path; 239 return basePath + PATH_SEPARATOR + path;
179 } 240 }
180 else 241 else
181 { 242 {
182 return path; 243 return path;
183 } 244 }
184 } 245 }
185 } 246 }
247 void DefaultFileSystem::Resolve(const std::string& path,
248 const ResolveCallback& callback) const
249 {
250 callback(Resolve(path));
251 }
186 252
187 void DefaultFileSystem::SetBasePath(const std::string& path) 253 void DefaultFileSystem::SetBasePath(const std::string& path)
188 { 254 {
189 basePath = path; 255 basePath = path;
190 256
191 if (*basePath.rbegin() == PATH_SEPARATOR) 257 if (*basePath.rbegin() == PATH_SEPARATOR)
192 { 258 {
193 basePath.resize(basePath.size() - 1); 259 basePath.resize(basePath.size() - 1);
194 } 260 }
195 } 261 }
196 262
OLDNEW

Powered by Google App Engine
This is Rietveld