[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 | |
pilgrim | 4af8c21 | 2014-09-05 17:30:15 | [diff] [blame] | 5 | #include "storage/browser/database/vfs_backend.h" |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 6 | |
avi | 5caf646e | 2015-12-21 21:21:50 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
thestig | 22dfc401 | 2014-09-05 08:29:44 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | e33cba4 | 2010-08-18 23:37:03 | [diff] [blame] | 12 | #include "third_party/sqlite/sqlite3.h" |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 13 | |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 14 | namespace storage { |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 15 | |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 16 | static const int kFileTypeMask = 0x00007F00; |
| 17 | |
| 18 | // static |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 19 | bool VfsBackend::OpenTypeIsReadWrite(int desired_flags) { |
[email protected] | 2f312ab | 2010-04-21 07:17:41 | [diff] [blame] | 20 | return (desired_flags & SQLITE_OPEN_READWRITE) != 0; |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | // static |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 24 | bool VfsBackend::OpenFileFlagsAreConsistent(int desired_flags) { |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 25 | const int file_type = desired_flags & kFileTypeMask; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 26 | const bool is_exclusive = (desired_flags & SQLITE_OPEN_EXCLUSIVE) != 0; |
| 27 | const bool is_delete = (desired_flags & SQLITE_OPEN_DELETEONCLOSE) != 0; |
| 28 | const bool is_create = (desired_flags & SQLITE_OPEN_CREATE) != 0; |
| 29 | const bool is_read_only = (desired_flags & SQLITE_OPEN_READONLY) != 0; |
| 30 | const bool is_read_write = (desired_flags & SQLITE_OPEN_READWRITE) != 0; |
| 31 | |
[email protected] | b5be3f53 | 2009-10-24 01:28:39 | [diff] [blame] | 32 | // All files should be opened either read-write or read-only, but not both. |
| 33 | if (is_read_only == is_read_write) |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 34 | return false; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 35 | |
[email protected] | b5be3f53 | 2009-10-24 01:28:39 | [diff] [blame] | 36 | // If a new file is created, it must also be writable. |
| 37 | if (is_create && !is_read_write) |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 38 | return false; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 39 | |
[email protected] | b5be3f53 | 2009-10-24 01:28:39 | [diff] [blame] | 40 | // If we're accessing an existing file, we cannot give exclusive access, and |
| 41 | // we can't delete it. |
[email protected] | fe615f3 | 2010-06-13 09:08:41 | [diff] [blame] | 42 | // Normally, we'd also check that 'is_delete' is false for a main DB, main |
| 43 | // journal or master journal file; however, when in incognito mode, we use |
| 44 | // the SQLITE_OPEN_DELETEONCLOSE flag when opening those files too and keep |
| 45 | // 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] | 46 | if ((is_exclusive || is_delete) && !is_create) |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 47 | return false; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 48 | |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 49 | // 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] | 50 | return (file_type == SQLITE_OPEN_MAIN_DB) || |
| 51 | (file_type == SQLITE_OPEN_TEMP_DB) || |
| 52 | (file_type == SQLITE_OPEN_MAIN_JOURNAL) || |
| 53 | (file_type == SQLITE_OPEN_TEMP_JOURNAL) || |
| 54 | (file_type == SQLITE_OPEN_SUBJOURNAL) || |
| 55 | (file_type == SQLITE_OPEN_MASTER_JOURNAL) || |
| 56 | (file_type == SQLITE_OPEN_TRANSIENT_DB); |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 59 | // static |
[email protected] | 382aa7e | 2014-03-28 01:13:37 | [diff] [blame] | 60 | base::File VfsBackend::OpenFile(const base::FilePath& file_path, |
| 61 | int desired_flags) { |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 62 | DCHECK(!file_path.empty()); |
| 63 | |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 64 | // Verify the flags for consistency and create the database |
| 65 | // directory if it doesn't exist. |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 66 | if (!OpenFileFlagsAreConsistent(desired_flags) || |
[email protected] | 382aa7e | 2014-03-28 01:13:37 | [diff] [blame] | 67 | !base::CreateDirectory(file_path.DirName())) { |
| 68 | return base::File(); |
| 69 | } |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 70 | |
[email protected] | c93efc7c5 | 2009-10-29 19:58:30 | [diff] [blame] | 71 | int flags = 0; |
[email protected] | 382aa7e | 2014-03-28 01:13:37 | [diff] [blame] | 72 | flags |= base::File::FLAG_READ; |
[email protected] | c93efc7c5 | 2009-10-29 19:58:30 | [diff] [blame] | 73 | if (desired_flags & SQLITE_OPEN_READWRITE) |
[email protected] | 382aa7e | 2014-03-28 01:13:37 | [diff] [blame] | 74 | flags |= base::File::FLAG_WRITE; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 75 | |
[email protected] | 382aa7e | 2014-03-28 01:13:37 | [diff] [blame] | 76 | if (!(desired_flags & SQLITE_OPEN_MAIN_DB)) |
| 77 | flags |= base::File::FLAG_EXCLUSIVE_READ | base::File::FLAG_EXCLUSIVE_WRITE; |
[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) ? |
[email protected] | 382aa7e | 2014-03-28 01:13:37 | [diff] [blame] | 80 | base::File::FLAG_OPEN_ALWAYS : base::File::FLAG_OPEN); |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 81 | |
[email protected] | 382aa7e | 2014-03-28 01:13:37 | [diff] [blame] | 82 | if (desired_flags & SQLITE_OPEN_EXCLUSIVE) |
| 83 | flags |= base::File::FLAG_EXCLUSIVE_READ | base::File::FLAG_EXCLUSIVE_WRITE; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 84 | |
[email protected] | c93efc7c5 | 2009-10-29 19:58:30 | [diff] [blame] | 85 | if (desired_flags & SQLITE_OPEN_DELETEONCLOSE) { |
[email protected] | 382aa7e | 2014-03-28 01:13:37 | [diff] [blame] | 86 | flags |= base::File::FLAG_TEMPORARY | base::File::FLAG_HIDDEN | |
| 87 | base::File::FLAG_DELETE_ON_CLOSE; |
[email protected] | c93efc7c5 | 2009-10-29 19:58:30 | [diff] [blame] | 88 | } |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 89 | |
[email protected] | 800ad56 | 2011-07-08 08:00:50 | [diff] [blame] | 90 | // This flag will allow us to delete the file later on from the browser |
| 91 | // process. |
[email protected] | 382aa7e | 2014-03-28 01:13:37 | [diff] [blame] | 92 | flags |= base::File::FLAG_SHARE_DELETE; |
[email protected] | 800ad56 | 2011-07-08 08:00:50 | [diff] [blame] | 93 | |
[email protected] | c93efc7c5 | 2009-10-29 19:58:30 | [diff] [blame] | 94 | // Try to open/create the DB file. |
[email protected] | 382aa7e | 2014-03-28 01:13:37 | [diff] [blame] | 95 | return base::File(file_path, flags); |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 98 | // static |
[email protected] | 382aa7e | 2014-03-28 01:13:37 | [diff] [blame] | 99 | base::File VfsBackend::OpenTempFileInDirectory(const base::FilePath& dir_path, |
| 100 | int desired_flags) { |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 101 | // We should be able to delete temp files when they're closed |
| 102 | // and create them as needed |
| 103 | if (!(desired_flags & SQLITE_OPEN_DELETEONCLOSE) || |
| 104 | !(desired_flags & SQLITE_OPEN_CREATE)) { |
[email protected] | 382aa7e | 2014-03-28 01:13:37 | [diff] [blame] | 105 | return base::File(); |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // Get a unique temp file name in the database directory. |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 109 | base::FilePath temp_file_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 110 | if (!base::CreateTemporaryFileInDir(dir_path, &temp_file_path)) |
[email protected] | 382aa7e | 2014-03-28 01:13:37 | [diff] [blame] | 111 | return base::File(); |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 112 | |
[email protected] | 382aa7e | 2014-03-28 01:13:37 | [diff] [blame] | 113 | return OpenFile(temp_file_path, desired_flags); |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 114 | } |
| 115 | |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 116 | // static |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 117 | int VfsBackend::DeleteFile(const base::FilePath& file_path, bool sync_dir) { |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 118 | if (!base::PathExists(file_path)) |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 119 | return SQLITE_OK; |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 120 | if (!base::DeleteFile(file_path, false)) |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 121 | return SQLITE_IOERR_DELETE; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 122 | |
| 123 | int error_code = SQLITE_OK; |
| 124 | #if defined(OS_POSIX) |
| 125 | if (sync_dir) { |
[email protected] | 382aa7e | 2014-03-28 01:13:37 | [diff] [blame] | 126 | base::File dir(file_path.DirName(), base::File::FLAG_READ); |
| 127 | if (dir.IsValid()) { |
| 128 | if (!dir.Flush()) |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 129 | error_code = SQLITE_IOERR_DIR_FSYNC; |
[email protected] | 382aa7e | 2014-03-28 01:13:37 | [diff] [blame] | 130 | } else { |
| 131 | error_code = SQLITE_CANTOPEN; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | #endif |
| 135 | return error_code; |
| 136 | } |
| 137 | |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 138 | // static |
avi | 5caf646e | 2015-12-21 21:21:50 | [diff] [blame] | 139 | uint32_t VfsBackend::GetFileAttributes(const base::FilePath& file_path) { |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 140 | #if defined(OS_WIN) |
avi | 5caf646e | 2015-12-21 21:21:50 | [diff] [blame] | 141 | uint32_t attributes = ::GetFileAttributes(file_path.value().c_str()); |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 142 | #elif defined(OS_POSIX) |
avi | 5caf646e | 2015-12-21 21:21:50 | [diff] [blame] | 143 | uint32_t attributes = 0; |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 144 | if (!access(file_path.value().c_str(), R_OK)) |
avi | 5caf646e | 2015-12-21 21:21:50 | [diff] [blame] | 145 | attributes |= static_cast<uint32_t>(R_OK); |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 146 | if (!access(file_path.value().c_str(), W_OK)) |
avi | 5caf646e | 2015-12-21 21:21:50 | [diff] [blame] | 147 | attributes |= static_cast<uint32_t>(W_OK); |
[email protected] | b5be3f53 | 2009-10-24 01:28:39 | [diff] [blame] | 148 | if (!attributes) |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 149 | attributes = -1; |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 150 | #endif |
| 151 | return attributes; |
| 152 | } |
| 153 | |
[email protected] | a3703dd | 2010-02-11 17:46:13 | [diff] [blame] | 154 | // static |
avi | 5caf646e | 2015-12-21 21:21:50 | [diff] [blame] | 155 | int64_t VfsBackend::GetFileSize(const base::FilePath& file_path) { |
| 156 | int64_t size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 157 | return (base::GetFileSize(file_path, &size) ? size : 0); |
[email protected] | da34ae0 | 2009-09-22 20:41:20 | [diff] [blame] | 158 | } |
| 159 | |
shess | 10ce3cc | 2015-04-06 18:52:16 | [diff] [blame] | 160 | // static |
avi | 5caf646e | 2015-12-21 21:21:50 | [diff] [blame] | 161 | bool VfsBackend::SetFileSize(const base::FilePath& file_path, int64_t size) { |
shess | 10ce3cc | 2015-04-06 18:52:16 | [diff] [blame] | 162 | int flags = 0; |
| 163 | flags |= base::File::FLAG_READ; |
| 164 | flags |= base::File::FLAG_WRITE; |
| 165 | flags |= base::File::FLAG_OPEN; |
| 166 | base::File file = base::File(file_path, flags); |
| 167 | if (!file.IsValid()) |
| 168 | return false; |
| 169 | return file.SetLength(size); |
| 170 | } |
| 171 | |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 172 | } // namespace storage |