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 "FileSystem.h"
35 #include "SQLiteDatabase.h"
36 #include "SQLiteStatement.h"
37 #include <sqlite3.h>
38
39 namespace WebCore {
40
SQLiteFileSystem()41 SQLiteFileSystem::SQLiteFileSystem()
42 {
43 }
44
registerSQLiteVFS()45 void SQLiteFileSystem::registerSQLiteVFS()
46 {
47 }
48
openDatabase(const String & fileName,sqlite3 ** database)49 int SQLiteFileSystem::openDatabase(const String& fileName, sqlite3** database)
50 {
51 // SQLite expects a null terminator on its UTF-16 strings.
52 String path = fileName;
53 return sqlite3_open16(path.charactersWithNullTermination(), database);
54 }
55
getFileNameForNewDatabase(const String & dbDir,const String &,const String &,SQLiteDatabase * db)56 String SQLiteFileSystem::getFileNameForNewDatabase(const String& dbDir, const String&,
57 const String&, SQLiteDatabase* db)
58 {
59 // try to get the next sequence number from the given database
60 // if we can't get a number, return an empty string
61 SQLiteStatement sequenceStatement(*db, "SELECT seq FROM sqlite_sequence WHERE name='Databases';");
62 if (sequenceStatement.prepare() != SQLResultOk)
63 return String();
64 int result = sequenceStatement.step();
65 int64_t seq = 0;
66 if (result == SQLResultRow)
67 seq = sequenceStatement.getColumnInt64(0);
68 else if (result != SQLResultDone)
69 return String();
70 sequenceStatement.finalize();
71
72 // increment the number until we can use it to form a file name that doesn't exist
73 String fileName;
74 do {
75 ++seq;
76 fileName = pathByAppendingComponent(dbDir, String::format("%016llx.db", seq));
77 } while (fileExists(fileName));
78
79 return String::format("%016llx.db", seq);
80 }
81
appendDatabaseFileNameToPath(const String & path,const String & fileName)82 String SQLiteFileSystem::appendDatabaseFileNameToPath(const String& path, const String& fileName)
83 {
84 return pathByAppendingComponent(path, fileName);
85 }
86
ensureDatabaseDirectoryExists(const String & path)87 bool SQLiteFileSystem::ensureDatabaseDirectoryExists(const String& path)
88 {
89 if (path.isEmpty())
90 return false;
91 return makeAllDirectories(path);
92 }
93
ensureDatabaseFileExists(const String & fileName,bool checkPathOnly)94 bool SQLiteFileSystem::ensureDatabaseFileExists(const String& fileName, bool checkPathOnly)
95 {
96 if (fileName.isEmpty())
97 return false;
98
99 if (checkPathOnly) {
100 String dir = directoryName(fileName);
101 return ensureDatabaseDirectoryExists(dir);
102 }
103
104 return fileExists(fileName);
105 }
106
deleteEmptyDatabaseDirectory(const String & path)107 bool SQLiteFileSystem::deleteEmptyDatabaseDirectory(const String& path)
108 {
109 return deleteEmptyDirectory(path);
110 }
111
deleteDatabaseFile(const String & fileName)112 bool SQLiteFileSystem::deleteDatabaseFile(const String& fileName)
113 {
114 return deleteFile(fileName);
115 }
116
getDatabaseFileSize(const String & fileName)117 long long SQLiteFileSystem::getDatabaseFileSize(const String& fileName)
118 {
119 long long size;
120 return getFileSize(fileName, size) ? size : 0;
121 }
122
123 } // namespace WebCore
124