Determine wallet file type based on file magic

This commit is contained in:
Andrew Chow
2020-05-26 20:54:05 -04:00
parent 6045f77003
commit ac38a87225
7 changed files with 83 additions and 30 deletions

View File

@@ -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");
}