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