OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/browser/database/vfs_backend.h" | 5 #include "webkit/browser/database/vfs_backend.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "third_party/sqlite/sqlite3.h" | 10 #include "third_party/sqlite/sqlite3.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 return (file_type == SQLITE_OPEN_MAIN_DB) || | 48 return (file_type == SQLITE_OPEN_MAIN_DB) || |
49 (file_type == SQLITE_OPEN_TEMP_DB) || | 49 (file_type == SQLITE_OPEN_TEMP_DB) || |
50 (file_type == SQLITE_OPEN_MAIN_JOURNAL) || | 50 (file_type == SQLITE_OPEN_MAIN_JOURNAL) || |
51 (file_type == SQLITE_OPEN_TEMP_JOURNAL) || | 51 (file_type == SQLITE_OPEN_TEMP_JOURNAL) || |
52 (file_type == SQLITE_OPEN_SUBJOURNAL) || | 52 (file_type == SQLITE_OPEN_SUBJOURNAL) || |
53 (file_type == SQLITE_OPEN_MASTER_JOURNAL) || | 53 (file_type == SQLITE_OPEN_MASTER_JOURNAL) || |
54 (file_type == SQLITE_OPEN_TRANSIENT_DB); | 54 (file_type == SQLITE_OPEN_TRANSIENT_DB); |
55 } | 55 } |
56 | 56 |
57 // static | 57 // static |
58 void VfsBackend::OpenFile(const base::FilePath& file_path, | 58 base::File VfsBackend::OpenFile(const base::FilePath& file_path, |
59 int desired_flags, | 59 int desired_flags) { |
60 base::PlatformFile* file_handle) { | |
61 DCHECK(!file_path.empty()); | 60 DCHECK(!file_path.empty()); |
62 | 61 |
63 // Verify the flags for consistency and create the database | 62 // Verify the flags for consistency and create the database |
64 // directory if it doesn't exist. | 63 // directory if it doesn't exist. |
65 if (!OpenFileFlagsAreConsistent(desired_flags) || | 64 if (!OpenFileFlagsAreConsistent(desired_flags) || |
66 !base::CreateDirectory(file_path.DirName())) | 65 !base::CreateDirectory(file_path.DirName())) { |
67 return; | 66 return base::File(); |
| 67 } |
68 | 68 |
69 int flags = 0; | 69 int flags = 0; |
70 flags |= base::PLATFORM_FILE_READ; | 70 flags |= base::File::FLAG_READ; |
71 if (desired_flags & SQLITE_OPEN_READWRITE) | 71 if (desired_flags & SQLITE_OPEN_READWRITE) |
72 flags |= base::PLATFORM_FILE_WRITE; | 72 flags |= base::File::FLAG_WRITE; |
73 | 73 |
74 if (!(desired_flags & SQLITE_OPEN_MAIN_DB)) { | 74 if (!(desired_flags & SQLITE_OPEN_MAIN_DB)) |
75 flags |= base::PLATFORM_FILE_EXCLUSIVE_READ | | 75 flags |= base::File::FLAG_EXCLUSIVE_READ | base::File::FLAG_EXCLUSIVE_WRITE; |
76 base::PLATFORM_FILE_EXCLUSIVE_WRITE; | |
77 } | |
78 | 76 |
79 flags |= ((desired_flags & SQLITE_OPEN_CREATE) ? | 77 flags |= ((desired_flags & SQLITE_OPEN_CREATE) ? |
80 base::PLATFORM_FILE_OPEN_ALWAYS : base::PLATFORM_FILE_OPEN); | 78 base::File::FLAG_OPEN_ALWAYS : base::File::FLAG_OPEN); |
81 | 79 |
82 if (desired_flags & SQLITE_OPEN_EXCLUSIVE) { | 80 if (desired_flags & SQLITE_OPEN_EXCLUSIVE) |
83 flags |= base::PLATFORM_FILE_EXCLUSIVE_READ | | 81 flags |= base::File::FLAG_EXCLUSIVE_READ | base::File::FLAG_EXCLUSIVE_WRITE; |
84 base::PLATFORM_FILE_EXCLUSIVE_WRITE; | |
85 } | |
86 | 82 |
87 if (desired_flags & SQLITE_OPEN_DELETEONCLOSE) { | 83 if (desired_flags & SQLITE_OPEN_DELETEONCLOSE) { |
88 flags |= base::PLATFORM_FILE_TEMPORARY | base::PLATFORM_FILE_HIDDEN | | 84 flags |= base::File::FLAG_TEMPORARY | base::File::FLAG_HIDDEN | |
89 base::PLATFORM_FILE_DELETE_ON_CLOSE; | 85 base::File::FLAG_DELETE_ON_CLOSE; |
90 } | 86 } |
91 | 87 |
92 // This flag will allow us to delete the file later on from the browser | 88 // This flag will allow us to delete the file later on from the browser |
93 // process. | 89 // process. |
94 flags |= base::PLATFORM_FILE_SHARE_DELETE; | 90 flags |= base::File::FLAG_SHARE_DELETE; |
95 | 91 |
96 // Try to open/create the DB file. | 92 // Try to open/create the DB file. |
97 *file_handle = | 93 return base::File(file_path, flags); |
98 base::CreatePlatformFile(file_path, flags, NULL, NULL); | |
99 } | 94 } |
100 | 95 |
101 // static | 96 // static |
102 void VfsBackend::OpenTempFileInDirectory( | 97 base::File VfsBackend::OpenTempFileInDirectory(const base::FilePath& dir_path, |
103 const base::FilePath& dir_path, | 98 int desired_flags) { |
104 int desired_flags, | |
105 base::PlatformFile* file_handle) { | |
106 // We should be able to delete temp files when they're closed | 99 // We should be able to delete temp files when they're closed |
107 // and create them as needed | 100 // and create them as needed |
108 if (!(desired_flags & SQLITE_OPEN_DELETEONCLOSE) || | 101 if (!(desired_flags & SQLITE_OPEN_DELETEONCLOSE) || |
109 !(desired_flags & SQLITE_OPEN_CREATE)) { | 102 !(desired_flags & SQLITE_OPEN_CREATE)) { |
110 return; | 103 return base::File(); |
111 } | 104 } |
112 | 105 |
113 // Get a unique temp file name in the database directory. | 106 // Get a unique temp file name in the database directory. |
114 base::FilePath temp_file_path; | 107 base::FilePath temp_file_path; |
115 if (!base::CreateTemporaryFileInDir(dir_path, &temp_file_path)) | 108 if (!base::CreateTemporaryFileInDir(dir_path, &temp_file_path)) |
116 return; | 109 return base::File(); |
117 | 110 |
118 OpenFile(temp_file_path, desired_flags, file_handle); | 111 return OpenFile(temp_file_path, desired_flags); |
119 } | 112 } |
120 | 113 |
121 // static | 114 // static |
122 int VfsBackend::DeleteFile(const base::FilePath& file_path, bool sync_dir) { | 115 int VfsBackend::DeleteFile(const base::FilePath& file_path, bool sync_dir) { |
123 if (!base::PathExists(file_path)) | 116 if (!base::PathExists(file_path)) |
124 return SQLITE_OK; | 117 return SQLITE_OK; |
125 if (!base::DeleteFile(file_path, false)) | 118 if (!base::DeleteFile(file_path, false)) |
126 return SQLITE_IOERR_DELETE; | 119 return SQLITE_IOERR_DELETE; |
127 | 120 |
128 int error_code = SQLITE_OK; | 121 int error_code = SQLITE_OK; |
129 #if defined(OS_POSIX) | 122 #if defined(OS_POSIX) |
130 if (sync_dir) { | 123 if (sync_dir) { |
131 base::PlatformFile dir_fd = base::CreatePlatformFile( | 124 base::File dir(file_path.DirName(), base::File::FLAG_READ); |
132 file_path.DirName(), base::PLATFORM_FILE_READ, NULL, NULL); | 125 if (dir.IsValid()) { |
133 if (dir_fd == base::kInvalidPlatformFileValue) { | 126 if (!dir.Flush()) |
| 127 error_code = SQLITE_IOERR_DIR_FSYNC; |
| 128 } else { |
134 error_code = SQLITE_CANTOPEN; | 129 error_code = SQLITE_CANTOPEN; |
135 } else { | |
136 if (fsync(dir_fd)) | |
137 error_code = SQLITE_IOERR_DIR_FSYNC; | |
138 base::ClosePlatformFile(dir_fd); | |
139 } | 130 } |
140 } | 131 } |
141 #endif | 132 #endif |
142 return error_code; | 133 return error_code; |
143 } | 134 } |
144 | 135 |
145 // static | 136 // static |
146 uint32 VfsBackend::GetFileAttributes(const base::FilePath& file_path) { | 137 uint32 VfsBackend::GetFileAttributes(const base::FilePath& file_path) { |
147 #if defined(OS_WIN) | 138 #if defined(OS_WIN) |
148 uint32 attributes = ::GetFileAttributes(file_path.value().c_str()); | 139 uint32 attributes = ::GetFileAttributes(file_path.value().c_str()); |
149 #elif defined(OS_POSIX) | 140 #elif defined(OS_POSIX) |
150 uint32 attributes = 0; | 141 uint32 attributes = 0; |
151 if (!access(file_path.value().c_str(), R_OK)) | 142 if (!access(file_path.value().c_str(), R_OK)) |
152 attributes |= static_cast<uint32>(R_OK); | 143 attributes |= static_cast<uint32>(R_OK); |
153 if (!access(file_path.value().c_str(), W_OK)) | 144 if (!access(file_path.value().c_str(), W_OK)) |
154 attributes |= static_cast<uint32>(W_OK); | 145 attributes |= static_cast<uint32>(W_OK); |
155 if (!attributes) | 146 if (!attributes) |
156 attributes = -1; | 147 attributes = -1; |
157 #endif | 148 #endif |
158 return attributes; | 149 return attributes; |
159 } | 150 } |
160 | 151 |
161 // static | 152 // static |
162 int64 VfsBackend::GetFileSize(const base::FilePath& file_path) { | 153 int64 VfsBackend::GetFileSize(const base::FilePath& file_path) { |
163 int64 size = 0; | 154 int64 size = 0; |
164 return (base::GetFileSize(file_path, &size) ? size : 0); | 155 return (base::GetFileSize(file_path, &size) ? size : 0); |
165 } | 156 } |
166 | 157 |
167 } // namespace webkit_database | 158 } // namespace webkit_database |
OLD | NEW |