OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_system_file_util.h" | 5 #include "webkit/fileapi/local_file_util.h" |
6 | 6 |
7 #include "base/file_util_proxy.h" | 7 #include "base/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_operation_context.h" | 10 #include "webkit/fileapi/file_system_operation_context.h" |
11 #include "webkit/fileapi/file_system_path_manager.h" | 11 #include "webkit/fileapi/file_system_path_manager.h" |
12 #include "webkit/fileapi/file_system_types.h" | 12 #include "webkit/fileapi/file_system_types.h" |
13 #include "webkit/fileapi/file_system_util.h" | 13 #include "webkit/fileapi/file_system_util.h" |
14 | 14 |
15 namespace fileapi { | 15 namespace fileapi { |
16 | 16 |
17 LocalFileSystemFileUtil::LocalFileSystemFileUtil( | 17 LocalFileUtil::LocalFileUtil(FileApiFileUtil* underlying_file_util) |
18 FileSystemFileUtil* underlying_file_util) | 18 : OverlayFileUtil(underlying_file_util) { |
19 : underlying_file_util_(underlying_file_util) { | |
20 } | 19 } |
21 | 20 |
22 LocalFileSystemFileUtil::~LocalFileSystemFileUtil() { | 21 LocalFileUtil::~LocalFileUtil() { |
23 } | 22 } |
24 | 23 |
25 PlatformFileError LocalFileSystemFileUtil::CreateOrOpen( | 24 PlatformFileError LocalFileUtil::CreateOrOpen( |
26 FileSystemOperationContext* context, | 25 FileSystemOperationContext* context, |
27 const FilePath& file_path, int file_flags, | 26 const FilePath& file_path, int file_flags, |
28 PlatformFile* file_handle, bool* created) { | 27 PlatformFile* file_handle, bool* created) { |
29 FilePath local_path = | 28 FilePath local_path = |
30 GetLocalPath(context, context->src_origin_url(), context->src_type(), | 29 GetLocalPath(context, context->src_origin_url(), context->src_type(), |
31 file_path); | 30 file_path); |
32 if (local_path.empty()) | 31 if (local_path.empty()) |
33 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 32 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
34 return underlying_file_util_->CreateOrOpen( | 33 return underlying_file_util_->CreateOrOpen( |
35 context, local_path, file_flags, file_handle, created); | 34 context, local_path, file_flags, file_handle, created); |
36 } | 35 } |
37 | 36 |
38 PlatformFileError LocalFileSystemFileUtil::EnsureFileExists( | 37 PlatformFileError LocalFileUtil::EnsureFileExists( |
39 FileSystemOperationContext* context, | 38 FileSystemOperationContext* context, |
40 const FilePath& file_path, | 39 const FilePath& file_path, |
41 bool* created) { | 40 bool* created) { |
42 FilePath local_path = | 41 FilePath local_path = |
43 GetLocalPath(context, context->src_origin_url(), context->src_type(), | 42 GetLocalPath(context, context->src_origin_url(), context->src_type(), |
44 file_path); | 43 file_path); |
45 if (local_path.empty()) | 44 if (local_path.empty()) |
46 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 45 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
47 return underlying_file_util_->EnsureFileExists( | 46 return underlying_file_util_->EnsureFileExists( |
48 context, local_path, created); | 47 context, local_path, created); |
49 } | 48 } |
50 | 49 |
51 PlatformFileError LocalFileSystemFileUtil::GetLocalFilePath( | 50 PlatformFileError LocalFileUtil::GetLocalFilePath( |
52 FileSystemOperationContext* context, | 51 FileSystemOperationContext* context, |
53 const FilePath& virtual_path, | 52 const FilePath& virtual_path, |
54 FilePath* local_path) { | 53 FilePath* local_path) { |
55 FilePath path = | 54 FilePath path = |
56 GetLocalPath(context, context->src_origin_url(), context->src_type(), | 55 GetLocalPath(context, context->src_origin_url(), context->src_type(), |
57 virtual_path); | 56 virtual_path); |
58 if (path.empty()) | 57 if (path.empty()) |
59 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 58 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
60 | 59 |
61 *local_path = path; | 60 *local_path = path; |
62 return base::PLATFORM_FILE_OK; | 61 return base::PLATFORM_FILE_OK; |
63 } | 62 } |
64 | 63 |
65 PlatformFileError LocalFileSystemFileUtil::GetFileInfo( | 64 PlatformFileError LocalFileUtil::GetFileInfo( |
66 FileSystemOperationContext* context, | 65 FileSystemOperationContext* context, |
67 const FilePath& file_path, | 66 const FilePath& file_path, |
68 base::PlatformFileInfo* file_info, | 67 base::PlatformFileInfo* file_info, |
69 FilePath* platform_file_path) { | 68 FilePath* platform_file_path) { |
70 FilePath local_path = | 69 FilePath local_path = |
71 GetLocalPath(context, context->src_origin_url(), context->src_type(), | 70 GetLocalPath(context, context->src_origin_url(), context->src_type(), |
72 file_path); | 71 file_path); |
73 if (local_path.empty()) | 72 if (local_path.empty()) |
74 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 73 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
75 return underlying_file_util_->GetFileInfo( | 74 return underlying_file_util_->GetFileInfo( |
76 context, local_path, file_info, platform_file_path); | 75 context, local_path, file_info, platform_file_path); |
77 } | 76 } |
78 | 77 |
79 PlatformFileError LocalFileSystemFileUtil::ReadDirectory( | 78 PlatformFileError LocalFileUtil::ReadDirectory( |
80 FileSystemOperationContext* context, | 79 FileSystemOperationContext* context, |
81 const FilePath& file_path, | 80 const FilePath& file_path, |
82 std::vector<base::FileUtilProxy::Entry>* entries) { | 81 std::vector<base::FileUtilProxy::Entry>* entries) { |
83 // TODO(kkanetkar): Implement directory read in multiple chunks. | 82 // TODO(kkanetkar): Implement directory read in multiple chunks. |
84 FilePath local_path = | 83 FilePath local_path = |
85 GetLocalPath(context, context->src_origin_url(), context->src_type(), | 84 GetLocalPath(context, context->src_origin_url(), context->src_type(), |
86 file_path); | 85 file_path); |
87 if (local_path.empty()) | 86 if (local_path.empty()) |
88 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 87 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
89 return underlying_file_util_->ReadDirectory( | 88 return underlying_file_util_->ReadDirectory( |
90 context, local_path, entries); | 89 context, local_path, entries); |
91 } | 90 } |
92 | 91 |
93 PlatformFileError LocalFileSystemFileUtil::CreateDirectory( | 92 PlatformFileError LocalFileUtil::CreateDirectory( |
94 FileSystemOperationContext* context, | 93 FileSystemOperationContext* context, |
95 const FilePath& file_path, | 94 const FilePath& file_path, |
96 bool exclusive, | 95 bool exclusive, |
97 bool recursive) { | 96 bool recursive) { |
98 FilePath local_path = | 97 FilePath local_path = |
99 GetLocalPath(context, context->src_origin_url(), context->src_type(), | 98 GetLocalPath(context, context->src_origin_url(), context->src_type(), |
100 file_path); | 99 file_path); |
101 if (local_path.empty()) | 100 if (local_path.empty()) |
102 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 101 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
103 return underlying_file_util_->CreateDirectory( | 102 return underlying_file_util_->CreateDirectory( |
104 context, local_path, exclusive, recursive); | 103 context, local_path, exclusive, recursive); |
105 } | 104 } |
106 | 105 |
107 PlatformFileError LocalFileSystemFileUtil::CopyOrMoveFile( | 106 PlatformFileError LocalFileUtil::CopyOrMoveFile( |
108 FileSystemOperationContext* context, | 107 FileSystemOperationContext* context, |
109 const FilePath& src_file_path, | 108 const FilePath& src_file_path, |
110 const FilePath& dest_file_path, | 109 const FilePath& dest_file_path, |
111 bool copy) { | 110 bool copy) { |
112 // TODO(ericu): If they share a root URL, this could be optimized. | 111 // TODO(ericu): If they share a root URL, this could be optimized. |
113 FilePath local_src_path = | 112 FilePath local_src_path = |
114 GetLocalPath(context, context->src_origin_url(), context->src_type(), | 113 GetLocalPath(context, context->src_origin_url(), context->src_type(), |
115 src_file_path); | 114 src_file_path); |
116 if (local_src_path.empty()) | 115 if (local_src_path.empty()) |
117 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 116 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
118 FilePath local_dest_path = | 117 FilePath local_dest_path = |
119 GetLocalPath(context, context->dest_origin_url(), context->dest_type(), | 118 GetLocalPath(context, context->dest_origin_url(), context->dest_type(), |
120 dest_file_path); | 119 dest_file_path); |
121 if (local_dest_path.empty()) | 120 if (local_dest_path.empty()) |
122 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 121 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
123 return underlying_file_util_->CopyOrMoveFile( | 122 return underlying_file_util_->CopyOrMoveFile( |
124 context, local_src_path, local_dest_path, copy); | 123 context, local_src_path, local_dest_path, copy); |
125 } | 124 } |
126 | 125 |
127 // TODO(dmikurube): Make it independent from CopyOrMoveFile. | 126 // TODO(dmikurube): Make it independent from CopyOrMoveFile. |
128 PlatformFileError LocalFileSystemFileUtil::CopyInForeignFile( | 127 PlatformFileError LocalFileUtil::CopyInForeignFile( |
129 FileSystemOperationContext* context, | 128 FileSystemOperationContext* context, |
130 const FilePath& src_file_path, | 129 const FilePath& src_file_path, |
131 const FilePath& dest_file_path) { | 130 const FilePath& dest_file_path) { |
132 if (src_file_path.empty()) | 131 if (src_file_path.empty()) |
133 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 132 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
134 FilePath local_dest_path = | 133 FilePath local_dest_path = |
135 GetLocalPath(context, context->dest_origin_url(), context->dest_type(), | 134 GetLocalPath(context, context->dest_origin_url(), context->dest_type(), |
136 dest_file_path); | 135 dest_file_path); |
137 if (local_dest_path.empty()) | 136 if (local_dest_path.empty()) |
138 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 137 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
139 return underlying_file_util_->CopyOrMoveFile( | 138 return underlying_file_util_->CopyOrMoveFile( |
140 context, src_file_path, local_dest_path, true); | 139 context, src_file_path, local_dest_path, true); |
141 } | 140 } |
142 | 141 |
143 PlatformFileError LocalFileSystemFileUtil::DeleteFile( | 142 PlatformFileError LocalFileUtil::DeleteFile( |
144 FileSystemOperationContext* context, | 143 FileSystemOperationContext* context, |
145 const FilePath& file_path) { | 144 const FilePath& file_path) { |
146 FilePath local_path = | 145 FilePath local_path = |
147 GetLocalPath(context, context->src_origin_url(), context->src_type(), | 146 GetLocalPath(context, context->src_origin_url(), context->src_type(), |
148 file_path); | 147 file_path); |
149 if (local_path.empty()) | 148 if (local_path.empty()) |
150 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 149 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
151 return underlying_file_util_->DeleteFile( | 150 return underlying_file_util_->DeleteFile( |
152 context, local_path); | 151 context, local_path); |
153 } | 152 } |
154 | 153 |
155 PlatformFileError LocalFileSystemFileUtil::DeleteSingleDirectory( | 154 PlatformFileError LocalFileUtil::DeleteSingleDirectory( |
156 FileSystemOperationContext* context, | 155 FileSystemOperationContext* context, |
157 const FilePath& file_path) { | 156 const FilePath& file_path) { |
158 FilePath local_path = | 157 FilePath local_path = |
159 GetLocalPath(context, context->src_origin_url(), context->src_type(), | 158 GetLocalPath(context, context->src_origin_url(), context->src_type(), |
160 file_path); | 159 file_path); |
161 if (local_path.empty()) | 160 if (local_path.empty()) |
162 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 161 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
163 return underlying_file_util_->DeleteSingleDirectory( | 162 return underlying_file_util_->DeleteSingleDirectory( |
164 context, local_path); | 163 context, local_path); |
165 } | 164 } |
166 | 165 |
167 PlatformFileError LocalFileSystemFileUtil::Touch( | 166 PlatformFileError LocalFileUtil::Touch( |
168 FileSystemOperationContext* context, | 167 FileSystemOperationContext* context, |
169 const FilePath& file_path, | 168 const FilePath& file_path, |
170 const base::Time& last_access_time, | 169 const base::Time& last_access_time, |
171 const base::Time& last_modified_time) { | 170 const base::Time& last_modified_time) { |
172 FilePath local_path = | 171 FilePath local_path = |
173 GetLocalPath(context, context->src_origin_url(), context->src_type(), | 172 GetLocalPath(context, context->src_origin_url(), context->src_type(), |
174 file_path); | 173 file_path); |
175 if (local_path.empty()) | 174 if (local_path.empty()) |
176 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 175 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
177 return underlying_file_util_->Touch( | 176 return underlying_file_util_->Touch( |
178 context, local_path, last_access_time, last_modified_time); | 177 context, local_path, last_access_time, last_modified_time); |
179 } | 178 } |
180 | 179 |
181 PlatformFileError LocalFileSystemFileUtil::Truncate( | 180 PlatformFileError LocalFileUtil::Truncate( |
182 FileSystemOperationContext* context, | 181 FileSystemOperationContext* context, |
183 const FilePath& file_path, | 182 const FilePath& file_path, |
184 int64 length) { | 183 int64 length) { |
185 FilePath local_path = | 184 FilePath local_path = |
186 GetLocalPath(context, context->src_origin_url(), context->src_type(), | 185 GetLocalPath(context, context->src_origin_url(), context->src_type(), |
187 file_path); | 186 file_path); |
188 if (local_path.empty()) | 187 if (local_path.empty()) |
189 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 188 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
190 return underlying_file_util_->Truncate( | 189 return underlying_file_util_->Truncate( |
191 context, local_path, length); | 190 context, local_path, length); |
192 } | 191 } |
193 | 192 |
194 bool LocalFileSystemFileUtil::PathExists( | 193 bool LocalFileUtil::PathExists( |
195 FileSystemOperationContext* context, | 194 FileSystemOperationContext* context, |
196 const FilePath& file_path) { | 195 const FilePath& file_path) { |
197 FilePath local_path = | 196 FilePath local_path = |
198 GetLocalPath(context, context->src_origin_url(), context->src_type(), | 197 GetLocalPath(context, context->src_origin_url(), context->src_type(), |
199 file_path); | 198 file_path); |
200 if (local_path.empty()) | 199 if (local_path.empty()) |
201 return false; | 200 return false; |
202 return underlying_file_util_->PathExists( | 201 return underlying_file_util_->PathExists( |
203 context, local_path); | 202 context, local_path); |
204 } | 203 } |
205 | 204 |
206 bool LocalFileSystemFileUtil::DirectoryExists( | 205 bool LocalFileUtil::DirectoryExists( |
207 FileSystemOperationContext* context, | 206 FileSystemOperationContext* context, |
208 const FilePath& file_path) { | 207 const FilePath& file_path) { |
209 FilePath local_path = | 208 FilePath local_path = |
210 GetLocalPath(context, context->src_origin_url(), context->src_type(), | 209 GetLocalPath(context, context->src_origin_url(), context->src_type(), |
211 file_path); | 210 file_path); |
212 if (local_path.empty()) | 211 if (local_path.empty()) |
213 return false; | 212 return false; |
214 return underlying_file_util_->DirectoryExists( | 213 return underlying_file_util_->DirectoryExists( |
215 context, local_path); | 214 context, local_path); |
216 } | 215 } |
217 | 216 |
218 bool LocalFileSystemFileUtil::IsDirectoryEmpty( | 217 bool LocalFileUtil::IsDirectoryEmpty( |
219 FileSystemOperationContext* context, | 218 FileSystemOperationContext* context, |
220 const FilePath& file_path) { | 219 const FilePath& file_path) { |
221 FilePath local_path = | 220 FilePath local_path = |
222 GetLocalPath(context, context->src_origin_url(), context->src_type(), | 221 GetLocalPath(context, context->src_origin_url(), context->src_type(), |
223 file_path); | 222 file_path); |
224 if (local_path.empty()) | 223 if (local_path.empty()) |
225 return true; | 224 return true; |
226 return underlying_file_util_->IsDirectoryEmpty( | 225 return underlying_file_util_->IsDirectoryEmpty( |
227 context, local_path); | 226 context, local_path); |
228 } | 227 } |
229 | 228 |
230 class LocalFileSystemFileEnumerator | 229 class LocalFileSystemFileEnumerator |
231 : public FileSystemFileUtil::AbstractFileEnumerator { | 230 : public FileApiFileUtil::AbstractFileEnumerator { |
232 public: | 231 public: |
233 LocalFileSystemFileEnumerator(const FilePath& platform_root_path, | 232 LocalFileSystemFileEnumerator(const FilePath& platform_root_path, |
234 const FilePath& virtual_root_path, | 233 const FilePath& virtual_root_path, |
235 bool recursive, | 234 bool recursive, |
236 file_util::FileEnumerator::FILE_TYPE file_type) | 235 file_util::FileEnumerator::FILE_TYPE file_type) |
237 : file_enum_(platform_root_path, recursive, file_type), | 236 : file_enum_(platform_root_path, recursive, file_type), |
238 platform_root_path_(platform_root_path), | 237 platform_root_path_(platform_root_path), |
239 virtual_root_path_(virtual_root_path) { | 238 virtual_root_path_(virtual_root_path) { |
240 } | 239 } |
241 | 240 |
(...skipping 17 matching lines...) Expand all Loading... |
259 platform_root_path_.AppendRelativePath(next, &path); | 258 platform_root_path_.AppendRelativePath(next, &path); |
260 return virtual_root_path_.Append(path); | 259 return virtual_root_path_.Append(path); |
261 } | 260 } |
262 | 261 |
263 bool LocalFileSystemFileEnumerator::IsDirectory() { | 262 bool LocalFileSystemFileEnumerator::IsDirectory() { |
264 file_util::FileEnumerator::FindInfo file_util_info; | 263 file_util::FileEnumerator::FindInfo file_util_info; |
265 file_enum_.GetFindInfo(&file_util_info); | 264 file_enum_.GetFindInfo(&file_util_info); |
266 return file_util::FileEnumerator::IsDirectory(file_util_info); | 265 return file_util::FileEnumerator::IsDirectory(file_util_info); |
267 } | 266 } |
268 | 267 |
269 FileSystemFileUtil::AbstractFileEnumerator* | 268 FileApiFileUtil::AbstractFileEnumerator* LocalFileUtil::CreateFileEnumerator( |
270 LocalFileSystemFileUtil::CreateFileEnumerator( | |
271 FileSystemOperationContext* context, | 269 FileSystemOperationContext* context, |
272 const FilePath& root_path) { | 270 const FilePath& root_path) { |
273 FilePath local_path = | 271 FilePath local_path = |
274 GetLocalPath(context, context->src_origin_url(), context->src_type(), | 272 GetLocalPath(context, context->src_origin_url(), context->src_type(), |
275 root_path); | 273 root_path); |
276 if (local_path.empty()) | 274 if (local_path.empty()) |
277 return new EmptyFileEnumerator(); | 275 return new EmptyFileEnumerator(); |
278 return new LocalFileSystemFileEnumerator( | 276 return new LocalFileSystemFileEnumerator( |
279 local_path, root_path, true, | 277 local_path, root_path, true, |
280 static_cast<file_util::FileEnumerator::FILE_TYPE>( | 278 static_cast<file_util::FileEnumerator::FILE_TYPE>( |
281 file_util::FileEnumerator::FILES | | 279 file_util::FileEnumerator::FILES | |
282 file_util::FileEnumerator::DIRECTORIES)); | 280 file_util::FileEnumerator::DIRECTORIES)); |
283 } | 281 } |
284 | 282 |
285 FilePath LocalFileSystemFileUtil::GetLocalPath( | 283 FilePath LocalFileUtil::GetLocalPath( |
286 FileSystemOperationContext* context, | 284 FileSystemOperationContext* context, |
287 const GURL& origin_url, | 285 const GURL& origin_url, |
288 FileSystemType type, | 286 FileSystemType type, |
289 const FilePath& virtual_path) { | 287 const FilePath& virtual_path) { |
290 FilePath root = context->file_system_context()->path_manager()-> | 288 FilePath root = context->file_system_context()->path_manager()-> |
291 ValidateFileSystemRootAndGetPathOnFileThread(origin_url, type, | 289 ValidateFileSystemRootAndGetPathOnFileThread(origin_url, type, |
292 virtual_path, false); | 290 virtual_path, false); |
293 if (root.empty()) | 291 if (root.empty()) |
294 return FilePath(); | 292 return FilePath(); |
295 return root.Append(virtual_path); | 293 return root.Append(virtual_path); |
296 } | 294 } |
297 | 295 |
298 } // namespace fileapi | 296 } // namespace fileapi |
OLD | NEW |