| 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/syncable/local_file_change_tracker.h" | 5 #include "webkit/fileapi/syncable/local_file_change_tracker.h" |
| 6 | 6 |
| 7 #include <queue> | 7 #include <queue> |
| 8 | 8 |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 13 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 14 #include "webkit/fileapi/file_system_context.h" | 14 #include "webkit/fileapi/file_system_context.h" |
| 15 #include "webkit/fileapi/file_system_file_util.h" | 15 #include "webkit/fileapi/file_system_file_util.h" |
| 16 #include "webkit/fileapi/file_system_operation_context.h" | 16 #include "webkit/fileapi/file_system_operation_context.h" |
| 17 #include "webkit/fileapi/file_system_util.h" | 17 #include "webkit/fileapi/file_system_util.h" |
| 18 #include "webkit/fileapi/syncable/local_file_sync_status.h" | 18 #include "webkit/fileapi/syncable/local_file_sync_status.h" |
| 19 #include "webkit/fileapi/syncable/syncable_file_system_util.h" | 19 #include "webkit/fileapi/syncable/syncable_file_system_util.h" |
| 20 | 20 |
| 21 namespace fileapi { | 21 namespace fileapi { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 const FilePath::CharType kDatabaseName[] = | 24 const base::FilePath::CharType kDatabaseName[] = |
| 25 FILE_PATH_LITERAL("LocalFileChangeTracker"); | 25 FILE_PATH_LITERAL("LocalFileChangeTracker"); |
| 26 const char kMark[] = "d"; | 26 const char kMark[] = "d"; |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 // A database class that stores local file changes in a local database. This | 29 // A database class that stores local file changes in a local database. This |
| 30 // object must be destructed on file_task_runner. | 30 // object must be destructed on file_task_runner. |
| 31 class LocalFileChangeTracker::TrackerDB { | 31 class LocalFileChangeTracker::TrackerDB { |
| 32 public: | 32 public: |
| 33 explicit TrackerDB(const FilePath& base_path); | 33 explicit TrackerDB(const base::FilePath& base_path); |
| 34 | 34 |
| 35 SyncStatusCode MarkDirty(const std::string& url); | 35 SyncStatusCode MarkDirty(const std::string& url); |
| 36 SyncStatusCode ClearDirty(const std::string& url); | 36 SyncStatusCode ClearDirty(const std::string& url); |
| 37 SyncStatusCode GetDirtyEntries(std::queue<FileSystemURL>* dirty_files); | 37 SyncStatusCode GetDirtyEntries(std::queue<FileSystemURL>* dirty_files); |
| 38 | 38 |
| 39 private: | 39 private: |
| 40 enum RecoveryOption { | 40 enum RecoveryOption { |
| 41 REPAIR_ON_CORRUPTION, | 41 REPAIR_ON_CORRUPTION, |
| 42 FAIL_ON_CORRUPTION, | 42 FAIL_ON_CORRUPTION, |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 SyncStatusCode Init(RecoveryOption recovery_option); | 45 SyncStatusCode Init(RecoveryOption recovery_option); |
| 46 SyncStatusCode Repair(const std::string& db_path); | 46 SyncStatusCode Repair(const std::string& db_path); |
| 47 void HandleError(const tracked_objects::Location& from_here, | 47 void HandleError(const tracked_objects::Location& from_here, |
| 48 const leveldb::Status& status); | 48 const leveldb::Status& status); |
| 49 | 49 |
| 50 const FilePath base_path_; | 50 const base::FilePath base_path_; |
| 51 scoped_ptr<leveldb::DB> db_; | 51 scoped_ptr<leveldb::DB> db_; |
| 52 SyncStatusCode db_status_; | 52 SyncStatusCode db_status_; |
| 53 | 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(TrackerDB); | 54 DISALLOW_COPY_AND_ASSIGN(TrackerDB); |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 LocalFileChangeTracker::ChangeInfo::ChangeInfo() : change_seq(-1) {} | 57 LocalFileChangeTracker::ChangeInfo::ChangeInfo() : change_seq(-1) {} |
| 58 LocalFileChangeTracker::ChangeInfo::~ChangeInfo() {} | 58 LocalFileChangeTracker::ChangeInfo::~ChangeInfo() {} |
| 59 | 59 |
| 60 // LocalFileChangeTracker ------------------------------------------------------ | 60 // LocalFileChangeTracker ------------------------------------------------------ |
| 61 | 61 |
| 62 LocalFileChangeTracker::LocalFileChangeTracker( | 62 LocalFileChangeTracker::LocalFileChangeTracker( |
| 63 const FilePath& base_path, | 63 const base::FilePath& base_path, |
| 64 base::SequencedTaskRunner* file_task_runner) | 64 base::SequencedTaskRunner* file_task_runner) |
| 65 : initialized_(false), | 65 : initialized_(false), |
| 66 file_task_runner_(file_task_runner), | 66 file_task_runner_(file_task_runner), |
| 67 tracker_db_(new TrackerDB(base_path)), | 67 tracker_db_(new TrackerDB(base_path)), |
| 68 current_change_seq_(0), | 68 current_change_seq_(0), |
| 69 num_changes_(0) { | 69 num_changes_(0) { |
| 70 } | 70 } |
| 71 | 71 |
| 72 LocalFileChangeTracker::~LocalFileChangeTracker() { | 72 LocalFileChangeTracker::~LocalFileChangeTracker() { |
| 73 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); | 73 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 if (status != SYNC_STATUS_OK) | 210 if (status != SYNC_STATUS_OK) |
| 211 return status; | 211 return status; |
| 212 | 212 |
| 213 FileSystemFileUtil* file_util = | 213 FileSystemFileUtil* file_util = |
| 214 file_system_context->GetFileUtil(kFileSystemTypeSyncable); | 214 file_system_context->GetFileUtil(kFileSystemTypeSyncable); |
| 215 DCHECK(file_util); | 215 DCHECK(file_util); |
| 216 scoped_ptr<FileSystemOperationContext> context( | 216 scoped_ptr<FileSystemOperationContext> context( |
| 217 new FileSystemOperationContext(file_system_context)); | 217 new FileSystemOperationContext(file_system_context)); |
| 218 | 218 |
| 219 base::PlatformFileInfo file_info; | 219 base::PlatformFileInfo file_info; |
| 220 FilePath platform_path; | 220 base::FilePath platform_path; |
| 221 | 221 |
| 222 while (!dirty_files.empty()) { | 222 while (!dirty_files.empty()) { |
| 223 const FileSystemURL url = dirty_files.front(); | 223 const FileSystemURL url = dirty_files.front(); |
| 224 dirty_files.pop(); | 224 dirty_files.pop(); |
| 225 DCHECK_EQ(url.type(), kFileSystemTypeSyncable); | 225 DCHECK_EQ(url.type(), kFileSystemTypeSyncable); |
| 226 | 226 |
| 227 switch (file_util->GetFileInfo(context.get(), url, | 227 switch (file_util->GetFileInfo(context.get(), url, |
| 228 &file_info, &platform_path)) { | 228 &file_info, &platform_path)) { |
| 229 case base::PLATFORM_FILE_OK: { | 229 case base::PLATFORM_FILE_OK: { |
| 230 if (!file_info.is_directory) { | 230 if (!file_info.is_directory) { |
| 231 RecordChange(url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, | 231 RecordChange(url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, |
| 232 SYNC_FILE_TYPE_FILE)); | 232 SYNC_FILE_TYPE_FILE)); |
| 233 break; | 233 break; |
| 234 } | 234 } |
| 235 | 235 |
| 236 RecordChange(url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, | 236 RecordChange(url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, |
| 237 SYNC_FILE_TYPE_DIRECTORY)); | 237 SYNC_FILE_TYPE_DIRECTORY)); |
| 238 | 238 |
| 239 // Push files and directories in this directory into |dirty_files|. | 239 // Push files and directories in this directory into |dirty_files|. |
| 240 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> enumerator( | 240 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> enumerator( |
| 241 file_util->CreateFileEnumerator(context.get(), | 241 file_util->CreateFileEnumerator(context.get(), |
| 242 url, | 242 url, |
| 243 false /* recursive */)); | 243 false /* recursive */)); |
| 244 FilePath path_each; | 244 base::FilePath path_each; |
| 245 while (!(path_each = enumerator->Next()).empty()) { | 245 while (!(path_each = enumerator->Next()).empty()) { |
| 246 dirty_files.push(CreateSyncableFileSystemURL( | 246 dirty_files.push(CreateSyncableFileSystemURL( |
| 247 url.origin(), url.filesystem_id(), path_each)); | 247 url.origin(), url.filesystem_id(), path_each)); |
| 248 } | 248 } |
| 249 break; | 249 break; |
| 250 } | 250 } |
| 251 case base::PLATFORM_FILE_ERROR_NOT_FOUND: { | 251 case base::PLATFORM_FILE_ERROR_NOT_FOUND: { |
| 252 // File represented by |url| has already been deleted. Since we cannot | 252 // File represented by |url| has already been deleted. Since we cannot |
| 253 // figure out if this file was directory or not from the URL, file | 253 // figure out if this file was directory or not from the URL, file |
| 254 // type is treated as SYNC_FILE_TYPE_UNKNOWN. | 254 // type is treated as SYNC_FILE_TYPE_UNKNOWN. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 281 UpdateNumChanges(); | 281 UpdateNumChanges(); |
| 282 return; | 282 return; |
| 283 } | 283 } |
| 284 info.change_seq = current_change_seq_++; | 284 info.change_seq = current_change_seq_++; |
| 285 change_seqs_[info.change_seq] = url; | 285 change_seqs_[info.change_seq] = url; |
| 286 UpdateNumChanges(); | 286 UpdateNumChanges(); |
| 287 } | 287 } |
| 288 | 288 |
| 289 // TrackerDB ------------------------------------------------------------------- | 289 // TrackerDB ------------------------------------------------------------------- |
| 290 | 290 |
| 291 LocalFileChangeTracker::TrackerDB::TrackerDB(const FilePath& base_path) | 291 LocalFileChangeTracker::TrackerDB::TrackerDB(const base::FilePath& base_path) |
| 292 : base_path_(base_path), | 292 : base_path_(base_path), |
| 293 db_status_(SYNC_STATUS_OK) {} | 293 db_status_(SYNC_STATUS_OK) {} |
| 294 | 294 |
| 295 SyncStatusCode LocalFileChangeTracker::TrackerDB::Init( | 295 SyncStatusCode LocalFileChangeTracker::TrackerDB::Init( |
| 296 RecoveryOption recovery_option) { | 296 RecoveryOption recovery_option) { |
| 297 if (db_.get() && db_status_ == SYNC_STATUS_OK) | 297 if (db_.get() && db_status_ == SYNC_STATUS_OK) |
| 298 return SYNC_STATUS_OK; | 298 return SYNC_STATUS_OK; |
| 299 | 299 |
| 300 std::string path = FilePathToString(base_path_.Append(kDatabaseName)); | 300 std::string path = FilePathToString(base_path_.Append(kDatabaseName)); |
| 301 leveldb::Options options; | 301 leveldb::Options options; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 db_.reset(); | 410 db_.reset(); |
| 411 return db_status_; | 411 return db_status_; |
| 412 } | 412 } |
| 413 dirty_files->push(url); | 413 dirty_files->push(url); |
| 414 iter->Next(); | 414 iter->Next(); |
| 415 } | 415 } |
| 416 return SYNC_STATUS_OK; | 416 return SYNC_STATUS_OK; |
| 417 } | 417 } |
| 418 | 418 |
| 419 } // namespace fileapi | 419 } // namespace fileapi |
| OLD | NEW |