OLD | NEW |
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/local_file_util.h" | 5 #include "webkit/browser/fileapi/local_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/files/file_util_proxy.h" | 9 #include "base/files/file_util_proxy.h" |
10 #include "url/gurl.h" | 10 #include "url/gurl.h" |
11 #include "webkit/browser/fileapi/async_file_util_adapter.h" | 11 #include "webkit/browser/fileapi/async_file_util_adapter.h" |
12 #include "webkit/browser/fileapi/file_system_context.h" | 12 #include "webkit/browser/fileapi/file_system_context.h" |
13 #include "webkit/browser/fileapi/file_system_operation_context.h" | 13 #include "webkit/browser/fileapi/file_system_operation_context.h" |
14 #include "webkit/browser/fileapi/file_system_url.h" | 14 #include "webkit/browser/fileapi/file_system_url.h" |
15 #include "webkit/browser/fileapi/native_file_util.h" | 15 #include "webkit/browser/fileapi/native_file_util.h" |
16 #include "webkit/common/fileapi/file_system_types.h" | 16 #include "webkit/common/fileapi/file_system_types.h" |
17 #include "webkit/common/fileapi/file_system_util.h" | 17 #include "webkit/common/fileapi/file_system_util.h" |
18 | 18 |
19 namespace fileapi { | 19 namespace fileapi { |
20 | 20 |
21 AsyncFileUtil* AsyncFileUtil::CreateForLocalFileSystem() { | 21 AsyncFileUtil* AsyncFileUtil::CreateForLocalFileSystem() { |
22 return new AsyncFileUtilAdapter(new LocalFileUtil()); | 22 return new AsyncFileUtilAdapter(new LocalFileUtil()); |
23 } | 23 } |
24 | 24 |
25 using base::PlatformFileError; | |
26 | |
27 class LocalFileEnumerator : public FileSystemFileUtil::AbstractFileEnumerator { | 25 class LocalFileEnumerator : public FileSystemFileUtil::AbstractFileEnumerator { |
28 public: | 26 public: |
29 LocalFileEnumerator(const base::FilePath& platform_root_path, | 27 LocalFileEnumerator(const base::FilePath& platform_root_path, |
30 const base::FilePath& virtual_root_path, | 28 const base::FilePath& virtual_root_path, |
31 int file_type) | 29 int file_type) |
32 : file_enum_(platform_root_path, false /* recursive */, file_type), | 30 : file_enum_(platform_root_path, false /* recursive */, file_type), |
33 platform_root_path_(platform_root_path), | 31 platform_root_path_(platform_root_path), |
34 virtual_root_path_(virtual_root_path) { | 32 virtual_root_path_(virtual_root_path) { |
35 } | 33 } |
36 | 34 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 } | 69 } |
72 | 70 |
73 bool LocalFileEnumerator::IsDirectory() { | 71 bool LocalFileEnumerator::IsDirectory() { |
74 return file_util_info_.IsDirectory(); | 72 return file_util_info_.IsDirectory(); |
75 } | 73 } |
76 | 74 |
77 LocalFileUtil::LocalFileUtil() {} | 75 LocalFileUtil::LocalFileUtil() {} |
78 | 76 |
79 LocalFileUtil::~LocalFileUtil() {} | 77 LocalFileUtil::~LocalFileUtil() {} |
80 | 78 |
81 PlatformFileError LocalFileUtil::CreateOrOpen( | 79 base::File::Error LocalFileUtil::CreateOrOpen( |
82 FileSystemOperationContext* context, | 80 FileSystemOperationContext* context, |
83 const FileSystemURL& url, int file_flags, | 81 const FileSystemURL& url, int file_flags, |
84 base::PlatformFile* file_handle, bool* created) { | 82 base::PlatformFile* file_handle, bool* created) { |
85 *created = false; | 83 *created = false; |
86 base::FilePath file_path; | 84 base::FilePath file_path; |
87 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 85 base::File::Error error = GetLocalFilePath(context, url, &file_path); |
88 if (error != base::PLATFORM_FILE_OK) | 86 if (error != base::File::FILE_OK) |
89 return error; | 87 return error; |
90 // Disallow opening files in symlinked paths. | 88 // Disallow opening files in symlinked paths. |
91 if (base::IsLink(file_path)) | 89 if (base::IsLink(file_path)) |
92 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 90 return base::File::FILE_ERROR_NOT_FOUND; |
93 return NativeFileUtil::CreateOrOpen( | 91 |
94 file_path, file_flags, file_handle, created); | 92 return NativeFileUtil::CreateOrOpen(file_path, file_flags, file_handle, |
| 93 created); |
95 } | 94 } |
96 | 95 |
97 PlatformFileError LocalFileUtil::Close(FileSystemOperationContext* context, | 96 base::File::Error LocalFileUtil::Close(FileSystemOperationContext* context, |
98 base::PlatformFile file) { | 97 base::PlatformFile file) { |
99 return NativeFileUtil::Close(file); | 98 return NativeFileUtil::Close(file); |
100 } | 99 } |
101 | 100 |
102 PlatformFileError LocalFileUtil::EnsureFileExists( | 101 base::File::Error LocalFileUtil::EnsureFileExists( |
103 FileSystemOperationContext* context, | 102 FileSystemOperationContext* context, |
104 const FileSystemURL& url, | 103 const FileSystemURL& url, |
105 bool* created) { | 104 bool* created) { |
106 base::FilePath file_path; | 105 base::FilePath file_path; |
107 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 106 base::File::Error error = GetLocalFilePath(context, url, &file_path); |
108 if (error != base::PLATFORM_FILE_OK) | 107 if (error != base::File::FILE_OK) |
109 return error; | 108 return error; |
110 return NativeFileUtil::EnsureFileExists(file_path, created); | 109 return NativeFileUtil::EnsureFileExists(file_path, created); |
111 } | 110 } |
112 | 111 |
113 PlatformFileError LocalFileUtil::CreateDirectory( | 112 base::File::Error LocalFileUtil::CreateDirectory( |
114 FileSystemOperationContext* context, | 113 FileSystemOperationContext* context, |
115 const FileSystemURL& url, | 114 const FileSystemURL& url, |
116 bool exclusive, | 115 bool exclusive, |
117 bool recursive) { | 116 bool recursive) { |
118 base::FilePath file_path; | 117 base::FilePath file_path; |
119 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 118 base::File::Error error = GetLocalFilePath(context, url, &file_path); |
120 if (error != base::PLATFORM_FILE_OK) | 119 if (error != base::File::FILE_OK) |
121 return error; | 120 return error; |
122 return NativeFileUtil::CreateDirectory(file_path, exclusive, recursive); | 121 return NativeFileUtil::CreateDirectory(file_path, exclusive, recursive); |
123 } | 122 } |
124 | 123 |
125 PlatformFileError LocalFileUtil::GetFileInfo( | 124 base::File::Error LocalFileUtil::GetFileInfo( |
126 FileSystemOperationContext* context, | 125 FileSystemOperationContext* context, |
127 const FileSystemURL& url, | 126 const FileSystemURL& url, |
128 base::PlatformFileInfo* file_info, | 127 base::File::Info* file_info, |
129 base::FilePath* platform_file_path) { | 128 base::FilePath* platform_file_path) { |
130 base::FilePath file_path; | 129 base::FilePath file_path; |
131 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 130 base::File::Error error = GetLocalFilePath(context, url, &file_path); |
132 if (error != base::PLATFORM_FILE_OK) | 131 if (error != base::File::FILE_OK) |
133 return error; | 132 return error; |
134 // We should not follow symbolic links in sandboxed file system. | 133 // We should not follow symbolic links in sandboxed file system. |
135 if (base::IsLink(file_path)) | 134 if (base::IsLink(file_path)) |
136 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 135 return base::File::FILE_ERROR_NOT_FOUND; |
| 136 |
137 error = NativeFileUtil::GetFileInfo(file_path, file_info); | 137 error = NativeFileUtil::GetFileInfo(file_path, file_info); |
138 if (error == base::PLATFORM_FILE_OK) | 138 if (error == base::File::FILE_OK) |
139 *platform_file_path = file_path; | 139 *platform_file_path = file_path; |
140 return error; | 140 return error; |
141 } | 141 } |
142 | 142 |
143 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> LocalFileUtil:: | 143 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> LocalFileUtil:: |
144 CreateFileEnumerator( | 144 CreateFileEnumerator( |
145 FileSystemOperationContext* context, | 145 FileSystemOperationContext* context, |
146 const FileSystemURL& root_url) { | 146 const FileSystemURL& root_url) { |
147 base::FilePath file_path; | 147 base::FilePath file_path; |
148 if (GetLocalFilePath(context, root_url, &file_path) != | 148 if (GetLocalFilePath(context, root_url, &file_path) != |
149 base::PLATFORM_FILE_OK) { | 149 base::File::FILE_OK) { |
150 return make_scoped_ptr(new EmptyFileEnumerator) | 150 return make_scoped_ptr(new EmptyFileEnumerator) |
151 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); | 151 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); |
152 } | 152 } |
153 return make_scoped_ptr(new LocalFileEnumerator( | 153 return make_scoped_ptr(new LocalFileEnumerator( |
154 file_path, root_url.path(), | 154 file_path, root_url.path(), |
155 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES)) | 155 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES)) |
156 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); | 156 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); |
157 } | 157 } |
158 | 158 |
159 PlatformFileError LocalFileUtil::GetLocalFilePath( | 159 base::File::Error LocalFileUtil::GetLocalFilePath( |
160 FileSystemOperationContext* context, | 160 FileSystemOperationContext* context, |
161 const FileSystemURL& url, | 161 const FileSystemURL& url, |
162 base::FilePath* local_file_path) { | 162 base::FilePath* local_file_path) { |
163 DCHECK(local_file_path); | 163 DCHECK(local_file_path); |
164 DCHECK(url.is_valid()); | 164 DCHECK(url.is_valid()); |
165 if (url.path().empty()) { | 165 if (url.path().empty()) { |
166 // Root direcory case, which should not be accessed. | 166 // Root direcory case, which should not be accessed. |
167 return base::PLATFORM_FILE_ERROR_ACCESS_DENIED; | 167 return base::File::FILE_ERROR_ACCESS_DENIED; |
168 } | 168 } |
169 *local_file_path = url.path(); | 169 *local_file_path = url.path(); |
170 return base::PLATFORM_FILE_OK; | 170 return base::File::FILE_OK; |
171 } | 171 } |
172 | 172 |
173 PlatformFileError LocalFileUtil::Touch( | 173 base::File::Error LocalFileUtil::Touch( |
174 FileSystemOperationContext* context, | 174 FileSystemOperationContext* context, |
175 const FileSystemURL& url, | 175 const FileSystemURL& url, |
176 const base::Time& last_access_time, | 176 const base::Time& last_access_time, |
177 const base::Time& last_modified_time) { | 177 const base::Time& last_modified_time) { |
178 base::FilePath file_path; | 178 base::FilePath file_path; |
179 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 179 base::File::Error error = GetLocalFilePath(context, url, &file_path); |
180 if (error != base::PLATFORM_FILE_OK) | 180 if (error != base::File::FILE_OK) |
181 return error; | 181 return error; |
182 return NativeFileUtil::Touch(file_path, last_access_time, last_modified_time); | 182 return NativeFileUtil::Touch(file_path, last_access_time, last_modified_time); |
183 } | 183 } |
184 | 184 |
185 PlatformFileError LocalFileUtil::Truncate( | 185 base::File::Error LocalFileUtil::Truncate( |
186 FileSystemOperationContext* context, | 186 FileSystemOperationContext* context, |
187 const FileSystemURL& url, | 187 const FileSystemURL& url, |
188 int64 length) { | 188 int64 length) { |
189 base::FilePath file_path; | 189 base::FilePath file_path; |
190 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 190 base::File::Error error = GetLocalFilePath(context, url, &file_path); |
191 if (error != base::PLATFORM_FILE_OK) | 191 if (error != base::File::FILE_OK) |
192 return error; | 192 return error; |
193 return NativeFileUtil::Truncate(file_path, length); | 193 return NativeFileUtil::Truncate(file_path, length); |
194 } | 194 } |
195 | 195 |
196 PlatformFileError LocalFileUtil::CopyOrMoveFile( | 196 base::File::Error LocalFileUtil::CopyOrMoveFile( |
197 FileSystemOperationContext* context, | 197 FileSystemOperationContext* context, |
198 const FileSystemURL& src_url, | 198 const FileSystemURL& src_url, |
199 const FileSystemURL& dest_url, | 199 const FileSystemURL& dest_url, |
200 CopyOrMoveOption option, | 200 CopyOrMoveOption option, |
201 bool copy) { | 201 bool copy) { |
202 base::FilePath src_file_path; | 202 base::FilePath src_file_path; |
203 PlatformFileError error = GetLocalFilePath(context, src_url, &src_file_path); | 203 base::File::Error error = GetLocalFilePath(context, src_url, &src_file_path); |
204 if (error != base::PLATFORM_FILE_OK) | 204 if (error != base::File::FILE_OK) |
205 return error; | 205 return error; |
206 | 206 |
207 base::FilePath dest_file_path; | 207 base::FilePath dest_file_path; |
208 error = GetLocalFilePath(context, dest_url, &dest_file_path); | 208 error = GetLocalFilePath(context, dest_url, &dest_file_path); |
209 if (error != base::PLATFORM_FILE_OK) | 209 if (error != base::File::FILE_OK) |
210 return error; | 210 return error; |
211 | 211 |
212 return NativeFileUtil::CopyOrMoveFile( | 212 return NativeFileUtil::CopyOrMoveFile( |
213 src_file_path, dest_file_path, option, | 213 src_file_path, dest_file_path, option, |
214 fileapi::NativeFileUtil::CopyOrMoveModeForDestination(dest_url, copy)); | 214 fileapi::NativeFileUtil::CopyOrMoveModeForDestination(dest_url, copy)); |
215 } | 215 } |
216 | 216 |
217 PlatformFileError LocalFileUtil::CopyInForeignFile( | 217 base::File::Error LocalFileUtil::CopyInForeignFile( |
218 FileSystemOperationContext* context, | 218 FileSystemOperationContext* context, |
219 const base::FilePath& src_file_path, | 219 const base::FilePath& src_file_path, |
220 const FileSystemURL& dest_url) { | 220 const FileSystemURL& dest_url) { |
221 if (src_file_path.empty()) | 221 if (src_file_path.empty()) |
222 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 222 return base::File::FILE_ERROR_INVALID_OPERATION; |
223 | 223 |
224 base::FilePath dest_file_path; | 224 base::FilePath dest_file_path; |
225 PlatformFileError error = | 225 base::File::Error error = |
226 GetLocalFilePath(context, dest_url, &dest_file_path); | 226 GetLocalFilePath(context, dest_url, &dest_file_path); |
227 if (error != base::PLATFORM_FILE_OK) | 227 if (error != base::File::FILE_OK) |
228 return error; | 228 return error; |
229 return NativeFileUtil::CopyOrMoveFile( | 229 return NativeFileUtil::CopyOrMoveFile( |
230 src_file_path, dest_file_path, FileSystemOperation::OPTION_NONE, | 230 src_file_path, dest_file_path, FileSystemOperation::OPTION_NONE, |
231 fileapi::NativeFileUtil::CopyOrMoveModeForDestination(dest_url, | 231 fileapi::NativeFileUtil::CopyOrMoveModeForDestination(dest_url, |
232 true /* copy */)); | 232 true /* copy */)); |
233 } | 233 } |
234 | 234 |
235 PlatformFileError LocalFileUtil::DeleteFile( | 235 base::File::Error LocalFileUtil::DeleteFile( |
236 FileSystemOperationContext* context, | 236 FileSystemOperationContext* context, |
237 const FileSystemURL& url) { | 237 const FileSystemURL& url) { |
238 base::FilePath file_path; | 238 base::FilePath file_path; |
239 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 239 base::File::Error error = GetLocalFilePath(context, url, &file_path); |
240 if (error != base::PLATFORM_FILE_OK) | 240 if (error != base::File::FILE_OK) |
241 return error; | 241 return error; |
242 return NativeFileUtil::DeleteFile(file_path); | 242 return NativeFileUtil::DeleteFile(file_path); |
243 } | 243 } |
244 | 244 |
245 PlatformFileError LocalFileUtil::DeleteDirectory( | 245 base::File::Error LocalFileUtil::DeleteDirectory( |
246 FileSystemOperationContext* context, | 246 FileSystemOperationContext* context, |
247 const FileSystemURL& url) { | 247 const FileSystemURL& url) { |
248 base::FilePath file_path; | 248 base::FilePath file_path; |
249 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 249 base::File::Error error = GetLocalFilePath(context, url, &file_path); |
250 if (error != base::PLATFORM_FILE_OK) | 250 if (error != base::File::FILE_OK) |
251 return error; | 251 return error; |
252 return NativeFileUtil::DeleteDirectory(file_path); | 252 return NativeFileUtil::DeleteDirectory(file_path); |
253 } | 253 } |
254 | 254 |
255 webkit_blob::ScopedFile LocalFileUtil::CreateSnapshotFile( | 255 webkit_blob::ScopedFile LocalFileUtil::CreateSnapshotFile( |
256 FileSystemOperationContext* context, | 256 FileSystemOperationContext* context, |
257 const FileSystemURL& url, | 257 const FileSystemURL& url, |
258 base::PlatformFileError* error, | 258 base::File::Error* error, |
259 base::PlatformFileInfo* file_info, | 259 base::File::Info* file_info, |
260 base::FilePath* platform_path) { | 260 base::FilePath* platform_path) { |
261 DCHECK(file_info); | 261 DCHECK(file_info); |
262 // We're just returning the local file information. | 262 // We're just returning the local file information. |
263 *error = GetFileInfo(context, url, file_info, platform_path); | 263 *error = GetFileInfo(context, url, file_info, platform_path); |
264 if (*error == base::PLATFORM_FILE_OK && file_info->is_directory) | 264 if (*error == base::File::FILE_OK && file_info->is_directory) |
265 *error = base::PLATFORM_FILE_ERROR_NOT_A_FILE; | 265 *error = base::File::FILE_ERROR_NOT_A_FILE; |
266 return webkit_blob::ScopedFile(); | 266 return webkit_blob::ScopedFile(); |
267 } | 267 } |
268 | 268 |
269 } // namespace fileapi | 269 } // namespace fileapi |
OLD | NEW |