| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/sync/syncable/directory_backing_store.h" | 5 #include "chrome/browser/sync/syncable/directory_backing_store.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #ifdef OS_MACOSX | 9 #ifdef OS_MACOSX |
| 10 #include <CoreFoundation/CoreFoundation.h> | 10 #include <CoreFoundation/CoreFoundation.h> |
| 11 #elif defined(OS_LINUX) | |
| 12 #include <glib.h> | |
| 13 #endif | 11 #endif |
| 14 | 12 |
| 15 #include <string> | 13 #include <string> |
| 16 | 14 |
| 17 #include "base/hash_tables.h" | 15 #include "base/hash_tables.h" |
| 18 #include "base/logging.h" | 16 #include "base/logging.h" |
| 19 #include "chrome/browser/sync/protocol/service_constants.h" | 17 #include "chrome/browser/sync/protocol/service_constants.h" |
| 20 #include "chrome/browser/sync/syncable/syncable-inl.h" | 18 #include "chrome/browser/sync/syncable/syncable-inl.h" |
| 21 #include "chrome/browser/sync/syncable/syncable_columns.h" | 19 #include "chrome/browser/sync/syncable/syncable_columns.h" |
| 22 #include "chrome/browser/sync/util/crypto_helpers.h" | 20 #include "chrome/browser/sync/util/crypto_helpers.h" |
| 23 #include "chrome/browser/sync/util/path_helpers.h" | 21 #include "chrome/browser/sync/util/path_helpers.h" |
| 24 #include "chrome/browser/sync/util/query_helpers.h" | 22 #include "chrome/browser/sync/util/query_helpers.h" |
| 25 #include "third_party/sqlite/preprocessed/sqlite3.h" | 23 #include "third_party/sqlite/preprocessed/sqlite3.h" |
| 26 | 24 |
| 25 // Sometimes threads contend on the DB lock itself, especially when one thread |
| 26 // is calling SaveChanges. In the worst case scenario, the user can put his |
| 27 // laptop to sleep during db contention, and wake up the laptop days later, so |
| 28 // infinity seems like the best choice here. |
| 29 const int kDirectoryBackingStoreBusyTimeoutMs = std::numeric_limits<int>::max(); |
| 30 |
| 27 // If sizeof(time_t) != sizeof(int32) we need to alter or expand the sqlite | 31 // If sizeof(time_t) != sizeof(int32) we need to alter or expand the sqlite |
| 28 // datatype. | 32 // datatype. |
| 29 COMPILE_ASSERT(sizeof(time_t) == sizeof(int32), time_t_is_not_int32); | 33 COMPILE_ASSERT(sizeof(time_t) == sizeof(int32), time_t_is_not_int32); |
| 30 | 34 |
| 31 using std::string; | 35 using std::string; |
| 32 | 36 |
| 33 namespace syncable { | 37 namespace syncable { |
| 34 | 38 |
| 35 // This just has to be big enough to hold an UPDATE or INSERT statement that | 39 // This just has to be big enough to hold an UPDATE or INSERT statement that |
| 36 // modifies all the columns in the entry table. | 40 // modifies all the columns in the entry table. |
| 37 static const string::size_type kUpdateStatementBufferSize = 2048; | 41 static const string::size_type kUpdateStatementBufferSize = 2048; |
| 38 | 42 |
| 39 // Increment this version whenever updating DB tables. | 43 // Increment this version whenever updating DB tables. |
| 40 static const int32 kCurrentDBVersion = 67; | 44 static const int32 kCurrentDBVersion = 67; |
| 41 | 45 |
| 46 #if OS_WIN |
| 42 // TODO(sync): remove | 47 // TODO(sync): remove |
| 43 static void PathNameMatch16(sqlite3_context* context, int argc, | 48 static void PathNameMatch16(sqlite3_context* context, int argc, |
| 44 sqlite3_value** argv) { | 49 sqlite3_value** argv) { |
| 45 const PathString pathspec(reinterpret_cast<const PathChar*> | 50 const PathString pathspec(reinterpret_cast<const PathChar*> |
| 46 (sqlite3_value_text16(argv[0])), sqlite3_value_bytes16(argv[0]) / 2); | 51 (sqlite3_value_text16(argv[0])), sqlite3_value_bytes16(argv[0]) / 2); |
| 47 | 52 |
| 48 const void* name_text = sqlite3_value_text16(argv[1]); | 53 const void* name_text = sqlite3_value_text16(argv[1]); |
| 49 int name_bytes = sqlite3_value_bytes16(argv[1]); | 54 int name_bytes = sqlite3_value_bytes16(argv[1]); |
| 50 // If the text is null, we need to avoid the PathString constructor. | 55 // If the text is null, we need to avoid the PathString constructor. |
| 51 if (name_text != NULL) { | 56 if (name_text != NULL) { |
| 52 // Have to copy to append a terminating 0 anyway. | 57 // Have to copy to append a terminating 0 anyway. |
| 53 const PathString name(reinterpret_cast<const PathChar*> | 58 const PathString name(reinterpret_cast<const PathChar*> |
| 54 (sqlite3_value_text16(argv[1])), | 59 (sqlite3_value_text16(argv[1])), |
| 55 sqlite3_value_bytes16(argv[1]) / 2); | 60 sqlite3_value_bytes16(argv[1]) / 2); |
| 56 sqlite3_result_int(context, PathNameMatch(name, pathspec)); | 61 sqlite3_result_int(context, PathNameMatch(name, pathspec)); |
| 57 } else { | 62 } else { |
| 58 sqlite3_result_int(context, PathNameMatch(PathString(), pathspec)); | 63 sqlite3_result_int(context, PathNameMatch(PathString(), pathspec)); |
| 59 } | 64 } |
| 60 } | 65 } |
| 61 | 66 |
| 62 // Sqlite allows setting of the escape character in an ESCAPE clause and | 67 // Sqlite allows setting of the escape character in an ESCAPE clause and |
| 63 // this character is passed in as a third character to the like function. | 68 // this character is passed in as a third character to the like function. |
| 64 // See: http://www.sqlite.org/lang_expr.html | 69 // See: http://www.sqlite.org/lang_expr.html |
| 65 static void PathNameMatch16WithEscape(sqlite3_context* context, | 70 static void PathNameMatch16WithEscape(sqlite3_context* context, |
| 66 int argc, sqlite3_value** argv) { | 71 int argc, sqlite3_value** argv) { |
| 67 // Never seen this called, but just in case. | 72 // Never seen this called, but just in case. |
| 68 LOG(FATAL) << "PathNameMatch16WithEscape() not implemented"; | 73 LOG(FATAL) << "PathNameMatch16WithEscape() not implemented"; |
| 69 } | 74 } |
| 75 #endif |
| 70 | 76 |
| 71 static void RegisterPathNameCollate(sqlite3* dbhandle) { | 77 static void RegisterPathNameCollate(sqlite3* dbhandle) { |
| 72 #ifdef OS_WIN | 78 #ifdef OS_WIN |
| 73 const int collate = SQLITE_UTF16; | 79 const int collate = SQLITE_UTF16; |
| 74 #else | 80 #else |
| 75 const int collate = SQLITE_UTF8; | 81 const int collate = SQLITE_UTF8; |
| 76 #endif | 82 #endif |
| 77 CHECK(SQLITE_OK == sqlite3_create_collation(dbhandle, "PATHNAME", collate, | 83 CHECK(SQLITE_OK == sqlite3_create_collation(dbhandle, "PATHNAME", collate, |
| 78 NULL, &ComparePathNames16)); | 84 NULL, &ComparePathNames16)); |
| 79 } | 85 } |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 } | 244 } |
| 239 query.push_back(')'); | 245 query.push_back(')'); |
| 240 return query; | 246 return query; |
| 241 } | 247 } |
| 242 | 248 |
| 243 /////////////////////////////////////////////////////////////////////////////// | 249 /////////////////////////////////////////////////////////////////////////////// |
| 244 // DirectoryBackingStore implementation. | 250 // DirectoryBackingStore implementation. |
| 245 | 251 |
| 246 DirectoryBackingStore::DirectoryBackingStore(const PathString& dir_name, | 252 DirectoryBackingStore::DirectoryBackingStore(const PathString& dir_name, |
| 247 const PathString& backing_filepath) | 253 const PathString& backing_filepath) |
| 248 : dir_name_(dir_name), backing_filepath_(backing_filepath), | 254 : load_dbhandle_(NULL), |
| 249 load_dbhandle_(NULL), save_dbhandle_(NULL) { | 255 save_dbhandle_(NULL), |
| 256 dir_name_(dir_name), |
| 257 backing_filepath_(backing_filepath) { |
| 250 } | 258 } |
| 251 | 259 |
| 252 DirectoryBackingStore::~DirectoryBackingStore() { | 260 DirectoryBackingStore::~DirectoryBackingStore() { |
| 253 if (NULL != load_dbhandle_) { | 261 if (NULL != load_dbhandle_) { |
| 254 sqlite3_close(load_dbhandle_); | 262 sqlite3_close(load_dbhandle_); |
| 255 load_dbhandle_ = NULL; | 263 load_dbhandle_ = NULL; |
| 256 } | 264 } |
| 257 if (NULL != save_dbhandle_) { | 265 if (NULL != save_dbhandle_) { |
| 258 sqlite3_close(save_dbhandle_); | 266 sqlite3_close(save_dbhandle_); |
| 259 save_dbhandle_ = NULL; | 267 save_dbhandle_ = NULL; |
| (...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 629 "?)", // cache_guid | 637 "?)", // cache_guid |
| 630 dir_name_, // id | 638 dir_name_, // id |
| 631 dir_name_, // name | 639 dir_name_, // name |
| 632 false, // initial_sync_ended | 640 false, // initial_sync_ended |
| 633 "", // store_birthday | 641 "", // store_birthday |
| 634 SYNC_ENGINE_VERSION_STRING, // db_create_version | 642 SYNC_ENGINE_VERSION_STRING, // db_create_version |
| 635 static_cast<int32>(time(0)), // db_create_time | 643 static_cast<int32>(time(0)), // db_create_time |
| 636 GenerateCacheGUID()); // cache_guid | 644 GenerateCacheGUID()); // cache_guid |
| 637 // Create the big metas table. | 645 // Create the big metas table. |
| 638 string query = "CREATE TABLE metas " + ComposeCreateTableColumnSpecs | 646 string query = "CREATE TABLE metas " + ComposeCreateTableColumnSpecs |
| 639 (g_metas_columns, g_metas_columns + ARRAYSIZE(g_metas_columns)); | 647 (g_metas_columns, g_metas_columns + arraysize(g_metas_columns)); |
| 640 result = SQLITE_DONE != result ? result : Exec(load_dbhandle_, query.c_str()); | 648 result = SQLITE_DONE != result ? result : Exec(load_dbhandle_, query.c_str()); |
| 641 // Insert the entry for the root into the metas table. | 649 // Insert the entry for the root into the metas table. |
| 642 const int64 now = Now(); | 650 const int64 now = Now(); |
| 643 result = SQLITE_DONE != result ? result : | 651 result = SQLITE_DONE != result ? result : |
| 644 Exec(load_dbhandle_, "INSERT INTO metas " | 652 Exec(load_dbhandle_, "INSERT INTO metas " |
| 645 "( id, metahandle, is_dir, ctime, mtime) " | 653 "( id, metahandle, is_dir, ctime, mtime) " |
| 646 "VALUES ( \"r\", 1, 1, ?, ?)", | 654 "VALUES ( \"r\", 1, 1, ?, ?)", |
| 647 now, now); | 655 now, now); |
| 648 result = SQLITE_DONE != result ? result : CreateExtendedAttributeTable(); | 656 result = SQLITE_DONE != result ? result : CreateExtendedAttributeTable(); |
| 649 return result; | 657 return result; |
| 650 } | 658 } |
| 651 | 659 |
| 652 sqlite3* DirectoryBackingStore::LazyGetSaveHandle() { | 660 sqlite3* DirectoryBackingStore::LazyGetSaveHandle() { |
| 653 if (!save_dbhandle_ && !OpenAndConfigureHandleHelper(&save_dbhandle_)) { | 661 if (!save_dbhandle_ && !OpenAndConfigureHandleHelper(&save_dbhandle_)) { |
| 654 DCHECK(FALSE) << "Unable to open handle for saving"; | 662 DCHECK(FALSE) << "Unable to open handle for saving"; |
| 655 return NULL; | 663 return NULL; |
| 656 } | 664 } |
| 657 return save_dbhandle_; | 665 return save_dbhandle_; |
| 658 } | 666 } |
| 659 | 667 |
| 660 } // namespace syncable | 668 } // namespace syncable |
| OLD | NEW |