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