LCOV - code coverage report
Current view: top level - src/common - config.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 94.3 % 122 115
Test Date: 2024-11-04 05:10:19 Functions: 100.0 % 6 6
Branches: 60.9 % 238 145

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2023 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <common/args.h>
       6                 :             : 
       7                 :             : #include <common/settings.h>
       8                 :             : #include <logging.h>
       9                 :             : #include <sync.h>
      10                 :             : #include <tinyformat.h>
      11                 :             : #include <univalue.h>
      12                 :             : #include <util/chaintype.h>
      13                 :             : #include <util/fs.h>
      14                 :             : #include <util/string.h>
      15                 :             : 
      16                 :             : #include <algorithm>
      17                 :             : #include <cassert>
      18                 :             : #include <cstdlib>
      19                 :             : #include <fstream>
      20                 :             : #include <iostream>
      21                 :             : #include <list>
      22                 :             : #include <map>
      23                 :             : #include <memory>
      24                 :             : #include <optional>
      25                 :             : #include <string>
      26                 :             : #include <string_view>
      27                 :             : #include <utility>
      28                 :             : #include <vector>
      29                 :             : 
      30                 :             : using util::TrimString;
      31                 :             : using util::TrimStringView;
      32                 :             : 
      33                 :       50392 : static bool GetConfigOptions(std::istream& stream, const std::string& filepath, std::string& error, std::vector<std::pair<std::string, std::string>>& options, std::list<SectionInfo>& sections)
      34                 :             : {
      35                 :       50392 :     std::string str, prefix;
      36                 :       50392 :     std::string::size_type pos;
      37                 :       50392 :     int linenr = 1;
      38   [ +  -  +  + ]:      267166 :     while (std::getline(stream, str)) {
      39                 :      216779 :         bool used_hash = false;
      40         [ +  + ]:      216779 :         if ((pos = str.find('#')) != std::string::npos) {
      41         [ +  - ]:          19 :             str = str.substr(0, pos);
      42                 :          19 :             used_hash = true;
      43                 :             :         }
      44   [ +  +  +  - ]:      216779 :         const static std::string pattern = " \t\r\n";
      45         [ +  - ]:      216779 :         str = TrimString(str, pattern);
      46         [ +  + ]:      216779 :         if (!str.empty()) {
      47   [ +  +  +  - ]:      216752 :             if (*str.begin() == '[' && *str.rbegin() == ']') {
      48         [ +  - ]:        1506 :                 const std::string section = str.substr(1, str.size() - 2);
      49   [ +  -  +  -  :        1506 :                 sections.emplace_back(SectionInfo{section, filepath, linenr});
                   +  - ]
      50         [ +  - ]:        1506 :                 prefix = section + '.';
      51         [ +  + ]:      216752 :             } else if (*str.begin() == '-') {
      52         [ +  - ]:           1 :                 error = strprintf("parse error on line %i: %s, options in configuration file must be specified without leading -", linenr, str);
      53                 :           1 :                 return false;
      54         [ +  + ]:      215245 :             } else if ((pos = str.find('=')) != std::string::npos) {
      55   [ +  -  +  -  :      215244 :                 std::string name = prefix + TrimString(std::string_view{str}.substr(0, pos), pattern);
                   +  - ]
      56   [ +  -  +  - ]:      215244 :                 std::string_view value = TrimStringView(std::string_view{str}.substr(pos + 1), pattern);
      57   [ +  +  -  + ]:      215244 :                 if (used_hash && name.find("rpcpassword") != std::string::npos) {
      58         [ +  - ]:           3 :                     error = strprintf("parse error on line %i, using # in rpcpassword can be ambiguous and should be avoided", linenr);
      59                 :           3 :                     return false;
      60                 :             :                 }
      61         [ +  - ]:      215241 :                 options.emplace_back(name, value);
      62   [ +  +  +  + ]:      215241 :                 if ((pos = name.rfind('.')) != std::string::npos && prefix.length() <= pos) {
      63   [ +  -  +  -  :      182788 :                     sections.emplace_back(SectionInfo{name.substr(0, pos), filepath, linenr});
                   +  - ]
      64                 :             :                 }
      65                 :      215244 :             } else {
      66         [ +  - ]:           1 :                 error = strprintf("parse error on line %i: %s", linenr, str);
      67   [ +  -  +  -  :           2 :                 if (str.size() >= 2 && str.substr(0, 2) == "no") {
             +  -  +  - ]
      68         [ +  - ]:           2 :                     error += strprintf(", if you intended to specify a negated option, use %s=1 instead", str);
      69                 :             :                 }
      70                 :           1 :                 return false;
      71                 :             :             }
      72                 :             :         }
      73                 :      216774 :         ++linenr;
      74                 :             :     }
      75                 :             :     return true;
      76                 :       50392 : }
      77                 :             : 
      78                 :      215235 : bool IsConfSupported(KeyInfo& key, std::string& error) {
      79         [ +  + ]:      215235 :     if (key.name == "conf") {
      80                 :           2 :         error = "conf cannot be set in the configuration file; use includeconf= if you want to include additional config files";
      81                 :           2 :         return false;
      82                 :             :     }
      83         [ +  + ]:      215233 :     if (key.name == "reindex") {
      84                 :             :         // reindex can be set in a config file but it is strongly discouraged as this will cause the node to reindex on
      85                 :             :         // every restart. Allow the config but throw a warning
      86                 :           1 :         LogPrintf("Warning: reindex=1 is set in the configuration file, which will significantly slow down startup. Consider removing or commenting out this option for better performance, unless there is currently a condition which makes rebuilding the indexes necessary\n");
      87                 :           1 :         return true;
      88                 :             :     }
      89                 :             :     return true;
      90                 :             : }
      91                 :             : 
      92                 :       50392 : bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& filepath, std::string& error, bool ignore_invalid_keys)
      93                 :             : {
      94                 :       50392 :     LOCK(cs_args);
      95                 :       50392 :     std::vector<std::pair<std::string, std::string>> options;
      96   [ +  -  +  + ]:       50392 :     if (!GetConfigOptions(stream, filepath, error, options, m_config_sections)) {
      97                 :             :         return false;
      98                 :             :     }
      99         [ +  + ]:      265620 :     for (const std::pair<std::string, std::string>& option : options) {
     100   [ +  -  +  - ]:      215235 :         KeyInfo key = InterpretKey(option.first);
     101   [ +  -  +  - ]:      215235 :         std::optional<unsigned int> flags = GetArgFlags('-' + key.name);
     102   [ +  -  +  + ]:      215235 :         if (!IsConfSupported(key, error)) return false;
     103         [ +  + ]:      215233 :         if (flags) {
     104         [ +  - ]:      206880 :             std::optional<common::SettingsValue> value = InterpretValue(key, &option.second, *flags, error);
     105         [ -  + ]:      206880 :             if (!value) {
     106                 :           0 :                 return false;
     107                 :             :             }
     108   [ +  -  +  -  :      206880 :             m_settings.ro_config[key.section][key.name].push_back(*value);
                   +  - ]
     109                 :      206880 :         } else {
     110         [ +  - ]:        8353 :             if (ignore_invalid_keys) {
     111         [ +  - ]:        8353 :                 LogPrintf("Ignoring unknown configuration value %s\n", option.first);
     112                 :             :             } else {
     113         [ #  # ]:           0 :                 error = strprintf("Invalid configuration value %s", option.first);
     114                 :           0 :                 return false;
     115                 :             :             }
     116                 :             :         }
     117                 :      215235 :     }
     118                 :             :     return true;
     119         [ +  - ]:      100784 : }
     120                 :             : 
     121                 :        1499 : bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
     122                 :             : {
     123                 :        1499 :     {
     124                 :        1499 :         LOCK(cs_args);
     125                 :        1499 :         m_settings.ro_config.clear();
     126                 :        1499 :         m_config_sections.clear();
     127   [ +  -  +  -  :        5996 :         m_config_path = AbsPathForConfigVal(*this, GetPathArg("-conf", BITCOIN_CONF_FILENAME), /*net_specific=*/false);
          +  -  +  -  +  
                      - ]
     128                 :           0 :     }
     129                 :             : 
     130                 :        1499 :     const auto conf_path{GetConfigFilePath()};
     131         [ +  - ]:        1499 :     std::ifstream stream{conf_path};
     132                 :             : 
     133                 :             :     // not ok to have a config file specified that cannot be opened
     134   [ +  -  +  -  :        2998 :     if (IsArgSet("-conf") && !stream.good()) {
          +  +  +  +  +  
                      + ]
     135   [ +  -  +  - ]:           2 :         error = strprintf("specified config file \"%s\" could not be opened.", fs::PathToString(conf_path));
     136                 :           1 :         return false;
     137                 :             :     }
     138                 :             :     // ok to not have a config file
     139         [ +  - ]:        1498 :     if (stream.good()) {
     140   [ +  -  +  -  :        2996 :         if (!ReadConfigStream(stream, fs::PathToString(conf_path), error, ignore_invalid_keys)) {
                   +  + ]
     141                 :             :             return false;
     142                 :             :         }
     143                 :             :         // `-includeconf` cannot be included in the command line arguments except
     144                 :             :         // as `-noincludeconf` (which indicates that no included conf file should be used).
     145                 :        1497 :         bool use_conf_file{true};
     146                 :        1497 :         {
     147         [ +  - ]:        1497 :             LOCK(cs_args);
     148   [ +  -  -  + ]:        1497 :             if (auto* includes = common::FindKey(m_settings.command_line_options, "includeconf")) {
     149                 :             :                 // ParseParameters() fails if a non-negated -includeconf is passed on the command-line
     150   [ #  #  #  # ]:           0 :                 assert(common::SettingsSpan(*includes).last_negated());
     151                 :             :                 use_conf_file = false;
     152                 :             :             }
     153                 :           0 :         }
     154         [ +  - ]:        1497 :         if (use_conf_file) {
     155         [ +  - ]:        1497 :             std::string chain_id = GetChainTypeString();
     156                 :        1497 :             std::vector<std::string> conf_file_names;
     157                 :             : 
     158                 :        7471 :             auto add_includes = [&](const std::string& network, size_t skip = 0) {
     159                 :        5974 :                 size_t num_values = 0;
     160                 :        5974 :                 LOCK(cs_args);
     161         [ +  + ]:        5974 :                 if (auto* section = common::FindKey(m_settings.ro_config, network)) {
     162   [ +  -  +  + ]:        5948 :                     if (auto* values = common::FindKey(*section, "includeconf")) {
     163   [ +  -  +  -  :          90 :                         for (size_t i = std::max(skip, common::SettingsSpan(*values).negated()); i < values->size(); ++i) {
                   +  + ]
     164   [ +  -  +  - ]:          26 :                             conf_file_names.push_back((*values)[i].get_str());
     165                 :             :                         }
     166                 :             :                         num_values = values->size();
     167                 :             :                     }
     168                 :             :                 }
     169         [ +  - ]:        5974 :                 return num_values;
     170                 :        5974 :             };
     171                 :             : 
     172                 :             :             // We haven't set m_network yet (that happens in SelectParams()), so manually check
     173                 :             :             // for network.includeconf args.
     174         [ +  - ]:        1497 :             const size_t chain_includes = add_includes(chain_id);
     175         [ +  - ]:        1497 :             const size_t default_includes = add_includes({});
     176                 :             : 
     177         [ +  + ]:        1515 :             for (const std::string& conf_file_name : conf_file_names) {
     178   [ +  -  +  - ]:          75 :                 std::ifstream conf_file_stream{AbsPathForConfigVal(*this, fs::PathFromString(conf_file_name), /*net_specific=*/false)};
     179         [ +  + ]:          25 :                 if (conf_file_stream.good()) {
     180   [ +  -  +  + ]:          24 :                     if (!ReadConfigStream(conf_file_stream, conf_file_name, error, ignore_invalid_keys)) {
     181                 :             :                         return false;
     182                 :             :                     }
     183         [ +  - ]:          18 :                     LogPrintf("Included configuration file %s\n", conf_file_name);
     184                 :             :                 } else {
     185         [ +  - ]:           1 :                     error = "Failed to include configuration file " + conf_file_name;
     186                 :           1 :                     return false;
     187                 :             :                 }
     188                 :          25 :             }
     189                 :             : 
     190                 :             :             // Warn about recursive -includeconf
     191                 :        1490 :             conf_file_names.clear();
     192         [ +  - ]:        1490 :             add_includes(chain_id, /* skip= */ chain_includes);
     193         [ +  - ]:        1490 :             add_includes({}, /* skip= */ default_includes);
     194         [ +  - ]:        1490 :             std::string chain_id_final = GetChainTypeString();
     195         [ -  + ]:        1490 :             if (chain_id_final != chain_id) {
     196                 :             :                 // Also warn about recursive includeconf for the chain that was specified in one of the includeconfs
     197         [ #  # ]:           0 :                 add_includes(chain_id_final);
     198                 :             :             }
     199         [ +  + ]:        1491 :             for (const std::string& conf_file_name : conf_file_names) {
     200         [ +  - ]:           1 :                 tfm::format(std::cerr, "warning: -includeconf cannot be used from included files; ignoring -includeconf=%s\n", conf_file_name);
     201                 :             :             }
     202                 :        1497 :         }
     203                 :             :     }
     204                 :             : 
     205                 :             :     // If datadir is changed in .conf file:
     206         [ +  - ]:        1490 :     ClearPathCache();
     207   [ +  -  +  + ]:        1490 :     if (!CheckDataDirOption(*this)) {
     208   [ +  -  +  -  :           1 :         error = strprintf("specified data directory \"%s\" does not exist.", GetArg("-datadir", ""));
             +  -  +  - ]
     209                 :           1 :         return false;
     210                 :             :     }
     211                 :             :     return true;
     212                 :        2998 : }
     213                 :             : 
     214                 :        9574 : fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific)
     215                 :             : {
     216         [ +  + ]:        9574 :     if (path.is_absolute()) {
     217                 :          81 :         return path;
     218                 :             :     }
     219   [ +  +  +  - ]:       18986 :     return fsbridge::AbsPathJoin(net_specific ? args.GetDataDirNet() : args.GetDataDirBase(), path);
     220                 :             : }
        

Generated by: LCOV version 2.0-1