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