1 | /* |
2 | * Copyright (C) 2009 Google Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions are |
6 | * met: |
7 | * |
8 | * * Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * * Redistributions in binary form must reproduce the above |
11 | * copyright notice, this list of conditions and the following disclaimer |
12 | * in the documentation and/or other materials provided with the |
13 | * distribution. |
14 | * * Neither the name of Google Inc. nor the names of its |
15 | * contributors may be used to endorse or promote products derived from |
16 | * this software without specific prior written permission. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
30 | |
31 | #include "config.h" |
32 | #include "SQLiteFileSystem.h" |
33 | |
34 | #include "SQLiteDatabase.h" |
35 | #include "SQLiteStatement.h" |
36 | #include <pal/crypto/CryptoDigest.h> |
37 | #include <sqlite3.h> |
38 | #include <wtf/FileSystem.h> |
39 | |
40 | #if PLATFORM(IOS_FAMILY) |
41 | #include <pal/spi/ios/SQLite3SPI.h> |
42 | #endif |
43 | |
44 | namespace WebCore { |
45 | |
46 | SQLiteFileSystem::SQLiteFileSystem() |
47 | { |
48 | } |
49 | |
50 | String SQLiteFileSystem::appendDatabaseFileNameToPath(const String& path, const String& fileName) |
51 | { |
52 | return FileSystem::pathByAppendingComponent(path, fileName); |
53 | } |
54 | |
55 | bool SQLiteFileSystem::ensureDatabaseDirectoryExists(const String& path) |
56 | { |
57 | if (path.isEmpty()) |
58 | return false; |
59 | return FileSystem::makeAllDirectories(path); |
60 | } |
61 | |
62 | bool SQLiteFileSystem::ensureDatabaseFileExists(const String& fileName, bool checkPathOnly) |
63 | { |
64 | if (fileName.isEmpty()) |
65 | return false; |
66 | |
67 | if (checkPathOnly) { |
68 | String dir = FileSystem::directoryName(fileName); |
69 | return ensureDatabaseDirectoryExists(dir); |
70 | } |
71 | |
72 | return FileSystem::fileExists(fileName); |
73 | } |
74 | |
75 | bool SQLiteFileSystem::deleteEmptyDatabaseDirectory(const String& path) |
76 | { |
77 | return FileSystem::deleteEmptyDirectory(path); |
78 | } |
79 | |
80 | bool SQLiteFileSystem::deleteDatabaseFile(const String& fileName) |
81 | { |
82 | String walFileName = makeString(fileName, "-wal"_s ); |
83 | String shmFileName = makeString(fileName, "-shm"_s ); |
84 | |
85 | // Try to delete all three files whether or not they are there. |
86 | FileSystem::deleteFile(fileName); |
87 | FileSystem::deleteFile(walFileName); |
88 | FileSystem::deleteFile(shmFileName); |
89 | |
90 | // If any of the wal or shm files remain after the delete attempt, the overall delete operation failed. |
91 | return !FileSystem::fileExists(fileName) && !FileSystem::fileExists(walFileName) && !FileSystem::fileExists(shmFileName); |
92 | } |
93 | |
94 | #if PLATFORM(IOS_FAMILY) |
95 | bool SQLiteFileSystem::truncateDatabaseFile(sqlite3* database) |
96 | { |
97 | return sqlite3_file_control(database, 0, SQLITE_TRUNCATE_DATABASE, 0) == SQLITE_OK; |
98 | } |
99 | #endif |
100 | |
101 | long long SQLiteFileSystem::getDatabaseFileSize(const String& fileName) |
102 | { |
103 | long long fileSize = 0; |
104 | long long totalSize = 0; |
105 | |
106 | if (FileSystem::getFileSize(fileName, fileSize)) |
107 | totalSize += fileSize; |
108 | |
109 | if (FileSystem::getFileSize(makeString(fileName, "-wal"_s ), fileSize)) |
110 | totalSize += fileSize; |
111 | |
112 | if (FileSystem::getFileSize(makeString(fileName, "-shm"_s ), fileSize)) |
113 | totalSize += fileSize; |
114 | |
115 | return totalSize; |
116 | } |
117 | |
118 | Optional<WallTime> SQLiteFileSystem::databaseCreationTime(const String& fileName) |
119 | { |
120 | return FileSystem::getFileCreationTime(fileName); |
121 | } |
122 | |
123 | Optional<WallTime> SQLiteFileSystem::databaseModificationTime(const String& fileName) |
124 | { |
125 | return FileSystem::getFileModificationTime(fileName); |
126 | } |
127 | |
128 | String SQLiteFileSystem::computeHashForFileName(const String& fileName) |
129 | { |
130 | auto cryptoDigest = PAL::CryptoDigest::create(PAL::CryptoDigest::Algorithm::SHA_256); |
131 | cryptoDigest->addBytes(fileName.utf8().data(), fileName.utf8().length()); |
132 | auto digest = cryptoDigest->computeHash(); |
133 | |
134 | // Convert digest to hex. |
135 | char* start = 0; |
136 | unsigned digestLength = digest.size(); |
137 | CString result = CString::newUninitialized(digestLength * 2, start); |
138 | char* buffer = start; |
139 | for (size_t i = 0; i < digestLength; ++i) { |
140 | snprintf(buffer, 3, "%02X" , digest.at(i)); |
141 | buffer += 2; |
142 | } |
143 | return String::fromUTF8(result); |
144 | } |
145 | |
146 | } // namespace WebCore |
147 | |