OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "sql/mojo/mojo_vfs.h" | 5 #include "sql/mojo/mojo_vfs.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "base/strings/stringprintf.h" |
9 #include "components/filesystem/public/interfaces/file.mojom.h" | 10 #include "components/filesystem/public/interfaces/file.mojom.h" |
10 #include "components/filesystem/public/interfaces/file_system.mojom.h" | 11 #include "components/filesystem/public/interfaces/file_system.mojom.h" |
11 #include "components/filesystem/public/interfaces/types.mojom.h" | 12 #include "components/filesystem/public/interfaces/types.mojom.h" |
12 #include "mojo/public/cpp/bindings/lib/template_util.h" | 13 #include "mojo/public/cpp/bindings/lib/template_util.h" |
13 #include "mojo/util/capture_util.h" | 14 #include "mojo/util/capture_util.h" |
14 #include "third_party/sqlite/sqlite3.h" | 15 #include "third_party/sqlite/sqlite3.h" |
15 | 16 |
16 using mojo::Capture; | 17 using mojo::Capture; |
17 | 18 |
18 namespace sql { | 19 namespace sql { |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 open_flags = filesystem::kFlagOpenAlways; | 239 open_flags = filesystem::kFlagOpenAlways; |
239 } else { | 240 } else { |
240 open_flags = filesystem::kFlagOpen; | 241 open_flags = filesystem::kFlagOpen; |
241 } | 242 } |
242 open_flags |= filesystem::kFlagRead; | 243 open_flags |= filesystem::kFlagRead; |
243 if (flags & SQLITE_OPEN_READWRITE) | 244 if (flags & SQLITE_OPEN_READWRITE) |
244 open_flags |= filesystem::kFlagWrite; | 245 open_flags |= filesystem::kFlagWrite; |
245 if (flags & SQLITE_OPEN_DELETEONCLOSE) | 246 if (flags & SQLITE_OPEN_DELETEONCLOSE) |
246 open_flags |= filesystem::kDeleteOnClose; | 247 open_flags |= filesystem::kDeleteOnClose; |
247 | 248 |
| 249 mojo::String mojo_name; |
| 250 if (name) { |
| 251 // Don't let callers open the pattern of our temporary databases. When we |
| 252 // open with a null name and SQLITE_OPEN_DELETEONCLOSE, we unlink the |
| 253 // database after we open it. If we create a database here, close it |
| 254 // normally, and then open the same file through the other path, we could |
| 255 // delete the database. |
| 256 CHECK(strncmp("Temp_", name, 5) != 0); |
| 257 mojo_name = name; |
| 258 } else { |
| 259 DCHECK(flags & SQLITE_OPEN_DELETEONCLOSE); |
| 260 static int temp_number = 0; |
| 261 mojo_name = base::StringPrintf("Temp_%d.db", temp_number++); |
| 262 } |
| 263 |
248 // Grab the incoming file | 264 // Grab the incoming file |
249 filesystem::FilePtr file_ptr; | 265 filesystem::FilePtr file_ptr; |
250 filesystem::FileError error = filesystem::FILE_ERROR_FAILED; | 266 filesystem::FileError error = filesystem::FILE_ERROR_FAILED; |
251 GetRootDirectory(mojo_vfs)->OpenFile(mojo::String(name), GetProxy(&file_ptr), | 267 GetRootDirectory(mojo_vfs)->OpenFile(mojo_name, GetProxy(&file_ptr), |
252 open_flags, Capture(&error)); | 268 open_flags, Capture(&error)); |
253 GetRootDirectory(mojo_vfs).WaitForIncomingResponse(); | 269 GetRootDirectory(mojo_vfs).WaitForIncomingResponse(); |
254 if (error != filesystem::FILE_ERROR_OK) { | 270 if (error != filesystem::FILE_ERROR_OK) { |
255 // TODO(erg): Translate more of the mojo error codes into sqlite error | 271 // TODO(erg): Translate more of the mojo error codes into sqlite error |
256 // codes. | 272 // codes. |
257 return SQLITE_CANTOPEN; | 273 return SQLITE_CANTOPEN; |
258 } | 274 } |
259 | 275 |
260 // Set the method table so we can be closed (and run the manual dtor call to | 276 // Set the method table so we can be closed (and run the manual dtor call to |
261 // match the following placement news). | 277 // match the following placement news). |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 | 420 |
405 CHECK(sqlite3_vfs_register(parent_, 1) == SQLITE_OK); | 421 CHECK(sqlite3_vfs_register(parent_, 1) == SQLITE_OK); |
406 CHECK(sqlite3_vfs_unregister(&mojo_vfs) == SQLITE_OK); | 422 CHECK(sqlite3_vfs_unregister(&mojo_vfs) == SQLITE_OK); |
407 } | 423 } |
408 | 424 |
409 filesystem::DirectoryPtr& ScopedMojoFilesystemVFS::GetDirectory() { | 425 filesystem::DirectoryPtr& ScopedMojoFilesystemVFS::GetDirectory() { |
410 return root_directory_; | 426 return root_directory_; |
411 } | 427 } |
412 | 428 |
413 } // namespace sql | 429 } // namespace sql |
OLD | NEW |