[email protected] | e7f009d | 2011-06-14 19:35:10 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 08b1f75f | 2013-05-22 22:02:38 | [diff] [blame] | 5 | #include "webkit/browser/database/vfs_backend.h" |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 6 | |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 7 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 8 | #include "base/files/file_path.h" |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | e33cba4 | 2010-08-18 23:37:03 | [diff] [blame] | 10 | #include "third_party/sqlite/sqlite3.h" |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 11 | |
| 12 | namespace webkit_database { |
| 13 | |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 14 | static const int kFileTypeMask = 0x00007F00; |
| 15 | |
| 16 | // static |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 17 | bool VfsBackend::OpenTypeIsReadWrite(int desired_flags) { |
[email protected] | 2f312ab | 2010-04-21 07:17:41 | [diff] [blame] | 18 | return (desired_flags & SQLITE_OPEN_READWRITE) != 0; |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | // static |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 22 | bool VfsBackend::OpenFileFlagsAreConsistent(int desired_flags) { |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 23 | const int file_type = desired_flags & kFileTypeMask; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 24 | const bool is_exclusive = (desired_flags & SQLITE_OPEN_EXCLUSIVE) != 0; |
| 25 | const bool is_delete = (desired_flags & SQLITE_OPEN_DELETEONCLOSE) != 0; |
| 26 | const bool is_create = (desired_flags & SQLITE_OPEN_CREATE) != 0; |
| 27 | const bool is_read_only = (desired_flags & SQLITE_OPEN_READONLY) != 0; |
| 28 | const bool is_read_write = (desired_flags & SQLITE_OPEN_READWRITE) != 0; |
| 29 | |
[email protected] | b5be3f53 | 2009-10-24 01:28:39 | [diff] [blame] | 30 | // All files should be opened either read-write or read-only, but not both. |
| 31 | if (is_read_only == is_read_write) |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 32 | return false; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 33 | |
[email protected] | b5be3f53 | 2009-10-24 01:28:39 | [diff] [blame] | 34 | // If a new file is created, it must also be writable. |
| 35 | if (is_create && !is_read_write) |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 36 | return false; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 37 | |
[email protected] | b5be3f53 | 2009-10-24 01:28:39 | [diff] [blame] | 38 | // If we're accessing an existing file, we cannot give exclusive access, and |
| 39 | // we can't delete it. |
[email protected] | fe615f3 | 2010-06-13 09:08:41 | [diff] [blame] | 40 | // Normally, we'd also check that 'is_delete' is false for a main DB, main |
| 41 | // journal or master journal file; however, when in incognito mode, we use |
| 42 | // the SQLITE_OPEN_DELETEONCLOSE flag when opening those files too and keep |
| 43 | // an open handle to them for as long as the incognito profile is around. |
[email protected] | b5be3f53 | 2009-10-24 01:28:39 | [diff] [blame] | 44 | if ((is_exclusive || is_delete) && !is_create) |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 45 | return false; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 46 | |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 47 | // Make sure we're opening the DB directory or that a file type is set. |
[email protected] | b5be3f53 | 2009-10-24 01:28:39 | [diff] [blame] | 48 | return (file_type == SQLITE_OPEN_MAIN_DB) || |
| 49 | (file_type == SQLITE_OPEN_TEMP_DB) || |
| 50 | (file_type == SQLITE_OPEN_MAIN_JOURNAL) || |
| 51 | (file_type == SQLITE_OPEN_TEMP_JOURNAL) || |
| 52 | (file_type == SQLITE_OPEN_SUBJOURNAL) || |
| 53 | (file_type == SQLITE_OPEN_MASTER_JOURNAL) || |
| 54 | (file_type == SQLITE_OPEN_TRANSIENT_DB); |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 57 | // static |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 58 | void VfsBackend::OpenFile(const base::FilePath& file_path, |
[email protected] | b5be3f53 | 2009-10-24 01:28:39 | [diff] [blame] | 59 | int desired_flags, |
[email protected] | fe615f3 | 2010-06-13 09:08:41 | [diff] [blame] | 60 | base::PlatformFile* file_handle) { |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 61 | DCHECK(!file_path.empty()); |
| 62 | |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 63 | // Verify the flags for consistency and create the database |
| 64 | // directory if it doesn't exist. |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 65 | if (!OpenFileFlagsAreConsistent(desired_flags) || |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 66 | !base::CreateDirectory(file_path.DirName())) |
[email protected] | c93efc7c5 | 2009-10-29 19:58:30 | [diff] [blame] | 67 | return; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 68 | |
[email protected] | c93efc7c5 | 2009-10-29 19:58:30 | [diff] [blame] | 69 | int flags = 0; |
| 70 | flags |= base::PLATFORM_FILE_READ; |
| 71 | if (desired_flags & SQLITE_OPEN_READWRITE) |
| 72 | flags |= base::PLATFORM_FILE_WRITE; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 73 | |
[email protected] | c93efc7c5 | 2009-10-29 19:58:30 | [diff] [blame] | 74 | if (!(desired_flags & SQLITE_OPEN_MAIN_DB)) { |
| 75 | flags |= base::PLATFORM_FILE_EXCLUSIVE_READ | |
| 76 | base::PLATFORM_FILE_EXCLUSIVE_WRITE; |
| 77 | } |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 78 | |
[email protected] | c93efc7c5 | 2009-10-29 19:58:30 | [diff] [blame] | 79 | flags |= ((desired_flags & SQLITE_OPEN_CREATE) ? |
| 80 | base::PLATFORM_FILE_OPEN_ALWAYS : base::PLATFORM_FILE_OPEN); |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 81 | |
[email protected] | c93efc7c5 | 2009-10-29 19:58:30 | [diff] [blame] | 82 | if (desired_flags & SQLITE_OPEN_EXCLUSIVE) { |
| 83 | flags |= base::PLATFORM_FILE_EXCLUSIVE_READ | |
| 84 | base::PLATFORM_FILE_EXCLUSIVE_WRITE; |
| 85 | } |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 86 | |
[email protected] | c93efc7c5 | 2009-10-29 19:58:30 | [diff] [blame] | 87 | if (desired_flags & SQLITE_OPEN_DELETEONCLOSE) { |
| 88 | flags |= base::PLATFORM_FILE_TEMPORARY | base::PLATFORM_FILE_HIDDEN | |
| 89 | base::PLATFORM_FILE_DELETE_ON_CLOSE; |
| 90 | } |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 91 | |
[email protected] | 800ad56 | 2011-07-08 08:00:50 | [diff] [blame] | 92 | // This flag will allow us to delete the file later on from the browser |
| 93 | // process. |
| 94 | flags |= base::PLATFORM_FILE_SHARE_DELETE; |
| 95 | |
[email protected] | c93efc7c5 | 2009-10-29 19:58:30 | [diff] [blame] | 96 | // Try to open/create the DB file. |
[email protected] | fe615f3 | 2010-06-13 09:08:41 | [diff] [blame] | 97 | *file_handle = |
[email protected] | ed65fec | 2010-08-31 19:30:27 | [diff] [blame] | 98 | base::CreatePlatformFile(file_path, flags, NULL, NULL); |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 99 | } |
| 100 | |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 101 | // static |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 102 | void VfsBackend::OpenTempFileInDirectory( |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 103 | const base::FilePath& dir_path, |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 104 | int desired_flags, |
[email protected] | fe615f3 | 2010-06-13 09:08:41 | [diff] [blame] | 105 | base::PlatformFile* file_handle) { |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 106 | // We should be able to delete temp files when they're closed |
| 107 | // and create them as needed |
| 108 | if (!(desired_flags & SQLITE_OPEN_DELETEONCLOSE) || |
| 109 | !(desired_flags & SQLITE_OPEN_CREATE)) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | // Get a unique temp file name in the database directory. |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 114 | base::FilePath temp_file_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 115 | if (!base::CreateTemporaryFileInDir(dir_path, &temp_file_path)) |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 116 | return; |
| 117 | |
[email protected] | fe615f3 | 2010-06-13 09:08:41 | [diff] [blame] | 118 | OpenFile(temp_file_path, desired_flags, file_handle); |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 119 | } |
| 120 | |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 121 | // static |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 122 | int VfsBackend::DeleteFile(const base::FilePath& file_path, bool sync_dir) { |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 123 | if (!base::PathExists(file_path)) |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 124 | return SQLITE_OK; |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 125 | if (!base::DeleteFile(file_path, false)) |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 126 | return SQLITE_IOERR_DELETE; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 127 | |
| 128 | int error_code = SQLITE_OK; |
| 129 | #if defined(OS_POSIX) |
| 130 | if (sync_dir) { |
| 131 | base::PlatformFile dir_fd = base::CreatePlatformFile( |
[email protected] | ed65fec | 2010-08-31 19:30:27 | [diff] [blame] | 132 | file_path.DirName(), base::PLATFORM_FILE_READ, NULL, NULL); |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 133 | if (dir_fd == base::kInvalidPlatformFileValue) { |
| 134 | error_code = SQLITE_CANTOPEN; |
| 135 | } else { |
[email protected] | b5be3f53 | 2009-10-24 01:28:39 | [diff] [blame] | 136 | if (fsync(dir_fd)) |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 137 | error_code = SQLITE_IOERR_DIR_FSYNC; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 138 | base::ClosePlatformFile(dir_fd); |
| 139 | } |
| 140 | } |
| 141 | #endif |
| 142 | return error_code; |
| 143 | } |
| 144 | |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 145 | // static |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 146 | uint32 VfsBackend::GetFileAttributes(const base::FilePath& file_path) { |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 147 | #if defined(OS_WIN) |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 148 | uint32 attributes = ::GetFileAttributes(file_path.value().c_str()); |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 149 | #elif defined(OS_POSIX) |
| 150 | uint32 attributes = 0; |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 151 | if (!access(file_path.value().c_str(), R_OK)) |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 152 | attributes |= static_cast<uint32>(R_OK); |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 153 | if (!access(file_path.value().c_str(), W_OK)) |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 154 | attributes |= static_cast<uint32>(W_OK); |
[email protected] | b5be3f53 | 2009-10-24 01:28:39 | [diff] [blame] | 155 | if (!attributes) |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 156 | attributes = -1; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 157 | #endif |
| 158 | return attributes; |
| 159 | } |
| 160 | |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 161 | // static |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 162 | int64 VfsBackend::GetFileSize(const base::FilePath& file_path) { |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 163 | int64 size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame^] | 164 | return (base::GetFileSize(file_path, &size) ? size : 0); |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 165 | } |
| 166 | |
[email protected] | b5be3f53 | 2009-10-24 01:28:39 | [diff] [blame] | 167 | } // namespace webkit_database |