diff options
Diffstat (limited to 'src/wallet/sqlite.cpp')
-rw-r--r-- | src/wallet/sqlite.cpp | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/wallet/sqlite.cpp b/src/wallet/sqlite.cpp index 5c30d72e84..ce390440d9 100644 --- a/src/wallet/sqlite.cpp +++ b/src/wallet/sqlite.cpp @@ -502,7 +502,8 @@ bool SQLiteBatch::TxnAbort() bool ExistsSQLiteDatabase(const fs::path& path) { - return false; + const fs::path file = path / DATABASE_FILENAME; + return fs::symlink_status(file).type() == fs::regular_file && IsSQLiteFile(file); } std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error) @@ -526,3 +527,26 @@ std::string SQLiteDatabaseVersion() { return std::string(sqlite3_libversion()); } + +bool IsSQLiteFile(const fs::path& path) +{ + if (!fs::exists(path)) return false; + + // A SQLite Database file is at least 512 bytes. + boost::system::error_code ec; + auto size = fs::file_size(path, ec); + if (ec) LogPrintf("%s: %s %s\n", __func__, ec.message(), path.string()); + if (size < 512) return false; + + fsbridge::ifstream file(path, std::ios::binary); + if (!file.is_open()) return false; + + // Magic is at beginning and is 16 bytes long + char magic[16]; + file.read(magic, 16); + file.close(); + + // Check the magic, see https://sqlite.org/fileformat2.html + std::string magic_str(magic); + return magic_str == std::string("SQLite format 3"); +} |