| 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 "base/bind.h" |
| 6 #include "base/memory/weak_ptr.h" |
| 7 #include "base/message_loop.h" |
| 8 #include "webkit/chromeos/fileapi/memory_file_util.h" |
| 9 |
| 10 namespace { |
| 11 const int kDefaultReadDirectoryBufferSize = 100; |
| 12 } // namespace |
| 13 |
| 14 namespace fileapi { |
| 15 |
| 16 // In-memory implementation of AsyncFileStream. |
| 17 class MemoryFileUtilAsyncFileStream : public AsyncFileStream { |
| 18 public: |
| 19 // |file_entry| is owned by MemoryFileUtil. |
| 20 MemoryFileUtilAsyncFileStream(MemoryFileUtil::FileEntry* file_entry, |
| 21 int flags) |
| 22 : file_entry_(file_entry), |
| 23 flags_(flags), |
| 24 offset_(0), |
| 25 weak_ptr_factory_(this) { |
| 26 } |
| 27 |
| 28 // AsyncFileStream override. |
| 29 virtual void Read(char* buffer, |
| 30 int64 length, |
| 31 const ReadWriteCallback& callback) OVERRIDE { |
| 32 MessageLoop::current()->PostTask( |
| 33 FROM_HERE, |
| 34 base::Bind(&MemoryFileUtilAsyncFileStream::DoRead, |
| 35 weak_ptr_factory_.GetWeakPtr(), |
| 36 buffer, length, callback)); |
| 37 } |
| 38 |
| 39 // AsyncFileStream override. |
| 40 virtual void Write(const char* buffer, |
| 41 int64 length, |
| 42 const ReadWriteCallback& callback) OVERRIDE { |
| 43 MessageLoop::current()->PostTask( |
| 44 FROM_HERE, |
| 45 base::Bind(&MemoryFileUtilAsyncFileStream::DoWrite, |
| 46 weak_ptr_factory_.GetWeakPtr(), |
| 47 buffer, length, callback)); |
| 48 } |
| 49 |
| 50 // AsyncFileStream override. |
| 51 virtual void Seek(int64 offset, |
| 52 const SeekCallback& callback) OVERRIDE { |
| 53 MessageLoop::current()->PostTask( |
| 54 FROM_HERE, |
| 55 base::Bind(&MemoryFileUtilAsyncFileStream::DoSeek, |
| 56 weak_ptr_factory_.GetWeakPtr(), |
| 57 offset, callback)); |
| 58 } |
| 59 |
| 60 private: |
| 61 // Callback used for Read(). |
| 62 void DoRead(char* buffer, |
| 63 int64 length, |
| 64 const ReadWriteCallback& callback) { |
| 65 |
| 66 if ((flags_ & base::PLATFORM_FILE_READ) == 0) { |
| 67 callback.Run(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, 0); |
| 68 return; |
| 69 } |
| 70 |
| 71 // Shorten the length so the read does not overrun. |
| 72 length = std::min(length, file_size() - offset_); |
| 73 |
| 74 const std::string& contents = file_entry_->contents; |
| 75 std::copy(contents.begin() + offset_, |
| 76 contents.begin() + offset_ + length, |
| 77 buffer); |
| 78 offset_ += length; |
| 79 |
| 80 callback.Run(base::PLATFORM_FILE_OK, length); |
| 81 } |
| 82 |
| 83 // Callback used for Write(). |
| 84 void DoWrite(const char* buffer, |
| 85 int64 length, |
| 86 const ReadWriteCallback& callback) { |
| 87 if ((flags_ & base::PLATFORM_FILE_WRITE) == 0) { |
| 88 callback.Run(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, 0); |
| 89 return; |
| 90 } |
| 91 |
| 92 // Extend the contents if needed. |
| 93 std::string* contents = &file_entry_->contents; |
| 94 if (offset_ + length > file_size()) |
| 95 contents->resize(offset_ + length, 0); // Fill with 0. |
| 96 |
| 97 std::copy(buffer, buffer + length, |
| 98 contents->begin() + offset_); |
| 99 file_entry_->last_modified = base::Time::Now(); |
| 100 offset_ += length; |
| 101 |
| 102 callback.Run(base::PLATFORM_FILE_OK, length); |
| 103 } |
| 104 |
| 105 // Callback used for Seek(). |
| 106 void DoSeek(int64 offset, |
| 107 const SeekCallback& callback) { |
| 108 if (offset > file_size()) { |
| 109 // Unlike lseek(2), we don't allow an offset larger than the file |
| 110 // size for this file implementation. |
| 111 callback.Run(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); |
| 112 return; |
| 113 } |
| 114 |
| 115 offset_ = offset; |
| 116 callback.Run(base::PLATFORM_FILE_OK); |
| 117 } |
| 118 |
| 119 // Returns the file size as int64. |
| 120 int64 file_size() const { |
| 121 return static_cast<int64>(file_entry_->contents.size()); |
| 122 } |
| 123 |
| 124 MemoryFileUtil::FileEntry* file_entry_; |
| 125 const int flags_; |
| 126 int64 offset_; |
| 127 base::WeakPtrFactory<MemoryFileUtilAsyncFileStream> weak_ptr_factory_; |
| 128 }; |
| 129 |
| 130 MemoryFileUtil::FileEntry::FileEntry() |
| 131 : is_directory(false) { |
| 132 } |
| 133 |
| 134 MemoryFileUtil::FileEntry::~FileEntry() { |
| 135 } |
| 136 |
| 137 MemoryFileUtil::MemoryFileUtil(const FilePath& root_path) |
| 138 : read_directory_buffer_size_(kDefaultReadDirectoryBufferSize) { |
| 139 FileEntry root; |
| 140 root.is_directory = true; |
| 141 root.last_modified = base::Time::Now(); |
| 142 |
| 143 files_[root_path] = root; |
| 144 } |
| 145 |
| 146 MemoryFileUtil::~MemoryFileUtil() { |
| 147 } |
| 148 |
| 149 // Depending on the flags value the flow of file opening will be one of |
| 150 // the following: |
| 151 // |
| 152 // PLATFORM_FILE_OPEN: |
| 153 // - GetFileInfo |
| 154 // - DidGetFileInfoForOpen |
| 155 // - OpenVerifiedFile |
| 156 // |
| 157 // PLATFORM_FILE_CREATE: |
| 158 // - Create |
| 159 // - DidCreateOrTruncateForOpen |
| 160 // - OpenVerifiedFile |
| 161 // |
| 162 // PLATFORM_FILE_OPEN_ALWAYS: |
| 163 // - GetFileInfo |
| 164 // - DidGetFileInfoForOpen |
| 165 // - OpenVerifiedFile OR Create |
| 166 // DidCreateOrTruncateForOpen |
| 167 // OpenVerifiedFile |
| 168 // |
| 169 // PLATFORM_FILE_CREATE_ALWAYS: |
| 170 // - Truncate |
| 171 // - OpenTruncatedFileOrCreate |
| 172 // - OpenVerifiedFile OR Create |
| 173 // DidCreateOrTruncateForOpen |
| 174 // OpenVerifiedFile |
| 175 // |
| 176 // PLATFORM_FILE_OPEN_TRUNCATED: |
| 177 // - Truncate |
| 178 // - DidCreateOrTruncateForOpen |
| 179 // - OpenVerifiedFile |
| 180 // |
| 181 void MemoryFileUtil::Open( |
| 182 const FilePath& file_path, |
| 183 int flags, |
| 184 const OpenCallback& callback) { |
| 185 int create_flag = flags & (base::PLATFORM_FILE_OPEN | |
| 186 base::PLATFORM_FILE_CREATE | |
| 187 base::PLATFORM_FILE_OPEN_ALWAYS | |
| 188 base::PLATFORM_FILE_CREATE_ALWAYS | |
| 189 base::PLATFORM_FILE_OPEN_TRUNCATED); |
| 190 switch (create_flag) { |
| 191 case base::PLATFORM_FILE_OPEN: |
| 192 case base::PLATFORM_FILE_OPEN_ALWAYS: |
| 193 GetFileInfo( |
| 194 file_path, |
| 195 base::Bind(&MemoryFileUtil::DidGetFileInfoForOpen, |
| 196 base::Unretained(this), file_path, flags, callback)); |
| 197 |
| 198 break; |
| 199 |
| 200 case base::PLATFORM_FILE_CREATE: |
| 201 Create( |
| 202 file_path, |
| 203 base::Bind(&MemoryFileUtil::DidCreateOrTruncateForOpen, |
| 204 base::Unretained(this), file_path, flags, 0, callback)); |
| 205 break; |
| 206 |
| 207 case base::PLATFORM_FILE_CREATE_ALWAYS: |
| 208 Truncate( |
| 209 file_path, |
| 210 0, |
| 211 base::Bind(&MemoryFileUtil::OpenTruncatedFileOrCreate, |
| 212 base::Unretained(this), file_path, flags, callback)); |
| 213 |
| 214 case base::PLATFORM_FILE_OPEN_TRUNCATED: |
| 215 Truncate( |
| 216 file_path, |
| 217 0, |
| 218 base::Bind(&MemoryFileUtil::DidCreateOrTruncateForOpen, |
| 219 base::Unretained(this), file_path, flags, 0, callback)); |
| 220 break; |
| 221 |
| 222 default: |
| 223 MessageLoop::current()->PostTask( |
| 224 FROM_HERE, |
| 225 base::Bind(callback, |
| 226 base::PLATFORM_FILE_ERROR_INVALID_OPERATION, |
| 227 static_cast<AsyncFileStream*>(NULL))); |
| 228 } |
| 229 } |
| 230 |
| 231 void MemoryFileUtil::GetFileInfo( |
| 232 const FilePath& file_path, |
| 233 const GetFileInfoCallback& callback) { |
| 234 MessageLoop::current()->PostTask( |
| 235 FROM_HERE, |
| 236 base::Bind(&MemoryFileUtil::DoGetFileInfo, base::Unretained(this), |
| 237 file_path.StripTrailingSeparators(), callback)); |
| 238 } |
| 239 |
| 240 void MemoryFileUtil::Create( |
| 241 const FilePath& file_path, |
| 242 const StatusCallback& callback) { |
| 243 MessageLoop::current()->PostTask( |
| 244 FROM_HERE, |
| 245 base::Bind(&MemoryFileUtil::DoCreate, base::Unretained(this), |
| 246 file_path.StripTrailingSeparators(), false, callback)); |
| 247 } |
| 248 |
| 249 void MemoryFileUtil::Truncate( |
| 250 const FilePath& file_path, |
| 251 int64 length, |
| 252 const StatusCallback& callback) { |
| 253 MessageLoop::current()->PostTask( |
| 254 FROM_HERE, |
| 255 base::Bind(&MemoryFileUtil::DoTruncate, base::Unretained(this), |
| 256 file_path.StripTrailingSeparators(), length, callback)); |
| 257 } |
| 258 |
| 259 void MemoryFileUtil::Touch( |
| 260 const FilePath& file_path, |
| 261 const base::Time& last_access_time, |
| 262 const base::Time& last_modified_time, |
| 263 const StatusCallback& callback) { |
| 264 MessageLoop::current()->PostTask( |
| 265 FROM_HERE, |
| 266 base::Bind(&MemoryFileUtil::DoTouch, base::Unretained(this), |
| 267 file_path.StripTrailingSeparators(), |
| 268 last_modified_time, callback)); |
| 269 } |
| 270 |
| 271 void MemoryFileUtil::Remove( |
| 272 const FilePath& file_path, |
| 273 bool recursive, |
| 274 const StatusCallback& callback) { |
| 275 if (recursive) { |
| 276 MessageLoop::current()->PostTask( |
| 277 FROM_HERE, |
| 278 base::Bind(&MemoryFileUtil::DoRemoveRecursive, |
| 279 base::Unretained(this), file_path.StripTrailingSeparators(), |
| 280 callback)); |
| 281 } else { |
| 282 MessageLoop::current()->PostTask( |
| 283 FROM_HERE, |
| 284 base::Bind(&MemoryFileUtil::DoRemoveSingleFile, |
| 285 base::Unretained(this), file_path.StripTrailingSeparators(), |
| 286 callback)); |
| 287 } |
| 288 } |
| 289 |
| 290 void MemoryFileUtil::CreateDirectory( |
| 291 const FilePath& dir_path, |
| 292 const StatusCallback& callback) { |
| 293 MessageLoop::current()->PostTask( |
| 294 FROM_HERE, |
| 295 base::Bind(&MemoryFileUtil::DoCreate, |
| 296 base::Unretained(this), dir_path.StripTrailingSeparators(), |
| 297 true, callback)); |
| 298 } |
| 299 |
| 300 void MemoryFileUtil::ReadDirectory( |
| 301 const FilePath& dir_path, |
| 302 const ReadDirectoryCallback& callback) { |
| 303 MessageLoop::current()->PostTask( |
| 304 FROM_HERE, |
| 305 base::Bind(&MemoryFileUtil::DoReadDirectory, |
| 306 base::Unretained(this), dir_path.StripTrailingSeparators(), |
| 307 FilePath(), callback)); |
| 308 } |
| 309 |
| 310 void MemoryFileUtil::DoGetFileInfo(const FilePath& file_path, |
| 311 const GetFileInfoCallback& callback) { |
| 312 base::PlatformFileInfo file_info; |
| 313 |
| 314 FileIterator file_it = files_.find(file_path); |
| 315 |
| 316 if (file_it == files_.end()) { |
| 317 callback.Run(base::PLATFORM_FILE_ERROR_NOT_FOUND, file_info); |
| 318 return; |
| 319 } |
| 320 const FileEntry& file_entry = file_it->second; |
| 321 |
| 322 file_info.size = file_entry.contents.size(); |
| 323 file_info.is_directory = file_entry.is_directory; |
| 324 file_info.is_symbolic_link = false; |
| 325 |
| 326 // In this file system implementation we store only one datetime. Many |
| 327 // popular file systems do the same. |
| 328 file_info.last_modified = file_entry.last_modified; |
| 329 file_info.last_accessed = file_entry.last_modified; |
| 330 file_info.creation_time = file_entry.last_modified; |
| 331 |
| 332 callback.Run(base::PLATFORM_FILE_OK, file_info); |
| 333 } |
| 334 |
| 335 void MemoryFileUtil::DoCreate( |
| 336 const FilePath& file_path, |
| 337 bool is_directory, |
| 338 const StatusCallback& callback) { |
| 339 if (FileExists(file_path)) { |
| 340 callback.Run(base::PLATFORM_FILE_ERROR_EXISTS); |
| 341 return; |
| 342 } |
| 343 |
| 344 if (!IsDirectory(file_path.DirName())) { |
| 345 callback.Run(base::PLATFORM_FILE_ERROR_FAILED); |
| 346 return; |
| 347 } |
| 348 |
| 349 FileEntry file; |
| 350 file.is_directory = is_directory; |
| 351 file.last_modified = base::Time::Now(); |
| 352 |
| 353 files_[file_path] = file; |
| 354 callback.Run(base::PLATFORM_FILE_OK); |
| 355 } |
| 356 |
| 357 void MemoryFileUtil::DoTruncate( |
| 358 const FilePath& file_path, |
| 359 int64 length, |
| 360 const StatusCallback& callback) { |
| 361 FileIterator file_it = files_.find(file_path); |
| 362 if (file_it == files_.end()) { |
| 363 callback.Run(base::PLATFORM_FILE_ERROR_NOT_FOUND); |
| 364 return; |
| 365 } |
| 366 |
| 367 FileEntry& file = file_it->second; |
| 368 |
| 369 // Fill the extended part with 0 if |length| is larger than the original |
| 370 // contents size. |
| 371 file.contents.resize(length, 0); |
| 372 callback.Run(base::PLATFORM_FILE_OK); |
| 373 } |
| 374 |
| 375 void MemoryFileUtil::DoTouch( |
| 376 const FilePath& file_path, |
| 377 const base::Time& last_modified_time, |
| 378 const StatusCallback& callback) { |
| 379 FileIterator file_it = files_.find(file_path); |
| 380 if (file_it == files_.end()) { |
| 381 callback.Run(base::PLATFORM_FILE_ERROR_NOT_FOUND); |
| 382 return; |
| 383 } |
| 384 |
| 385 FileEntry& file = file_it->second; |
| 386 |
| 387 file.last_modified = last_modified_time; |
| 388 callback.Run(base::PLATFORM_FILE_OK); |
| 389 } |
| 390 |
| 391 void MemoryFileUtil::DoRemoveSingleFile( |
| 392 const FilePath& file_path, |
| 393 const StatusCallback& callback) { |
| 394 FileIterator file_it = files_.find(file_path); |
| 395 if (file_it == files_.end()) { |
| 396 callback.Run(base::PLATFORM_FILE_ERROR_NOT_FOUND); |
| 397 return; |
| 398 } |
| 399 |
| 400 FileEntry& file = file_it->second; |
| 401 if (file.is_directory) { |
| 402 // Check if the directory is empty. This can be done by checking if |
| 403 // the next file is present under the directory. Note that |files_| is |
| 404 // a map hence the file names are sorted by names. |
| 405 FileIterator tmp_it = file_it; |
| 406 ++tmp_it; |
| 407 if (tmp_it != files_.end() && file_path.IsParent(tmp_it->first)) { |
| 408 callback.Run(base::PLATFORM_FILE_ERROR_NOT_A_FILE); |
| 409 return; |
| 410 } |
| 411 } |
| 412 |
| 413 files_.erase(file_it); |
| 414 callback.Run(base::PLATFORM_FILE_OK); |
| 415 } |
| 416 |
| 417 void MemoryFileUtil::DoRemoveRecursive( |
| 418 const FilePath& file_path, |
| 419 const StatusCallback& callback) { |
| 420 FileIterator file_it = files_.find(file_path); |
| 421 if (file_it == files_.end()) { |
| 422 callback.Run(base::PLATFORM_FILE_ERROR_NOT_FOUND); |
| 423 return; |
| 424 } |
| 425 |
| 426 FileEntry& file = file_it->second; |
| 427 if (!file.is_directory) { |
| 428 files_.erase(file_it); |
| 429 callback.Run(base::PLATFORM_FILE_OK); |
| 430 return; |
| 431 } |
| 432 |
| 433 // Remove the directory itself. |
| 434 files_.erase(file_it++); |
| 435 // Remove files under the directory. |
| 436 while (file_it != files_.end()) { |
| 437 if (file_path.IsParent(file_it->first)) { |
| 438 files_.erase(file_it++); |
| 439 } else { |
| 440 break; |
| 441 } |
| 442 } |
| 443 callback.Run(base::PLATFORM_FILE_OK); |
| 444 } |
| 445 |
| 446 void MemoryFileUtil::DoReadDirectory( |
| 447 const FilePath& dir_path, |
| 448 const FilePath& in_from, |
| 449 const ReadDirectoryCallback& callback) { |
| 450 FilePath from = in_from; |
| 451 read_directory_buffer_.clear(); |
| 452 |
| 453 if (!FileExists(dir_path)) { |
| 454 callback.Run(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 455 read_directory_buffer_, true); |
| 456 return; |
| 457 } |
| 458 |
| 459 if (!IsDirectory(dir_path)) { |
| 460 callback.Run(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY, |
| 461 read_directory_buffer_, true); |
| 462 return; |
| 463 } |
| 464 |
| 465 if (from.empty()) |
| 466 from = dir_path; |
| 467 |
| 468 bool completed = true; |
| 469 |
| 470 // Here we iterate over all paths in files_ starting with the prefix |from|. |
| 471 // It is not very efficient in case of a deep tree with many files in |
| 472 // subdirectories. If ever we'll need efficiency from this implementation of |
| 473 // FS, this should be changed. |
| 474 for (ConstFileIterator it = files_.lower_bound(from); |
| 475 it != files_.end(); |
| 476 ++it) { |
| 477 if (dir_path == it->first) // Skip the directory itself. |
| 478 continue; |
| 479 if (!dir_path.IsParent(it->first)) // Not in the directory. |
| 480 break; |
| 481 if (it->first.DirName() != dir_path) // a file in subdirectory |
| 482 continue; |
| 483 |
| 484 if (read_directory_buffer_.size() == read_directory_buffer_size_) { |
| 485 from = it->first; |
| 486 completed = false; |
| 487 break; |
| 488 } |
| 489 |
| 490 const FileEntry& file = it->second; |
| 491 DirectoryEntry entry; |
| 492 entry.name = it->first.BaseName().value(); |
| 493 entry.is_directory = file.is_directory; |
| 494 entry.size = file.contents.size(); |
| 495 entry.last_modified_time = file.last_modified; |
| 496 |
| 497 read_directory_buffer_.push_back(entry); |
| 498 } |
| 499 |
| 500 callback.Run(base::PLATFORM_FILE_OK, read_directory_buffer_, completed); |
| 501 |
| 502 if (!completed) { |
| 503 MessageLoop::current()->PostTask( |
| 504 FROM_HERE, |
| 505 base::Bind(&MemoryFileUtil::DoReadDirectory, |
| 506 base::Unretained(this), dir_path, |
| 507 from, callback)); |
| 508 } |
| 509 } |
| 510 |
| 511 void MemoryFileUtil::OpenVerifiedFile( |
| 512 const FilePath& file_path, |
| 513 int flags, |
| 514 const OpenCallback& callback) { |
| 515 FileIterator file_it = files_.find(file_path); |
| 516 // The existence of the file is guranteed here. |
| 517 DCHECK(file_it != files_.end()); |
| 518 |
| 519 FileEntry* file_entry = &file_it->second; |
| 520 callback.Run(base::PLATFORM_FILE_OK, |
| 521 new MemoryFileUtilAsyncFileStream(file_entry, flags)); |
| 522 } |
| 523 |
| 524 void MemoryFileUtil::DidGetFileInfoForOpen( |
| 525 const FilePath& file_path, |
| 526 int flags, |
| 527 const OpenCallback& callback, |
| 528 PlatformFileError get_info_result, |
| 529 const base::PlatformFileInfo& file_info) { |
| 530 if (get_info_result == base::PLATFORM_FILE_OK && file_info.is_directory) { |
| 531 callback.Run(base::PLATFORM_FILE_ERROR_NOT_A_FILE, NULL); |
| 532 return; |
| 533 } |
| 534 |
| 535 if (get_info_result == base::PLATFORM_FILE_OK) { |
| 536 OpenVerifiedFile(file_path, flags, callback); |
| 537 return; |
| 538 } |
| 539 |
| 540 if (get_info_result == base::PLATFORM_FILE_ERROR_NOT_FOUND && |
| 541 flags & base::PLATFORM_FILE_CREATE_ALWAYS) { |
| 542 Create(file_path, |
| 543 base::Bind(&MemoryFileUtil::DidCreateOrTruncateForOpen, |
| 544 base::Unretained(this), file_path, flags, 0, callback)); |
| 545 return; |
| 546 } |
| 547 |
| 548 callback.Run(get_info_result, NULL); |
| 549 } |
| 550 |
| 551 void MemoryFileUtil::OpenTruncatedFileOrCreate( |
| 552 const FilePath& file_path, |
| 553 int flags, |
| 554 const OpenCallback& callback, |
| 555 PlatformFileError result) { |
| 556 if (result == base::PLATFORM_FILE_OK) { |
| 557 OpenVerifiedFile(file_path, flags, callback); |
| 558 return; |
| 559 } |
| 560 |
| 561 if (result == base::PLATFORM_FILE_ERROR_NOT_FOUND) { |
| 562 Create( |
| 563 file_path, |
| 564 base::Bind(&MemoryFileUtil::DidCreateOrTruncateForOpen, |
| 565 base::Unretained(this), file_path, flags, 0, callback)); |
| 566 return; |
| 567 } |
| 568 |
| 569 callback.Run(result, NULL); |
| 570 } |
| 571 |
| 572 void MemoryFileUtil::DidCreateOrTruncateForOpen( |
| 573 const FilePath& file_path, |
| 574 int flags, |
| 575 int64 size, |
| 576 const OpenCallback& callback, |
| 577 PlatformFileError result) { |
| 578 if (result != base::PLATFORM_FILE_OK) { |
| 579 callback.Run(result, NULL); |
| 580 return; |
| 581 } |
| 582 |
| 583 OpenVerifiedFile(file_path, flags, callback); |
| 584 } |
| 585 |
| 586 } // namespace file_api |
| OLD | NEW |