This is an automated email from the ASF dual-hosted git repository. amc pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push: new 6065340f88 libswoc: Replace ts::file with swoc::file. (#9581) 6065340f88 is described below commit 6065340f880a0b00cefdc3c221249e945aeff674 Author: Alan M. Carroll <a...@apache.org> AuthorDate: Fri Apr 14 11:47:05 2023 -0500 libswoc: Replace ts::file with swoc::file. (#9581) --- include/tscore/ts_file.h | 340 -------------------- iocore/cache/CacheHosting.cc | 7 +- iocore/cache/test/main.cc | 7 +- iocore/hostdb/HostDB.cc | 20 +- iocore/hostdb/HostFile.cc | 8 +- iocore/hostdb/HostFile.h | 5 +- iocore/hostdb/Makefile.am | 2 +- iocore/hostdb/P_HostDBProcessor.h | 2 +- iocore/hostdb/test_HostFile.cc | 12 +- iocore/io_uring/unit_tests/test_diskIO.cc | 15 +- iocore/net/SSLSecret.cc | 5 +- iocore/net/SSLUtils.cc | 4 +- mgmt/rpc/config/JsonRPCConfig.cc | 5 +- mgmt/rpc/server/unit_tests/test_rpcserver.cc | 7 +- plugins/experimental/tls_bridge/tls_bridge.cc | 18 +- plugins/experimental/traffic_dump/session_data.cc | 14 +- plugins/experimental/traffic_dump/session_data.h | 7 +- plugins/experimental/traffic_dump/traffic_dump.cc | 6 +- proxy/ControlMatcher.cc | 4 +- proxy/http/remap/PluginDso.cc | 18 +- proxy/http/remap/PluginDso.h | 14 +- proxy/http/remap/PluginFactory.cc | 6 +- proxy/http/remap/RemapConfig.cc | 8 +- proxy/http/remap/unit-tests/test_PluginDso.cc | 34 +- proxy/http/remap/unit-tests/test_PluginFactory.cc | 14 +- proxy/http/remap/unit-tests/test_RemapPlugin.cc | 2 +- proxy/logging/RolledLogDeleter.cc | 7 +- proxy/logging/unit-tests/test_RolledLogDeleter.cc | 8 +- src/records/RecCore.cc | 7 +- src/traffic_cache_tool/CacheDefs.h | 14 +- src/traffic_cache_tool/CacheScan.h | 2 +- src/traffic_cache_tool/CacheTool.cc | 66 ++-- src/traffic_cache_tool/Makefile.inc | 1 - src/traffic_server/traffic_server.cc | 5 +- src/tscore/CMakeLists.txt | 2 - src/tscore/Makefile.am | 2 - src/tscore/ts_file.cc | 344 --------------------- src/tscore/unit_tests/test_ts_file.cc | 278 ----------------- .../jsonrpc/plugins/jsonrpc_plugin_handler_test.cc | 9 +- 39 files changed, 194 insertions(+), 1135 deletions(-) diff --git a/include/tscore/ts_file.h b/include/tscore/ts_file.h deleted file mode 100644 index 8a9eff2bef..0000000000 --- a/include/tscore/ts_file.h +++ /dev/null @@ -1,340 +0,0 @@ -/** @file - - Simple path and file utilities. - - @section license License - - Licensed to the Apache Software Foundation (ASF) under one - or more contributor license agreements. See the NOTICE file - distributed with this work for additional information - regarding copyright ownership. The ASF licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - */ - -#pragma once - -#include <string> -#include <string_view> -#include <array> -#include <system_error> -#include <sys/stat.h> -#include "tscore/ink_memory.h" -#include "tscpp/util/TextView.h" -#include "tscore/BufferWriter.h" - -namespace ts -{ -namespace file -{ - /** Utility class for file system paths. - */ - class path - { - using self_type = path; - - public: - using value_type = char; - using string_type = std::string; - static constexpr char preferred_separator = value_type{'/'}; - - /// Default construct empty path. - path() = default; - - /// Copy constructor - copies the path. - path(const self_type &that) = default; - - /// Move constructor. - path(self_type &&that) = default; - - /// Construct from a null terminated string. - explicit path(const char *src); - - /// Construct from a string view. - path(std::string_view src); - // template < typename ... Args > explicit path(std::string_view base, Args... rest); - - /// Move from an existing string - path(std::string &&that); - - /// Replace the path with a copy of @a that. - self_type &operator=(const self_type &that) = default; - - /// Replace the path with the contents of @a that. - self_type &operator=(self_type &&that) = default; - - /// Assign @a p as the path. - self_type &operator=(std::string_view p); - - /** Append or replace path with @a that. - * - * If @a that is absolute, it replaces @a this. Otherwise @a that is appended with exactly one - * separator. - * - * @param that Filesystem path. - * @return @a this - */ - self_type &operator/=(const self_type &that); - self_type &operator/=(std::string_view that); - - /// Check if the path is empty. - bool empty() const; - - /// Check if the path is absolute. - bool is_absolute() const; - - /// Check if the path is not absolute. - bool is_relative() const; - - /// Access the path explicitly. - char const *c_str() const; - - /// Get a view of the path. - std::string_view view() const; - - /// Get a copy of the path. - std::string string() const; - - /// Get relative path - self_type relative_path(); - - /// Get parent path - path parent_path(); - - protected: - std::string _path; ///< File path. - }; - - /// Information about a file. - class file_status - { - using self_type = file_status; - - public: - protected: - struct ::stat _stat; ///< File information. - - friend self_type status(const path &, std::error_code &) noexcept; - - friend int file_type(const self_type &); - friend time_t modification_time(const file_status &fs); - friend uintmax_t file_size(const self_type &); - friend bool is_regular_file(const file_status &); - friend bool is_dir(const file_status &); - friend bool is_char_device(const file_status &); - friend bool is_block_device(const file_status &); - }; - - /** Get the status of the file at @a p. - * - * @param p Path to file. - * @param ec Error code return. - * @return Status of the file. - */ - file_status status(const path &p, std::error_code &ec) noexcept; - - // Related free functions. - // These are separate because they are not part of std::filesystem::path. - - /// Return the file type value. - int file_type(const file_status &fs); - - /// Return modification time - time_t modification_time(const file_status &fs); - - /// Check if the path is to a regular file. - bool is_regular_file(const file_status &fs); - - /// Check if the path is to a directory. - bool is_dir(const file_status &p); - - /// Check if the path is to a character device. - bool is_char_device(const file_status &fs); - - /// Check if the path is to a block device. - bool is_block_device(const file_status &fs); - - /// Size of the file or block device. - uintmax_t file_size(const file_status &fs); - - /// Check if file is readable. - bool is_readable(const path &s); - - // Get directory location suitable for temporary files - path temp_directory_path(); - - // Returns current path. - path current_path(); - - // Returns return the canonicalized absolute pathname - path canonical(const path &p, std::error_code &ec); - - // Return the filename derived from path p. - // - // This is made to match the std::filesystem::path::filename behavior: - // https://en.cppreference.com/w/cpp/filesystem/path/filename - // - // Examples: - // given "/foo/bar.txt", this returns "bar.txt" - // given "/foo/bar", this returns "bar" - // given "/foo/bar/", this returns "" - // given "/", this returns "" - path filename(const path &p); - - // Checks if the file/directory exists - bool exists(const path &p); - - // Create directories - bool create_directories(const path &p, std::error_code &ec, mode_t mode = 0775) noexcept; - - // Copy files ("from" cannot be directory). @todo make it more generic - bool copy(const path &from, const path &to, std::error_code &ec); - - // Removes files and directories recursively - bool remove(const path &path, std::error_code &ec); - - /** Load the file at @a p into a @c std::string - * - * @param p Path to file - * @return The contents of the file. - */ - std::string load(const path &p, std::error_code &ec); - /* ------------------------------------------------------------------- */ - - inline path::path(char const *src) : _path(src) {} - - inline path::path(std::string_view base) : _path(base) {} - - inline path::path(std::string &&that) : _path(std::move(that)) {} - - inline path & - path::operator=(std::string_view p) - { - _path.assign(p); - return *this; - } - - inline char const * - path::c_str() const - { - return _path.c_str(); - } - - inline std::string_view - path::view() const - { - return _path; - } - - inline std::string - path::string() const - { - return _path; - } - - inline bool - path::empty() const - { - return _path.empty(); - } - - inline bool - path::is_absolute() const - { - return !_path.empty() && preferred_separator == _path[0]; - } - - inline bool - path::is_relative() const - { - return !this->is_absolute(); - } - - inline path & - path::operator/=(const self_type &that) - { - return *this /= std::string_view(that._path); - } - - inline path - path::relative_path() - { - return (this->is_absolute()) ? path(_path.substr(sizeof(preferred_separator))) : *this; - } - - inline path - path::parent_path() - { - if (this->is_absolute() && _path.substr(sizeof(preferred_separator)).empty()) { - // No relative path - return *this; - } - - const size_t last_slash_idx = _path.find_last_of(preferred_separator); - if (std::string::npos != last_slash_idx) { - return path(_path.substr(0, last_slash_idx)); - } else { - return path(); - } - } - - /** Combine two strings as file paths. - - @return A @c path with the combined path. - */ - inline path - operator/(const path &lhs, const path &rhs) - { - return path(lhs) /= rhs; - } - - inline path - operator/(path &&lhs, const path &rhs) - { - return path(std::move(lhs)) /= rhs; - } - - inline path - operator/(const path &lhs, std::string_view rhs) - { - return path(lhs) /= rhs; - } - - inline path - operator/(path &&lhs, std::string_view rhs) - { - return path(std::move(lhs)) /= rhs; - } - - inline bool - operator==(const path &lhs, const path &rhs) - { - return lhs.string() == rhs.string(); - } - - inline bool - operator!=(const path &lhs, const path &rhs) - { - return lhs.string() != rhs.string(); - } - - /* ------------------------------------------------------------------- */ -} // namespace file - -inline BufferWriter & -bwformat(BufferWriter &w, BWFSpec const &spec, file::path const &path) -{ - return bwformat(w, spec, path.string()); -} - -} // namespace ts -/* ------------------------------------------------------------------- */ diff --git a/iocore/cache/CacheHosting.cc b/iocore/cache/CacheHosting.cc index b74a8d51d1..e5683cc661 100644 --- a/iocore/cache/CacheHosting.cc +++ b/iocore/cache/CacheHosting.cc @@ -21,13 +21,14 @@ limitations under the License. */ +#include "swoc/swoc_file.h" + #include "P_Cache.h" #include "tscore/I_Layout.h" #include "tscore/HostLookup.h" #include "tscore/Tokenizer.h" #include "tscore/Regression.h" #include "tscore/Filenames.h" -#include "tscore/ts_file.h" extern int gndisks; @@ -393,7 +394,7 @@ int CacheHostTable::BuildTable(const char *config_file_path) { std::error_code ec; - std::string content{ts::file::load(ts::file::path{config_file_path}, ec)}; + std::string content{swoc::file::load(swoc::file::path{config_file_path}, ec)}; if (ec) { switch (ec.value()) { @@ -593,7 +594,7 @@ ConfigVolumes::read_config_file() Note("%s loading ...", ts::filename::VOLUME); std::error_code ec; - std::string content{ts::file::load(ts::file::path{config_path}, ec)}; + std::string content{swoc::file::load(swoc::file::path{config_path}, ec)}; if (ec) { switch (ec.value()) { diff --git a/iocore/cache/test/main.cc b/iocore/cache/test/main.cc index 55ef9f90ca..e4910e114b 100644 --- a/iocore/cache/test/main.cc +++ b/iocore/cache/test/main.cc @@ -23,10 +23,11 @@ #define CATCH_CONFIG_MAIN #include "main.h" -#include "tscore/ts_file.h" #include <unistd.h> +#include "swoc/swoc_file.h" + #define THREADS 1 #define DIAGS_LOG_FILE "diags.log" @@ -41,8 +42,8 @@ temp_prefix() tmpdir = "/tmp"; } snprintf(buffer, sizeof(buffer), "%s/cachetest.XXXXXX", tmpdir); - auto prefix = ts::file::path(mkdtemp(buffer)); - bool result = ts::file::create_directories(prefix / "var" / "trafficserver", err, 0755); + auto prefix = swoc::file::path(mkdtemp(buffer)); + bool result = swoc::file::create_directories(prefix / "var" / "trafficserver", err, 0755); if (!result) { Debug("cache test", "Failed to create directories for test: %s(%s)", prefix.c_str(), err.message().c_str()); } diff --git a/iocore/hostdb/HostDB.cc b/iocore/hostdb/HostDB.cc index 0bdef44773..43462d041f 100644 --- a/iocore/hostdb/HostDB.cc +++ b/iocore/hostdb/HostDB.cc @@ -21,12 +21,13 @@ limitations under the License. */ +#include "swoc/swoc_file.h" + #include "Main.h" #include "P_HostDB.h" #include "P_RefCountCacheSerializer.h" #include "tscore/I_Layout.h" #include "Show.h" -#include "tscore/ts_file.h" #include "tscore/ink_apidefs.h" #include "tscore/bwf_std_format.h" #include "MgmtDefs.h" // MgmtInt, MgmtFloat, etc @@ -70,7 +71,7 @@ static ts_time hostdb_last_timestamp{TS_TIME_ZERO}; static ts_time hostdb_hostfile_update_timestamp{TS_TIME_ZERO}; static char hostdb_filename[PATH_NAME_MAX] = DEFAULT_HOST_DB_FILENAME; int hostdb_max_count = DEFAULT_HOST_DB_SIZE; -static ts::file::path hostdb_hostfile_path; +static swoc::file::path hostdb_hostfile_path; ts_seconds hostdb_sync_frequency{0}; int hostdb_disable_reverse_lookup = 0; int hostdb_max_iobuf_index = BUFFER_SIZE_INDEX_32K; @@ -206,7 +207,7 @@ HostDB_Config_Init() HostDBCache hostDB; -void UpdateHostsFile(ts::file::path const &path, ts_seconds interval); +void UpdateHostsFile(swoc::file::path const &path, ts_seconds interval); static inline bool is_addr_valid(uint8_t af, ///< Address family (format of data) @@ -1513,7 +1514,8 @@ HostDBContinuation::backgroundEvent(int /* event ATS_UNUSED */, Event * /* e ATS REC_ReadConfigString(path, "proxy.config.hostdb.host_file.path", sizeof(path)); if (0 != strcasecmp(hostdb_hostfile_path.string(), path)) { Debug("hostdb", "%s", - ts::bwprint(dbg, R"(Updating hosts file from "{}" to "{}")", hostdb_hostfile_path, ts::bwf::FirstOf(path, "")).c_str()); + ts::bwprint(dbg, R"(Updating hosts file from "{}" to "{}")", hostdb_hostfile_path.view(), ts::bwf::FirstOf(path, "")) + .c_str()); // path to hostfile changed hostdb_hostfile_update_timestamp = TS_TIME_ZERO; // never updated from this file hostdb_hostfile_path = path; @@ -1521,17 +1523,17 @@ HostDBContinuation::backgroundEvent(int /* event ATS_UNUSED */, Event * /* e ATS } else if (!hostdb_hostfile_path.empty()) { hostdb_last_timestamp = hostdb_current_timestamp; std::error_code ec; - auto stat{ts::file::status(hostdb_hostfile_path, ec)}; + auto stat{swoc::file::status(hostdb_hostfile_path, ec)}; if (!ec) { - if (ts_clock::from_time_t(modification_time(stat)) > hostdb_hostfile_update_timestamp) { + if (swoc::file::last_write_time(stat) > hostdb_hostfile_update_timestamp) { update_p = true; // same file but it's changed. } } else { - Debug("hostdb", "%s", ts::bwprint(dbg, R"(Failed to stat host file "{}" - {})", hostdb_hostfile_path, ec).c_str()); + Debug("hostdb", "%s", ts::bwprint(dbg, R"(Failed to stat host file "{}" - {})", hostdb_hostfile_path.view(), ec).c_str()); } } if (update_p) { - Debug("hostdb", "%s", ts::bwprint(dbg, R"(Updating from host file "{}")", hostdb_hostfile_path).c_str()); + Debug("hostdb", "%s", ts::bwprint(dbg, R"(Updating from host file "{}")", hostdb_hostfile_path.view()).c_str()); UpdateHostsFile(hostdb_hostfile_path, hostdb_hostfile_check_interval); } } @@ -1995,7 +1997,7 @@ HostDBFileContinuation::destroy() std::atomic<bool> HostDBFileUpdateActive{false}; void -UpdateHostsFile(ts::file::path const &path, ts_seconds interval) +UpdateHostsFile(swoc::file::path const &path, ts_seconds interval) { // Test and set for update in progress. bool flag = false; diff --git a/iocore/hostdb/HostFile.cc b/iocore/hostdb/HostFile.cc index 6a55990aa8..a791edcf14 100644 --- a/iocore/hostdb/HostFile.cc +++ b/iocore/hostdb/HostFile.cc @@ -21,6 +21,8 @@ limitations under the License. */ +#include "swoc/swoc_file.h" + #include "HostFile.h" #include "P_HostDBProcessor.h" @@ -80,15 +82,15 @@ HostFile::lookup(const HostDBHash &hash) } std::shared_ptr<HostFile> -ParseHostFile(ts::file::path const &path, ts_seconds interval) +ParseHostFile(swoc::file::path const &path, ts_seconds interval) { std::shared_ptr<HostFile> hf; - Debug_bw("hostdb", R"(Loading host file "{}")", path); + Debug_bw("hostdb", R"(Loading host file "{}")", path.view()); if (!path.empty()) { std::error_code ec; - std::string content = ts::file::load(path, ec); + std::string content = swoc::file::load(path, ec); if (!ec) { HostAddrMap addr_map; AddrHostMap host_map; diff --git a/iocore/hostdb/HostFile.h b/iocore/hostdb/HostFile.h index 48a614f088..5900a40663 100644 --- a/iocore/hostdb/HostFile.h +++ b/iocore/hostdb/HostFile.h @@ -23,9 +23,10 @@ #pragma once +#include "swoc/swoc_file.h" + #include "I_HostDBProcessor.h" #include "tscpp/util/TextView.h" -#include "tscore/ts_file.h" #include <memory> #include <unordered_map> @@ -48,4 +49,4 @@ struct HostFile { HostFileReverseMap reverse; }; -std::shared_ptr<HostFile> ParseHostFile(ts::file::path const &path, ts_seconds interval); +std::shared_ptr<HostFile> ParseHostFile(swoc::file::path const &path, ts_seconds interval); diff --git a/iocore/hostdb/Makefile.am b/iocore/hostdb/Makefile.am index 09ae1ee67b..ed12bbc7fb 100644 --- a/iocore/hostdb/Makefile.am +++ b/iocore/hostdb/Makefile.am @@ -89,7 +89,7 @@ test_HostFile_CPPFLAGS = \ -I$(abs_top_srcdir)/tests/include \ -I$(abs_top_srcdir)/proxy/http/remap -test_HostFile_LDADD = $(test_LD_ADD) +test_HostFile_LDADD = $(test_LD_ADD) @SWOC_LIBS@ test_HostFile_SOURCES = \ test_HostFile.cc \ diff --git a/iocore/hostdb/P_HostDBProcessor.h b/iocore/hostdb/P_HostDBProcessor.h index f2f90b6cd3..40fca995aa 100644 --- a/iocore/hostdb/P_HostDBProcessor.h +++ b/iocore/hostdb/P_HostDBProcessor.h @@ -29,13 +29,13 @@ #include <unordered_map> +#include "swoc/swoc_file.h" #include <tscpp/util/TsSharedMutex.h> #include "I_HostDBProcessor.h" #include "P_RefCountCache.h" #include "tscore/PendingAction.h" #include "tscore/TsBuffer.h" -#include "tscore/ts_file.h" // // Data diff --git a/iocore/hostdb/test_HostFile.cc b/iocore/hostdb/test_HostFile.cc index 7b728c3f12..10624db270 100644 --- a/iocore/hostdb/test_HostFile.cc +++ b/iocore/hostdb/test_HostFile.cc @@ -24,13 +24,17 @@ #define CATCH_CONFIG_MAIN #include "catch.hpp" +#include <string> + +#include "swoc/bwf_base.h" + #include "HostFile.h" #include "P_HostDBProcessor.h" const std::string_view hosts_data = "127.0.0.1 localhost\n::1 localhost\n1.2.3.4 host1\n4.3.2.1 host2 host3\n"_sv; void -spit(const ts::file::path &p, std::string_view data) +spit(const swoc::file::path &p, std::string_view data) { std::ofstream f(p.c_str(), std::ios::trunc); f.write(data.data(), data.size()); @@ -39,11 +43,11 @@ spit(const ts::file::path &p, std::string_view data) TEST_CASE("HostFile", "[hostdb]") { - auto tmp = ts::file::temp_directory_path(); - ts::LocalBufferWriter<1024> w; + auto tmp = swoc::file::temp_directory_path(); + swoc::LocalBufferWriter<1024> w; w.print("{}/localhost.{}", tmp, ::getpid()); - auto hostfilepath = ts::file::path(w.view()); + auto hostfilepath = swoc::file::path(w.view()); spit(hostfilepath, hosts_data); diff --git a/iocore/io_uring/unit_tests/test_diskIO.cc b/iocore/io_uring/unit_tests/test_diskIO.cc index ab7a390666..87eba54ca0 100644 --- a/iocore/io_uring/unit_tests/test_diskIO.cc +++ b/iocore/io_uring/unit_tests/test_diskIO.cc @@ -24,8 +24,9 @@ #include <atomic> #include "catch.hpp" +#include "swoc/swoc_file.h" + #include "I_IO_URING.h" -#include "tscore/ts_file.h" #include <functional> @@ -33,15 +34,15 @@ #include <netinet/in.h> #include <arpa/inet.h> -ts::file::path +swoc::file::path temp_prefix(const char *basename) { char buffer[PATH_MAX]; std::error_code err; - auto tmpdir = ts::file::temp_directory_path(); + auto tmpdir = swoc::file::temp_directory_path(); snprintf(buffer, sizeof(buffer), "%s/%s.XXXXXX", tmpdir.c_str(), basename); - auto prefix = ts::file::path(mkdtemp(buffer)); - bool result = ts::file::create_directories(prefix, err, 0755); + auto prefix = swoc::file::path(mkdtemp(buffer)); + bool result = swoc::file::create_directories(prefix, err, 0755); if (!result) { throw std::runtime_error("Failed to create directory"); } @@ -51,7 +52,7 @@ temp_prefix(const char *basename) } int -open_path(const ts::file::path &path, int oflags = O_CREAT | O_RDWR, int mode = 0644) +open_path(const swoc::file::path &path, int oflags = O_CREAT | O_RDWR, int mode = 0644) { return open(path.c_str(), oflags, mode); } @@ -126,7 +127,7 @@ TEST_CASE("disk_io", "[io_uring]") auto tmp = temp_prefix("disk_io"); - REQUIRE(ts::file::exists(tmp)); + REQUIRE(swoc::file::exists(tmp)); auto apath = tmp / "a"; diff --git a/iocore/net/SSLSecret.cc b/iocore/net/SSLSecret.cc index 0b72eba68f..95052046ac 100644 --- a/iocore/net/SSLSecret.cc +++ b/iocore/net/SSLSecret.cc @@ -19,8 +19,9 @@ limitations under the License. */ +#include "swoc/swoc_file.h" + #include "InkAPIInternal.h" // Added to include the ssl_hook and lifestyle_hook definitions -#include "tscore/ts_file.h" #include "P_SSLConfig.h" #include <utility> @@ -64,7 +65,7 @@ SSLSecret::loadFile(const std::string &name) { Debug("ssl_secret", "SSLSecret::loadFile(%s)", name.c_str()); std::error_code error; - std::string const data = ts::file::load(ts::file::path(name), error); + std::string const data = swoc::file::load(swoc::file::path(name), error); if (error) { Debug("ssl_secret_err", "SSLSecret::loadFile(%s) failed error code=%d message=%s", name.c_str(), error.value(), error.message().c_str()); diff --git a/iocore/net/SSLUtils.cc b/iocore/net/SSLUtils.cc index c92d5d3252..ea8e57a887 100644 --- a/iocore/net/SSLUtils.cc +++ b/iocore/net/SSLUtils.cc @@ -19,6 +19,7 @@ limitations under the License. */ +#include "swoc/swoc_file.h" #include "swoc/Errata.h" #include "swoc/bwf_std.h" @@ -33,7 +34,6 @@ #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" @@ -1973,7 +1973,7 @@ SSLMultiCertConfigLoader::load(SSLCertLookup *lookup) Note("%s loading ...", ts::filename::SSL_MULTICERT); std::error_code ec; - std::string content{ts::file::load(ts::file::path{params->configFilePath}, ec)}; + std::string content{swoc::file::load(swoc::file::path{params->configFilePath}, ec)}; if (ec) { switch (ec.value()) { case ENOENT: diff --git a/mgmt/rpc/config/JsonRPCConfig.cc b/mgmt/rpc/config/JsonRPCConfig.cc index aabb8e0bc1..e3dfe85aca 100644 --- a/mgmt/rpc/config/JsonRPCConfig.cc +++ b/mgmt/rpc/config/JsonRPCConfig.cc @@ -19,10 +19,11 @@ */ #include <string> +#include "swoc/swoc_file.h" + #include "JsonRPCConfig.h" #include "tscore/Diags.h" -#include "tscore/ts_file.h" #include "records/I_RecCore.h" #include "rpc/jsonrpc/JsonRPCManager.h" @@ -78,7 +79,7 @@ void RPCConfig::load_from_file(std::string const &filePath) { std::error_code ec; - std::string content{ts::file::load(ts::file::path{filePath}, ec)}; + std::string content{swoc::file::load(swoc::file::path{filePath}, ec)}; if (ec) { Warning("Cannot open the config file: %s - %s", filePath.c_str(), strerror(ec.value())); diff --git a/mgmt/rpc/server/unit_tests/test_rpcserver.cc b/mgmt/rpc/server/unit_tests/test_rpcserver.cc index 9fffce978b..71f7118fcc 100644 --- a/mgmt/rpc/server/unit_tests/test_rpcserver.cc +++ b/mgmt/rpc/server/unit_tests/test_rpcserver.cc @@ -31,8 +31,9 @@ #include <chrono> #include <fstream> +#include "swoc/swoc_file.h" + #include <tscore/BufferWriter.h> -#include <tscore/ts_file.h> #include "ts/ts.h" #include "rpc/jsonrpc/JsonRPC.h" @@ -46,7 +47,7 @@ #define DEFINE_JSONRPC_PROTO_FUNCTION(fn) ts::Rv<YAML::Node> fn(std::string_view const &id, const YAML::Node ¶ms) -namespace fs = ts::file; +namespace fs = swoc::file; namespace rpc { @@ -556,5 +557,5 @@ TEST_CASE("Test configuration parsing from a file. UDS Server", "[file]") REQUIRE(socket->get_conf().lockPathName == lockPathName); std::error_code ec; - REQUIRE(fs::remove(sandboxDir, ec)); + REQUIRE(fs::remove_all(sandboxDir, ec) > 0); } diff --git a/plugins/experimental/tls_bridge/tls_bridge.cc b/plugins/experimental/tls_bridge/tls_bridge.cc index d9622e7d3a..f03abe9e9b 100644 --- a/plugins/experimental/tls_bridge/tls_bridge.cc +++ b/plugins/experimental/tls_bridge/tls_bridge.cc @@ -13,12 +13,14 @@ the License. */ -#include "ts/ts.h" #include <atomic> #include <vector> #include <cinttypes> + +#include "swoc/swoc_file.h" + +#include "ts/ts.h" #include "tscpp/util/TextView.h" -#include "tscore/ts_file.h" #include "regex.h" using ts::TextView; @@ -97,7 +99,7 @@ private: * @param service The destination service. * @param ln Line number, or 0 if from plugin.config. */ - void load_pair(std::string_view rxp, std::string_view service, ts::file::path const &src, int ln = 0); + void load_pair(std::string_view rxp, std::string_view service, swoc::file::path const &src, int ln = 0); }; inline int @@ -107,7 +109,7 @@ BridgeConfig::count() const } void -BridgeConfig::load_pair(std::string_view rxp, std::string_view service, ts::file::path const &src, int ln) +BridgeConfig::load_pair(std::string_view rxp, std::string_view service, swoc::file::path const &src, int ln) { Regex r; // Unfortunately PCRE can only compile null terminated strings... @@ -127,7 +129,7 @@ BridgeConfig::load_pair(std::string_view rxp, std::string_view service, ts::file void BridgeConfig::load_config(int argc, const char *argv[]) { - static const ts::file::path plugin_config_fp{"plugin.config"}; + static const swoc::file::path plugin_config_fp{"plugin.config"}; for (int i = 0; i < argc; i += 2) { if (argv[i] == CONFIG_FILE_ARG) { @@ -135,13 +137,13 @@ BridgeConfig::load_config(int argc, const char *argv[]) TSError("[%s] Invalid '%.*s' argument - no file name found.", PLUGIN_NAME, int(CONFIG_FILE_ARG.size()), CONFIG_FILE_ARG.data()); } else { - ts::file::path fp(argv[i + 1]); + swoc::file::path fp(argv[i + 1]); std::error_code ec; if (!fp.is_absolute()) { - fp = ts::file::path{TS_CONFIG_DIR} / fp; // slap the config dir on it to make it absolute. + fp = swoc::file::path{TS_CONFIG_DIR} / fp; // slap the config dir on it to make it absolute. } // bulk load the file. - std::string content{ts::file::load(fp, ec)}; + std::string content{swoc::file::load(fp, ec)}; if (ec) { TSError("[%s] Invalid '%.*s' argument - unable to read file '%s' : %s.", PLUGIN_NAME, int(CONFIG_FILE_ARG.size()), CONFIG_FILE_ARG.data(), fp.c_str(), ec.message().c_str()); diff --git a/plugins/experimental/traffic_dump/session_data.cc b/plugins/experimental/traffic_dump/session_data.cc index 3cbac0f619..17c793cccf 100644 --- a/plugins/experimental/traffic_dump/session_data.cc +++ b/plugins/experimental/traffic_dump/session_data.cc @@ -156,7 +156,7 @@ std::atomic<int64_t> SessionData::sample_pool_size = default_sample_pool_size; std::atomic<int64_t> SessionData::max_disk_usage = default_max_disk_usage; std::atomic<int64_t> SessionData::disk_usage = 0; std::atomic<bool> SessionData::enforce_disk_limit = default_enforce_disk_limit; -ts::file::path SessionData::log_directory{default_log_directory}; +swoc::file::path SessionData::log_directory{default_log_directory}; uint64_t SessionData::session_counter = 0; std::string SessionData::sni_filter; std::optional<IpAddr> SessionData::client_ip_filter = std::nullopt; @@ -425,10 +425,10 @@ SessionData::session_aio_handler(TSCont contp, TSEvent event, void *edata) TSContDataSet(contp, nullptr); close(ssnData->log_fd); std::error_code ec; - ts::file::file_status st = ts::file::status(ssnData->log_name, ec); + swoc::file::file_status st = swoc::file::status(ssnData->log_name, ec); if (!ec) { - disk_usage += ts::file::file_size(st); - TSDebug(debug_tag, "Finish a session with log file of %" PRIuMAX " bytes", ts::file::file_size(st)); + disk_usage += swoc::file::file_size(st); + TSDebug(debug_tag, "Finish a session with log file of %" PRIuMAX " bytes", swoc::file::file_size(st)); } delete ssnData; return TS_SUCCESS; @@ -528,12 +528,12 @@ SessionData::global_session_handler(TSCont contp, TSEvent event, void *edata) // Initialize AIO file const std::lock_guard<std::recursive_mutex> _(ssnData->disk_io_mutex); if (ssnData->log_fd < 0) { - ts::file::path log_p = log_directory / ts::file::path(std::string(client_str, 3)); - ts::file::path log_f = log_p / ts::file::path(session_hex_name); + swoc::file::path log_p = log_directory / swoc::file::path(std::string(client_str, 3)); + swoc::file::path log_f = log_p / swoc::file::path(session_hex_name); // Create subdir if not existing std::error_code ec; - ts::file::status(log_p, ec); + swoc::file::status(log_p, ec); if (ec && mkdir(log_p.c_str(), 0755) == -1) { TSDebug(debug_tag, "global_session_handler(): Failed to create dir %s", log_p.c_str()); TSError("[%s] Failed to create dir %s", debug_tag, log_p.c_str()); diff --git a/plugins/experimental/traffic_dump/session_data.h b/plugins/experimental/traffic_dump/session_data.h index 094f02e5be..1fd2396dc9 100644 --- a/plugins/experimental/traffic_dump/session_data.h +++ b/plugins/experimental/traffic_dump/session_data.h @@ -30,9 +30,10 @@ #include <string_view> #include <optional> +#include "swoc/swoc_file.h" + #include "ts/ts.h" #include "tscore/ink_inet.h" -#include "tscore/ts_file.h" namespace traffic_dump { @@ -68,7 +69,7 @@ private: /// Whether this session has been closed. bool ssn_closed = false; /// The filename for this session's dump file. - ts::file::path log_name; + swoc::file::path log_name; /// Whether the first transaction in this session has been written. bool has_written_first_transaction = false; /// The HTTP version specified in the client protocol stack, or empty string @@ -102,7 +103,7 @@ private: static std::atomic<bool> enforce_disk_limit; /// The directory into which to put the dump files. - static ts::file::path log_directory; + static swoc::file::path log_directory; /// Only sessions with this SNI will be dumped (if set). static std::string sni_filter; diff --git a/plugins/experimental/traffic_dump/traffic_dump.cc b/plugins/experimental/traffic_dump/traffic_dump.cc index 11d0192968..4e4b238c9b 100644 --- a/plugins/experimental/traffic_dump/traffic_dump.cc +++ b/plugins/experimental/traffic_dump/traffic_dump.cc @@ -87,7 +87,7 @@ TSPluginInit(int argc, char const *argv[]) bool dump_body = false; bool sensitive_fields_were_specified = false; traffic_dump::sensitive_fields_t user_specified_fields; - ts::file::path log_dir{traffic_dump::SessionData::default_log_directory}; + swoc::file::path log_dir{traffic_dump::SessionData::default_log_directory}; int64_t sample_pool_size = traffic_dump::SessionData::default_sample_pool_size; int64_t max_disk_usage = traffic_dump::SessionData::default_max_disk_usage; bool enforce_disk_limit = traffic_dump::SessionData::default_enforce_disk_limit; @@ -141,7 +141,7 @@ TSPluginInit(int argc, char const *argv[]) break; } case 'l': { - log_dir = ts::file::path{optarg}; + log_dir = swoc::file::path{optarg}; break; } case 's': { @@ -169,7 +169,7 @@ TSPluginInit(int argc, char const *argv[]) } } if (!log_dir.is_absolute()) { - log_dir = ts::file::path(TSInstallDirGet()) / log_dir; + log_dir = swoc::file::path(TSInstallDirGet()) / log_dir; } if (sni_filter.empty()) { if (!traffic_dump::SessionData::init(log_dir.view(), enforce_disk_limit, max_disk_usage, sample_pool_size, client_ip_filter)) { diff --git a/proxy/ControlMatcher.cc b/proxy/ControlMatcher.cc index e0150a48f4..6e072105d0 100644 --- a/proxy/ControlMatcher.cc +++ b/proxy/ControlMatcher.cc @@ -31,11 +31,11 @@ #include <sys/types.h> #include "swoc/bwf_ip.h" +#include "swoc/swoc_file.h" #include "tscore/ink_config.h" #include "tscore/MatcherUtils.h" #include "tscore/Tokenizer.h" -#include "tscore/ts_file.h" #include "ConfigProcessor.h" #include "ControlMatcher.h" #include "CacheControl.h" @@ -934,7 +934,7 @@ int ControlMatcher<Data, MatchResult>::BuildTable() { std::error_code ec; - std::string content{ts::file::load(ts::file::path{config_file_path}, ec)}; + std::string content{swoc::file::load(swoc::file::path{config_file_path}, ec)}; if (ec) { switch (ec.value()) { case ENOENT: diff --git a/proxy/http/remap/PluginDso.cc b/proxy/http/remap/PluginDso.cc index 0c84012d7b..81b4c3600a 100644 --- a/proxy/http/remap/PluginDso.cc +++ b/proxy/http/remap/PluginDso.cc @@ -85,9 +85,8 @@ PluginDso::load(std::string &error) /* Save the time for later checking if DSO got modified in consecutive DSO reloads */ std::error_code ec; - fs::file_status fs = fs::status(_effectivePath, ec); - _mtime = fs::modification_time(fs); - PluginDebug(_tag, "plugin '%s' modification time %ld", _configPath.c_str(), _mtime); + _mtime = fs::last_write_time(_effectivePath, ec); + PluginDebug(_tag, "plugin '%s' modification time %ld", _configPath.c_str(), ts_clock::to_time_t(_mtime)); /* Now attempt to load the plugin DSO */ #if defined(darwin) @@ -212,7 +211,7 @@ PluginDso::runtimePath() const * @return modification time. */ -time_t +swoc::file::file_time_type PluginDso::modTime() const { return _mtime; @@ -243,7 +242,7 @@ PluginDso::clean(std::string &error) return; } - if (false == remove(_runtimePath, _errorCode)) { + if (0 == remove_all(_runtimePath, _errorCode)) { error.append("failed to remove runtime copy: ").append(_errorCode.message()); } } @@ -316,10 +315,11 @@ PluginDso::LoadedPlugins::remove(PluginDso *plugin) PluginDso * PluginDso::LoadedPlugins::findByEffectivePath(const fs::path &path, bool dynamicReloadEnabled) { - struct stat sb; - time_t mtime = 0; - if (0 == stat(path.c_str(), &sb)) { - mtime = sb.st_mtime; + std::error_code ec; + auto fs = fs::status(path, ec); + ts_clock::time_point mtime; + if (!ec) { + mtime = fs::last_write_time(fs); } SCOPED_MUTEX_LOCK(lock, _mutex, this_ethread()); diff --git a/proxy/http/remap/PluginDso.h b/proxy/http/remap/PluginDso.h index e8d359d636..642c27b31b 100644 --- a/proxy/http/remap/PluginDso.h +++ b/proxy/http/remap/PluginDso.h @@ -29,6 +29,8 @@ #pragma once +#include "swoc/swoc_file.h" + #include <unordered_map> #include <dlfcn.h> #include <vector> @@ -37,8 +39,8 @@ #include "ts/apidefs.h" #include "ts/remap.h" -#include "tscore/ts_file.h" -namespace fs = ts::file; + +namespace fs = swoc::file; #include "tscore/Ptr.h" #include "I_EventSystem.h" @@ -71,7 +73,7 @@ public: /* Accessors for effective and runtime paths */ const fs::path &effectivePath() const; const fs::path &runtimePath() const; - time_t modTime() const; + swoc::file::file_time_type modTime() const; void *dlOpenHandle() const; void * dlh() const @@ -164,9 +166,9 @@ protected: void *_dlh = nullptr; /** @brief dlopen handler used internally in this class, used as flag for loaded vs unloaded (nullptr) */ std::error_code _errorCode; /** @brief used in filesystem calls */ - static constexpr const char *const _tag = "plugin_dso"; /** @brief log tag used by this class */ - time_t _mtime = 0; /* @brief modification time of the DSO's file, used for checking */ - bool _preventiveCleaning = true; + static constexpr const char *const _tag = "plugin_dso"; /** @brief log tag used by this class */ + swoc::file::file_time_type _mtime{fs::file_time_type::min()}; /* @brief modification time of the DSO's file, used for checking */ + bool _preventiveCleaning = true; static Ptr<LoadedPlugins> _plugins; /** @brief a global list of plugins, usually maintained by a plugin factory or plugin instance itself */ diff --git a/proxy/http/remap/PluginFactory.cc b/proxy/http/remap/PluginFactory.cc index 729b1b475a..254288dd8a 100644 --- a/proxy/http/remap/PluginFactory.cc +++ b/proxy/http/remap/PluginFactory.cc @@ -102,7 +102,7 @@ PluginFactory::~PluginFactory() _instList.apply([](RemapPluginInst *pluginInst) -> void { delete pluginInst; }); _instList.clear(); - fs::remove(_runtimeDir, _ec); + fs::remove_all(_runtimeDir, _ec); PluginDebug(_tag, "destroyed plugin factory %s", getUuid()); delete _uuid; @@ -235,7 +235,7 @@ PluginFactory::getEffectivePath(const fs::path &configPath) { if (configPath.is_absolute()) { if (fs::exists(configPath)) { - return fs::canonical(configPath.string(), _ec); + return fs::canonical(configPath, _ec); } else { return fs::path(); } @@ -308,5 +308,5 @@ PluginFactory::indicatePostReload(bool reloadSuccessful) void PluginFactory::clean(std::string &error) { - fs::remove(_runtimeDir, _ec); + fs::remove_all(_runtimeDir, _ec); } diff --git a/proxy/http/remap/RemapConfig.cc b/proxy/http/remap/RemapConfig.cc index 2798517bb4..d608df172f 100644 --- a/proxy/http/remap/RemapConfig.cc +++ b/proxy/http/remap/RemapConfig.cc @@ -21,6 +21,8 @@ * limitations under the License. */ +#include "swoc/swoc_file.h" + #include "RemapConfig.h" #include "UrlRewrite.h" #include "ReverseProxy.h" @@ -29,9 +31,7 @@ #include "tscore/ink_platform.h" #include "tscore/List.h" #include "tscore/ink_cap.h" -#include "tscore/ink_file.h" #include "tscore/Tokenizer.h" -#include "tscore/ts_file.h" #include "tscore/Filenames.h" #include "IPAllow.h" #include "PluginFactory.h" @@ -819,7 +819,7 @@ remap_load_plugin(const char **argv, int argc, url_mapping *mp, char *errbuf, in REC_ReadConfigInteger(elevate_access, "proxy.config.plugin.load_elevated"); ElevateAccess access(elevate_access ? ElevateAccess::FILE_PRIVILEGE : 0); - pi = rewrite->pluginFactory.getRemapPlugin(ts::file::path(const_cast<const char *>(c)), parc, pargv, error, + pi = rewrite->pluginFactory.getRemapPlugin(swoc::file::path(const_cast<const char *>(c)), parc, pargv, error, isPluginDynamicReloadEnabled()); } // done elevating access @@ -942,7 +942,7 @@ remap_parse_config_bti(const char *path, BUILD_TABLE_INFO *bti) const char *type_id_str; std::error_code ec; - std::string content{ts::file::load(ts::file::path{path}, ec)}; + std::string content{swoc::file::load(swoc::file::path{path}, ec)}; if (ec.value() == ENOENT) { // a missing file is ok - treat as empty, no rules. return true; } diff --git a/proxy/http/remap/unit-tests/test_PluginDso.cc b/proxy/http/remap/unit-tests/test_PluginDso.cc index 5939b70a73..43c6ae5191 100644 --- a/proxy/http/remap/unit-tests/test_PluginDso.cc +++ b/proxy/http/remap/unit-tests/test_PluginDso.cc @@ -54,7 +54,7 @@ static fs::path runtimePath = runtimeDir / configPath; void clean() { - fs::remove(sandboxDir, ec); + fs::remove_all(sandboxDir, ec); } /* Mock used only to make PluginDso concrete enough to be tested */ @@ -120,7 +120,7 @@ SCENARIO("loading plugins", "[plugin][core]") CHECK(runtimePath == plugin.runtimePath()); CHECK(fs::exists(runtimePath)); } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } WHEN("loading a valid plugin") @@ -133,14 +133,14 @@ SCENARIO("loading plugins", "[plugin][core]") CHECK(error.empty()); std::error_code ec; fs::file_status fs = fs::status(effectivePath, ec); - CHECK(plugin.modTime() == fs::modification_time(fs)); + CHECK(plugin.modTime() == fs::last_write_time(fs)); } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } WHEN("loading a valid plugin but missing runtime dir") { - CHECK(fs::remove(runtimeDir, ec)); + CHECK(fs::remove_all(runtimeDir, ec) > 0); CHECK_FALSE(fs::exists(runtimePath)); bool result = plugin.load(error); @@ -149,7 +149,7 @@ SCENARIO("loading plugins", "[plugin][core]") CHECK_FALSE(true == result); CHECK("failed to create a copy: No such file or directory" == error); } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } WHEN("loading a valid plugin twice in a row") @@ -167,7 +167,7 @@ SCENARIO("loading plugins", "[plugin][core]") CHECK_FALSE(true == result); CHECK("plugin already loaded" == error); } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } WHEN("explicitly unloading a valid but not loaded plugin") @@ -184,7 +184,7 @@ SCENARIO("loading plugins", "[plugin][core]") CHECK_FALSE(error.empty()); CHECK_FALSE(fs::exists(runtimePath)); } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } WHEN("unloading a valid plugin twice in a row") @@ -205,7 +205,7 @@ SCENARIO("loading plugins", "[plugin][core]") CHECK_FALSE(true == result); CHECK("no plugin loaded" == error); } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } WHEN("explicitly unloading a valid and loaded plugin") @@ -234,7 +234,7 @@ SCENARIO("loading plugins", "[plugin][core]") /* Runtime DSO should not be found anymore */ CHECK_FALSE(fs::exists(runtimePath)); } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } WHEN("implicitly unloading a valid and loaded plugin") @@ -258,7 +258,7 @@ SCENARIO("loading plugins", "[plugin][core]") /* Runtime path should be removed after unloading */ CHECK_FALSE(fs::exists(runtimePath)); } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } } @@ -276,7 +276,7 @@ SCENARIO("loading plugins", "[plugin][core]") CHECK_FALSE(true == result); CHECK("empty effective path" == error); CHECK(plugin.effectivePath().empty()); - CHECK(0 == plugin.modTime()); + CHECK(swoc::file::file_time_type::min() == plugin.modTime()); CHECK(runtimePath == plugin.runtimePath()); CHECK_FALSE(fs::exists(runtimePath)); } @@ -315,7 +315,7 @@ SCENARIO("loading plugins", "[plugin][core]") /* Runtime DSO should not exist since the load failed. */ CHECK_FALSE(fs::exists(runtimePath)); } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } } } @@ -352,7 +352,7 @@ SCENARIO("looking for symbols inside a plugin DSO", "[plugin][core]") CHECK(nullptr != s); CHECK(error.empty()); } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } WHEN("looking for non-existing symbol") @@ -364,7 +364,7 @@ SCENARIO("looking for symbols inside a plugin DSO", "[plugin][core]") CHECK(nullptr == s); CHECK_FALSE(error.empty()); } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } WHEN("looking for multiple existing symbols") @@ -380,7 +380,7 @@ SCENARIO("looking for symbols inside a plugin DSO", "[plugin][core]") CHECK(error.empty()); } } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } /* The following version function is used only for unit-testing of the plugin factory functionality */ @@ -394,7 +394,7 @@ SCENARIO("looking for symbols inside a plugin DSO", "[plugin][core]") int ver = version ? version() : -1; CHECK(1 == ver); } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } } } diff --git a/proxy/http/remap/unit-tests/test_PluginFactory.cc b/proxy/http/remap/unit-tests/test_PluginFactory.cc index e12016b652..f516a831e2 100644 --- a/proxy/http/remap/unit-tests/test_PluginFactory.cc +++ b/proxy/http/remap/unit-tests/test_PluginFactory.cc @@ -111,7 +111,7 @@ static fs::path pluginBuildDir = fs::current_path() / "unit-tests/.libs"; void clean() { - fs::remove(sandboxDir, ec); + fs::remove_all(sandboxDir, ec); } static int @@ -514,7 +514,7 @@ SCENARIO("multiple search dirs + multiple or no plugins installed", "[plugin][co CHECK(abEffectivePath == pluginInst->_plugin.effectivePath()); CHECK(absRuntimePath == pluginInst->_plugin.runtimePath()); } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } WHEN("a valid plugin is found in the first search path") @@ -528,12 +528,12 @@ SCENARIO("multiple search dirs + multiple or no plugins installed", "[plugin][co CHECK(effectivePath1 == pluginInst->_plugin.effectivePath()); CHECK(runtimePath1 == pluginInst->_plugin.runtimePath()); } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } WHEN("the first search dir is missing the plugin but the second search has it") { - CHECK(fs::remove(effectivePath1, ec)); + CHECK(fs::remove_all(effectivePath1, ec) > 0); RemapPluginInst *pluginInst = factory.getRemapPlugin(configPath, 0, nullptr, error, isPluginDynamicReloadEnabled()); THEN("Expect it to successfully load the one found in the second search dir") @@ -543,7 +543,7 @@ SCENARIO("multiple search dirs + multiple or no plugins installed", "[plugin][co CHECK(effectivePath2 == pluginInst->_plugin.effectivePath()); CHECK(runtimePath2 == pluginInst->_plugin.runtimePath()); } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } WHEN("the first and second search dir are missing the plugin but the third search has it") @@ -559,7 +559,7 @@ SCENARIO("multiple search dirs + multiple or no plugins installed", "[plugin][co CHECK(effectivePath3 == pluginInst->_plugin.effectivePath()); CHECK(runtimePath3 == pluginInst->_plugin.runtimePath()); } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } WHEN("none of the search dirs contains a valid plugin") @@ -577,7 +577,7 @@ SCENARIO("multiple search dirs + multiple or no plugins installed", "[plugin][co CHECK_FALSE(fs::exists(runtimePath2)); CHECK_FALSE(fs::exists(runtimePath3)); } - CHECK(fs::remove(sandboxDir, ec)); + CHECK(fs::remove_all(sandboxDir, ec) > 0); } } } diff --git a/proxy/http/remap/unit-tests/test_RemapPlugin.cc b/proxy/http/remap/unit-tests/test_RemapPlugin.cc index 0035eafdb3..82e82db469 100644 --- a/proxy/http/remap/unit-tests/test_RemapPlugin.cc +++ b/proxy/http/remap/unit-tests/test_RemapPlugin.cc @@ -49,7 +49,7 @@ static fs::path pluginBuildDir = fs::current_path() / "unit-tests/.libs"; void clean() { - fs::remove(sandboxDir, ec); + fs::remove_all(sandboxDir, ec); } /* Mock used only to make unit testing convenient to check if callbacks are really called and check errors */ diff --git a/proxy/logging/RolledLogDeleter.cc b/proxy/logging/RolledLogDeleter.cc index 6c8a6e207c..01911cb795 100644 --- a/proxy/logging/RolledLogDeleter.cc +++ b/proxy/logging/RolledLogDeleter.cc @@ -26,12 +26,13 @@ #include <sys/stat.h> #include <unistd.h> +#include "swoc/swoc_file.h" + #include "RolledLogDeleter.h" #include "LogUtils.h" -#include "tscore/ts_file.h" #include "tscpp/util/TextView.h" -namespace fs = ts::file; +namespace fs = swoc::file; LogDeletingInfo::LogDeletingInfo(const char *_logname, int _min_count) : logname(_logname), @@ -79,7 +80,7 @@ RolledLogDeleter::register_log_type_for_deletion(std::string_view log_type, int bool RolledLogDeleter::consider_for_candidacy(std::string_view log_path, int64_t file_size, time_t modification_time) { - const fs::path rolled_log_file = fs::filename(log_path); + const fs::path rolled_log_file = fs::path(log_path).filename(); auto iter = deleting_info.find(LogUtils::get_unrolled_filename(rolled_log_file.view())); if (iter == deleting_info.end()) { return false; diff --git a/proxy/logging/unit-tests/test_RolledLogDeleter.cc b/proxy/logging/unit-tests/test_RolledLogDeleter.cc index 83f96bac22..af2220578b 100644 --- a/proxy/logging/unit-tests/test_RolledLogDeleter.cc +++ b/proxy/logging/unit-tests/test_RolledLogDeleter.cc @@ -21,14 +21,14 @@ limitations under the License. */ -#include <RolledLogDeleter.h> +#include "swoc/swoc_file.h" -#include "tscore/ts_file.h" +#include <RolledLogDeleter.h> #define CATCH_CONFIG_MAIN #include "catch.hpp" -namespace fs = ts::file; +namespace fs = swoc::file; const fs::path log_dir("/home/y/logs/trafficserver"); @@ -307,4 +307,4 @@ TEST_CASE("verify priority enforcement", "[RolledLogDeleter]") verify_there_are_no_candidates(deleter); } -} \ No newline at end of file +} diff --git a/src/records/RecCore.cc b/src/records/RecCore.cc index 20daab92f6..8fa774aadd 100644 --- a/src/records/RecCore.cc +++ b/src/records/RecCore.cc @@ -21,11 +21,12 @@ limitations under the License. */ +#include "swoc/swoc_file.h" + #include "tscore/ink_platform.h" #include "tscore/ink_memory.h" #include "tscore/ink_string.h" #include "tscore/Filenames.h" -#include "tscore/ts_file.h" #include "records/I_RecordsConfig.h" #include "records/P_RecFile.h" @@ -222,8 +223,8 @@ RecCoreInit(Diags *_diags) // Make sure there is no legacy file, if so we drop a BIG WARNING and fail. // This is to avoid issues with someone ignoring that we now use records.yaml - ts::file::path old_config{RecConfigReadConfigPath(nullptr, "records.config")}; - if (ts::file::exists(old_config)) { + swoc::file::path old_config{RecConfigReadConfigPath(nullptr, "records.config")}; + if (swoc::file::is_readable(old_config)) { RecLog(DL_Fatal, "**** Found a legacy config file (%s). Please remove it and migrate to the new YAML format before continuing. ****", old_config.c_str()); diff --git a/src/traffic_cache_tool/CacheDefs.h b/src/traffic_cache_tool/CacheDefs.h index 45947d1370..0cd9f9ca20 100644 --- a/src/traffic_cache_tool/CacheDefs.h +++ b/src/traffic_cache_tool/CacheDefs.h @@ -27,14 +27,16 @@ #include <iostream> #include <list> -#include "tscore/I_Version.h" +#include "swoc/swoc_file.h" #include "swoc/Scalar.h" + +#include "tscore/I_Version.h" +#include "tscore/ink_memory.h" #include "tscore/Regex.h" #include "tscore/Errata.h" #include "tscpp/util/TextView.h" #include "tscore/ink_file.h" #include "tscore/CryptoHash.h" -#include "tscore/ts_file.h" namespace ts::tag { @@ -276,10 +278,10 @@ class DFA; // this class matches url of the format : scheme://hostname:port/path;params?query struct url_matcher { - url_matcher(ts::file::path const &path) // file contains a list of regex + url_matcher(swoc::file::path const &path) // file contains a list of regex { std::error_code ec; - std::string load_content = ts::file::load(path, ec); + std::string load_content = swoc::file::load(path, ec); ts::TextView fileContent(load_content); if (ec.value() == 0) { const char **patterns; @@ -440,7 +442,7 @@ dir_to_offset(const CacheDirEntry *d, const CacheDirEntry *seg) struct Stripe; struct Span { - Span(ts::file::path const &path) : _path(path) {} + Span(swoc::file::path const &path) : _path(path) {} Errata load(); Errata loadDevice(); bool isEmpty() const; @@ -455,7 +457,7 @@ struct Span { ts::Rv<Stripe *> allocStripe(int vol_idx, const CacheStripeBlocks &len); Errata updateHeader(); ///< Update serialized header and write to disk. - ts::file::path _path; ///< File system location of span. + swoc::file::path _path; ///< File system location of span. ats_scoped_fd _fd; ///< Open file descriptor for span. int _vol_idx = 0; ///< Forced volume. CacheStoreBlocks _base; ///< Offset to first usable byte. diff --git a/src/traffic_cache_tool/CacheScan.h b/src/traffic_cache_tool/CacheScan.h index 6ab716a268..5a01d3e80f 100644 --- a/src/traffic_cache_tool/CacheScan.h +++ b/src/traffic_cache_tool/CacheScan.h @@ -43,7 +43,7 @@ class CacheScan url_matcher *u_matcher; public: - CacheScan(Stripe *str, ts::file::path const &path) : stripe(str) + CacheScan(Stripe *str, swoc::file::path const &path) : stripe(str) { if (!path.empty()) { u_matcher = new url_matcher(path); diff --git a/src/traffic_cache_tool/CacheTool.cc b/src/traffic_cache_tool/CacheTool.cc index c7b545f819..10e9d1d09f 100644 --- a/src/traffic_cache_tool/CacheTool.cc +++ b/src/traffic_cache_tool/CacheTool.cc @@ -62,8 +62,8 @@ extern int cache_config_min_average_object_size; extern CacheStoreBlocks Vol_hash_alloc_size; extern int OPEN_RW_FLAG; const Bytes ts::CacheSpan::OFFSET{CacheStoreBlocks{1}}; -ts::file::path SpanFile; -ts::file::path VolumeFile; +swoc::file::path SpanFile; +swoc::file::path VolumeFile; ts::ArgParser parser; Errata err; @@ -102,7 +102,7 @@ Volume::clear() /* --------------------------------------------------------------------------------------- */ /// Data parsed from the volume config file. struct VolumeConfig { - Errata load(ts::file::path const &path); + Errata load(swoc::file::path const &path); /// Data direct from the config file. struct Data { @@ -183,10 +183,10 @@ VolumeConfig::convertToAbsolute(const ts::CacheStripeBlocks &n) struct Cache { ~Cache(); - Errata loadSpan(ts::file::path const &path); - Errata loadSpanConfig(ts::file::path const &path); - Errata loadSpanDirect(ts::file::path const &path, int vol_idx = -1, const Bytes &size = Bytes(-1)); - Errata loadURLs(ts::file::path const &path); + Errata loadSpan(swoc::file::path const &path); + Errata loadSpanConfig(swoc::file::path const &path); + Errata loadSpanDirect(swoc::file::path const &path, int vol_idx = -1, const Bytes &size = Bytes(-1)); + Errata loadURLs(swoc::file::path const &path); Errata allocStripe(Span *span, int vol_idx, const CacheStripeBlocks &len); @@ -274,10 +274,10 @@ class VolumeAllocator public: VolumeAllocator(); - Errata load(ts::file::path const &spanFile, ts::file::path const &volumeFile); + Errata load(swoc::file::path const &spanFile, swoc::file::path const &volumeFile); Errata fillEmptySpans(); Errata fillAllSpans(); - Errata allocateSpan(ts::file::path const &spanFile); + Errata allocateSpan(swoc::file::path const &spanFile); void dumpVolumes(); protected: @@ -288,7 +288,7 @@ protected: VolumeAllocator::VolumeAllocator() = default; Errata -VolumeAllocator::load(ts::file::path const &spanFile, ts::file::path const &volumeFile) +VolumeAllocator::load(swoc::file::path const &spanFile, swoc::file::path const &volumeFile) { Errata zret; @@ -340,7 +340,7 @@ VolumeAllocator::fillEmptySpans() } Errata -VolumeAllocator::allocateSpan(ts::file::path const &input_file_path) +VolumeAllocator::allocateSpan(swoc::file::path const &input_file_path) { Errata zret; for (auto span : _cache._spans) { @@ -453,17 +453,17 @@ VolumeAllocator::allocateFor(Span &span) } /* --------------------------------------------------------------------------------------- */ Errata -Cache::loadSpan(ts::file::path const &path) +Cache::loadSpan(swoc::file::path const &path) { Errata zret; std::error_code ec; - auto fs = ts::file::status(path, ec); + auto fs = swoc::file::status(path, ec); if (path.empty()) { zret = Errata::Message(0, EINVAL, "A span file specified by --spans is required"); - } else if (!ts::file::is_readable(path)) { + } else if (!swoc::file::is_readable(path)) { zret = Errata::Message(0, EPERM, '\'', path.string(), "' is not readable."); - } else if (ts::file::is_regular_file(fs)) { + } else if (swoc::file::is_regular_file(fs)) { zret = this->loadSpanConfig(path); } else { zret = this->loadSpanDirect(path); @@ -472,7 +472,7 @@ Cache::loadSpan(ts::file::path const &path) } Errata -Cache::loadSpanDirect(ts::file::path const &path, int vol_idx, const Bytes &size) +Cache::loadSpanDirect(swoc::file::path const &path, int vol_idx, const Bytes &size) { Errata zret; std::unique_ptr<Span> span(new Span(path)); @@ -506,14 +506,14 @@ Cache::loadSpanDirect(ts::file::path const &path, int vol_idx, const Bytes &size } Errata -Cache::loadSpanConfig(ts::file::path const &path) +Cache::loadSpanConfig(swoc::file::path const &path) { static const ts::TextView TAG_ID("id"); static const ts::TextView TAG_VOL("volume"); Errata zret; std::error_code ec; - std::string load_content = ts::file::load(path, ec); + std::string load_content = swoc::file::load(path, ec); if (ec.value() == 0) { ts::TextView content(load_content); while (content) { @@ -541,7 +541,7 @@ Cache::loadSpanConfig(ts::file::path const &path) } } } - zret = this->loadSpan(ts::file::path(localpath)); + zret = this->loadSpan(swoc::file::path(localpath)); } } } else { @@ -551,14 +551,14 @@ Cache::loadSpanConfig(ts::file::path const &path) } Errata -Cache::loadURLs(ts::file::path const &path) +Cache::loadURLs(swoc::file::path const &path) { static const ts::TextView TAG_VOL("url"); ts::URLparser loadURLparser; Errata zret; std::error_code ec; - std::string load_content = ts::file::load(path, ec); + std::string load_content = swoc::file::load(path, ec); if (ec.value() == 0) { ts::TextView content(load_content); while (!content.empty()) { @@ -697,13 +697,13 @@ Span::load() { Errata zret; std::error_code ec; - auto fs = ts::file::status(_path, ec); + auto fs = swoc::file::status(_path, ec); - if (!ts::file::is_readable(_path)) { + if (!swoc::file::is_readable(_path)) { zret = Errata::Message(0, EPERM, _path.string(), " is not readable."); - } else if (ts::file::is_char_device(fs) || ts::file::is_block_device(fs)) { + } else if (swoc::file::is_char_device(fs) || swoc::file::is_block_device(fs)) { zret = this->loadDevice(); - } else if (ts::file::is_dir(fs)) { + } else if (swoc::file::is_dir(fs)) { zret.push(0, 1, "Directory support not yet available"); } else { zret.push(0, EBADF, _path.string(), " is not a valid file type"); @@ -1028,7 +1028,7 @@ Cache::key_to_stripe(CryptoHash *key, const char *hostname, int host_len) /* --------------------------------------------------------------------------------------- */ Errata -VolumeConfig::load(ts::file::path const &path) +VolumeConfig::load(swoc::file::path const &path) { static const ts::TextView TAG_SIZE("size"); static const ts::TextView TAG_VOL("volume"); @@ -1038,7 +1038,7 @@ VolumeConfig::load(ts::file::path const &path) int ln = 0; std::error_code ec; - std::string load_content = ts::file::load(path, ec); + std::string load_content = swoc::file::load(path, ec); if (ec.value() == 0) { ts::TextView content(load_content); while (content) { @@ -1171,7 +1171,7 @@ Clear_Spans() } void -Find_Stripe(ts::file::path const &input_file_path) +Find_Stripe(swoc::file::path const &input_file_path) { // scheme=http user=u password=p host=172.28.56.109 path=somepath query=somequery port=1234 // input file format: scheme://hostname:port/somepath;params?somequery user=USER password=PASS @@ -1277,7 +1277,7 @@ Check_Freelist(const std::string &devicePath) } void -Init_disk(ts::file::path const &input_file_path) +Init_disk(swoc::file::path const &input_file_path) { Cache cache; VolumeAllocator va; @@ -1292,7 +1292,7 @@ Init_disk(ts::file::path const &input_file_path) } void -Get_Response(ts::file::path const &input_file_path) +Get_Response(swoc::file::path const &input_file_path) { // scheme=http user=u password=p host=172.28.56.109 path=somepath query=somequery port=1234 // input file format: scheme://hostname:port/somepath;params?somequery user=USER password=PASS @@ -1327,7 +1327,7 @@ Get_Response(ts::file::path const &input_file_path) } } -void static scan_span(Span *span, ts::file::path const ®ex_path) +void static scan_span(Span *span, swoc::file::path const ®ex_path) { for (auto strp : span->_stripes) { strp->loadMeta(); @@ -1344,7 +1344,7 @@ void static scan_span(Span *span, ts::file::path const ®ex_path) } void -Scan_Cache(ts::file::path const ®ex_path) +Scan_Cache(swoc::file::path const ®ex_path) { Cache cache; std::vector<std::thread> threadPool; @@ -1365,7 +1365,7 @@ Scan_Cache(ts::file::path const ®ex_path) int main(int argc, const char *argv[]) { - ts::file::path input_url_file; + swoc::file::path input_url_file; std::string inputFile; parser.add_global_usage(std::string(argv[0]) + " --spans <SPAN> --volume <FILE> <COMMAND> [<SUBCOMMAND> ...]\n"); diff --git a/src/traffic_cache_tool/Makefile.inc b/src/traffic_cache_tool/Makefile.inc index 71dc559e00..42e653a8a0 100644 --- a/src/traffic_cache_tool/Makefile.inc +++ b/src/traffic_cache_tool/Makefile.inc @@ -45,7 +45,6 @@ traffic_cache_tool_traffic_cache_tool_LDADD = \ $(top_builddir)/src/tscore/.libs/ink_string.o \ $(top_builddir)/src/tscore/.libs/BufferWriterFormat.o \ $(top_builddir)/src/tscore/.libs/InkErrno.o \ - $(top_builddir)/src/tscore/.libs/ts_file.o \ $(top_builddir)/src/tscore/.libs/Errata.o \ $(top_builddir)/src/tscpp/util/.libs/TextView.o \ $(top_builddir)/src/tscore/.libs/Regex.o \ diff --git a/src/traffic_server/traffic_server.cc b/src/traffic_server/traffic_server.cc index d149475052..98772854c0 100644 --- a/src/traffic_server/traffic_server.cc +++ b/src/traffic_server/traffic_server.cc @@ -30,6 +30,8 @@ ****************************************************************************/ +#include "swoc/swoc_file.h" + #include "tscore/ink_platform.h" #include "tscore/ink_sys_control.h" #include "tscore/ink_args.h" @@ -40,7 +42,6 @@ #include "tscore/hugepages.h" #include "tscore/runroot.h" #include "tscore/Filenames.h" -#include "tscore/ts_file.h" #include "ts/ts.h" // This is sadly needed because of us using TSThreadInit() for some reason. @@ -1016,7 +1017,7 @@ load_plugin(plugin_type_t plugin_type, const fs::path &plugin_path, std::string error = error_os.str(); return false; } - const auto runtime_path = temporary_directory / ts::file::filename(plugin_path); + const auto runtime_path = temporary_directory / plugin_path.filename(); const fs::path unused_config; auto plugin_info = std::make_unique<RemapPluginInfo>(unused_config, plugin_path, runtime_path); bool loaded = plugin_info->load(error); diff --git a/src/tscore/CMakeLists.txt b/src/tscore/CMakeLists.txt index 0c3fe6254a..0dfae75d22 100644 --- a/src/tscore/CMakeLists.txt +++ b/src/tscore/CMakeLists.txt @@ -100,7 +100,6 @@ add_library(tscore SHARED lockfile.cc runroot.cc signals.cc - ts_file.cc ) add_dependencies(tscore ParseRules tscpputil) target_include_directories(tscore PRIVATE @@ -139,7 +138,6 @@ add_executable(test_tscore unit_tests/test_ink_memory.cc unit_tests/test_layout.cc unit_tests/test_scoped_resource.cc - unit_tests/test_ts_file.cc unit_tests/unit_test_main.cc ) target_link_libraries(test_tscore PRIVATE tscore inkevent records_p libswoc) diff --git a/src/tscore/Makefile.am b/src/tscore/Makefile.am index 8944c45ed6..142d6f63d0 100644 --- a/src/tscore/Makefile.am +++ b/src/tscore/Makefile.am @@ -122,7 +122,6 @@ libtscore_la_SOURCES = \ LogMessage.cc \ Throttler.cc \ Tokenizer.cc \ - ts_file.cc \ Version.cc \ X509HostnameValidator.cc @@ -192,7 +191,6 @@ test_tscore_SOURCES = \ unit_tests/test_scoped_resource.cc \ unit_tests/test_Throttler.cc \ unit_tests/test_Tokenizer.cc \ - unit_tests/test_ts_file.cc \ unit_tests/test_Version.cc \ unit_tests/test_Errata.cc \ unit_tests/test_MMH.cc \ diff --git a/src/tscore/ts_file.cc b/src/tscore/ts_file.cc deleted file mode 100644 index f77faaefe5..0000000000 --- a/src/tscore/ts_file.cc +++ /dev/null @@ -1,344 +0,0 @@ -/** @file - - Minimalist version of std::filesystem. - - @section license License - - Licensed to the Apache Software Foundation (ASF) under one or more contributor license - agreements. See the NOTICE file distributed with this work for additional information regarding - copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 - (the "License"); you may not use this file except in compliance with the License. You may - obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software distributed under the - License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - express or implied. See the License for the specific language governing permissions and - limitations under the License. - */ - -#include "tscore/ts_file.h" -#include <fcntl.h> -#include <sys/types.h> -#include <dirent.h> - -namespace ts -{ -namespace file -{ - path & - path::operator/=(std::string_view that) - { - if (!that.empty()) { // don't waste time appending nothing. - if (that.front() == preferred_separator || _path.empty()) { - _path.assign(that); - } else { - if (_path.back() == preferred_separator) { - _path.reserve(_path.size() + that.size()); - } else { - _path.reserve(_path.size() + that.size() + 1); - _path.push_back(preferred_separator); - } - _path.append(that); - } - } - return *this; - } - - file_status - status(const path &p, std::error_code &ec) noexcept - { - file_status zret; - if (::stat(p.c_str(), &zret._stat) >= 0) { - ec.clear(); - } else { - ec = std::error_code(errno, std::system_category()); - } - return zret; - } - - path - temp_directory_path() - { - /* ISO/IEC 9945 (POSIX): The path supplied by the first environment variable found in the list TMPDIR, TMP, TEMP, TEMPDIR. - * If none of these are found, "/tmp" */ - char const *folder = nullptr; - if ((nullptr == (folder = getenv("TMPDIR"))) && (nullptr == (folder = getenv("TMP"))) && - (nullptr == (folder = getenv("TEMPDIR")))) { - folder = "/tmp"; - } - return path(folder); - } - - path - current_path() - { - char cwd[PATH_MAX]; - if (::getcwd(cwd, sizeof(cwd)) != nullptr) { - return path(cwd); - } - return path(); - } - - path - canonical(const path &p, std::error_code &ec) - { - if (p.empty()) { - ec = std::error_code(EINVAL, std::system_category()); - return path(); - } - - char buf[PATH_MAX + 1]; - char *res = ::realpath(p.c_str(), buf); - if (res) { - ec = std::error_code(); - return path(res); - } - - ec = std::error_code(errno, std::system_category()); - return path(); - } - - path - filename(const path &p) - { - const size_t last_slash_idx = p.string().find_last_of(p.preferred_separator); - return p.string().substr(last_slash_idx + 1); - } - - bool - exists(const path &p) - { - std::error_code ec; - status(p, ec); - return !(ec && ENOENT == ec.value()); - } - - static bool - do_mkdir(const path &p, std::error_code &ec, mode_t mode) - { - struct stat st; - if (stat(p.c_str(), &st) != 0) { - if (mkdir(p.c_str(), mode) != 0 && errno != EEXIST) { - ec = std::error_code(errno, std::system_category()); - return false; - } - } else if (!S_ISDIR(st.st_mode)) { - ec = std::error_code(ENOTDIR, std::system_category()); - return false; - } - return true; - } - - bool - create_directories(const path &p, std::error_code &ec, mode_t mode) noexcept - { - if (p.empty()) { - ec = std::error_code(EINVAL, std::system_category()); - return false; - } - - bool result = false; - ec = std::error_code(); - - size_t pos = 0; - std::string token; - while ((pos = p.string().find_first_of(p.preferred_separator, pos)) != std::string::npos) { - token = p.string().substr(0, pos); - if (!token.empty()) { - result = do_mkdir(path(token), ec, mode); - } - pos = pos + sizeof(p.preferred_separator); - } - - if (result) { - result = do_mkdir(p, ec, mode); - } - return result; - } - - bool - copy(const path &from, const path &to, std::error_code &ec) - { - static int BUF_SIZE = 65536; - FILE *src, *dst; - char buf[BUF_SIZE]; - int bufsize = BUF_SIZE; - - if (from.empty() || to.empty()) { - ec = std::error_code(EINVAL, std::system_category()); - return false; - } - - ec = std::error_code(); - - std::error_code err; - path final_to; - file_status s = status(to, err); - if (!(err && ENOENT == err.value()) && is_dir(s)) { - const auto file = filename(from); - final_to = to / file; - } else { - final_to = to; - } - - if (nullptr == (src = fopen(from.c_str(), "r"))) { - ec = std::error_code(errno, std::system_category()); - return false; - } - if (nullptr == (dst = fopen(final_to.c_str(), "w"))) { - ec = std::error_code(errno, std::system_category()); - fclose(src); - return false; - } - - while (true) { - size_t in = fread(buf, 1, bufsize, src); - if (0 == in) { - break; - } - size_t out = fwrite(buf, 1, in, dst); - if (0 == out) { - break; - } - } - - fclose(src); - fclose(dst); - - return true; - } - - static bool - remove_path(const path &p, std::error_code &ec) - { - DIR *dir; - struct dirent *entry; - bool res = true; - std::error_code err; - - file_status s = status(p, err); - if (err && ENOENT == err.value()) { - // file/dir does not exist - return false; - } else if (is_regular_file(s)) { - // regular file, try to remove it! - if (unlink(p.c_str()) != 0) { - ec = std::error_code(errno, std::system_category()); - res = false; - } - return res; - } else if (!is_dir(s)) { - // not a directory - ec = std::error_code(ENOTDIR, std::system_category()); - return false; - } - - // recursively remove nested files and directories - if (nullptr == (dir = opendir(p.c_str()))) { - ec = std::error_code(errno, std::system_category()); - return false; - } - - while (nullptr != (entry = readdir(dir))) { - if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) { - continue; - } - - remove_path(p / entry->d_name, ec); - } - - if (0 != rmdir(p.c_str())) { - ec = std::error_code(errno, std::system_category()); - } - - closedir(dir); - return true; - } - - bool - remove(const path &p, std::error_code &ec) - { - if (p.empty()) { - ec = std::error_code(EINVAL, std::system_category()); - return false; - } - - ec = std::error_code(); - return remove_path(p, ec); - } // namespace file - - int - file_type(const file_status &fs) - { - return fs._stat.st_mode & S_IFMT; - } - - time_t - modification_time(const file_status &fs) - { - return fs._stat.st_mtime; - } - uintmax_t - file_size(const file_status &fs) - { - return fs._stat.st_size; - } - - bool - is_char_device(const file_status &fs) - { - return file_type(fs) == S_IFCHR; - } - - bool - is_block_device(const file_status &fs) - { - return file_type(fs) == S_IFBLK; - } - - bool - is_regular_file(const file_status &fs) - { - return file_type(fs) == S_IFREG; - } - - bool - is_dir(const file_status &fs) - { - return file_type(fs) == S_IFDIR; - } - - bool - is_readable(const path &p) - { - return 0 == access(p.c_str(), R_OK); - } - - std::string - load(const path &p, std::error_code &ec) - { - std::string zret; - ats_scoped_fd fd(::open(p.c_str(), O_RDONLY)); - ec.clear(); - if (fd < 0) { - ec = std::error_code(errno, std::system_category()); - } else { - struct stat info; - if (0 != ::fstat(fd, &info)) { - ec = std::error_code(errno, std::system_category()); - } else { - int n = info.st_size; - zret.resize(n); - auto read_len = ::read(fd, const_cast<char *>(zret.data()), n); - if (read_len < n) { - ec = std::error_code(errno, std::system_category()); - } - } - } - return zret; - } - -} // namespace file -} // namespace ts diff --git a/src/tscore/unit_tests/test_ts_file.cc b/src/tscore/unit_tests/test_ts_file.cc deleted file mode 100644 index 2c59944684..0000000000 --- a/src/tscore/unit_tests/test_ts_file.cc +++ /dev/null @@ -1,278 +0,0 @@ -/** @file - - ts::file unit tests. - - @section license License - - Licensed to the Apache Software Foundation (ASF) under one - or more contributor license agreements. See the NOTICE file - distributed with this work for additional information - regarding copyright ownership. The ASF licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -#include <iostream> -#include <fstream> /* ofstream */ - -#include "tscore/ts_file.h" -#include "../../../tests/include/catch.hpp" - -using ts::file::path; - -// -------------------- -TEST_CASE("ts_file", "[libts][ts_file]") -{ - path p1("/home"); - REQUIRE(p1.string() == "/home"); - auto p2 = p1 / "bob"; - REQUIRE(p2.string() == "/home/bob"); - p2 = p2 / "git/ats/"; - REQUIRE(p2.string() == "/home/bob/git/ats/"); - p2 /= "lib/ts"; - REQUIRE(p2.string() == "/home/bob/git/ats/lib/ts"); - p2 /= "/home/dave"; - REQUIRE(p2.string() == "/home/dave"); - path p3 = path("/home/dave") / "git/tools"; - REQUIRE(p3.string() == "/home/dave/git/tools"); -} - -TEST_CASE("ts_file_io", "[libts][ts_file_io]") -{ - path file("/etc/hosts"); - std::error_code ec; - std::string content = ts::file::load(file, ec); - REQUIRE(ec.value() == 0); - REQUIRE(content.size() > 0); - REQUIRE(content.find("localhost") != content.npos); - - // Check some file properties. - REQUIRE(ts::file::is_readable(file) == true); - auto fs = ts::file::status(file, ec); - REQUIRE(ec.value() == 0); - REQUIRE(ts::file::is_dir(fs) == false); - REQUIRE(ts::file::is_regular_file(fs) == true); - - // Failure case. - file = "unit-tests/no_such_file.txt"; - content = ts::file::load(file, ec); - REQUIRE(ec.value() == 2); - REQUIRE(ts::file::is_readable(file) == false); -} - -TEST_CASE("ts_file::path::parent_path", "[libts][fs_file]") -{ - CHECK(ts::file::path("/").parent_path() == path("/")); - CHECK(ts::file::path("/absolute/path/file.txt").parent_path() == ts::file::path("/absolute/path")); - CHECK(ts::file::path("/absolute/path/.").parent_path() == ts::file::path("/absolute/path")); - - CHECK(ts::file::path("relative/path/file.txt").parent_path() == ts::file::path("relative/path")); - CHECK(ts::file::path("relative/path/.").parent_path() == ts::file::path("relative/path")); - CHECK(ts::file::path(".").parent_path() == ts::file::path("")); -} - -static std::string -setenvvar(const std::string &name, const std::string &value) -{ - std::string saved; - if (nullptr != getenv(name.c_str())) { - saved.assign(value); - } - - if (!value.empty()) { - setenv(name.c_str(), value.c_str(), 1); - } else { - unsetenv(name.c_str()); - } - - return saved; -} - -TEST_CASE("ts_file::path::temp_directory_path", "[libts][fs_file]") -{ - // Clean all temp dir env variables. - std::string s1 = setenvvar("TMPDIR", std::string()); - std::string s2 = setenvvar("TEMPDIR", std::string()); - std::string s3 = setenvvar("TMP", std::string()); - std::string s; - - // If nothing defined return "/tmp" - CHECK(ts::file::temp_directory_path() == ts::file::path("/tmp")); - - // TMPDIR defined. - s = setenvvar("TMPDIR", "/temp_dirname1"); - CHECK(ts::file::temp_directory_path() == ts::file::path("/temp_dirname1")); - setenvvar("TMPDIR", s); - - // TEMPDIR - s = setenvvar("TEMPDIR", "/temp_dirname"); - CHECK(ts::file::temp_directory_path() == ts::file::path("/temp_dirname")); - // TMP defined, it should take precedence over TEMPDIR. - s = setenvvar("TMP", "/temp_dirname1"); - CHECK(ts::file::temp_directory_path() == ts::file::path("/temp_dirname1")); - // TMPDIR defined, it should take precedence over TMP. - s = setenvvar("TMPDIR", "/temp_dirname2"); - CHECK(ts::file::temp_directory_path() == ts::file::path("/temp_dirname2")); - setenvvar("TMPDIR", s); - setenvvar("TMP", s); - setenvvar("TEMPDIR", s); - - // Restore all temp dir env variables to their previous state. - setenvvar("TMPDIR", s1); - setenvvar("TEMPDIR", s2); - setenvvar("TMP", s3); -} - -TEST_CASE("ts_file::path::create_directories", "[libts][fs_file]") -{ - std::error_code ec; - path tempdir = ts::file::temp_directory_path(); - - CHECK_FALSE(ts::file::create_directories(path(), ec)); - CHECK(ec.value() == EINVAL); - - path testdir1 = tempdir / "dir1"; - CHECK(ts::file::create_directories(testdir1, ec)); - CHECK(ts::file::exists(testdir1)); - - path testdir2 = testdir1 / "dir2"; - CHECK(ts::file::create_directories(testdir1, ec)); - CHECK(ts::file::exists(testdir1)); - - // Cleanup - CHECK(ts::file::remove(testdir1, ec)); - CHECK_FALSE(ts::file::exists(testdir1)); -} - -TEST_CASE("ts_file::path::remove", "[libts][fs_file]") -{ - std::error_code ec; - path tempdir = ts::file::temp_directory_path(); - - CHECK_FALSE(ts::file::remove(path(), ec)); - CHECK(ec.value() == EINVAL); - - path testdir1 = tempdir / "dir1"; - path testdir2 = testdir1 / "dir2"; - path file1 = testdir2 / "test.txt"; - - // Simple creation and removal of a directory /tmp/dir1 - CHECK(ts::file::create_directories(testdir1, ec)); - CHECK(ts::file::exists(testdir1)); - CHECK(ts::file::remove(testdir1, ec)); - CHECK_FALSE(ts::file::exists(testdir1)); - - // Create /tmp/dir1/dir2 and remove /tmp/dir1/dir2 => /tmp/dir1 should exist - CHECK(ts::file::create_directories(testdir2, ec)); - CHECK(ts::file::remove(testdir2, ec)); - CHECK(ts::file::exists(testdir1)); - - // Create a file, remove it, test if exists and then attempting to remove it again should fail. - CHECK(ts::file::create_directories(testdir2, ec)); - std::ofstream file(file1.string()); - file << "Simple test file"; - file.close(); - CHECK(ts::file::exists(file1)); - CHECK(ts::file::remove(file1, ec)); - CHECK_FALSE(ts::file::exists(file1)); - CHECK_FALSE(ts::file::remove(file1, ec)); - - // Clean up. - CHECK(ts::file::remove(testdir1, ec)); - CHECK_FALSE(ts::file::exists(testdir1)); -} - -TEST_CASE("ts_file::path::canonical", "[libts][fs_file]") -{ - std::error_code ec; - path tempdir = ts::file::canonical(ts::file::temp_directory_path(), ec); - path testdir1 = tempdir / "dir1"; - path testdir2 = testdir1 / "dir2"; - path testdir3 = testdir2 / "dir3"; - path unorthodox = testdir3 / path("..") / path("..") / "dir2"; - - // Invalid empty path. - CHECK(path() == ts::file::canonical(path(), ec)); - CHECK(ec.value() == EINVAL); - - // Fail if directory does not exist - CHECK(path() == ts::file::canonical(unorthodox, ec)); - CHECK(ec.value() == ENOENT); - - // Create the dir3 and test again - CHECK(create_directories(testdir3, ec)); - CHECK(ts::file::exists(testdir3)); - CHECK(ts::file::exists(testdir2)); - CHECK(ts::file::exists(testdir1)); - CHECK(ts::file::exists(unorthodox)); - CHECK(ts::file::canonical(unorthodox, ec) == testdir2); - CHECK(ec.value() == 0); - - // Cleanup - CHECK(ts::file::remove(testdir1, ec)); - CHECK_FALSE(ts::file::exists(testdir1)); -} - -TEST_CASE("ts_file::path::filename", "[libts][fs_file]") -{ - CHECK(ts::file::filename(path("/foo/bar.txt")) == path("bar.txt")); - CHECK(ts::file::filename(path("/foo/.bar")) == path(".bar")); - CHECK(ts::file::filename(path("/foo/bar")) == path("bar")); - CHECK(ts::file::filename(path("/foo/bar/")) == path("")); - CHECK(ts::file::filename(path("/foo/.")) == path(".")); - CHECK(ts::file::filename(path("/foo/..")) == path("..")); - CHECK(ts::file::filename(path("/foo/../bar")) == path("bar")); - CHECK(ts::file::filename(path("/foo/../bar/")) == path("")); - CHECK(ts::file::filename(path(".")) == path(".")); - CHECK(ts::file::filename(path("..")) == path("..")); - CHECK(ts::file::filename(path("/")) == path("")); - CHECK(ts::file::filename(path("//host")) == path("host")); -} - -TEST_CASE("ts_file::path::copy", "[libts][fs_file]") -{ - std::error_code ec; - path tempdir = ts::file::temp_directory_path(); - path testdir1 = tempdir / "dir1"; - path testdir2 = testdir1 / "dir2"; - path file1 = testdir2 / "test1.txt"; - path file2 = testdir2 / "test2.txt"; - - // Invalid empty path, both to and from parameters. - CHECK_FALSE(ts::file::copy(path(), path(), ec)); - CHECK(ec.value() == EINVAL); - - CHECK(ts::file::create_directories(testdir2, ec)); - std::ofstream file(file1.string()); - file << "Simple test file"; - file.close(); - CHECK(ts::file::exists(file1)); - - // Invalid empty path, now from parameter is ok but to is empty - CHECK_FALSE(ts::file::copy(file1, path(), ec)); - CHECK(ec.value() == EINVAL); - - // successful copy: "to" is directory - CHECK(ts::file::copy(file1, testdir2, ec)); - CHECK(ec.value() == 0); - - // successful copy: "to" is file - CHECK(ts::file::copy(file1, file2, ec)); - CHECK(ec.value() == 0); - - // Compare the content - CHECK(ts::file::load(file1, ec) == ts::file::load(file2, ec)); - - // Cleanup - CHECK(ts::file::remove(testdir1, ec)); - CHECK_FALSE(ts::file::exists(testdir1)); -} diff --git a/tests/gold_tests/jsonrpc/plugins/jsonrpc_plugin_handler_test.cc b/tests/gold_tests/jsonrpc/plugins/jsonrpc_plugin_handler_test.cc index eec8b07adb..10de46a26a 100644 --- a/tests/gold_tests/jsonrpc/plugins/jsonrpc_plugin_handler_test.cc +++ b/tests/gold_tests/jsonrpc/plugins/jsonrpc_plugin_handler_test.cc @@ -26,8 +26,9 @@ #include <algorithm> #include <fstream> +#include "swoc/swoc_file.h" + #include "yaml-cpp/yaml.h" -#include "tscore/ts_file.h" #include "tscore/Errata.h" #include "tscore/I_Layout.h" #include "tscore/BufferWriter.h" @@ -117,7 +118,7 @@ struct HostItem { int CB_handle_rpc_io_call(TSCont contp, TSEvent event, void *data) { - namespace fs = ts::file; + namespace fs = swoc::file; TSDebug(PLUGIN_NAME, "Working on the update now"); YAML::Node params = *static_cast<YAML::Node *>(TSContDataGet(contp)); @@ -160,10 +161,10 @@ CB_handle_rpc_io_call(TSCont contp, TSEvent event, void *data) // Basic stuffs here. // We open the file if exist, we update/add the host in the structure. For simplicity we do not delete anything. - fs::path sandbox = ts::file::current_path(); + fs::path sandbox = fs::current_path(); fs::path dumpFile = sandbox / "my_test_plugin_dump.yaml"; bool newFile{false}; - if (!ts::file::exists(dumpFile)) { + if (!fs::exists(dumpFile)) { newFile = true; }