比特币源码研读(8)-main函数(7)

in #btc7 years ago

2)读取配置文件:读取配置文件,如果配置文件出错,则弹出错误提示框,并且程序退出

try

{

 gArgs.ReadConfigFile(gArgs.GetArg("-conf",BITCOIN_CONF_FILENAME));

}
catch (const std::exception&e)

{

 fprintf(stderr,"Error reading configuration file: %s\n",e.what());

 returnfalse;

}
该函数流程图如下:

1.png

分析1:BITCOIN_CONF_FILENAME,宏定义在util.cpp文件中,定义如下:

const char * const

BITCOIN_CONF_FILENAME = "bitcoin.conf";
bitcoin.conf是客户端的配置文件,位置是bitcoin/contrib/init,该文件定义了很多env,如下

env BITCOIND_BIN="/usr/bin/bitcoind"

env BITCOIND_USER="bitcoin"

env BITCOIND_GROUP="bitcoin"

env BITCOIND_PIDDIR="/var/run/bitcoind"

envBITCOIND_PIDFILE="/var/run/bitcoind/bitcoind.pid"

envBITCOIND_CONFIGFILE="/etc/bitcoin/bitcoin.conf"

env BITCOIND_DATADIR="/var/lib/bitcoind"

分析2:GetArg函数

2.png

由函数可以看出,如果找到conf文件,则用这个conf文件,如果没有找到,则用默认文件。

分析3:ReadConfigFile

voidArgsManager::ReadConfigFile(const std::string& confPath)

{

 fs::ifstream streamConfig(GetConfigFile(confPath));

 if (!streamConfig.good())

return; // No bitcoin.conf file is OK

{

LOCK(cs_args);

std::set setOptions;

setOptions.insert("*");

for(boost::program_options::detail::config_file_iterator it(streamConfig,setOptions), end; it != end; ++it)

{

// Don't overwrite existingsettings so command line settings override bitcoin.conf

std::string strKey =std::string("-") + it->string_key;

std::string strValue =it->value[0];

InterpretNegativeSetting(strKey,strValue);

if (mapArgs.count(strKey) == 0)

mapArgs[strKey] = strValue;

mapMultiArgs[strKey].push_back(strValue);

}
}
// If datadir is changed in .conf file:

ClearDatadirCache();

}
fs::ifstream把硬盘上的文件读取到内存中,定义一个文件流streamConfig

ifstream的详细解释见:http://www.cplusplus.com/reference/fstream/ifstream/

streamConfig读取的文件是GetConfigFile(confPath),我们再看看这个函数:

fs::pathGetConfigFile(const std::string& confPath)

{

fs::path pathConfigFile(confPath);

if (!pathConfigFile.is_complete())

pathConfigFile = GetDataDir(false) /pathConfigFile;

return pathConfigFile;

}
读取confPath,也就是BITCOIN_CONF_FILENAME文件,判断文件是否完整,如果不完整则读取默认的配置文件,GetDataDir函数见 比特币源码研读(7)-main函数(6)

紧接着就开始判断这个文件是否为conf文件,如果不是则直接退出

if(!streamConfig.good())

return; // No bitcoin.conf file is OK
如果判断文件正常,开始执行for循环,读取配置文件的信息,写到mapArgs和mapMultiArgs中。

最后,清空文件路径:函数最后是为防止配置文件中设置了数据目录参数datadir,通过ClearDatadirCache()函数将数据文件路径参数设置为空目录,这样下次进入GetDataDir()时,我们将会根据新的datadir创建数据目录。

// If datadir is changed in .conffile:

ClearDatadirCache();
////////////////////////读取配置文件函数分析完毕////////////////////

区块链研习社比特币源码研读班 electroman

以下是广告:

我们区块链研习社已创建“区块链研习社币圈交流”小密圈”,在小密圈中,我们将带领大家一起学习区块链的原理与投资,还将提供区块链基本原理解答、交易所注册与交易操作、ICO交易与操作、投资分析、风险分析等内容。

区块链研习社币圈交流.png

Sort:  

学习了,感谢!

Congratulations @electroman! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of posts published
Award for the number of upvotes received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Congratulations @electroman! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You got your First payout

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Congratulations @electroman! You have received a personal award!

1 Year on Steemit
Click on the badge to view your Board of Honor.

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @electroman! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!