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/fileapi/local_file_util.h" | 5 #include "webkit/fileapi/local_file_util.h" |
6 | 6 |
7 #include "base/files/file_util_proxy.h" | 7 #include "base/files/file_util_proxy.h" |
8 #include "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
9 #include "webkit/fileapi/file_system_context.h" | 9 #include "webkit/fileapi/file_system_context.h" |
10 #include "webkit/fileapi/file_system_mount_point_provider.h" | 10 #include "webkit/fileapi/file_system_mount_point_provider.h" |
11 #include "webkit/fileapi/file_system_operation_context.h" | 11 #include "webkit/fileapi/file_system_operation_context.h" |
12 #include "webkit/fileapi/file_system_types.h" | 12 #include "webkit/fileapi/file_system_types.h" |
13 #include "webkit/fileapi/file_system_url.h" | 13 #include "webkit/fileapi/file_system_url.h" |
14 #include "webkit/fileapi/file_system_util.h" | 14 #include "webkit/fileapi/file_system_util.h" |
15 #include "webkit/fileapi/native_file_util.h" | 15 #include "webkit/fileapi/native_file_util.h" |
16 | 16 |
17 namespace fileapi { | 17 namespace fileapi { |
18 | 18 |
19 using base::PlatformFileError; | 19 using base::PlatformFileError; |
20 | 20 |
21 class LocalFileEnumerator : public FileSystemFileUtil::AbstractFileEnumerator { | 21 class LocalFileEnumerator : public FileSystemFileUtil::AbstractFileEnumerator { |
22 public: | 22 public: |
23 LocalFileEnumerator(const FilePath& platform_root_path, | 23 LocalFileEnumerator(const base::FilePath& platform_root_path, |
24 const FilePath& virtual_root_path, | 24 const base::FilePath& virtual_root_path, |
25 bool recursive, | 25 bool recursive, |
26 int file_type) | 26 int file_type) |
27 : file_enum_(platform_root_path, recursive, file_type), | 27 : file_enum_(platform_root_path, recursive, file_type), |
28 platform_root_path_(platform_root_path), | 28 platform_root_path_(platform_root_path), |
29 virtual_root_path_(virtual_root_path) { | 29 virtual_root_path_(virtual_root_path) { |
30 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
31 memset(&file_util_info_, 0, sizeof(file_util_info_)); | 31 memset(&file_util_info_, 0, sizeof(file_util_info_)); |
32 #endif // defined(OS_WIN) | 32 #endif // defined(OS_WIN) |
33 } | 33 } |
34 | 34 |
35 ~LocalFileEnumerator() {} | 35 ~LocalFileEnumerator() {} |
36 | 36 |
37 virtual FilePath Next() OVERRIDE; | 37 virtual base::FilePath Next() OVERRIDE; |
38 virtual int64 Size() OVERRIDE; | 38 virtual int64 Size() OVERRIDE; |
39 virtual base::Time LastModifiedTime() OVERRIDE; | 39 virtual base::Time LastModifiedTime() OVERRIDE; |
40 virtual bool IsDirectory() OVERRIDE; | 40 virtual bool IsDirectory() OVERRIDE; |
41 | 41 |
42 private: | 42 private: |
43 file_util::FileEnumerator file_enum_; | 43 file_util::FileEnumerator file_enum_; |
44 file_util::FileEnumerator::FindInfo file_util_info_; | 44 file_util::FileEnumerator::FindInfo file_util_info_; |
45 FilePath platform_root_path_; | 45 base::FilePath platform_root_path_; |
46 FilePath virtual_root_path_; | 46 base::FilePath virtual_root_path_; |
47 }; | 47 }; |
48 | 48 |
49 FilePath LocalFileEnumerator::Next() { | 49 base::FilePath LocalFileEnumerator::Next() { |
50 FilePath next = file_enum_.Next(); | 50 base::FilePath next = file_enum_.Next(); |
51 // Don't return symlinks. | 51 // Don't return symlinks. |
52 while (!next.empty() && file_util::IsLink(next)) | 52 while (!next.empty() && file_util::IsLink(next)) |
53 next = file_enum_.Next(); | 53 next = file_enum_.Next(); |
54 if (next.empty()) | 54 if (next.empty()) |
55 return next; | 55 return next; |
56 file_enum_.GetFindInfo(&file_util_info_); | 56 file_enum_.GetFindInfo(&file_util_info_); |
57 | 57 |
58 FilePath path; | 58 base::FilePath path; |
59 platform_root_path_.AppendRelativePath(next, &path); | 59 platform_root_path_.AppendRelativePath(next, &path); |
60 return virtual_root_path_.Append(path); | 60 return virtual_root_path_.Append(path); |
61 } | 61 } |
62 | 62 |
63 int64 LocalFileEnumerator::Size() { | 63 int64 LocalFileEnumerator::Size() { |
64 return file_util::FileEnumerator::GetFilesize(file_util_info_); | 64 return file_util::FileEnumerator::GetFilesize(file_util_info_); |
65 } | 65 } |
66 | 66 |
67 base::Time LocalFileEnumerator::LastModifiedTime() { | 67 base::Time LocalFileEnumerator::LastModifiedTime() { |
68 return file_util::FileEnumerator::GetLastModifiedTime(file_util_info_); | 68 return file_util::FileEnumerator::GetLastModifiedTime(file_util_info_); |
69 } | 69 } |
70 | 70 |
71 bool LocalFileEnumerator::IsDirectory() { | 71 bool LocalFileEnumerator::IsDirectory() { |
72 return file_util::FileEnumerator::IsDirectory(file_util_info_); | 72 return file_util::FileEnumerator::IsDirectory(file_util_info_); |
73 } | 73 } |
74 | 74 |
75 LocalFileUtil::LocalFileUtil() { | 75 LocalFileUtil::LocalFileUtil() { |
76 } | 76 } |
77 | 77 |
78 LocalFileUtil::~LocalFileUtil() { | 78 LocalFileUtil::~LocalFileUtil() { |
79 } | 79 } |
80 | 80 |
81 PlatformFileError LocalFileUtil::CreateOrOpen( | 81 PlatformFileError LocalFileUtil::CreateOrOpen( |
82 FileSystemOperationContext* context, | 82 FileSystemOperationContext* context, |
83 const FileSystemURL& url, int file_flags, | 83 const FileSystemURL& url, int file_flags, |
84 base::PlatformFile* file_handle, bool* created) { | 84 base::PlatformFile* file_handle, bool* created) { |
85 FilePath file_path; | 85 base::FilePath file_path; |
86 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 86 PlatformFileError error = GetLocalFilePath(context, url, &file_path); |
87 if (error != base::PLATFORM_FILE_OK) | 87 if (error != base::PLATFORM_FILE_OK) |
88 return error; | 88 return error; |
89 return NativeFileUtil::CreateOrOpen( | 89 return NativeFileUtil::CreateOrOpen( |
90 file_path, file_flags, file_handle, created); | 90 file_path, file_flags, file_handle, created); |
91 } | 91 } |
92 | 92 |
93 PlatformFileError LocalFileUtil::Close(FileSystemOperationContext* context, | 93 PlatformFileError LocalFileUtil::Close(FileSystemOperationContext* context, |
94 base::PlatformFile file) { | 94 base::PlatformFile file) { |
95 return NativeFileUtil::Close(file); | 95 return NativeFileUtil::Close(file); |
96 } | 96 } |
97 | 97 |
98 PlatformFileError LocalFileUtil::EnsureFileExists( | 98 PlatformFileError LocalFileUtil::EnsureFileExists( |
99 FileSystemOperationContext* context, | 99 FileSystemOperationContext* context, |
100 const FileSystemURL& url, | 100 const FileSystemURL& url, |
101 bool* created) { | 101 bool* created) { |
102 FilePath file_path; | 102 base::FilePath file_path; |
103 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 103 PlatformFileError error = GetLocalFilePath(context, url, &file_path); |
104 if (error != base::PLATFORM_FILE_OK) | 104 if (error != base::PLATFORM_FILE_OK) |
105 return error; | 105 return error; |
106 return NativeFileUtil::EnsureFileExists(file_path, created); | 106 return NativeFileUtil::EnsureFileExists(file_path, created); |
107 } | 107 } |
108 | 108 |
109 PlatformFileError LocalFileUtil::CreateDirectory( | 109 PlatformFileError LocalFileUtil::CreateDirectory( |
110 FileSystemOperationContext* context, | 110 FileSystemOperationContext* context, |
111 const FileSystemURL& url, | 111 const FileSystemURL& url, |
112 bool exclusive, | 112 bool exclusive, |
113 bool recursive) { | 113 bool recursive) { |
114 FilePath file_path; | 114 base::FilePath file_path; |
115 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 115 PlatformFileError error = GetLocalFilePath(context, url, &file_path); |
116 if (error != base::PLATFORM_FILE_OK) | 116 if (error != base::PLATFORM_FILE_OK) |
117 return error; | 117 return error; |
118 return NativeFileUtil::CreateDirectory(file_path, exclusive, recursive); | 118 return NativeFileUtil::CreateDirectory(file_path, exclusive, recursive); |
119 } | 119 } |
120 | 120 |
121 PlatformFileError LocalFileUtil::GetFileInfo( | 121 PlatformFileError LocalFileUtil::GetFileInfo( |
122 FileSystemOperationContext* context, | 122 FileSystemOperationContext* context, |
123 const FileSystemURL& url, | 123 const FileSystemURL& url, |
124 base::PlatformFileInfo* file_info, | 124 base::PlatformFileInfo* file_info, |
125 FilePath* platform_file_path) { | 125 base::FilePath* platform_file_path) { |
126 FilePath file_path; | 126 base::FilePath file_path; |
127 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 127 PlatformFileError error = GetLocalFilePath(context, url, &file_path); |
128 if (error != base::PLATFORM_FILE_OK) | 128 if (error != base::PLATFORM_FILE_OK) |
129 return error; | 129 return error; |
130 // We should not follow symbolic links in sandboxed file system. | 130 // We should not follow symbolic links in sandboxed file system. |
131 if (file_util::IsLink(file_path)) | 131 if (file_util::IsLink(file_path)) |
132 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 132 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
133 error = NativeFileUtil::GetFileInfo(file_path, file_info); | 133 error = NativeFileUtil::GetFileInfo(file_path, file_info); |
134 if (error == base::PLATFORM_FILE_OK) | 134 if (error == base::PLATFORM_FILE_OK) |
135 *platform_file_path = file_path; | 135 *platform_file_path = file_path; |
136 return error; | 136 return error; |
137 } | 137 } |
138 | 138 |
139 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> LocalFileUtil:: | 139 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> LocalFileUtil:: |
140 CreateFileEnumerator( | 140 CreateFileEnumerator( |
141 FileSystemOperationContext* context, | 141 FileSystemOperationContext* context, |
142 const FileSystemURL& root_url, | 142 const FileSystemURL& root_url, |
143 bool recursive) { | 143 bool recursive) { |
144 FilePath file_path; | 144 base::FilePath file_path; |
145 if (GetLocalFilePath(context, root_url, &file_path) != | 145 if (GetLocalFilePath(context, root_url, &file_path) != |
146 base::PLATFORM_FILE_OK) { | 146 base::PLATFORM_FILE_OK) { |
147 return make_scoped_ptr(new EmptyFileEnumerator) | 147 return make_scoped_ptr(new EmptyFileEnumerator) |
148 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); | 148 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); |
149 } | 149 } |
150 return make_scoped_ptr(new LocalFileEnumerator( | 150 return make_scoped_ptr(new LocalFileEnumerator( |
151 file_path, root_url.path(), recursive, | 151 file_path, root_url.path(), recursive, |
152 file_util::FileEnumerator::FILES | | 152 file_util::FileEnumerator::FILES | |
153 file_util::FileEnumerator::DIRECTORIES)) | 153 file_util::FileEnumerator::DIRECTORIES)) |
154 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); | 154 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); |
155 } | 155 } |
156 | 156 |
157 PlatformFileError LocalFileUtil::GetLocalFilePath( | 157 PlatformFileError LocalFileUtil::GetLocalFilePath( |
158 FileSystemOperationContext* context, | 158 FileSystemOperationContext* context, |
159 const FileSystemURL& url, | 159 const FileSystemURL& url, |
160 FilePath* local_file_path) { | 160 base::FilePath* local_file_path) { |
161 FileSystemMountPointProvider* provider = | 161 FileSystemMountPointProvider* provider = |
162 context->file_system_context()->GetMountPointProvider(url.type()); | 162 context->file_system_context()->GetMountPointProvider(url.type()); |
163 DCHECK(provider); | 163 DCHECK(provider); |
164 FilePath root = provider->GetFileSystemRootPathOnFileThread(url, false); | 164 base::FilePath root = provider->GetFileSystemRootPathOnFileThread(url, false); |
165 if (root.empty()) | 165 if (root.empty()) |
166 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 166 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
167 *local_file_path = root.Append(url.path()); | 167 *local_file_path = root.Append(url.path()); |
168 return base::PLATFORM_FILE_OK; | 168 return base::PLATFORM_FILE_OK; |
169 } | 169 } |
170 | 170 |
171 PlatformFileError LocalFileUtil::Touch( | 171 PlatformFileError LocalFileUtil::Touch( |
172 FileSystemOperationContext* context, | 172 FileSystemOperationContext* context, |
173 const FileSystemURL& url, | 173 const FileSystemURL& url, |
174 const base::Time& last_access_time, | 174 const base::Time& last_access_time, |
175 const base::Time& last_modified_time) { | 175 const base::Time& last_modified_time) { |
176 FilePath file_path; | 176 base::FilePath file_path; |
177 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 177 PlatformFileError error = GetLocalFilePath(context, url, &file_path); |
178 if (error != base::PLATFORM_FILE_OK) | 178 if (error != base::PLATFORM_FILE_OK) |
179 return error; | 179 return error; |
180 return NativeFileUtil::Touch(file_path, last_access_time, last_modified_time); | 180 return NativeFileUtil::Touch(file_path, last_access_time, last_modified_time); |
181 } | 181 } |
182 | 182 |
183 PlatformFileError LocalFileUtil::Truncate( | 183 PlatformFileError LocalFileUtil::Truncate( |
184 FileSystemOperationContext* context, | 184 FileSystemOperationContext* context, |
185 const FileSystemURL& url, | 185 const FileSystemURL& url, |
186 int64 length) { | 186 int64 length) { |
187 FilePath file_path; | 187 base::FilePath file_path; |
188 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 188 PlatformFileError error = GetLocalFilePath(context, url, &file_path); |
189 if (error != base::PLATFORM_FILE_OK) | 189 if (error != base::PLATFORM_FILE_OK) |
190 return error; | 190 return error; |
191 return NativeFileUtil::Truncate(file_path, length); | 191 return NativeFileUtil::Truncate(file_path, length); |
192 } | 192 } |
193 | 193 |
194 PlatformFileError LocalFileUtil::CopyOrMoveFile( | 194 PlatformFileError LocalFileUtil::CopyOrMoveFile( |
195 FileSystemOperationContext* context, | 195 FileSystemOperationContext* context, |
196 const FileSystemURL& src_url, | 196 const FileSystemURL& src_url, |
197 const FileSystemURL& dest_url, | 197 const FileSystemURL& dest_url, |
198 bool copy) { | 198 bool copy) { |
199 FilePath src_file_path; | 199 base::FilePath src_file_path; |
200 PlatformFileError error = GetLocalFilePath(context, src_url, &src_file_path); | 200 PlatformFileError error = GetLocalFilePath(context, src_url, &src_file_path); |
201 if (error != base::PLATFORM_FILE_OK) | 201 if (error != base::PLATFORM_FILE_OK) |
202 return error; | 202 return error; |
203 | 203 |
204 FilePath dest_file_path; | 204 base::FilePath dest_file_path; |
205 error = GetLocalFilePath(context, dest_url, &dest_file_path); | 205 error = GetLocalFilePath(context, dest_url, &dest_file_path); |
206 if (error != base::PLATFORM_FILE_OK) | 206 if (error != base::PLATFORM_FILE_OK) |
207 return error; | 207 return error; |
208 | 208 |
209 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, copy); | 209 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, copy); |
210 } | 210 } |
211 | 211 |
212 PlatformFileError LocalFileUtil::CopyInForeignFile( | 212 PlatformFileError LocalFileUtil::CopyInForeignFile( |
213 FileSystemOperationContext* context, | 213 FileSystemOperationContext* context, |
214 const FilePath& src_file_path, | 214 const base::FilePath& src_file_path, |
215 const FileSystemURL& dest_url) { | 215 const FileSystemURL& dest_url) { |
216 if (src_file_path.empty()) | 216 if (src_file_path.empty()) |
217 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 217 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
218 | 218 |
219 FilePath dest_file_path; | 219 base::FilePath dest_file_path; |
220 PlatformFileError error = | 220 PlatformFileError error = |
221 GetLocalFilePath(context, dest_url, &dest_file_path); | 221 GetLocalFilePath(context, dest_url, &dest_file_path); |
222 if (error != base::PLATFORM_FILE_OK) | 222 if (error != base::PLATFORM_FILE_OK) |
223 return error; | 223 return error; |
224 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, true); | 224 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, true); |
225 } | 225 } |
226 | 226 |
227 PlatformFileError LocalFileUtil::DeleteFile( | 227 PlatformFileError LocalFileUtil::DeleteFile( |
228 FileSystemOperationContext* context, | 228 FileSystemOperationContext* context, |
229 const FileSystemURL& url) { | 229 const FileSystemURL& url) { |
230 FilePath file_path; | 230 base::FilePath file_path; |
231 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 231 PlatformFileError error = GetLocalFilePath(context, url, &file_path); |
232 if (error != base::PLATFORM_FILE_OK) | 232 if (error != base::PLATFORM_FILE_OK) |
233 return error; | 233 return error; |
234 return NativeFileUtil::DeleteFile(file_path); | 234 return NativeFileUtil::DeleteFile(file_path); |
235 } | 235 } |
236 | 236 |
237 PlatformFileError LocalFileUtil::DeleteDirectory( | 237 PlatformFileError LocalFileUtil::DeleteDirectory( |
238 FileSystemOperationContext* context, | 238 FileSystemOperationContext* context, |
239 const FileSystemURL& url) { | 239 const FileSystemURL& url) { |
240 FilePath file_path; | 240 base::FilePath file_path; |
241 PlatformFileError error = GetLocalFilePath(context, url, &file_path); | 241 PlatformFileError error = GetLocalFilePath(context, url, &file_path); |
242 if (error != base::PLATFORM_FILE_OK) | 242 if (error != base::PLATFORM_FILE_OK) |
243 return error; | 243 return error; |
244 return NativeFileUtil::DeleteDirectory(file_path); | 244 return NativeFileUtil::DeleteDirectory(file_path); |
245 } | 245 } |
246 | 246 |
247 base::PlatformFileError LocalFileUtil::CreateSnapshotFile( | 247 base::PlatformFileError LocalFileUtil::CreateSnapshotFile( |
248 FileSystemOperationContext* context, | 248 FileSystemOperationContext* context, |
249 const FileSystemURL& url, | 249 const FileSystemURL& url, |
250 base::PlatformFileInfo* file_info, | 250 base::PlatformFileInfo* file_info, |
251 FilePath* platform_path, | 251 base::FilePath* platform_path, |
252 SnapshotFilePolicy* policy) { | 252 SnapshotFilePolicy* policy) { |
253 DCHECK(policy); | 253 DCHECK(policy); |
254 DCHECK(file_info); | 254 DCHECK(file_info); |
255 // We're just returning the local file information. | 255 // We're just returning the local file information. |
256 *policy = kSnapshotFileLocal; | 256 *policy = kSnapshotFileLocal; |
257 base::PlatformFileError error = | 257 base::PlatformFileError error = |
258 GetFileInfo(context, url, file_info, platform_path); | 258 GetFileInfo(context, url, file_info, platform_path); |
259 if (error == base::PLATFORM_FILE_OK && file_info->is_directory) | 259 if (error == base::PLATFORM_FILE_OK && file_info->is_directory) |
260 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; | 260 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; |
261 return error; | 261 return error; |
262 } | 262 } |
263 | 263 |
264 } // namespace fileapi | 264 } // namespace fileapi |
OLD | NEW |