This is an automated email from the ASF dual-hosted git repository. zwoop pushed a commit to branch 9.0.x in repository https://gitbox.apache.org/repos/asf/trafficserver.git
commit 73e74ff07ad63f412bd09ff6900a34c37023b9c7 Author: Damian Meden <damian.me...@oath.com> AuthorDate: Thu Feb 27 15:20:08 2020 +0000 Add more flexible error handling when open a config file. (cherry picked from commit 853b010e31c6d9847e394ea2f12942ad85034aee) --- iocore/cache/CacheHosting.cc | 47 +++++++++++++++++++++++------------------ iocore/dns/SplitDNS.cc | 3 +-- iocore/net/SSLUtils.cc | 22 +++++++++++-------- proxy/ControlMatcher.cc | 24 +++++++++++---------- proxy/http/remap/RemapConfig.cc | 18 +++++++++++----- 5 files changed, 67 insertions(+), 47 deletions(-) diff --git a/iocore/cache/CacheHosting.cc b/iocore/cache/CacheHosting.cc index 24930c7..31c9bc9 100644 --- a/iocore/cache/CacheHosting.cc +++ b/iocore/cache/CacheHosting.cc @@ -27,6 +27,7 @@ #include "tscore/Tokenizer.h" #include "tscore/Regression.h" #include "tscore/Filenames.h" +#include "tscore/ts_file.h" extern int gndisks; @@ -396,20 +397,22 @@ CacheHostTable::BuildTableFromString(const char *config_file_path, char *file_bu int CacheHostTable::BuildTable(const char *config_file_path) { - char *file_buf; - int ret; + std::error_code ec; + std::string content{ts::file::load(ts::file::path{config_file_path}, ec)}; - file_buf = readIntoBuffer(config_file_path, matcher_name, nullptr); - - if (file_buf == nullptr) { - Warning("Cannot read the config file: %s", config_file_path); - gen_host_rec.Init(type); - return 0; + if (ec) { + switch (ec.value()) { + case ENOENT: + Warning("Cannot open the config file: %s - %s", config_file_path, strerror(ec.value())); + break; + default: + Error("%s failed to load: %s", config_file_path, strerror(ec.value())); + gen_host_rec.Init(type); + return 0; + } } - ret = BuildTableFromString(config_file_path, file_buf); - ats_free(file_buf); - return ret; + return BuildTableFromString(config_file_path, content.data()); } int @@ -592,23 +595,27 @@ void ConfigVolumes::read_config_file() { ats_scoped_str config_path; - char *file_buf; config_path = RecConfigReadConfigPath("proxy.config.cache.volume_filename"); ink_release_assert(config_path); Note("%s loading ...", ts::filename::VOLUME); - file_buf = readIntoBuffer(config_path, "[CacheVolition]", nullptr); - if (file_buf == nullptr) { - Error("%s failed to load", ts::filename::VOLUME); - Warning("Cannot read the config file: %s", (const char *)config_path); - return; - } + std::error_code ec; + std::string content{ts::file::load(ts::file::path{config_path}, ec)}; - BuildListFromString(config_path, file_buf); - ats_free(file_buf); + if (ec) { + switch (ec.value()) { + case ENOENT: + Warning("Cannot open the config file: %s - %s", (const char *)config_path, strerror(ec.value())); + break; + default: + Error("%s failed to load: %s", ts::filename::VOLUME, strerror(ec.value())); + return; + } + } + BuildListFromString(config_path, content.data()); Note("volume.config finished loading"); return; diff --git a/iocore/dns/SplitDNS.cc b/iocore/dns/SplitDNS.cc index 2ad8c24..3606adc 100644 --- a/iocore/dns/SplitDNS.cc +++ b/iocore/dns/SplitDNS.cc @@ -139,8 +139,7 @@ SplitDNSConfig::reconfigure() params->m_DNSSrvrTable = new DNS_table("proxy.config.dns.splitdns.filename", modulePrefix, &sdns_dest_tags); if (nullptr == params->m_DNSSrvrTable || (0 == params->m_DNSSrvrTable->getEntryCount())) { - Error("%s failed to load", ts::filename::SPLITDNS); - Warning("No NAMEDs provided! Disabling SplitDNS"); + Warning("Failed to load %s - No NAMEDs provided! Disabling SplitDNS", ts::filename::SPLITDNS); gsplit_dns_enabled = 0; delete params; return; diff --git a/iocore/net/SSLUtils.cc b/iocore/net/SSLUtils.cc index 779f030..99433fc 100644 --- a/iocore/net/SSLUtils.cc +++ b/iocore/net/SSLUtils.cc @@ -29,6 +29,7 @@ #include "tscore/ink_mutex.h" #include "tscore/Filenames.h" #include "records/I_RecHttp.h" +#include "tscore/ts_file.h" #include "P_Net.h" #include "InkAPIInternal.h" @@ -1593,7 +1594,6 @@ SSLMultiCertConfigLoader::load(SSLCertLookup *lookup) char *tok_state = nullptr; char *line = nullptr; - ats_scoped_str file_buf; unsigned line_num = 0; matcher_line line_info; @@ -1601,13 +1601,17 @@ SSLMultiCertConfigLoader::load(SSLCertLookup *lookup) Note("%s loading ...", ts::filename::SSL_MULTICERT); - if (params->configFilePath) { - file_buf = readIntoBuffer(params->configFilePath, __func__, nullptr); - } - - if (!file_buf) { - Error("failed to read SSL certificate configuration from %s", params->configFilePath); - return false; + std::error_code ec; + std::string content{ts::file::load(ts::file::path{params->configFilePath}, ec)}; + if (ec) { + switch (ec.value()) { + case ENOENT: + Warning("Cannot open SSL certificate configuration from %s - %s", params->configFilePath, strerror(ec.value())); + break; + default: + Error("Failed to read SSL certificate configuration from %s - %s", params->configFilePath, strerror(ec.value())); + return false; + } } // Optionally elevate/allow file access to read root-only @@ -1616,7 +1620,7 @@ SSLMultiCertConfigLoader::load(SSLCertLookup *lookup) REC_ReadConfigInteger(elevate_setting, "proxy.config.ssl.cert.load_elevated"); ElevateAccess elevate_access(elevate_setting ? ElevateAccess::FILE_PRIVILEGE : 0); - line = tokLine(file_buf, &tok_state); + line = tokLine(content.data(), &tok_state); while (line != nullptr) { line_num++; diff --git a/proxy/ControlMatcher.cc b/proxy/ControlMatcher.cc index 7dff474..9695457 100644 --- a/proxy/ControlMatcher.cc +++ b/proxy/ControlMatcher.cc @@ -33,6 +33,7 @@ #include "tscore/ink_config.h" #include "tscore/MatcherUtils.h" #include "tscore/Tokenizer.h" +#include "tscore/ts_file.h" #include "ProxyConfig.h" #include "ControlMatcher.h" #include "CacheControl.h" @@ -935,19 +936,20 @@ template <class Data, class MatchResult> int ControlMatcher<Data, MatchResult>::BuildTable() { - // File I/O Locals - char *file_buf; - int ret; - - file_buf = readIntoBuffer(config_file_path, matcher_name, nullptr); - - if (file_buf == nullptr) { - return 1; + std::error_code ec; + std::string content{ts::file::load(ts::file::path{config_file_path}, ec)}; + if (ec) { + switch (ec.value()) { + case ENOENT: + Warning("ControlMatcher - Cannot open config file: %s - %s", config_file_path, strerror(ec.value())); + break; + default: + Error("ControlMatcher - %s failed to load: %s", config_file_path, strerror(ec.value())); + return 1; + } } - ret = BuildTableFromString(file_buf); - ats_free(file_buf); - return ret; + return BuildTableFromString(content.data()); } /**************************************************************** diff --git a/proxy/http/remap/RemapConfig.cc b/proxy/http/remap/RemapConfig.cc index fa0acd5..5844dca 100644 --- a/proxy/http/remap/RemapConfig.cc +++ b/proxy/http/remap/RemapConfig.cc @@ -31,6 +31,7 @@ #include "tscore/ink_cap.h" #include "tscore/ink_file.h" #include "tscore/Tokenizer.h" +#include "tscore/ts_file.h" #include "IPAllow.h" #include "PluginFactory.h" @@ -941,15 +942,22 @@ remap_parse_config_bti(const char *path, BUILD_TABLE_INFO *bti) bool is_cur_mapping_regex; const char *type_id_str; - ats_scoped_str file_buf(readIntoBuffer(path, modulePrefix, nullptr)); - if (!file_buf) { - Warning("can't load remapping configuration file %s", path); - return false; + std::error_code ec; + std::string content{ts::file::load(ts::file::path{path}, ec)}; + if (ec) { + switch (ec.value()) { + case ENOENT: + Warning("Can't open remapping configuration file %s - %s", path, strerror(ec.value())); + break; + default: + Error("Failed load remapping configuration file %s - %s", path, strerror(ec.value())); + return false; + } } Debug("url_rewrite", "[BuildTable] UrlRewrite::BuildTable()"); - for (cur_line = tokLine(file_buf, &tok_state, '\\'); cur_line != nullptr;) { + for (cur_line = tokLine(content.data(), &tok_state, '\\'); cur_line != nullptr;) { reg_map = nullptr; new_mapping = nullptr; errStrBuf[0] = 0;