Skip to content

Commit

Permalink
Replace 'std::filesystem' with 'fs' for cleaner code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackarain committed Oct 25, 2023
1 parent c3fc4c9 commit c3b1ad5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
18 changes: 10 additions & 8 deletions proxy/include/proxy/fileop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,24 @@

namespace fileop {

namespace fs = std::filesystem;

template<typename T>
concept ByteType = std::same_as<T, uint8_t> || std::same_as<T, char>;

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<ByteType T>
Expand Down Expand Up @@ -100,18 +102,18 @@ namespace fileop {


template<ByteType T>
std::streamsize read(const std::filesystem::path& file, std::span<T> val)
std::streamsize read(const fs::path& file, std::span<T> 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);
Expand Down Expand Up @@ -148,7 +150,7 @@ namespace fileop {


template<ByteType T>
std::streamsize write(const std::filesystem::path& file, std::span<T> val)
std::streamsize write(const fs::path& file, std::span<T> val)
{
details::create_parent_directories(file);

Expand All @@ -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);

Expand Down
26 changes: 14 additions & 12 deletions proxy/include/proxy/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ namespace std {

namespace util {

namespace fs = std::filesystem;

#ifndef LOGGING_DISABLE_BOOST_ASIO_ENDPOINT
namespace net = boost::asio;
#endif
Expand Down Expand Up @@ -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__()
Expand All @@ -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);
}

Expand Down Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -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
Expand All @@ -809,7 +811,7 @@ class auto_logger_file__
return;
}

std::filesystem::remove(fn, ignore_ec);
fs::remove(fn, ignore_ec);
});
th.detach();
#endif
Expand All @@ -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 };
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 12 additions & 15 deletions proxy/include/proxy/proxy_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ namespace proxy {

namespace urls = boost::urls; // form <boost/url.hpp>

namespace fs = std::filesystem;

using string_body = http::string_body;
using dynamic_body = http::dynamic_body;
using buffer_body = http::buffer_body;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -2035,8 +2037,6 @@ namespace proxy {

inline net::awaitable<void> normal_web_server(http::request<http::string_body>& req)
{
namespace fs = std::filesystem;

boost::system::error_code ec;

beast::flat_buffer buffer;
Expand Down Expand Up @@ -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;
Expand All @@ -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
};
Expand All @@ -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 =
Expand Down Expand Up @@ -2353,8 +2352,6 @@ namespace proxy {
inline net::awaitable<void> on_http_get(
const http_context& hctx, std::string filename = "")
{
namespace fs = std::filesystem;

static std::map<std::string, std::string> mimes =
{
{ ".html", "text/html; charset=utf-8" },
Expand Down Expand Up @@ -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;
Expand All @@ -2843,22 +2840,22 @@ 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
{
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;
Expand Down
5 changes: 3 additions & 2 deletions server/proxy_server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -159,7 +160,7 @@ inline std::optional<int> 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"))
{
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c3b1ad5

Please sign in to comment.