From c3b1ad5776234fb61c8a8907ca4960a6c957a716 Mon Sep 17 00:00:00 2001 From: jackarain Date: Wed, 25 Oct 2023 16:57:09 +0800 Subject: [PATCH] Replace 'std::filesystem' with 'fs' for cleaner code. --- proxy/include/proxy/fileop.hpp | 18 ++++++++++-------- proxy/include/proxy/logging.hpp | 26 ++++++++++++++------------ proxy/include/proxy/proxy_server.hpp | 27 ++++++++++++--------------- server/proxy_server/main.cpp | 5 +++-- 4 files changed, 39 insertions(+), 37 deletions(-) diff --git a/proxy/include/proxy/fileop.hpp b/proxy/include/proxy/fileop.hpp index 5aaf5fcae9..464156f722 100644 --- a/proxy/include/proxy/fileop.hpp +++ b/proxy/include/proxy/fileop.hpp @@ -22,22 +22,24 @@ namespace fileop { + namespace fs = std::filesystem; + template concept ByteType = std::same_as || std::same_as; namespace details { - inline void create_parent_directories(const std::filesystem::path& p) + inline void create_parent_directories(const fs::path& p) { std::error_code ec; - if (std::filesystem::exists(p, ec)) + if (fs::exists(p, ec)) return; if (ec) return; - std::filesystem::create_directories(p.parent_path(), ec); + fs::create_directories(p.parent_path(), ec); } template @@ -100,18 +102,18 @@ namespace fileop { template - std::streamsize read(const std::filesystem::path& file, std::span val) + std::streamsize read(const fs::path& file, std::span val) { std::fstream f(file, std::ios_base::binary | std::ios_base::in); return details::read(*f.rdbuf(), val); } inline std::streamsize - read(const std::filesystem::path& file, std::string& val) + read(const fs::path& file, std::string& val) { std::fstream f(file, std::ios_base::binary | std::ios_base::in); std::error_code ec; - auto fsize = std::filesystem::file_size(file, ec); + auto fsize = fs::file_size(file, ec); if (ec) return -1; val.resize(fsize); @@ -148,7 +150,7 @@ namespace fileop { template - std::streamsize write(const std::filesystem::path& file, std::span val) + std::streamsize write(const fs::path& file, std::span val) { details::create_parent_directories(file); @@ -161,7 +163,7 @@ namespace fileop { } inline std::streamsize - write(const std::filesystem::path& file, std::string_view val) + write(const fs::path& file, std::string_view val) { details::create_parent_directories(file); diff --git a/proxy/include/proxy/logging.hpp b/proxy/include/proxy/logging.hpp index b37e893b19..1b701ddb14 100644 --- a/proxy/include/proxy/logging.hpp +++ b/proxy/include/proxy/logging.hpp @@ -175,6 +175,8 @@ namespace std { namespace util { + namespace fs = std::filesystem; + #ifndef LOGGING_DISABLE_BOOST_ASIO_ENDPOINT namespace net = boost::asio; #endif @@ -699,8 +701,8 @@ class auto_logger_file__ return; std::error_code ignore_ec; - if (!std::filesystem::exists(m_log_path, ignore_ec)) - std::filesystem::create_directories( + if (!fs::exists(m_log_path, ignore_ec)) + fs::create_directories( m_log_path.parent_path(), ignore_ec); } ~auto_logger_file__() @@ -718,8 +720,8 @@ class auto_logger_file__ return; std::error_code ignore_ec; - if (!std::filesystem::exists(m_log_path, ignore_ec)) - std::filesystem::create_directories( + if (!fs::exists(m_log_path, ignore_ec)) + fs::create_directories( m_log_path.parent_path(), ignore_ec); } @@ -761,8 +763,8 @@ class auto_logger_file__ m_ofstream->close(); m_ofstream.reset(); - auto logpath = std::filesystem::path(m_log_path.parent_path()); - std::filesystem::path filename; + auto logpath = fs::path(m_log_path.parent_path()); + fs::path filename; if constexpr (LOG_MAXFILE_SIZE <= 0) { auto logfile = std::format("{:04d}{:02d}{:02d}-{:02d}.log", @@ -784,10 +786,10 @@ class auto_logger_file__ m_last_time = time; std::error_code ec; - if (!std::filesystem::copy_file(m_log_path, filename, ec)) + if (!fs::copy_file(m_log_path, filename, ec)) break; - std::filesystem::resize_file(m_log_path, 0, ec); + fs::resize_file(m_log_path, 0, ec); m_log_size = 0; #ifdef LOGGING_ENABLE_COMPRESS_LOGS @@ -800,7 +802,7 @@ class auto_logger_file__ if (!logging_compress__::do_compress_gz(fn)) { auto file = fn + logging_compress__::LOGGING_GZ_SUFFIX; - std::filesystem::remove(file, ignore_ec); + fs::remove(file, ignore_ec); if (ignore_ec) std::cerr << "delete log failed: " << file @@ -809,7 +811,7 @@ class auto_logger_file__ return; } - std::filesystem::remove(fn, ignore_ec); + fs::remove(fn, ignore_ec); }); th.detach(); #endif @@ -832,7 +834,7 @@ class auto_logger_file__ } private: - std::filesystem::path m_log_path{"./logs"}; + fs::path m_log_path{"./logs"}; ofstream_ptr m_ofstream; int64_t m_last_time{ -1 }; std::size_t m_log_size{ 0 }; @@ -1601,7 +1603,7 @@ class logger___ return *this; } #endif - inline logger___& operator<<(const std::filesystem::path& p) noexcept + inline logger___& operator<<(const fs::path& p) noexcept { if (!global_logging___) return *this; diff --git a/proxy/include/proxy/proxy_server.hpp b/proxy/include/proxy/proxy_server.hpp index 115b1b5595..d40246a2d9 100644 --- a/proxy/include/proxy/proxy_server.hpp +++ b/proxy/include/proxy/proxy_server.hpp @@ -81,6 +81,8 @@ namespace proxy { namespace urls = boost::urls; // form + namespace fs = std::filesystem; + using string_body = http::string_body; using dynamic_body = http::dynamic_body; using buffer_body = http::buffer_body; @@ -1843,7 +1845,7 @@ namespace proxy { if (m_option.proxy_pass_use_ssl_) { // 设置 ssl cert 证书目录. - if (std::filesystem::exists(m_option.ssl_cert_path_)) + if (fs::exists(m_option.ssl_cert_path_)) { m_ssl_context.add_verify_path( m_option.ssl_cert_path_, ec); @@ -2035,8 +2037,6 @@ namespace proxy { inline net::awaitable normal_web_server(http::request& req) { - namespace fs = std::filesystem; - boost::system::error_code ec; beast::flat_buffer buffer; @@ -2141,7 +2141,7 @@ namespace proxy { co_return; } - inline std::filesystem::path path_cat( + inline fs::path path_cat( const std::wstring& doc, const std::wstring& target) { size_t start_pos = 0; @@ -2163,11 +2163,11 @@ namespace proxy { if (doc.back() == L'/' || doc.back() == L'\\') slash = L""; - return std::filesystem::path(doc + slash + std::wstring(sv)); + return fs::path(doc + slash + std::wstring(sv)); #else if (doc.back() == L'/') slash = L""; - return std::filesystem::path( + return fs::path( boost::nowide::narrow(doc + slash + std::wstring(sv))); #endif // WIN32 }; @@ -2176,7 +2176,6 @@ namespace proxy { const http_context& hctx) { using namespace std::literals; - namespace fs = std::filesystem; namespace chrono = std::chrono; constexpr static auto head_fmt = @@ -2353,8 +2352,6 @@ namespace proxy { inline net::awaitable on_http_get( const http_context& hctx, std::string filename = "") { - namespace fs = std::filesystem; - static std::map mimes = { { ".html", "text/html; charset=utf-8" }, @@ -2827,10 +2824,10 @@ Content-Length: 0 if (!m_option.ssl_cert_path_.empty()) { - auto dir = std::filesystem::path(m_option.ssl_cert_path_); + auto dir = fs::path(m_option.ssl_cert_path_); auto pwd = dir / "ssl_crt.pwd"; - if (std::filesystem::exists(pwd)) + if (fs::exists(pwd)) m_ssl_context.set_password_callback( [&pwd]([[maybe_unused]] auto... args) { std::string password; @@ -2843,14 +2840,14 @@ Content-Length: 0 auto key = dir / "ssl_key.pem"; auto dh = dir / "ssl_dh.pem"; - if (std::filesystem::exists(cert)) + if (fs::exists(cert)) m_ssl_context.use_certificate_chain_file(cert.string()); - if (std::filesystem::exists(key)) + if (fs::exists(key)) m_ssl_context.use_private_key_file( key.string(), boost::asio::ssl::context::pem); - if (std::filesystem::exists(dh)) + if (fs::exists(dh)) m_ssl_context.use_tmp_dh_file(dh.string()); } else @@ -2858,7 +2855,7 @@ Content-Length: 0 m_ssl_context.set_password_callback( [&]([[maybe_unused]] auto... args) { const auto& pwd = m_option.ssl_certificate_passwd_; - if (!std::filesystem::exists(pwd)) + if (!fs::exists(pwd)) return pwd; std::string password; diff --git a/server/proxy_server/main.cpp b/server/proxy_server/main.cpp index 63107c13e5..42bf5893f1 100644 --- a/server/proxy_server/main.cpp +++ b/server/proxy_server/main.cpp @@ -25,6 +25,7 @@ namespace po = boost::program_options; #include "main.hpp" namespace net = boost::asio; +namespace fs = std::filesystem; using net::ip::tcp; using namespace proxy; @@ -159,7 +160,7 @@ inline std::optional try_as_int(const boost::any& var) inline void print_args(int argc, char** argv, const po::variables_map& vm) { LOG_INFO << "Current directory: " - << std::filesystem::current_path().string(); + << fs::current_path().string(); if (!vm.count("config")) { @@ -260,7 +261,7 @@ int main(int argc, char** argv) if (vm.count("config")) { - if (!std::filesystem::exists(config)) + if (!fs::exists(config)) { std::cerr << "No such config file: " << config << std::endl; return EXIT_FAILURE;