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/database/vfs_backend.h" | 5 #include "webkit/database/vfs_backend.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.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 FilePath& file_path, | 58 void VfsBackend::OpenFile(const base::FilePath& file_path, |
59 int desired_flags, | 59 int desired_flags, |
60 base::PlatformFile* file_handle) { | 60 base::PlatformFile* file_handle) { |
61 DCHECK(!file_path.empty()); | 61 DCHECK(!file_path.empty()); |
62 | 62 |
63 // Verify the flags for consistency and create the database | 63 // Verify the flags for consistency and create the database |
64 // directory if it doesn't exist. | 64 // directory if it doesn't exist. |
65 if (!OpenFileFlagsAreConsistent(desired_flags) || | 65 if (!OpenFileFlagsAreConsistent(desired_flags) || |
66 !file_util::CreateDirectory(file_path.DirName())) | 66 !file_util::CreateDirectory(file_path.DirName())) |
67 return; | 67 return; |
68 | 68 |
(...skipping 24 matching lines...) Expand all Loading... |
93 // process. | 93 // process. |
94 flags |= base::PLATFORM_FILE_SHARE_DELETE; | 94 flags |= base::PLATFORM_FILE_SHARE_DELETE; |
95 | 95 |
96 // Try to open/create the DB file. | 96 // Try to open/create the DB file. |
97 *file_handle = | 97 *file_handle = |
98 base::CreatePlatformFile(file_path, flags, NULL, NULL); | 98 base::CreatePlatformFile(file_path, flags, NULL, NULL); |
99 } | 99 } |
100 | 100 |
101 // static | 101 // static |
102 void VfsBackend::OpenTempFileInDirectory( | 102 void VfsBackend::OpenTempFileInDirectory( |
103 const FilePath& dir_path, | 103 const base::FilePath& dir_path, |
104 int desired_flags, | 104 int desired_flags, |
105 base::PlatformFile* file_handle) { | 105 base::PlatformFile* file_handle) { |
106 // We should be able to delete temp files when they're closed | 106 // We should be able to delete temp files when they're closed |
107 // and create them as needed | 107 // and create them as needed |
108 if (!(desired_flags & SQLITE_OPEN_DELETEONCLOSE) || | 108 if (!(desired_flags & SQLITE_OPEN_DELETEONCLOSE) || |
109 !(desired_flags & SQLITE_OPEN_CREATE)) { | 109 !(desired_flags & SQLITE_OPEN_CREATE)) { |
110 return; | 110 return; |
111 } | 111 } |
112 | 112 |
113 // Get a unique temp file name in the database directory. | 113 // Get a unique temp file name in the database directory. |
114 FilePath temp_file_path; | 114 base::FilePath temp_file_path; |
115 if (!file_util::CreateTemporaryFileInDir(dir_path, &temp_file_path)) | 115 if (!file_util::CreateTemporaryFileInDir(dir_path, &temp_file_path)) |
116 return; | 116 return; |
117 | 117 |
118 OpenFile(temp_file_path, desired_flags, file_handle); | 118 OpenFile(temp_file_path, desired_flags, file_handle); |
119 } | 119 } |
120 | 120 |
121 // static | 121 // static |
122 int VfsBackend::DeleteFile(const FilePath& file_path, bool sync_dir) { | 122 int VfsBackend::DeleteFile(const base::FilePath& file_path, bool sync_dir) { |
123 if (!file_util::PathExists(file_path)) | 123 if (!file_util::PathExists(file_path)) |
124 return SQLITE_OK; | 124 return SQLITE_OK; |
125 if (!file_util::Delete(file_path, false)) | 125 if (!file_util::Delete(file_path, false)) |
126 return SQLITE_IOERR_DELETE; | 126 return SQLITE_IOERR_DELETE; |
127 | 127 |
128 int error_code = SQLITE_OK; | 128 int error_code = SQLITE_OK; |
129 #if defined(OS_POSIX) | 129 #if defined(OS_POSIX) |
130 if (sync_dir) { | 130 if (sync_dir) { |
131 base::PlatformFile dir_fd = base::CreatePlatformFile( | 131 base::PlatformFile dir_fd = base::CreatePlatformFile( |
132 file_path.DirName(), base::PLATFORM_FILE_READ, NULL, NULL); | 132 file_path.DirName(), base::PLATFORM_FILE_READ, NULL, NULL); |
133 if (dir_fd == base::kInvalidPlatformFileValue) { | 133 if (dir_fd == base::kInvalidPlatformFileValue) { |
134 error_code = SQLITE_CANTOPEN; | 134 error_code = SQLITE_CANTOPEN; |
135 } else { | 135 } else { |
136 if (fsync(dir_fd)) | 136 if (fsync(dir_fd)) |
137 error_code = SQLITE_IOERR_DIR_FSYNC; | 137 error_code = SQLITE_IOERR_DIR_FSYNC; |
138 base::ClosePlatformFile(dir_fd); | 138 base::ClosePlatformFile(dir_fd); |
139 } | 139 } |
140 } | 140 } |
141 #endif | 141 #endif |
142 return error_code; | 142 return error_code; |
143 } | 143 } |
144 | 144 |
145 // static | 145 // static |
146 uint32 VfsBackend::GetFileAttributes(const FilePath& file_path) { | 146 uint32 VfsBackend::GetFileAttributes(const base::FilePath& file_path) { |
147 #if defined(OS_WIN) | 147 #if defined(OS_WIN) |
148 uint32 attributes = ::GetFileAttributes(file_path.value().c_str()); | 148 uint32 attributes = ::GetFileAttributes(file_path.value().c_str()); |
149 #elif defined(OS_POSIX) | 149 #elif defined(OS_POSIX) |
150 uint32 attributes = 0; | 150 uint32 attributes = 0; |
151 if (!access(file_path.value().c_str(), R_OK)) | 151 if (!access(file_path.value().c_str(), R_OK)) |
152 attributes |= static_cast<uint32>(R_OK); | 152 attributes |= static_cast<uint32>(R_OK); |
153 if (!access(file_path.value().c_str(), W_OK)) | 153 if (!access(file_path.value().c_str(), W_OK)) |
154 attributes |= static_cast<uint32>(W_OK); | 154 attributes |= static_cast<uint32>(W_OK); |
155 if (!attributes) | 155 if (!attributes) |
156 attributes = -1; | 156 attributes = -1; |
157 #endif | 157 #endif |
158 return attributes; | 158 return attributes; |
159 } | 159 } |
160 | 160 |
161 // static | 161 // static |
162 int64 VfsBackend::GetFileSize(const FilePath& file_path) { | 162 int64 VfsBackend::GetFileSize(const base::FilePath& file_path) { |
163 int64 size = 0; | 163 int64 size = 0; |
164 return (file_util::GetFileSize(file_path, &size) ? size : 0); | 164 return (file_util::GetFileSize(file_path, &size) ? size : 0); |
165 } | 165 } |
166 | 166 |
167 } // namespace webkit_database | 167 } // namespace webkit_database |
OLD | NEW |