LCOV - code coverage report
Current view: top level - src/wallet - db.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 72 80 90.0 %
Date: 2022-04-21 14:51:19 Functions: 6 6 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 40 48 83.3 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
#       2                 :            : // Copyright (c) 2009-2021 The Bitcoin Core developers
#       3                 :            : // Distributed under the MIT software license, see the accompanying
#       4                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#       5                 :            : 
#       6                 :            : #include <chainparams.h>
#       7                 :            : #include <fs.h>
#       8                 :            : #include <logging.h>
#       9                 :            : #include <util/system.h>
#      10                 :            : #include <wallet/db.h>
#      11                 :            : 
#      12                 :            : #include <exception>
#      13                 :            : #include <fstream>
#      14                 :            : #include <string>
#      15                 :            : #include <system_error>
#      16                 :            : #include <vector>
#      17                 :            : 
#      18                 :            : namespace wallet {
#      19                 :            : std::vector<fs::path> ListDatabases(const fs::path& wallet_dir)
#      20                 :         13 : {
#      21                 :         13 :     std::vector<fs::path> paths;
#      22                 :         13 :     std::error_code ec;
#      23                 :            : 
#      24         [ +  + ]:        416 :     for (auto it = fs::recursive_directory_iterator(wallet_dir, ec); it != fs::recursive_directory_iterator(); it.increment(ec)) {
#      25         [ -  + ]:        403 :         if (ec) {
#      26         [ #  # ]:          0 :             if (fs::is_directory(*it)) {
#      27                 :          0 :                 it.disable_recursion_pending();
#      28                 :          0 :                 LogPrintf("%s: %s %s -- skipping.\n", __func__, ec.message(), fs::PathToString(it->path()));
#      29                 :          0 :             } else {
#      30                 :          0 :                 LogPrintf("%s: %s %s\n", __func__, ec.message(), fs::PathToString(it->path()));
#      31                 :          0 :             }
#      32                 :          0 :             continue;
#      33                 :          0 :         }
#      34                 :            : 
#      35                 :        403 :         try {
#      36                 :        403 :             const fs::path path{it->path().lexically_relative(wallet_dir)};
#      37                 :            : 
#      38 [ +  + ][ +  + ]:        403 :             if (it->status().type() == fs::file_type::directory &&
#      39 [ +  + ][ +  + ]:        403 :                 (IsBDBFile(BDBDataFile(it->path())) || IsSQLiteFile(SQLiteDataFile(it->path())))) {
#      40                 :            :                 // Found a directory which contains wallet.dat btree file, add it as a wallet.
#      41                 :         70 :                 paths.emplace_back(path);
#      42 [ +  + ][ +  + ]:        333 :             } else if (it.depth() == 0 && it->symlink_status().type() == fs::file_type::regular && IsBDBFile(it->path())) {
#         [ +  + ][ +  + ]
#      43         [ +  + ]:         18 :                 if (it->path().filename() == "wallet.dat") {
#      44                 :            :                     // Found top-level wallet.dat btree file, add top level directory ""
#      45                 :            :                     // as a wallet.
#      46                 :          8 :                     paths.emplace_back();
#      47                 :         10 :                 } else {
#      48                 :            :                     // Found top-level btree file not called wallet.dat. Current bitcoin
#      49                 :            :                     // software will never create these files but will allow them to be
#      50                 :            :                     // opened in a shared database environment for backwards compatibility.
#      51                 :            :                     // Add it to the list of available wallets.
#      52                 :         10 :                     paths.emplace_back(path);
#      53                 :         10 :                 }
#      54                 :         18 :             }
#      55                 :        403 :         } catch (const std::exception& e) {
#      56                 :         12 :             LogPrintf("%s: Error scanning %s: %s\n", __func__, fs::PathToString(it->path()), e.what());
#      57                 :         12 :             it.disable_recursion_pending();
#      58                 :         12 :         }
#      59                 :        403 :     }
#      60                 :            : 
#      61                 :         13 :     return paths;
#      62                 :         13 : }
#      63                 :            : 
#      64                 :            : fs::path BDBDataFile(const fs::path& wallet_path)
#      65                 :       2038 : {
#      66         [ +  + ]:       2038 :     if (fs::is_regular_file(wallet_path)) {
#      67                 :            :         // Special case for backwards compatibility: if wallet path points to an
#      68                 :            :         // existing file, treat it as the path to a BDB data file in a parent
#      69                 :            :         // directory that also contains BDB log files.
#      70                 :         53 :         return wallet_path;
#      71                 :       1985 :     } else {
#      72                 :            :         // Normal case: Interpret wallet path as a directory path containing
#      73                 :            :         // data and log files.
#      74                 :       1985 :         return wallet_path / "wallet.dat";
#      75                 :       1985 :     }
#      76                 :       2038 : }
#      77                 :            : 
#      78                 :            : fs::path SQLiteDataFile(const fs::path& path)
#      79                 :       1745 : {
#      80                 :       1745 :     return path / "wallet.dat";
#      81                 :       1745 : }
#      82                 :            : 
#      83                 :            : bool IsBDBFile(const fs::path& path)
#      84                 :       1470 : {
#      85         [ +  + ]:       1470 :     if (!fs::exists(path)) return false;
#      86                 :            : 
#      87                 :            :     // A Berkeley DB Btree file has at least 4K.
#      88                 :            :     // This check also prevents opening lock files.
#      89                 :        630 :     std::error_code ec;
#      90                 :        630 :     auto size = fs::file_size(path, ec);
#      91         [ -  + ]:        630 :     if (ec) LogPrintf("%s: %s %s\n", __func__, ec.message(), fs::PathToString(path));
#      92         [ +  + ]:        630 :     if (size < 4096) return false;
#      93                 :            : 
#      94                 :        606 :     std::ifstream file{path, std::ios::binary};
#      95         [ -  + ]:        606 :     if (!file.is_open()) return false;
#      96                 :            : 
#      97                 :        606 :     file.seekg(12, std::ios::beg); // Magic bytes start at offset 12
#      98                 :        606 :     uint32_t data = 0;
#      99                 :        606 :     file.read((char*) &data, sizeof(data)); // Read 4 bytes of file to compare against magic
#     100                 :            : 
#     101                 :            :     // Berkeley DB Btree magic bytes, from:
#     102                 :            :     //  https://github.com/file/file/blob/5824af38469ec1ca9ac3ffd251e7afe9dc11e227/magic/Magdir/database#L74-L75
#     103                 :            :     //  - big endian systems - 00 05 31 62
#     104                 :            :     //  - little endian systems - 62 31 05 00
#     105 [ +  + ][ -  + ]:        606 :     return data == 0x00053162 || data == 0x62310500;
#     106                 :        606 : }
#     107                 :            : 
#     108                 :            : bool IsSQLiteFile(const fs::path& path)
#     109                 :       1371 : {
#     110         [ +  + ]:       1371 :     if (!fs::exists(path)) return false;
#     111                 :            : 
#     112                 :            :     // A SQLite Database file is at least 512 bytes.
#     113                 :        505 :     std::error_code ec;
#     114                 :        505 :     auto size = fs::file_size(path, ec);
#     115         [ -  + ]:        505 :     if (ec) LogPrintf("%s: %s %s\n", __func__, ec.message(), fs::PathToString(path));
#     116         [ +  + ]:        505 :     if (size < 512) return false;
#     117                 :            : 
#     118                 :        503 :     std::ifstream file{path, std::ios::binary};
#     119         [ -  + ]:        503 :     if (!file.is_open()) return false;
#     120                 :            : 
#     121                 :            :     // Magic is at beginning and is 16 bytes long
#     122                 :        503 :     char magic[16];
#     123                 :        503 :     file.read(magic, 16);
#     124                 :            : 
#     125                 :            :     // Application id is at offset 68 and 4 bytes long
#     126                 :        503 :     file.seekg(68, std::ios::beg);
#     127                 :        503 :     char app_id[4];
#     128                 :        503 :     file.read(app_id, 4);
#     129                 :            : 
#     130                 :        503 :     file.close();
#     131                 :            : 
#     132                 :            :     // Check the magic, see https://sqlite.org/fileformat2.html
#     133                 :        503 :     std::string magic_str(magic, 16);
#     134         [ +  + ]:        503 :     if (magic_str != std::string("SQLite format 3", 16)) {
#     135                 :        303 :         return false;
#     136                 :        303 :     }
#     137                 :            : 
#     138                 :            :     // Check the application id matches our network magic
#     139                 :        200 :     return memcmp(Params().MessageStart(), app_id, 4) == 0;
#     140                 :        503 : }
#     141                 :            : 
#     142                 :            : void ReadDatabaseArgs(const ArgsManager& args, DatabaseOptions& options)
#     143                 :       1623 : {
#     144                 :            :     // Override current options with args values, if any were specified
#     145                 :       1623 :     options.use_unsafe_sync = args.GetBoolArg("-unsafesqlitesync", options.use_unsafe_sync);
#     146                 :       1623 :     options.use_shared_memory = !args.GetBoolArg("-privdb", !options.use_shared_memory);
#     147                 :       1623 :     options.max_log_mb = args.GetIntArg("-dblogsize", options.max_log_mb);
#     148                 :       1623 : }
#     149                 :            : 
#     150                 :            : } // namespace wallet

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a