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#include "FileSystem.h" 36#include "SQLiteDatabase.h" 37#include "SQLiteStatement.h" 38#include <inttypes.h> 39#include <sqlite3.h> 40 41#if PLATFORM(IOS) 42#include <sqlite3_private.h> 43#endif 44 45namespace WebCore { 46 47SQLiteFileSystem::SQLiteFileSystem() 48{ 49} 50 51int SQLiteFileSystem::openDatabase(const String& filename, sqlite3** database, bool) 52{ 53 return sqlite3_open(filename.utf8().data(), database); 54} 55 56String 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("%016" PRIx64 ".db", seq)); 77 } while (fileExists(fileName)); 78 79 return String::format("%016" PRIx64 ".db", seq); 80} 81 82String SQLiteFileSystem::appendDatabaseFileNameToPath(const String& path, const String& fileName) 83{ 84 return pathByAppendingComponent(path, fileName); 85} 86 87bool SQLiteFileSystem::ensureDatabaseDirectoryExists(const String& path) 88{ 89 if (path.isEmpty()) 90 return false; 91 return makeAllDirectories(path); 92} 93 94bool 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 107bool SQLiteFileSystem::deleteEmptyDatabaseDirectory(const String& path) 108{ 109 return deleteEmptyDirectory(path); 110} 111 112bool SQLiteFileSystem::deleteDatabaseFile(const String& fileName) 113{ 114 return deleteFile(fileName); 115} 116 117#if PLATFORM(IOS) 118bool SQLiteFileSystem::truncateDatabaseFile(sqlite3* database) 119{ 120 return sqlite3_file_control(database, 0, SQLITE_TRUNCATE_DATABASE, 0) == SQLITE_OK; 121} 122#endif 123 124long long SQLiteFileSystem::getDatabaseFileSize(const String& fileName) 125{ 126 long long size; 127 return getFileSize(fileName, size) ? size : 0; 128} 129 130double SQLiteFileSystem::databaseCreationTime(const String& fileName) 131{ 132 time_t time; 133 return getFileCreationTime(fileName, time) ? time : 0; 134} 135 136double SQLiteFileSystem::databaseModificationTime(const String& fileName) 137{ 138 time_t time; 139 return getFileModificationTime(fileName, time) ? time : 0; 140} 141 142} // namespace WebCore 143