| 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 #if defined(OS_MACOSX) | 9 #if defined(OS_MACOSX) |
| 10 #include <CoreFoundation/CoreFoundation.h> | 10 #include <CoreFoundation/CoreFoundation.h> |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 using std::string; | 31 using std::string; |
| 32 | 32 |
| 33 namespace syncable { | 33 namespace syncable { |
| 34 | 34 |
| 35 // This just has to be big enough to hold an UPDATE or INSERT statement that | 35 // This just has to be big enough to hold an UPDATE or INSERT statement that |
| 36 // modifies all the columns in the entry table. | 36 // modifies all the columns in the entry table. |
| 37 static const string::size_type kUpdateStatementBufferSize = 2048; | 37 static const string::size_type kUpdateStatementBufferSize = 2048; |
| 38 | 38 |
| 39 // Increment this version whenever updating DB tables. | 39 // Increment this version whenever updating DB tables. |
| 40 static const int32 kCurrentDBVersion = 67; | 40 static const int32 kCurrentDBVersion = 68; |
| 41 | |
| 42 #if OS_WIN | |
| 43 // TODO(sync): remove | |
| 44 static void PathNameMatch16(sqlite3_context* context, int argc, | |
| 45 sqlite3_value** argv) { | |
| 46 const PathString pathspec(reinterpret_cast<const PathChar*> | |
| 47 (sqlite3_value_text16(argv[0])), sqlite3_value_bytes16(argv[0]) / 2); | |
| 48 | |
| 49 const void* name_text = sqlite3_value_text16(argv[1]); | |
| 50 int name_bytes = sqlite3_value_bytes16(argv[1]); | |
| 51 // If the text is null, we need to avoid the PathString constructor. | |
| 52 if (name_text != NULL) { | |
| 53 // Have to copy to append a terminating 0 anyway. | |
| 54 const PathString name(reinterpret_cast<const PathChar*> | |
| 55 (sqlite3_value_text16(argv[1])), | |
| 56 sqlite3_value_bytes16(argv[1]) / 2); | |
| 57 sqlite3_result_int(context, PathNameMatch(name, pathspec)); | |
| 58 } else { | |
| 59 sqlite3_result_int(context, PathNameMatch(PathString(), pathspec)); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 // Sqlite allows setting of the escape character in an ESCAPE clause and | |
| 64 // this character is passed in as a third character to the like function. | |
| 65 // See: http://www.sqlite.org/lang_expr.html | |
| 66 static void PathNameMatch16WithEscape(sqlite3_context* context, | |
| 67 int argc, sqlite3_value** argv) { | |
| 68 // Never seen this called, but just in case. | |
| 69 LOG(FATAL) << "PathNameMatch16WithEscape() not implemented"; | |
| 70 } | |
| 71 #endif | |
| 72 | 41 |
| 73 static void RegisterPathNameCollate(sqlite3* dbhandle) { | 42 static void RegisterPathNameCollate(sqlite3* dbhandle) { |
| 74 const int collate = SQLITE_UTF8; | 43 const int collate = SQLITE_UTF8; |
| 75 CHECK(SQLITE_OK == sqlite3_create_collation(dbhandle, "PATHNAME", collate, | 44 CHECK(SQLITE_OK == sqlite3_create_collation(dbhandle, "PATHNAME", collate, |
| 76 NULL, &ComparePathNames16)); | 45 NULL, &ComparePathNames16)); |
| 77 } | 46 } |
| 78 | 47 |
| 79 // Replace the LIKE operator with our own implementation that does file spec | |
| 80 // matching like "*.txt". | |
| 81 static void RegisterPathNameMatch(sqlite3* dbhandle) { | |
| 82 // We only register this on Windows. We use the normal sqlite | |
| 83 // matching function on mac/linux. | |
| 84 // note that the function PathNameMatch() does a simple == | |
| 85 // comparison on mac, so that would have to be fixed if | |
| 86 // we really wanted to use PathNameMatch on mac/linux w/ the | |
| 87 // same pattern strings as we do on windows. | |
| 88 #if defined(OS_WIN) | |
| 89 CHECK(SQLITE_OK == sqlite3_create_function(dbhandle, "like", | |
| 90 2, SQLITE_ANY, NULL, &PathNameMatch16, NULL, NULL)); | |
| 91 CHECK(SQLITE_OK == sqlite3_create_function(dbhandle, "like", | |
| 92 3, SQLITE_ANY, NULL, &PathNameMatch16WithEscape, NULL, NULL)); | |
| 93 #endif // OS_WIN | |
| 94 } | |
| 95 | |
| 96 static inline bool IsSqliteErrorOurFault(int result) { | 48 static inline bool IsSqliteErrorOurFault(int result) { |
| 97 switch (result) { | 49 switch (result) { |
| 98 case SQLITE_MISMATCH: | 50 case SQLITE_MISMATCH: |
| 99 case SQLITE_CONSTRAINT: | 51 case SQLITE_CONSTRAINT: |
| 100 case SQLITE_MISUSE: | 52 case SQLITE_MISUSE: |
| 101 case SQLITE_RANGE: | 53 case SQLITE_RANGE: |
| 102 return true; | 54 return true; |
| 103 default: | 55 default: |
| 104 return false; | 56 return false; |
| 105 } | 57 } |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 sqlite3_close(save_dbhandle_); | 210 sqlite3_close(save_dbhandle_); |
| 259 save_dbhandle_ = NULL; | 211 save_dbhandle_ = NULL; |
| 260 } | 212 } |
| 261 } | 213 } |
| 262 | 214 |
| 263 bool DirectoryBackingStore::OpenAndConfigureHandleHelper( | 215 bool DirectoryBackingStore::OpenAndConfigureHandleHelper( |
| 264 sqlite3** handle) const { | 216 sqlite3** handle) const { |
| 265 if (SQLITE_OK == SqliteOpen(backing_filepath_, handle)) { | 217 if (SQLITE_OK == SqliteOpen(backing_filepath_, handle)) { |
| 266 sqlite3_busy_timeout(*handle, kDirectoryBackingStoreBusyTimeoutMs); | 218 sqlite3_busy_timeout(*handle, kDirectoryBackingStoreBusyTimeoutMs); |
| 267 RegisterPathNameCollate(*handle); | 219 RegisterPathNameCollate(*handle); |
| 268 RegisterPathNameMatch(*handle); | 220 |
| 269 return true; | 221 return true; |
| 270 } | 222 } |
| 271 return false; | 223 return false; |
| 272 } | 224 } |
| 273 | 225 |
| 274 DirOpenResult DirectoryBackingStore::Load(MetahandlesIndex* entry_bucket, | 226 DirOpenResult DirectoryBackingStore::Load(MetahandlesIndex* entry_bucket, |
| 275 ExtendedAttributes* xattrs_bucket, | 227 ExtendedAttributes* xattrs_bucket, |
| 276 Directory::KernelLoadInfo* kernel_load_info) { | 228 Directory::KernelLoadInfo* kernel_load_info) { |
| 277 DCHECK(load_dbhandle_ == NULL); | 229 DCHECK(load_dbhandle_ == NULL); |
| 278 if (!OpenAndConfigureHandleHelper(&load_dbhandle_)) | 230 if (!OpenAndConfigureHandleHelper(&load_dbhandle_)) |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 651 | 603 |
| 652 sqlite3* DirectoryBackingStore::LazyGetSaveHandle() { | 604 sqlite3* DirectoryBackingStore::LazyGetSaveHandle() { |
| 653 if (!save_dbhandle_ && !OpenAndConfigureHandleHelper(&save_dbhandle_)) { | 605 if (!save_dbhandle_ && !OpenAndConfigureHandleHelper(&save_dbhandle_)) { |
| 654 DCHECK(FALSE) << "Unable to open handle for saving"; | 606 DCHECK(FALSE) << "Unable to open handle for saving"; |
| 655 return NULL; | 607 return NULL; |
| 656 } | 608 } |
| 657 return save_dbhandle_; | 609 return save_dbhandle_; |
| 658 } | 610 } |
| 659 | 611 |
| 660 } // namespace syncable | 612 } // namespace syncable |
| OLD | NEW |