LCOV - code coverage report
Current view: top level - src/wallet - db.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 8.0 % 75 6
Test Date: 2024-07-04 04:02:30 Functions: 37.5 % 8 3
Branches: 0.4 % 248 1

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

Generated by: LCOV version 2.0-1