Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(827)

Side by Side Diff: webkit/browser/fileapi/native_file_util.cc

Issue 145303002: Convert Media Galleries to use base::File (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "webkit/browser/fileapi/native_file_util.h" 5 #include "webkit/browser/fileapi/native_file_util.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/files/file_enumerator.h" 8 #include "base/files/file_enumerator.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "net/base/file_stream.h" 10 #include "net/base/file_stream.h"
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 bytes_written += bytes_written_partial; 66 bytes_written += bytes_written_partial;
67 } 67 }
68 } 68 }
69 69
70 return outfile.FlushSync() >= 0; 70 return outfile.FlushSync() >= 0;
71 } 71 }
72 72
73 } // namespace 73 } // namespace
74 74
75 using base::PlatformFile; 75 using base::PlatformFile;
76 using base::PlatformFileError;
77 76
78 class NativeFileEnumerator : public FileSystemFileUtil::AbstractFileEnumerator { 77 class NativeFileEnumerator : public FileSystemFileUtil::AbstractFileEnumerator {
79 public: 78 public:
80 NativeFileEnumerator(const base::FilePath& root_path, 79 NativeFileEnumerator(const base::FilePath& root_path,
81 bool recursive, 80 bool recursive,
82 int file_type) 81 int file_type)
83 : file_enum_(root_path, recursive, file_type) { 82 : file_enum_(root_path, recursive, file_type) {
84 } 83 }
85 84
86 virtual ~NativeFileEnumerator() {} 85 virtual ~NativeFileEnumerator() {}
(...skipping 29 matching lines...) Expand all
116 115
117 NativeFileUtil::CopyOrMoveMode NativeFileUtil::CopyOrMoveModeForDestination( 116 NativeFileUtil::CopyOrMoveMode NativeFileUtil::CopyOrMoveModeForDestination(
118 const FileSystemURL& dest_url, bool copy) { 117 const FileSystemURL& dest_url, bool copy) {
119 if (copy) { 118 if (copy) {
120 return dest_url.mount_option().copy_sync_option() == COPY_SYNC_OPTION_SYNC ? 119 return dest_url.mount_option().copy_sync_option() == COPY_SYNC_OPTION_SYNC ?
121 COPY_SYNC : COPY_NOSYNC; 120 COPY_SYNC : COPY_NOSYNC;
122 } 121 }
123 return MOVE; 122 return MOVE;
124 } 123 }
125 124
126 PlatformFileError NativeFileUtil::CreateOrOpen( 125 base::File::Error NativeFileUtil::CreateOrOpen(
127 const base::FilePath& path, int file_flags, 126 const base::FilePath& path, int file_flags,
128 PlatformFile* file_handle, bool* created) { 127 PlatformFile* file_handle, bool* created) {
129 if (!base::DirectoryExists(path.DirName())) { 128 if (!base::DirectoryExists(path.DirName())) {
130 // If its parent does not exist, should return NOT_FOUND error. 129 // If its parent does not exist, should return NOT_FOUND error.
131 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 130 return base::File::FILE_ERROR_NOT_FOUND;
132 } 131 }
133 if (base::DirectoryExists(path)) 132 if (base::DirectoryExists(path))
134 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; 133 return base::File::FILE_ERROR_NOT_A_FILE;
135 PlatformFileError error_code = base::PLATFORM_FILE_OK; 134
135 // TODO(rvargas): convert this code to use base::File.
136 base::PlatformFileError error_code = base::PLATFORM_FILE_OK;
136 *file_handle = base::CreatePlatformFile(path, file_flags, 137 *file_handle = base::CreatePlatformFile(path, file_flags,
137 created, &error_code); 138 created, &error_code);
138 return error_code; 139 return static_cast<base::File::Error>(error_code);
139 } 140 }
140 141
141 PlatformFileError NativeFileUtil::Close(PlatformFile file_handle) { 142 base::File::Error NativeFileUtil::Close(PlatformFile file_handle) {
142 if (!base::ClosePlatformFile(file_handle)) 143 if (!base::ClosePlatformFile(file_handle))
143 return base::PLATFORM_FILE_ERROR_FAILED; 144 return base::File::FILE_ERROR_FAILED;
144 return base::PLATFORM_FILE_OK; 145 return base::File::FILE_OK;
145 } 146 }
146 147
147 PlatformFileError NativeFileUtil::EnsureFileExists( 148 base::File::Error NativeFileUtil::EnsureFileExists(
148 const base::FilePath& path, 149 const base::FilePath& path,
149 bool* created) { 150 bool* created) {
150 if (!base::DirectoryExists(path.DirName())) 151 if (!base::DirectoryExists(path.DirName()))
151 // If its parent does not exist, should return NOT_FOUND error. 152 // If its parent does not exist, should return NOT_FOUND error.
152 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 153 return base::File::FILE_ERROR_NOT_FOUND;
153 PlatformFileError error_code = base::PLATFORM_FILE_OK; 154
154 // Tries to create the |path| exclusively. This should fail 155 // Tries to create the |path| exclusively. This should fail
155 // with base::PLATFORM_FILE_ERROR_EXISTS if the path already exists. 156 // with base::File::FILE_ERROR_EXISTS if the path already exists.
156 PlatformFile handle = base::CreatePlatformFile( 157 base::File file(path, base::File::FLAG_CREATE | base::File::FLAG_READ);
157 path, 158
158 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, 159 if (file.IsValid()) {
159 created, &error_code); 160 if (created)
160 if (error_code == base::PLATFORM_FILE_ERROR_EXISTS) { 161 *created = file.created();
162 return base::File::FILE_OK;
163 }
164
165 base::File::Error error_code = file.error_details();
166 if (error_code == base::File::FILE_ERROR_EXISTS) {
161 // Make sure created_ is false. 167 // Make sure created_ is false.
162 if (created) 168 if (created)
163 *created = false; 169 *created = false;
164 error_code = base::PLATFORM_FILE_OK; 170 error_code = base::File::FILE_OK;
165 } 171 }
166 if (handle != base::kInvalidPlatformFileValue)
167 base::ClosePlatformFile(handle);
168 return error_code; 172 return error_code;
169 } 173 }
170 174
171 PlatformFileError NativeFileUtil::CreateDirectory( 175 base::File::Error NativeFileUtil::CreateDirectory(
172 const base::FilePath& path, 176 const base::FilePath& path,
173 bool exclusive, 177 bool exclusive,
174 bool recursive) { 178 bool recursive) {
175 // If parent dir of file doesn't exist. 179 // If parent dir of file doesn't exist.
176 if (!recursive && !base::PathExists(path.DirName())) 180 if (!recursive && !base::PathExists(path.DirName()))
177 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 181 return base::File::FILE_ERROR_NOT_FOUND;
178 182
179 bool path_exists = base::PathExists(path); 183 bool path_exists = base::PathExists(path);
180 if (exclusive && path_exists) 184 if (exclusive && path_exists)
181 return base::PLATFORM_FILE_ERROR_EXISTS; 185 return base::File::FILE_ERROR_EXISTS;
182 186
183 // If file exists at the path. 187 // If file exists at the path.
184 if (path_exists && !base::DirectoryExists(path)) 188 if (path_exists && !base::DirectoryExists(path))
185 return base::PLATFORM_FILE_ERROR_EXISTS; 189 return base::File::FILE_ERROR_EXISTS;
186 190
187 if (!base::CreateDirectory(path)) 191 if (!base::CreateDirectory(path))
188 return base::PLATFORM_FILE_ERROR_FAILED; 192 return base::File::FILE_ERROR_FAILED;
189 193
190 if (!SetPlatformSpecificDirectoryPermissions(path)) { 194 if (!SetPlatformSpecificDirectoryPermissions(path)) {
191 // Since some file systems don't support permission setting, we do not treat 195 // Since some file systems don't support permission setting, we do not treat
192 // an error from the function as the failure of copying. Just log it. 196 // an error from the function as the failure of copying. Just log it.
193 LOG(WARNING) << "Setting directory permission failed: " 197 LOG(WARNING) << "Setting directory permission failed: "
194 << path.AsUTF8Unsafe(); 198 << path.AsUTF8Unsafe();
195 } 199 }
196 200
197 return base::PLATFORM_FILE_OK; 201 return base::File::FILE_OK;
198 } 202 }
199 203
200 PlatformFileError NativeFileUtil::GetFileInfo( 204 base::File::Error NativeFileUtil::GetFileInfo(
201 const base::FilePath& path, 205 const base::FilePath& path,
202 base::PlatformFileInfo* file_info) { 206 base::File::Info* file_info) {
203 if (!base::PathExists(path)) 207 if (!base::PathExists(path))
204 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 208 return base::File::FILE_ERROR_NOT_FOUND;
205 209
206 // TODO(rvargas): convert this code to use base::File. 210 if (!base::GetFileInfo(path, file_info))
207 if (!base::GetFileInfo(path, reinterpret_cast<base::File::Info*>(file_info))) 211 return base::File::FILE_ERROR_FAILED;
208 return base::PLATFORM_FILE_ERROR_FAILED; 212 return base::File::FILE_OK;
209 return base::PLATFORM_FILE_OK;
210 } 213 }
211 214
212 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> 215 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator>
213 NativeFileUtil::CreateFileEnumerator(const base::FilePath& root_path, 216 NativeFileUtil::CreateFileEnumerator(const base::FilePath& root_path,
214 bool recursive) { 217 bool recursive) {
215 return make_scoped_ptr(new NativeFileEnumerator( 218 return make_scoped_ptr(new NativeFileEnumerator(
216 root_path, recursive, 219 root_path, recursive,
217 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES)) 220 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES))
218 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); 221 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>();
219 } 222 }
220 223
221 PlatformFileError NativeFileUtil::Touch( 224 base::File::Error NativeFileUtil::Touch(
222 const base::FilePath& path, 225 const base::FilePath& path,
223 const base::Time& last_access_time, 226 const base::Time& last_access_time,
224 const base::Time& last_modified_time) { 227 const base::Time& last_modified_time) {
225 if (!base::TouchFile(path, last_access_time, last_modified_time)) 228 if (!base::TouchFile(path, last_access_time, last_modified_time))
226 return base::PLATFORM_FILE_ERROR_FAILED; 229 return base::File::FILE_ERROR_FAILED;
227 return base::PLATFORM_FILE_OK; 230 return base::File::FILE_OK;
228 } 231 }
229 232
230 PlatformFileError NativeFileUtil::Truncate( 233 base::File::Error NativeFileUtil::Truncate(const base::FilePath& path,
231 const base::FilePath& path, int64 length) { 234 int64 length) {
232 PlatformFileError error_code(base::PLATFORM_FILE_ERROR_FAILED); 235 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_WRITE);
233 PlatformFile file = 236 if (!file.IsValid())
234 base::CreatePlatformFile( 237 return file.error_details();
235 path, 238
236 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, 239 if (!file.SetLength(length))
237 NULL, 240 return base::File::FILE_ERROR_FAILED;
238 &error_code); 241
239 if (error_code != base::PLATFORM_FILE_OK) { 242 return base::File::FILE_OK;
240 return error_code;
241 }
242 DCHECK_NE(base::kInvalidPlatformFileValue, file);
243 if (!base::TruncatePlatformFile(file, length))
244 error_code = base::PLATFORM_FILE_ERROR_FAILED;
245 base::ClosePlatformFile(file);
246 return error_code;
247 } 243 }
248 244
249 bool NativeFileUtil::PathExists(const base::FilePath& path) { 245 bool NativeFileUtil::PathExists(const base::FilePath& path) {
250 return base::PathExists(path); 246 return base::PathExists(path);
251 } 247 }
252 248
253 bool NativeFileUtil::DirectoryExists(const base::FilePath& path) { 249 bool NativeFileUtil::DirectoryExists(const base::FilePath& path) {
254 return base::DirectoryExists(path); 250 return base::DirectoryExists(path);
255 } 251 }
256 252
257 PlatformFileError NativeFileUtil::CopyOrMoveFile( 253 base::File::Error NativeFileUtil::CopyOrMoveFile(
258 const base::FilePath& src_path, 254 const base::FilePath& src_path,
259 const base::FilePath& dest_path, 255 const base::FilePath& dest_path,
260 FileSystemOperation::CopyOrMoveOption option, 256 FileSystemOperation::CopyOrMoveOption option,
261 CopyOrMoveMode mode) { 257 CopyOrMoveMode mode) {
262 base::PlatformFileInfo info; 258 base::File::Info info;
263 base::PlatformFileError error = NativeFileUtil::GetFileInfo(src_path, &info); 259 base::File::Error error = NativeFileUtil::GetFileInfo(src_path, &info);
264 if (error != base::PLATFORM_FILE_OK) 260 if (error != base::File::FILE_OK)
265 return error; 261 return error;
266 if (info.is_directory) 262 if (info.is_directory)
267 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; 263 return base::File::FILE_ERROR_NOT_A_FILE;
268 base::Time last_modified = info.last_modified; 264 base::Time last_modified = info.last_modified;
269 265
270 error = NativeFileUtil::GetFileInfo(dest_path, &info); 266 error = NativeFileUtil::GetFileInfo(dest_path, &info);
271 if (error != base::PLATFORM_FILE_OK && 267 if (error != base::File::FILE_OK &&
272 error != base::PLATFORM_FILE_ERROR_NOT_FOUND) 268 error != base::File::FILE_ERROR_NOT_FOUND)
273 return error; 269 return error;
274 if (info.is_directory) 270 if (info.is_directory)
275 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; 271 return base::File::FILE_ERROR_INVALID_OPERATION;
276 if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) { 272 if (error == base::File::FILE_ERROR_NOT_FOUND) {
277 error = NativeFileUtil::GetFileInfo(dest_path.DirName(), &info); 273 error = NativeFileUtil::GetFileInfo(dest_path.DirName(), &info);
278 if (error != base::PLATFORM_FILE_OK) 274 if (error != base::File::FILE_OK)
279 return error; 275 return error;
280 if (!info.is_directory) 276 if (!info.is_directory)
281 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 277 return base::File::FILE_ERROR_NOT_FOUND;
282 } 278 }
283 279
284 switch (mode) { 280 switch (mode) {
285 case COPY_NOSYNC: 281 case COPY_NOSYNC:
286 if (!base::CopyFile(src_path, dest_path)) 282 if (!base::CopyFile(src_path, dest_path))
287 return base::PLATFORM_FILE_ERROR_FAILED; 283 return base::File::FILE_ERROR_FAILED;
288 break; 284 break;
289 case COPY_SYNC: 285 case COPY_SYNC:
290 if (!CopyFileAndSync(src_path, dest_path)) 286 if (!CopyFileAndSync(src_path, dest_path))
291 return base::PLATFORM_FILE_ERROR_FAILED; 287 return base::File::FILE_ERROR_FAILED;
292 break; 288 break;
293 case MOVE: 289 case MOVE:
294 if (!base::Move(src_path, dest_path)) 290 if (!base::Move(src_path, dest_path))
295 return base::PLATFORM_FILE_ERROR_FAILED; 291 return base::File::FILE_ERROR_FAILED;
296 break; 292 break;
297 } 293 }
298 294
299 // Preserve the last modified time. Do not return error here even if 295 // Preserve the last modified time. Do not return error here even if
300 // the setting is failed, because the copy itself is successfully done. 296 // the setting is failed, because the copy itself is successfully done.
301 if (option == FileSystemOperation::OPTION_PRESERVE_LAST_MODIFIED) 297 if (option == FileSystemOperation::OPTION_PRESERVE_LAST_MODIFIED)
302 base::TouchFile(dest_path, last_modified, last_modified); 298 base::TouchFile(dest_path, last_modified, last_modified);
303 299
304 return base::PLATFORM_FILE_OK; 300 return base::File::FILE_OK;
305 } 301 }
306 302
307 PlatformFileError NativeFileUtil::DeleteFile(const base::FilePath& path) { 303 base::File::Error NativeFileUtil::DeleteFile(const base::FilePath& path) {
308 if (!base::PathExists(path)) 304 if (!base::PathExists(path))
309 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 305 return base::File::FILE_ERROR_NOT_FOUND;
310 if (base::DirectoryExists(path)) 306 if (base::DirectoryExists(path))
311 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; 307 return base::File::FILE_ERROR_NOT_A_FILE;
312 if (!base::DeleteFile(path, false)) 308 if (!base::DeleteFile(path, false))
313 return base::PLATFORM_FILE_ERROR_FAILED; 309 return base::File::FILE_ERROR_FAILED;
314 return base::PLATFORM_FILE_OK; 310 return base::File::FILE_OK;
315 } 311 }
316 312
317 PlatformFileError NativeFileUtil::DeleteDirectory(const base::FilePath& path) { 313 base::File::Error NativeFileUtil::DeleteDirectory(const base::FilePath& path) {
318 if (!base::PathExists(path)) 314 if (!base::PathExists(path))
319 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 315 return base::File::FILE_ERROR_NOT_FOUND;
320 if (!base::DirectoryExists(path)) 316 if (!base::DirectoryExists(path))
321 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; 317 return base::File::FILE_ERROR_NOT_A_DIRECTORY;
322 if (!base::IsDirectoryEmpty(path)) 318 if (!base::IsDirectoryEmpty(path))
323 return base::PLATFORM_FILE_ERROR_NOT_EMPTY; 319 return base::File::FILE_ERROR_NOT_EMPTY;
324 if (!base::DeleteFile(path, false)) 320 if (!base::DeleteFile(path, false))
325 return base::PLATFORM_FILE_ERROR_FAILED; 321 return base::File::FILE_ERROR_FAILED;
326 return base::PLATFORM_FILE_OK; 322 return base::File::FILE_OK;
327 } 323 }
328 324
329 } // namespace fileapi 325 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/browser/fileapi/native_file_util.h ('k') | webkit/browser/fileapi/native_file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698