From b5aa1855372f2620b5da3ed9c2719df47751f21b Mon Sep 17 00:00:00 2001 From: John McQuah Date: Thu, 8 Jun 2023 11:42:58 -0400 Subject: [PATCH] cache the pkgmk.conf settings to avoid repeated disk reads (FS#595) --- src/configuration.cpp | 48 +++++++++++++++++++++++++++++- src/configuration.h | 7 +++++ src/installtransaction.cpp | 61 ++------------------------------------ src/installtransaction.h | 7 ----- src/prtget.cpp | 12 ++++---- 5 files changed, 64 insertions(+), 71 deletions(-) diff --git a/src/configuration.cpp b/src/configuration.cpp index c4a7b12..a00b25e 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -36,7 +36,8 @@ Configuration::Configuration( const std::string& configFile, m_useRegex( false ), m_followSoftdeps( false ), m_makeCommand( "" ), m_addCommand( "" ), - m_removeCommand( "" ), m_runscriptCommand( "" ) + m_removeCommand( "" ), m_runscriptCommand( "" ), + m_compressionMode( "" ), m_packageDir( "" ) { } @@ -89,6 +90,25 @@ string Configuration::logFilePattern() const return m_logFilePattern; } +string Configuration::packageDir() const +{ + string value = ""; + char line[256]; + string cmd = "eval " + m_packageDir + " && echo \"$PKGMK_PACKAGE_DIR\""; + FILE* p = popen(cmd.c_str(), "r"); + if (p) { + fgets(line, 256, p); + value = StringHelper::stripWhiteSpace(line); + pclose(p); + } + + return value; +} + +string Configuration::compressionMode() const +{ + return ( m_compressionMode == "" ) ? "gz" : m_compressionMode; +} const list< pair >& Configuration::rootList() const { @@ -215,6 +235,32 @@ void Configuration::parseLine(const string& line, bool prepend) } } +bool Configuration::parsePkgmkConf(int readOrder) +{ + string fileName = (readOrder == 1) ? "/etc/pkgmk.conf" : "/usr/bin/pkgmk"; + FILE* fp = fopen(fileName.c_str(), "r"); + if (!fp) + return false; + + string s; + char line[256]; + while (fgets(line, 256, fp)) { + s = StringHelper::stripWhiteSpace(getValueBefore(line,'#')); + if ( StringHelper::startsWith(s, "PKGMK_COMPRESSION_MODE=") && + (readOrder==1 || m_compressionMode == "") ) { + m_compressionMode = s.substr(23); + StringHelper::replaceAll(m_compressionMode,"\"",""); + StringHelper::replaceAll(m_compressionMode,"'",""); + } else if ( StringHelper::startsWith(s, "PKGMK_PACKAGE_DIR=") && + (readOrder==1 || m_packageDir == "" ) ) { + m_packageDir = s; + } + } + fclose(fp); + + return ( m_compressionMode != "" && m_packageDir != "" ); +} + bool Configuration::runScripts() const { return m_runScripts; diff --git a/src/configuration.h b/src/configuration.h index 3b69ff6..6325432 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -45,6 +45,8 @@ public: bool useRegex() const; bool followSoftdeps() const; + bool parsePkgmkConf(int readOrder); + void addConfig(const std::string& line, bool configSet, bool configPrepend); @@ -54,6 +56,9 @@ public: std::string removeCommand() const; std::string runscriptCommand() const; + std::string compressionMode() const; + std::string packageDir() const; + private: std::string m_configFile; const ArgParser* m_parser; @@ -80,6 +85,8 @@ private: std::string m_removeCommand; std::string m_runscriptCommand; + std::string m_compressionMode; + std::string m_packageDir; void parseLine(const std::string& line, bool prepend=false); }; diff --git a/src/installtransaction.cpp b/src/installtransaction.cpp index 400b3d8..0054f2a 100644 --- a/src/installtransaction.cpp +++ b/src/installtransaction.cpp @@ -308,9 +308,9 @@ const { } // -- build - string pkgdest = getPkgmkPackageDir(); - const string builtPkg = package->name() + "#" + package->version() + "-" + - package->release() + ".pkg.tar." + getPkgmkCompressionMode(); + string pkgdest = m_config->packageDir(); + string builtPkg = package->name() + "#" + package->version() + "-" + + package->release() + ".pkg.tar." + m_config->compressionMode(); string builtPkgPath = ( pkgdest != "" ) ? pkgdest + "/" + builtPkg : portdir + "/" + builtPkg ; string cmd = PKGMK_DEFAULT_COMMAND; @@ -657,62 +657,7 @@ bool InstallTransaction::calcDependencies( ) return true; } - -string InstallTransaction::getPkgmkSetting(const string& setting) -{ - string value = ""; - value = getPkgmkSettingFromFile(setting, "/etc/pkgmk.conf"); - if (value.size() == 0) { - value = getPkgmkSettingFromFile(setting, "/usr/bin/pkgmk"); - } - - return value; -} - -string InstallTransaction::getPkgmkSettingFromFile(const string& setting, const string& fileName) -{ - FILE* fp = fopen(fileName.c_str(), "r"); - if (!fp) - return ""; - - string candidate; - string s; - char line[256]; - while (fgets(line, 256, fp)) { - s = StringHelper::stripWhiteSpace(line); - if (StringHelper::startsWith(s, setting + "=")) { - candidate = s; - } - } - fclose(fp); - - string value = ""; - if (candidate.length() > 0) { - string cmd = "eval " + candidate + " && echo $" + setting; - FILE* p = popen(cmd.c_str(), "r"); - if (p) { - fgets(line, 256, p); - value = StringHelper::stripWhiteSpace(line); - pclose(p); - } - } - - return value; -} - const list& InstallTransaction::ignoredPackages() const { return m_ignoredPackages; } - -string InstallTransaction::getPkgmkPackageDir() -{ - return getPkgmkSetting("PKGMK_PACKAGE_DIR"); -} - -string InstallTransaction::getPkgmkCompressionMode() -{ - string value = getPkgmkSetting("PKGMK_COMPRESSION_MODE"); - - return value.size() ? value : "gz"; -} diff --git a/src/installtransaction.h b/src/installtransaction.h index cca802d..54e7250 100644 --- a/src/installtransaction.h +++ b/src/installtransaction.h @@ -91,9 +91,6 @@ public: const list< pair >& missing() const; const list< pair >& installError() const; - static string getPkgmkPackageDir(); - static string getPkgmkCompressionMode(); - private: bool calculateDependencies(); void checkDependencies( bool greedy, const Package* package, int depends=-1 ); @@ -104,10 +101,6 @@ private: InstallInfo& info ) const; bool isRequested(const string pname); - static string getPkgmkSetting(const string& setting); - static string getPkgmkSettingFromFile(const string& setting, - const string& fileName); - PkgDB* m_pkgDB; DepResolver m_resolver; const Repository* m_repo; diff --git a/src/prtget.cpp b/src/prtget.cpp index 4354312..9d633f2 100644 --- a/src/prtget.cpp +++ b/src/prtget.cpp @@ -609,7 +609,7 @@ void PrtGet::executeTransaction( InstallTransaction& transaction ) { break; case InstallTransaction::PKGDEST_ERROR: cout << m_appName << ": error changing to PKGDEST directory " - << transaction.getPkgmkPackageDir() << endl; + << m_config->packageDir() << endl; failed = true; break; case InstallTransaction::PKGADD_FAILURE: @@ -743,6 +743,10 @@ void PrtGet::readConfig() { } } + if (! m_config->parsePkgmkConf(1)) { + m_config->parsePkgmkConf(0); + } + const list< pair >& configData = m_parser->configData(); list< pair >::const_iterator it = @@ -1925,14 +1929,12 @@ void PrtGet::dumpConfig() { cout.setf( ios::left, ios::adjustfield ); cout.width( 20 ); cout.fill( ' ' ); - cout << " Package dir: " << InstallTransaction::getPkgmkPackageDir() - << endl; + cout << " Package dir: " << m_config->packageDir() << endl; cout.setf( ios::left, ios::adjustfield ); cout.width( 20 ); cout.fill( ' ' ); - cout << " Compression mode: " - << InstallTransaction::getPkgmkCompressionMode() << endl; + cout << " Compression mode: " << m_config->compressionMode() << endl; cout << endl; list< pair >::const_iterator it =