| 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 "components/drive/chromeos/resource_metadata.h" | 5 #include "components/drive/chromeos/resource_metadata.h" | 
| 6 | 6 | 
| 7 #include <limits.h> | 7 #include <limits.h> | 
| 8 #include <stddef.h> | 8 #include <stddef.h> | 
| 9 | 9 | 
| 10 #include "base/bind.h" | 10 #include "base/bind.h" | 
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 73 ResourceMetadata::ResourceMetadata( | 73 ResourceMetadata::ResourceMetadata( | 
| 74     ResourceMetadataStorage* storage, | 74     ResourceMetadataStorage* storage, | 
| 75     FileCache* cache, | 75     FileCache* cache, | 
| 76     scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) | 76     scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) | 
| 77     : blocking_task_runner_(blocking_task_runner), | 77     : blocking_task_runner_(blocking_task_runner), | 
| 78       storage_(storage), | 78       storage_(storage), | 
| 79       cache_(cache) { | 79       cache_(cache) { | 
| 80 } | 80 } | 
| 81 | 81 | 
| 82 FileError ResourceMetadata::Initialize() { | 82 FileError ResourceMetadata::Initialize() { | 
| 83   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 83   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 84   return SetUpDefaultEntries(); | 84   return SetUpDefaultEntries(); | 
| 85 } | 85 } | 
| 86 | 86 | 
| 87 void ResourceMetadata::Destroy() { | 87 void ResourceMetadata::Destroy() { | 
| 88   DCHECK(thread_checker_.CalledOnValidThread()); | 88   DCHECK(thread_checker_.CalledOnValidThread()); | 
| 89 | 89 | 
| 90   blocking_task_runner_->PostTask( | 90   blocking_task_runner_->PostTask( | 
| 91       FROM_HERE, | 91       FROM_HERE, | 
| 92       base::Bind(&ResourceMetadata::DestroyOnBlockingPool, | 92       base::Bind(&ResourceMetadata::DestroyOnBlockingPool, | 
| 93                  base::Unretained(this))); | 93                  base::Unretained(this))); | 
| 94 } | 94 } | 
| 95 | 95 | 
| 96 FileError ResourceMetadata::Reset() { | 96 FileError ResourceMetadata::Reset() { | 
| 97   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 97   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 98 | 98 | 
| 99   if (!EnoughDiskSpaceIsAvailableForDBOperation(storage_->directory_path())) | 99   if (!EnoughDiskSpaceIsAvailableForDBOperation(storage_->directory_path())) | 
| 100     return FILE_ERROR_NO_LOCAL_SPACE; | 100     return FILE_ERROR_NO_LOCAL_SPACE; | 
| 101 | 101 | 
| 102   FileError error = storage_->SetLargestChangestamp(0); | 102   FileError error = storage_->SetLargestChangestamp(0); | 
| 103   if (error != FILE_ERROR_OK) | 103   if (error != FILE_ERROR_OK) | 
| 104     return error; | 104     return error; | 
| 105 | 105 | 
| 106   // Remove all root entries. | 106   // Remove all root entries. | 
| 107   std::unique_ptr<Iterator> it = GetIterator(); | 107   std::unique_ptr<Iterator> it = GetIterator(); | 
| 108   for (; !it->IsAtEnd(); it->Advance()) { | 108   for (; !it->IsAtEnd(); it->Advance()) { | 
| 109     if (it->GetValue().parent_local_id().empty()) { | 109     if (it->GetValue().parent_local_id().empty()) { | 
| 110       error = RemoveEntryRecursively(it->GetID()); | 110       error = RemoveEntryRecursively(it->GetID()); | 
| 111       if (error != FILE_ERROR_OK) | 111       if (error != FILE_ERROR_OK) | 
| 112         return error; | 112         return error; | 
| 113     } | 113     } | 
| 114   } | 114   } | 
| 115   if (it->HasError()) | 115   if (it->HasError()) | 
| 116     return FILE_ERROR_FAILED; | 116     return FILE_ERROR_FAILED; | 
| 117 | 117 | 
| 118   return SetUpDefaultEntries(); | 118   return SetUpDefaultEntries(); | 
| 119 } | 119 } | 
| 120 | 120 | 
| 121 ResourceMetadata::~ResourceMetadata() { | 121 ResourceMetadata::~ResourceMetadata() { | 
| 122   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 122   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 123 } | 123 } | 
| 124 | 124 | 
| 125 FileError ResourceMetadata::SetUpDefaultEntries() { | 125 FileError ResourceMetadata::SetUpDefaultEntries() { | 
| 126   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 126   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 127 | 127 | 
| 128   // Initialize "/drive". | 128   // Initialize "/drive". | 
| 129   ResourceEntry entry; | 129   ResourceEntry entry; | 
| 130   FileError error = storage_->GetEntry(util::kDriveGrandRootLocalId, &entry); | 130   FileError error = storage_->GetEntry(util::kDriveGrandRootLocalId, &entry); | 
| 131   if (error == FILE_ERROR_NOT_FOUND) { | 131   if (error == FILE_ERROR_NOT_FOUND) { | 
| 132     ResourceEntry root; | 132     ResourceEntry root; | 
| 133     root.mutable_file_info()->set_is_directory(true); | 133     root.mutable_file_info()->set_is_directory(true); | 
| 134     root.set_local_id(util::kDriveGrandRootLocalId); | 134     root.set_local_id(util::kDriveGrandRootLocalId); | 
| 135     root.set_title(util::kDriveGrandRootDirName); | 135     root.set_title(util::kDriveGrandRootDirName); | 
| 136     root.set_base_name(util::kDriveGrandRootDirName); | 136     root.set_base_name(util::kDriveGrandRootDirName); | 
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 220         return error; | 220         return error; | 
| 221     } else if (error != FILE_ERROR_OK) { | 221     } else if (error != FILE_ERROR_OK) { | 
| 222       return error; | 222       return error; | 
| 223     } | 223     } | 
| 224   } | 224   } | 
| 225 | 225 | 
| 226   return FILE_ERROR_OK; | 226   return FILE_ERROR_OK; | 
| 227 } | 227 } | 
| 228 | 228 | 
| 229 void ResourceMetadata::DestroyOnBlockingPool() { | 229 void ResourceMetadata::DestroyOnBlockingPool() { | 
| 230   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 230   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 231   delete this; | 231   delete this; | 
| 232 } | 232 } | 
| 233 | 233 | 
| 234 FileError ResourceMetadata::GetLargestChangestamp(int64_t* out_value) { | 234 FileError ResourceMetadata::GetLargestChangestamp(int64_t* out_value) { | 
| 235   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 235   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 236   return storage_->GetLargestChangestamp(out_value); | 236   return storage_->GetLargestChangestamp(out_value); | 
| 237 } | 237 } | 
| 238 | 238 | 
| 239 FileError ResourceMetadata::SetLargestChangestamp(int64_t value) { | 239 FileError ResourceMetadata::SetLargestChangestamp(int64_t value) { | 
| 240   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 240   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 241 | 241 | 
| 242   if (!EnoughDiskSpaceIsAvailableForDBOperation(storage_->directory_path())) | 242   if (!EnoughDiskSpaceIsAvailableForDBOperation(storage_->directory_path())) | 
| 243     return FILE_ERROR_NO_LOCAL_SPACE; | 243     return FILE_ERROR_NO_LOCAL_SPACE; | 
| 244 | 244 | 
| 245   return storage_->SetLargestChangestamp(value); | 245   return storage_->SetLargestChangestamp(value); | 
| 246 } | 246 } | 
| 247 | 247 | 
| 248 FileError ResourceMetadata::AddEntry(const ResourceEntry& entry, | 248 FileError ResourceMetadata::AddEntry(const ResourceEntry& entry, | 
| 249                                      std::string* out_id) { | 249                                      std::string* out_id) { | 
| 250   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 250   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 251   DCHECK(entry.local_id().empty()); | 251   DCHECK(entry.local_id().empty()); | 
| 252 | 252 | 
| 253   if (!EnoughDiskSpaceIsAvailableForDBOperation(storage_->directory_path())) | 253   if (!EnoughDiskSpaceIsAvailableForDBOperation(storage_->directory_path())) | 
| 254     return FILE_ERROR_NO_LOCAL_SPACE; | 254     return FILE_ERROR_NO_LOCAL_SPACE; | 
| 255 | 255 | 
| 256   ResourceEntry parent; | 256   ResourceEntry parent; | 
| 257   FileError error = storage_->GetEntry(entry.parent_local_id(), &parent); | 257   FileError error = storage_->GetEntry(entry.parent_local_id(), &parent); | 
| 258   if (error != FILE_ERROR_OK) | 258   if (error != FILE_ERROR_OK) | 
| 259     return error; | 259     return error; | 
| 260   if (!parent.file_info().is_directory()) | 260   if (!parent.file_info().is_directory()) | 
| (...skipping 23 matching lines...) Expand all  Loading... | 
| 284 | 284 | 
| 285   error = PutEntryUnderDirectory(new_entry); | 285   error = PutEntryUnderDirectory(new_entry); | 
| 286   if (error != FILE_ERROR_OK) | 286   if (error != FILE_ERROR_OK) | 
| 287     return error; | 287     return error; | 
| 288 | 288 | 
| 289   *out_id = local_id; | 289   *out_id = local_id; | 
| 290   return FILE_ERROR_OK; | 290   return FILE_ERROR_OK; | 
| 291 } | 291 } | 
| 292 | 292 | 
| 293 FileError ResourceMetadata::RemoveEntry(const std::string& id) { | 293 FileError ResourceMetadata::RemoveEntry(const std::string& id) { | 
| 294   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 294   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 295 | 295 | 
| 296   if (!EnoughDiskSpaceIsAvailableForDBOperation(storage_->directory_path())) | 296   if (!EnoughDiskSpaceIsAvailableForDBOperation(storage_->directory_path())) | 
| 297     return FILE_ERROR_NO_LOCAL_SPACE; | 297     return FILE_ERROR_NO_LOCAL_SPACE; | 
| 298 | 298 | 
| 299   // Disallow deletion of default entries. | 299   // Disallow deletion of default entries. | 
| 300   if (IsImmutableEntry(id)) | 300   if (IsImmutableEntry(id)) | 
| 301     return FILE_ERROR_ACCESS_DENIED; | 301     return FILE_ERROR_ACCESS_DENIED; | 
| 302 | 302 | 
| 303   ResourceEntry entry; | 303   ResourceEntry entry; | 
| 304   FileError error = storage_->GetEntry(id, &entry); | 304   FileError error = storage_->GetEntry(id, &entry); | 
| 305   if (error != FILE_ERROR_OK) | 305   if (error != FILE_ERROR_OK) | 
| 306     return error; | 306     return error; | 
| 307 | 307 | 
| 308   return RemoveEntryRecursively(id); | 308   return RemoveEntryRecursively(id); | 
| 309 } | 309 } | 
| 310 | 310 | 
| 311 FileError ResourceMetadata::GetResourceEntryById(const std::string& id, | 311 FileError ResourceMetadata::GetResourceEntryById(const std::string& id, | 
| 312                                                  ResourceEntry* out_entry) { | 312                                                  ResourceEntry* out_entry) { | 
| 313   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 313   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 314   DCHECK(!id.empty()); | 314   DCHECK(!id.empty()); | 
| 315   DCHECK(out_entry); | 315   DCHECK(out_entry); | 
| 316 | 316 | 
| 317   return storage_->GetEntry(id, out_entry); | 317   return storage_->GetEntry(id, out_entry); | 
| 318 } | 318 } | 
| 319 | 319 | 
| 320 FileError ResourceMetadata::GetResourceEntryByPath(const base::FilePath& path, | 320 FileError ResourceMetadata::GetResourceEntryByPath(const base::FilePath& path, | 
| 321                                                    ResourceEntry* out_entry) { | 321                                                    ResourceEntry* out_entry) { | 
| 322   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 322   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 323   DCHECK(out_entry); | 323   DCHECK(out_entry); | 
| 324 | 324 | 
| 325   std::string id; | 325   std::string id; | 
| 326   FileError error = GetIdByPath(path, &id); | 326   FileError error = GetIdByPath(path, &id); | 
| 327   if (error != FILE_ERROR_OK) | 327   if (error != FILE_ERROR_OK) | 
| 328     return error; | 328     return error; | 
| 329 | 329 | 
| 330   return GetResourceEntryById(id, out_entry); | 330   return GetResourceEntryById(id, out_entry); | 
| 331 } | 331 } | 
| 332 | 332 | 
| 333 FileError ResourceMetadata::ReadDirectoryByPath( | 333 FileError ResourceMetadata::ReadDirectoryByPath( | 
| 334     const base::FilePath& path, | 334     const base::FilePath& path, | 
| 335     ResourceEntryVector* out_entries) { | 335     ResourceEntryVector* out_entries) { | 
| 336   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 336   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 337   DCHECK(out_entries); | 337   DCHECK(out_entries); | 
| 338 | 338 | 
| 339   std::string id; | 339   std::string id; | 
| 340   FileError error = GetIdByPath(path, &id); | 340   FileError error = GetIdByPath(path, &id); | 
| 341   if (error != FILE_ERROR_OK) | 341   if (error != FILE_ERROR_OK) | 
| 342     return error; | 342     return error; | 
| 343   return ReadDirectoryById(id, out_entries); | 343   return ReadDirectoryById(id, out_entries); | 
| 344 } | 344 } | 
| 345 | 345 | 
| 346 FileError ResourceMetadata::ReadDirectoryById( | 346 FileError ResourceMetadata::ReadDirectoryById( | 
| 347     const std::string& id, | 347     const std::string& id, | 
| 348     ResourceEntryVector* out_entries) { | 348     ResourceEntryVector* out_entries) { | 
| 349   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 349   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 350   DCHECK(out_entries); | 350   DCHECK(out_entries); | 
| 351 | 351 | 
| 352   ResourceEntry entry; | 352   ResourceEntry entry; | 
| 353   FileError error = GetResourceEntryById(id, &entry); | 353   FileError error = GetResourceEntryById(id, &entry); | 
| 354   if (error != FILE_ERROR_OK) | 354   if (error != FILE_ERROR_OK) | 
| 355     return error; | 355     return error; | 
| 356 | 356 | 
| 357   if (!entry.file_info().is_directory()) | 357   if (!entry.file_info().is_directory()) | 
| 358     return FILE_ERROR_NOT_A_DIRECTORY; | 358     return FILE_ERROR_NOT_A_DIRECTORY; | 
| 359 | 359 | 
| 360   std::vector<std::string> children; | 360   std::vector<std::string> children; | 
| 361   error = storage_->GetChildren(id, &children); | 361   error = storage_->GetChildren(id, &children); | 
| 362   if (error != FILE_ERROR_OK) | 362   if (error != FILE_ERROR_OK) | 
| 363     return error; | 363     return error; | 
| 364 | 364 | 
| 365   ResourceEntryVector entries(children.size()); | 365   ResourceEntryVector entries(children.size()); | 
| 366   for (size_t i = 0; i < children.size(); ++i) { | 366   for (size_t i = 0; i < children.size(); ++i) { | 
| 367     error = storage_->GetEntry(children[i], &entries[i]); | 367     error = storage_->GetEntry(children[i], &entries[i]); | 
| 368     if (error != FILE_ERROR_OK) | 368     if (error != FILE_ERROR_OK) | 
| 369       return error; | 369       return error; | 
| 370   } | 370   } | 
| 371   out_entries->swap(entries); | 371   out_entries->swap(entries); | 
| 372   return FILE_ERROR_OK; | 372   return FILE_ERROR_OK; | 
| 373 } | 373 } | 
| 374 | 374 | 
| 375 FileError ResourceMetadata::RefreshEntry(const ResourceEntry& entry) { | 375 FileError ResourceMetadata::RefreshEntry(const ResourceEntry& entry) { | 
| 376   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 376   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 377 | 377 | 
| 378   if (!EnoughDiskSpaceIsAvailableForDBOperation(storage_->directory_path())) | 378   if (!EnoughDiskSpaceIsAvailableForDBOperation(storage_->directory_path())) | 
| 379     return FILE_ERROR_NO_LOCAL_SPACE; | 379     return FILE_ERROR_NO_LOCAL_SPACE; | 
| 380 | 380 | 
| 381   ResourceEntry old_entry; | 381   ResourceEntry old_entry; | 
| 382   FileError error = storage_->GetEntry(entry.local_id(), &old_entry); | 382   FileError error = storage_->GetEntry(entry.local_id(), &old_entry); | 
| 383   if (error != FILE_ERROR_OK) | 383   if (error != FILE_ERROR_OK) | 
| 384     return error; | 384     return error; | 
| 385 | 385 | 
| 386   if (IsImmutableEntry(entry.local_id()) || | 386   if (IsImmutableEntry(entry.local_id()) || | 
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 424   } else if (updated_entry.file_specific_info().has_cache_state()) { | 424   } else if (updated_entry.file_specific_info().has_cache_state()) { | 
| 425     updated_entry.mutable_file_specific_info()->clear_cache_state(); | 425     updated_entry.mutable_file_specific_info()->clear_cache_state(); | 
| 426   } | 426   } | 
| 427   // Remove from the old parent and add it to the new parent with the new data. | 427   // Remove from the old parent and add it to the new parent with the new data. | 
| 428   return PutEntryUnderDirectory(updated_entry); | 428   return PutEntryUnderDirectory(updated_entry); | 
| 429 } | 429 } | 
| 430 | 430 | 
| 431 FileError ResourceMetadata::GetSubDirectoriesRecursively( | 431 FileError ResourceMetadata::GetSubDirectoriesRecursively( | 
| 432     const std::string& id, | 432     const std::string& id, | 
| 433     std::set<base::FilePath>* sub_directories) { | 433     std::set<base::FilePath>* sub_directories) { | 
| 434   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 434   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 435 | 435 | 
| 436   std::vector<std::string> children; | 436   std::vector<std::string> children; | 
| 437   FileError error = storage_->GetChildren(id, &children); | 437   FileError error = storage_->GetChildren(id, &children); | 
| 438   if (error != FILE_ERROR_OK) | 438   if (error != FILE_ERROR_OK) | 
| 439     return error; | 439     return error; | 
| 440   for (size_t i = 0; i < children.size(); ++i) { | 440   for (size_t i = 0; i < children.size(); ++i) { | 
| 441     ResourceEntry entry; | 441     ResourceEntry entry; | 
| 442     error = storage_->GetEntry(children[i], &entry); | 442     error = storage_->GetEntry(children[i], &entry); | 
| 443     if (error != FILE_ERROR_OK) | 443     if (error != FILE_ERROR_OK) | 
| 444       return error; | 444       return error; | 
| 445     if (entry.file_info().is_directory()) { | 445     if (entry.file_info().is_directory()) { | 
| 446       base::FilePath path; | 446       base::FilePath path; | 
| 447       error = GetFilePath(children[i], &path); | 447       error = GetFilePath(children[i], &path); | 
| 448       if (error != FILE_ERROR_OK) | 448       if (error != FILE_ERROR_OK) | 
| 449         return error; | 449         return error; | 
| 450       sub_directories->insert(path); | 450       sub_directories->insert(path); | 
| 451       GetSubDirectoriesRecursively(children[i], sub_directories); | 451       GetSubDirectoriesRecursively(children[i], sub_directories); | 
| 452     } | 452     } | 
| 453   } | 453   } | 
| 454   return FILE_ERROR_OK; | 454   return FILE_ERROR_OK; | 
| 455 } | 455 } | 
| 456 | 456 | 
| 457 FileError ResourceMetadata::GetChildId(const std::string& parent_local_id, | 457 FileError ResourceMetadata::GetChildId(const std::string& parent_local_id, | 
| 458                                        const std::string& base_name, | 458                                        const std::string& base_name, | 
| 459                                        std::string* out_child_id) { | 459                                        std::string* out_child_id) { | 
| 460   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 460   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 461   return storage_->GetChild(parent_local_id, base_name, out_child_id); | 461   return storage_->GetChild(parent_local_id, base_name, out_child_id); | 
| 462 } | 462 } | 
| 463 | 463 | 
| 464 std::unique_ptr<ResourceMetadata::Iterator> ResourceMetadata::GetIterator() { | 464 std::unique_ptr<ResourceMetadata::Iterator> ResourceMetadata::GetIterator() { | 
| 465   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 465   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 466 | 466 | 
| 467   return storage_->GetIterator(); | 467   return storage_->GetIterator(); | 
| 468 } | 468 } | 
| 469 | 469 | 
| 470 FileError ResourceMetadata::GetFilePath(const std::string& id, | 470 FileError ResourceMetadata::GetFilePath(const std::string& id, | 
| 471                                         base::FilePath* out_file_path) { | 471                                         base::FilePath* out_file_path) { | 
| 472   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 472   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 473 | 473 | 
| 474   ResourceEntry entry; | 474   ResourceEntry entry; | 
| 475   FileError error = storage_->GetEntry(id, &entry); | 475   FileError error = storage_->GetEntry(id, &entry); | 
| 476   if (error != FILE_ERROR_OK) | 476   if (error != FILE_ERROR_OK) | 
| 477     return error; | 477     return error; | 
| 478 | 478 | 
| 479   base::FilePath path; | 479   base::FilePath path; | 
| 480   if (!entry.parent_local_id().empty()) { | 480   if (!entry.parent_local_id().empty()) { | 
| 481     error = GetFilePath(entry.parent_local_id(), &path); | 481     error = GetFilePath(entry.parent_local_id(), &path); | 
| 482     if (error != FILE_ERROR_OK) | 482     if (error != FILE_ERROR_OK) | 
| 483       return error; | 483       return error; | 
| 484   } else if (entry.local_id() != util::kDriveGrandRootLocalId) { | 484   } else if (entry.local_id() != util::kDriveGrandRootLocalId) { | 
| 485     DVLOG(1) << "Entries not under the grand root don't have paths."; | 485     DVLOG(1) << "Entries not under the grand root don't have paths."; | 
| 486     return FILE_ERROR_NOT_FOUND; | 486     return FILE_ERROR_NOT_FOUND; | 
| 487   } | 487   } | 
| 488   path = path.Append(base::FilePath::FromUTF8Unsafe(entry.base_name())); | 488   path = path.Append(base::FilePath::FromUTF8Unsafe(entry.base_name())); | 
| 489   *out_file_path = path; | 489   *out_file_path = path; | 
| 490   return FILE_ERROR_OK; | 490   return FILE_ERROR_OK; | 
| 491 } | 491 } | 
| 492 | 492 | 
| 493 FileError ResourceMetadata::GetIdByPath(const base::FilePath& file_path, | 493 FileError ResourceMetadata::GetIdByPath(const base::FilePath& file_path, | 
| 494                                         std::string* out_id) { | 494                                         std::string* out_id) { | 
| 495   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 495   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 496 | 496 | 
| 497   // Start from the root. | 497   // Start from the root. | 
| 498   std::vector<base::FilePath::StringType> components; | 498   std::vector<base::FilePath::StringType> components; | 
| 499   file_path.GetComponents(&components); | 499   file_path.GetComponents(&components); | 
| 500   if (components.empty() || | 500   if (components.empty() || | 
| 501       components[0] != util::GetDriveGrandRootPath().value()) | 501       components[0] != util::GetDriveGrandRootPath().value()) | 
| 502     return FILE_ERROR_NOT_FOUND; | 502     return FILE_ERROR_NOT_FOUND; | 
| 503 | 503 | 
| 504   // Iterate over the remaining components. | 504   // Iterate over the remaining components. | 
| 505   std::string id = util::kDriveGrandRootLocalId; | 505   std::string id = util::kDriveGrandRootLocalId; | 
| 506   for (size_t i = 1; i < components.size(); ++i) { | 506   for (size_t i = 1; i < components.size(); ++i) { | 
| 507     const std::string component = base::FilePath(components[i]).AsUTF8Unsafe(); | 507     const std::string component = base::FilePath(components[i]).AsUTF8Unsafe(); | 
| 508     std::string child_id; | 508     std::string child_id; | 
| 509     FileError error = storage_->GetChild(id, component, &child_id); | 509     FileError error = storage_->GetChild(id, component, &child_id); | 
| 510     if (error != FILE_ERROR_OK) | 510     if (error != FILE_ERROR_OK) | 
| 511       return error; | 511       return error; | 
| 512     id = child_id; | 512     id = child_id; | 
| 513   } | 513   } | 
| 514   *out_id = id; | 514   *out_id = id; | 
| 515   return FILE_ERROR_OK; | 515   return FILE_ERROR_OK; | 
| 516 } | 516 } | 
| 517 | 517 | 
| 518 FileError ResourceMetadata::GetIdByResourceId(const std::string& resource_id, | 518 FileError ResourceMetadata::GetIdByResourceId(const std::string& resource_id, | 
| 519                                               std::string* out_local_id) { | 519                                               std::string* out_local_id) { | 
| 520   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 520   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 521   return storage_->GetIdByResourceId(resource_id, out_local_id); | 521   return storage_->GetIdByResourceId(resource_id, out_local_id); | 
| 522 } | 522 } | 
| 523 | 523 | 
| 524 FileError ResourceMetadata::PutEntryUnderDirectory(const ResourceEntry& entry) { | 524 FileError ResourceMetadata::PutEntryUnderDirectory(const ResourceEntry& entry) { | 
| 525   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 525   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 526   DCHECK(!entry.local_id().empty()); | 526   DCHECK(!entry.local_id().empty()); | 
| 527   DCHECK(!entry.parent_local_id().empty()); | 527   DCHECK(!entry.parent_local_id().empty()); | 
| 528 | 528 | 
| 529   std::string base_name; | 529   std::string base_name; | 
| 530   FileError error = GetDeduplicatedBaseName(entry, &base_name); | 530   FileError error = GetDeduplicatedBaseName(entry, &base_name); | 
| 531   if (error != FILE_ERROR_OK) | 531   if (error != FILE_ERROR_OK) | 
| 532     return error; | 532     return error; | 
| 533   ResourceEntry updated_entry(entry); | 533   ResourceEntry updated_entry(entry); | 
| 534   updated_entry.set_base_name(base_name); | 534   updated_entry.set_base_name(base_name); | 
| 535   return storage_->PutEntry(updated_entry); | 535   return storage_->PutEntry(updated_entry); | 
| 536 } | 536 } | 
| 537 | 537 | 
| 538 FileError ResourceMetadata::GetDeduplicatedBaseName( | 538 FileError ResourceMetadata::GetDeduplicatedBaseName( | 
| 539     const ResourceEntry& entry, | 539     const ResourceEntry& entry, | 
| 540     std::string* base_name) { | 540     std::string* base_name) { | 
| 541   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 541   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 542   DCHECK(!entry.parent_local_id().empty()); | 542   DCHECK(!entry.parent_local_id().empty()); | 
| 543   DCHECK(!entry.title().empty()); | 543   DCHECK(!entry.title().empty()); | 
| 544 | 544 | 
| 545   // The entry name may have been changed due to prior name de-duplication. | 545   // The entry name may have been changed due to prior name de-duplication. | 
| 546   // We need to first restore the file name based on the title before going | 546   // We need to first restore the file name based on the title before going | 
| 547   // through name de-duplication again when it is added to another directory. | 547   // through name de-duplication again when it is added to another directory. | 
| 548   *base_name = entry.title(); | 548   *base_name = entry.title(); | 
| 549   if (entry.has_file_specific_info() && | 549   if (entry.has_file_specific_info() && | 
| 550       entry.file_specific_info().is_hosted_document()) { | 550       entry.file_specific_info().is_hosted_document()) { | 
| 551     *base_name += entry.file_specific_info().document_extension(); | 551     *base_name += entry.file_specific_info().document_extension(); | 
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 594       smallest_known_unused_modifier = modifier; | 594       smallest_known_unused_modifier = modifier; | 
| 595     } else { | 595     } else { | 
| 596       largest_known_used_modifier = modifier; | 596       largest_known_used_modifier = modifier; | 
| 597     } | 597     } | 
| 598   } | 598   } | 
| 599   *base_name = GetUniquifiedName(*base_name, smallest_known_unused_modifier); | 599   *base_name = GetUniquifiedName(*base_name, smallest_known_unused_modifier); | 
| 600   return FILE_ERROR_OK; | 600   return FILE_ERROR_OK; | 
| 601 } | 601 } | 
| 602 | 602 | 
| 603 FileError ResourceMetadata::RemoveEntryRecursively(const std::string& id) { | 603 FileError ResourceMetadata::RemoveEntryRecursively(const std::string& id) { | 
| 604   DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 604   DCHECK(blocking_task_runner_->RunsTasksInCurrentSequence()); | 
| 605 | 605 | 
| 606   ResourceEntry entry; | 606   ResourceEntry entry; | 
| 607   FileError error = storage_->GetEntry(id, &entry); | 607   FileError error = storage_->GetEntry(id, &entry); | 
| 608   if (error != FILE_ERROR_OK) | 608   if (error != FILE_ERROR_OK) | 
| 609     return error; | 609     return error; | 
| 610 | 610 | 
| 611   if (entry.file_info().is_directory()) { | 611   if (entry.file_info().is_directory()) { | 
| 612     std::vector<std::string> children; | 612     std::vector<std::string> children; | 
| 613     error = storage_->GetChildren(id, &children); | 613     error = storage_->GetChildren(id, &children); | 
| 614     if (error != FILE_ERROR_OK) | 614     if (error != FILE_ERROR_OK) | 
| 615       return error; | 615       return error; | 
| 616     for (size_t i = 0; i < children.size(); ++i) { | 616     for (size_t i = 0; i < children.size(); ++i) { | 
| 617       error = RemoveEntryRecursively(children[i]); | 617       error = RemoveEntryRecursively(children[i]); | 
| 618       if (error != FILE_ERROR_OK) | 618       if (error != FILE_ERROR_OK) | 
| 619         return error; | 619         return error; | 
| 620     } | 620     } | 
| 621   } | 621   } | 
| 622 | 622 | 
| 623   error = cache_->Remove(id); | 623   error = cache_->Remove(id); | 
| 624   if (error != FILE_ERROR_OK) | 624   if (error != FILE_ERROR_OK) | 
| 625     return error; | 625     return error; | 
| 626 | 626 | 
| 627   return storage_->RemoveEntry(id); | 627   return storage_->RemoveEntry(id); | 
| 628 } | 628 } | 
| 629 | 629 | 
| 630 }  // namespace internal | 630 }  // namespace internal | 
| 631 }  // namespace drive | 631 }  // namespace drive | 
| OLD | NEW | 
|---|