add a cmake option to switch between std and boost filesystem

This commit is contained in:
Chunting Gu
2021-05-27 11:15:08 +08:00
parent 39719c4ded
commit df96f969bc
31 changed files with 227 additions and 195 deletions
+7 -11
View File
@@ -1,7 +1,5 @@
#include "webcc/body.h"
#include "boost/filesystem/operations.hpp"
#include "webcc/logger.h"
#include "webcc/utility.h"
@@ -9,8 +7,6 @@
#include "webcc/gzip.h"
#endif
namespace bfs = boost::filesystem;
namespace webcc {
// -----------------------------------------------------------------------------
@@ -157,7 +153,7 @@ void FormBody::Free(std::size_t index) {
// -----------------------------------------------------------------------------
FileBody::FileBody(const bfs::path& path, std::size_t chunk_size)
FileBody::FileBody(const fs::path& path, std::size_t chunk_size)
: path_(path), chunk_size_(chunk_size), auto_delete_(false), size_(0) {
size_ = utility::TellSize(path_);
if (size_ == kInvalidLength) {
@@ -165,15 +161,15 @@ FileBody::FileBody(const bfs::path& path, std::size_t chunk_size)
}
}
FileBody::FileBody(const bfs::path& path, bool auto_delete)
FileBody::FileBody(const fs::path& path, bool auto_delete)
: path_(path), chunk_size_(0), auto_delete_(auto_delete), size_(0) {
// Don't need to tell file size.
}
FileBody::~FileBody() {
if (auto_delete_ && !path_.empty()) {
boost::system::error_code ec;
bfs::remove(path_, ec);
fs::error_code ec;
fs::remove(path_, ec);
if (ec) {
LOG_ERRO("Failed to remove file (%s).", ec.message().c_str());
}
@@ -209,7 +205,7 @@ void FileBody::Dump(std::ostream& os, const std::string& prefix) const {
os << prefix << "<file: " << path_.string() << ">" << std::endl;
}
bool FileBody::Move(const bfs::path& new_path) {
bool FileBody::Move(const fs::path& new_path) {
if (path_ == new_path) {
return false;
}
@@ -218,8 +214,8 @@ bool FileBody::Move(const bfs::path& new_path) {
ifstream_.close();
}
boost::system::error_code ec;
bfs::rename(path_, new_path, ec);
fs::error_code ec;
fs::rename(path_, new_path, ec);
if (ec) {
LOG_ERRO("Failed to rename file (%s).", ec.message().c_str());
+8 -11
View File
@@ -1,15 +1,12 @@
#ifndef WEBCC_BODY_H_
#define WEBCC_BODY_H_
#include <fstream>
#include <memory>
#include <string>
#include <utility>
#include "boost/filesystem/fstream.hpp"
#include "boost/filesystem/path.hpp"
#include "webcc/common.h"
#include "webcc/fs.h"
namespace webcc {
@@ -154,14 +151,14 @@ private:
class FileBody : public Body {
public:
// For message to be sent out.
FileBody(const boost::filesystem::path& path, std::size_t chunk_size);
FileBody(const fs::path& path, std::size_t chunk_size);
// For message received.
// No |chunk_size| is needed since you don't iterate the payload of a
// received message.
// If |auto_delete| is true, the file will be deleted on destructor unless it
// is moved to another path (see Move()).
FileBody(const boost::filesystem::path& path, bool auto_delete = false);
FileBody(const fs::path& path, bool auto_delete = false);
~FileBody() override;
@@ -175,7 +172,7 @@ public:
void Dump(std::ostream& os, const std::string& prefix) const override;
const boost::filesystem::path& path() const {
const fs::path& path() const {
return path_;
}
@@ -188,17 +185,17 @@ public:
// If |new_path| resolves to an existing non-directory file, it is removed.
// If |new_path| resolves to an existing directory, it is removed if empty
// on ISO/IEC 9945 but is an error on Windows.
// See boost::filesystem::rename() for more details.
bool Move(const boost::filesystem::path& new_path);
// See fs::rename() for more details.
bool Move(const fs::path& new_path);
private:
boost::filesystem::path path_;
fs::path path_;
std::size_t chunk_size_;
bool auto_delete_;
std::size_t size_; // File size in bytes
boost::filesystem::ifstream ifstream_;
fs::ifstream ifstream_;
std::string chunk_;
};
+1 -3
View File
@@ -8,8 +8,6 @@
#include "webcc/string.h"
#include "webcc/utility.h"
namespace bfs = boost::filesystem;
namespace webcc {
// -----------------------------------------------------------------------------
@@ -194,7 +192,7 @@ FormPartPtr FormPart::New(const std::string& name, std::string&& data,
return form_part;
}
FormPartPtr FormPart::NewFile(const std::string& name, const bfs::path& path,
FormPartPtr FormPart::NewFile(const std::string& name, const fs::path& path,
const std::string& media_type) {
auto form_part = std::make_shared<FormPart>();
+3 -5
View File
@@ -6,8 +6,7 @@
#include <utility>
#include <vector>
#include "boost/filesystem/path.hpp"
#include "webcc/fs.h"
#include "webcc/globals.h"
namespace webcc {
@@ -158,8 +157,7 @@ public:
// Construct a file part.
// The file name will be extracted from path.
// The media type, if not provided, will be inferred from file extension.
static FormPartPtr NewFile(const std::string& name,
const boost::filesystem::path& path,
static FormPartPtr NewFile(const std::string& name, const fs::path& path,
const std::string& media_type = "");
// API: SERVER
@@ -230,7 +228,7 @@ private:
std::string name_;
// The path of the file to post.
boost::filesystem::path path_;
fs::path path_;
// The original local file name.
// E.g., "baby.jpg".
+3
View File
@@ -17,4 +17,7 @@
// Set 1/0 to enable/disable GZIP compression.
#define WEBCC_ENABLE_GZIP 0
// Set 1 to use C++17 filesystem or 0 to use Boost filesystem.
#define WEBCC_USE_STD_FILESYSTEM 1
#endif // WEBCC_CONFIG_H_
+3
View File
@@ -19,4 +19,7 @@
// Set 1/0 to enable/disable GZIP compression.
#define WEBCC_ENABLE_GZIP @WEBCC_ENABLE_GZIP@
// Set 1 to use C++17 filesystem or 0 to use Boost filesystem.
#define WEBCC_USE_STD_FILESYSTEM @WEBCC_USE_STD_FILESYSTEM@
#endif // WEBCC_CONFIG_H_
+65
View File
@@ -0,0 +1,65 @@
#ifndef WEBCC_FS_H_
#define WEBCC_FS_H_
// Use std or boost filesystem according to config.
#include "webcc/config.h" // for WEBCC_USE_STD_FILESYSTEM
#if WEBCC_USE_STD_FILESYSTEM
#include <filesystem>
#include <fstream>
#else
#include "boost/filesystem/fstream.hpp"
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
#endif // WEBCC_USE_STD_FILESYSTEM
namespace webcc {
namespace fs {
#if WEBCC_USE_STD_FILESYSTEM
// types
using std::error_code;
using std::ifstream;
using std::ofstream;
using std::filesystem::path;
using std::filesystem::filesystem_error;
// functions
using std::filesystem::rename;
using std::filesystem::remove;
using std::filesystem::exists;
using std::filesystem::is_directory;
using std::filesystem::is_regular_file;
using std::filesystem::create_directory;
using std::filesystem::create_directories;
using std::filesystem::current_path;
using std::filesystem::temp_directory_path;
#else
// types
using boost::system::error_code;
using boost::filesystem::ifstream;
using boost::filesystem::ofstream;
using boost::filesystem::path;
using boost::filesystem::filesystem_error;
// functions
using boost::filesystem::rename;
using boost::filesystem::remove;
using boost::filesystem::exists;
using boost::filesystem::is_directory;
using boost::filesystem::is_regular_file;
using boost::filesystem::create_directory;
using boost::filesystem::create_directories;
using boost::filesystem::current_path;
using boost::filesystem::temp_directory_path;
#endif // WEBCC_USE_STD_FILESYSTEM
} // namespace fs
} // namespace webcc
#endif // WEBCC_FS_H_
+2
View File
@@ -126,6 +126,8 @@ namespace media_types {
const char* const kApplicationJson = "application/json";
const char* const kApplicationSoapXml = "application/soap+xml";
const char* const kApplicationFormUrlEncoded =
"application/x-www-form-urlencoded";
const char* const kTextPlain = "text/plain";
const char* const kTextXml = "text/xml";
+9 -13
View File
@@ -18,11 +18,7 @@
// For getting thread ID.
#include <sys/syscall.h>
#include <sys/types.h>
#endif
#include "boost/filesystem/operations.hpp"
namespace bfs = boost::filesystem;
#endif // defined(_WIN32) || defined(_WIN64)
namespace webcc {
@@ -36,7 +32,7 @@ static const char* kLevelNames[] = {
// -----------------------------------------------------------------------------
static FILE* FOpen(const bfs::path& path, bool overwrite) {
static FILE* FOpen(const fs::path& path, bool overwrite) {
#if (defined(_WIN32) || defined(_WIN64))
return _wfopen(path.wstring().c_str(), overwrite ? L"w+" : L"a+");
#else
@@ -53,7 +49,7 @@ struct Logger {
}
}
void Init(const bfs::path& path, int _modes) {
void Init(const fs::path& path, int _modes) {
modes = _modes;
// Create log file only if necessary.
@@ -155,14 +151,14 @@ static std::string GetThreadID() {
return thread_id;
}
static bfs::path InitLogPath(const bfs::path& dir) {
static fs::path InitLogPath(const fs::path& dir) {
if (dir.empty()) {
return bfs::current_path() / WEBCC_LOG_FILE_NAME;
return fs::current_path() / WEBCC_LOG_FILE_NAME;
}
boost::system::error_code ec;
if (!bfs::exists(dir, ec) || !bfs::is_directory(dir, ec)) {
if (!bfs::create_directories(dir, ec) || ec) {
fs::error_code ec;
if (!fs::exists(dir, ec) || !fs::is_directory(dir, ec)) {
if (!fs::create_directories(dir, ec) || ec) {
return {};
}
}
@@ -170,7 +166,7 @@ static bfs::path InitLogPath(const bfs::path& dir) {
return (dir / WEBCC_LOG_FILE_NAME);
}
void LogInit(const bfs::path& dir, int modes) {
void LogInit(const fs::path& dir, int modes) {
// Suppose this is called from the main thread.
g_main_thread_id = DoGetThreadID();
+2 -2
View File
@@ -9,7 +9,7 @@
#include <cstring> // for strrchr()
#include <string>
#include "boost/filesystem/path.hpp"
#include "webcc/fs.h"
// Log levels.
// VERB is similar to DEBUG commonly used by other projects.
@@ -44,7 +44,7 @@ const int LOG_FILE_OVERWRITE = LOG_FILE | LOG_OVERWRITE;
// Initialize logger.
// If |dir| is empty, log file will be generated in current directory.
void LogInit(const boost::filesystem::path& dir, int modes);
void LogInit(const fs::path& dir, int modes);
void Log(int level, const char* file, int line, const char* format, ...);
+2 -5
View File
@@ -1,7 +1,6 @@
#include "webcc/parser.h"
#include "boost/algorithm/string.hpp"
#include "boost/filesystem/operations.hpp"
#include "webcc/logger.h"
#include "webcc/message.h"
@@ -12,8 +11,6 @@
#include "webcc/gzip.h"
#endif
namespace bfs = boost::filesystem;
namespace webcc {
// -----------------------------------------------------------------------------
@@ -70,7 +67,7 @@ bool StringBodyHandler::Finish() {
bool FileBodyHandler::OpenFile() {
try {
temp_path_ = bfs::temp_directory_path();
temp_path_ = fs::temp_directory_path();
// Generate a random string as file name.
// A replacement of boost::filesystem::unique_path().
@@ -79,7 +76,7 @@ bool FileBodyHandler::OpenFile() {
LOG_VERB("Generate a temp path for streaming: %s",
temp_path_.string().c_str());
} catch (const bfs::filesystem_error&) {
} catch (const fs::filesystem_error&) {
LOG_ERRO("Failed to generate temp path for streaming");
return false;
}
+3 -5
View File
@@ -4,10 +4,8 @@
#include <fstream>
#include <string>
#include "boost/filesystem/fstream.hpp"
#include "boost/filesystem/path.hpp"
#include "webcc/common.h"
#include "webcc/fs.h"
#include "webcc/globals.h"
namespace webcc {
@@ -84,8 +82,8 @@ public:
private:
std::size_t streamed_size_ = 0;
boost::filesystem::ofstream ofstream_;
boost::filesystem::path temp_path_;
fs::ofstream ofstream_;
fs::path temp_path_;
};
// -----------------------------------------------------------------------------
+2 -4
View File
@@ -9,8 +9,6 @@
#include "webcc/gzip.h"
#endif
namespace bfs = boost::filesystem;
namespace webcc {
RequestPtr RequestBuilder::operator()() {
@@ -67,7 +65,7 @@ RequestBuilder& RequestBuilder::AcceptGzip(bool gzip) {
#endif // WEBCC_ENABLE_GZIP
RequestBuilder& RequestBuilder::File(const bfs::path& path,
RequestBuilder& RequestBuilder::File(const fs::path& path,
bool infer_media_type,
std::size_t chunk_size) {
body_.reset(new FileBody{ path, chunk_size });
@@ -80,7 +78,7 @@ RequestBuilder& RequestBuilder::File(const bfs::path& path,
}
RequestBuilder& RequestBuilder::FormFile(const std::string& name,
const bfs::path& path,
const fs::path& path,
const std::string& media_type) {
assert(!name.empty());
return Form(FormPart::NewFile(name, path, media_type));
+19 -18
View File
@@ -4,26 +4,29 @@
#include <string>
#include <vector>
#include "boost/filesystem/path.hpp"
#include "webcc/fs.h"
#include "webcc/request.h"
#include "webcc/url.h"
// -----------------------------------------------------------------------------
// Handy macros for creating a RequestBuilder.
#define WEBCC_GET(url) webcc::RequestBuilder{}.Get(url, false)
#define WEBCC_GET_ENC(url) webcc::RequestBuilder{}.Get(url, true)
#define WEBCC_HEAD(url) webcc::RequestBuilder{}.Head(url, false)
#define WEBCC_HEAD_ENC(url) webcc::RequestBuilder{}.Head(url, true)
#define WEBCC_POST(url) webcc::RequestBuilder{}.Post(url, false)
#define WEBCC_POST_ENC(url) webcc::RequestBuilder{}.Post(url, true)
#define WEBCC_PUT(url) webcc::RequestBuilder{}.Put(url, false)
#define WEBCC_PUT_ENC(url) webcc::RequestBuilder{}.Put(url, true)
#define WEBCC_DELETE(url) webcc::RequestBuilder{}.Delete(url, false)
#define WEBCC_DELETE_ENC(url) webcc::RequestBuilder{}.Delete(url, true)
#define WEBCC_PATCH(url) webcc::RequestBuilder{}.Patch(url, false)
#define WEBCC_PATCH_ENC(url) webcc::RequestBuilder{}.Patch(url, true)
#define WEBCC_RB webcc::RequestBuilder{}
// clang-format off
#define WEBCC_GET(url) WEBCC_RB.Get(url, false)
#define WEBCC_GET_ENC(url) WEBCC_RB.Get(url, true)
#define WEBCC_HEAD(url) WEBCC_RB.Head(url, false)
#define WEBCC_HEAD_ENC(url) WEBCC_RB.Head(url, true)
#define WEBCC_POST(url) WEBCC_RB.Post(url, false)
#define WEBCC_POST_ENC(url) WEBCC_RB.Post(url, true)
#define WEBCC_PUT(url) WEBCC_RB.Put(url, false)
#define WEBCC_PUT_ENC(url) WEBCC_RB.Put(url, true)
#define WEBCC_DELETE(url) WEBCC_RB.Delete(url, false)
#define WEBCC_DELETE_ENC(url) WEBCC_RB.Delete(url, true)
#define WEBCC_PATCH(url) WEBCC_RB.Patch(url, false)
#define WEBCC_PATCH_ENC(url) WEBCC_RB.Patch(url, true)
// clang-format on
// -----------------------------------------------------------------------------
@@ -143,8 +146,7 @@ public:
// Use the file content as body.
// NOTE: Error::kFileError might be thrown.
RequestBuilder& File(const boost::filesystem::path& path,
bool infer_media_type = true,
RequestBuilder& File(const fs::path& path, bool infer_media_type = true,
std::size_t chunk_size = 1024);
// Add a form part.
@@ -154,8 +156,7 @@ public:
}
// Add a form part of file.
RequestBuilder& FormFile(const std::string& name,
const boost::filesystem::path& path,
RequestBuilder& FormFile(const std::string& name, const fs::path& path,
const std::string& media_type = "");
// Add a form part of string data.
+1 -3
View File
@@ -8,8 +8,6 @@
#include "webcc/gzip.h"
#endif
namespace bfs = boost::filesystem;
namespace webcc {
ResponsePtr ResponseBuilder::operator()() {
@@ -45,7 +43,7 @@ ResponsePtr ResponseBuilder::operator()() {
return response;
}
ResponseBuilder& ResponseBuilder::File(const bfs::path& path,
ResponseBuilder& ResponseBuilder::File(const fs::path& path,
bool infer_media_type,
std::size_t chunk_size) {
body_.reset(new FileBody{ path, chunk_size });
+2 -4
View File
@@ -4,8 +4,7 @@
#include <string>
#include <vector>
#include "boost/filesystem/path.hpp"
#include "webcc/fs.h"
#include "webcc/request.h"
#include "webcc/response.h"
@@ -96,8 +95,7 @@ public:
// Use the file content as body.
// NOTE: Error::kFileError might be thrown.
ResponseBuilder& File(const boost::filesystem::path& path,
bool infer_media_type = true,
ResponseBuilder& File(const fs::path& path, bool infer_media_type = true,
std::size_t chunk_size = 1024);
ResponseBuilder& Header(const std::string& key, const std::string& value) {
+10 -10
View File
@@ -9,16 +9,16 @@ namespace webcc {
// -----------------------------------------------------------------------------
namespace {
void SplitStartLine(const std::string& line, std::vector<std::string>* parts) {
const char SPACE = ' ';
// Split HTTP response status line to three parts.
// Don't use the general split function because the reason part might also
// contain spaces.
static void SplitStatusLine(const std::string& line,
std::vector<std::string>* parts) {
std::size_t off = 0;
std::size_t pos = 0;
for (std::size_t i = 0; i < 2; ++i) {
pos = line.find(SPACE, off);
pos = line.find(' ', off);
if (pos == std::string::npos) {
break;
}
@@ -26,7 +26,9 @@ void SplitStartLine(const std::string& line, std::vector<std::string>* parts) {
parts->push_back(line.substr(off, pos - off));
off = pos + 1;
for (; off < line.size() && line[off] == SPACE; ++off) {
// Skip spaces
while (off < line.size() && line[off] == ' ') {
++off;
}
}
@@ -35,8 +37,6 @@ void SplitStartLine(const std::string& line, std::vector<std::string>* parts) {
}
}
} // namespace
// -----------------------------------------------------------------------------
void ResponseParser::Init(Response* response, bool stream) {
@@ -48,7 +48,7 @@ void ResponseParser::Init(Response* response, bool stream) {
bool ResponseParser::ParseStartLine(const std::string& line) {
std::vector<std::string> parts;
SplitStartLine(line, &parts);
SplitStatusLine(line, &parts);
if (parts.size() < 2) {
LOG_ERRO("Invalid HTTP response status line: %s", line.c_str());
+5 -9
View File
@@ -4,22 +4,18 @@
#include <fstream>
#include <utility>
#include "boost/filesystem/operations.hpp"
#include "webcc/body.h"
#include "webcc/logger.h"
#include "webcc/request.h"
#include "webcc/response.h"
#include "webcc/utility.h"
namespace bfs = boost::filesystem;
using tcp = boost::asio::ip::tcp;
namespace webcc {
Server::Server(boost::asio::ip::tcp protocol, std::uint16_t port,
const bfs::path& doc_root)
const fs::path& doc_root)
: protocol_(protocol),
port_(port),
doc_root_(doc_root),
@@ -304,10 +300,10 @@ bool Server::MatchViewOrStatic(const std::string& method,
// Try to match a static file.
if (method == methods::kGet && !doc_root_.empty()) {
bfs::path path = doc_root_ / url;
fs::path path = doc_root_ / url;
boost::system::error_code ec;
if (!bfs::is_directory(path, ec) && bfs::exists(path, ec)) {
fs::error_code ec;
if (!fs::is_directory(path, ec) && fs::exists(path, ec)) {
return true;
}
}
@@ -323,7 +319,7 @@ ResponsePtr Server::ServeStatic(RequestPtr request) {
return {};
}
bfs::path path = doc_root_ / request->url().path();
fs::path path = doc_root_ / request->url().path();
try {
// NOTE: FileBody might throw Error::kFileError.
+3 -4
View File
@@ -5,14 +5,13 @@
#include <thread>
#include <vector>
#include "boost/filesystem/path.hpp"
#include "boost/asio/io_context.hpp"
#include "boost/asio/ip/tcp.hpp"
#include "boost/asio/signal_set.hpp"
#include "webcc/connection.h"
#include "webcc/connection_pool.h"
#include "webcc/fs.h"
#include "webcc/queue.h"
#include "webcc/router.h"
#include "webcc/url.h"
@@ -22,7 +21,7 @@ namespace webcc {
class Server : public Router {
public:
Server(boost::asio::ip::tcp protocol, std::uint16_t port,
const boost::filesystem::path& doc_root = {});
const fs::path& doc_root = {});
Server(const Server&) = delete;
Server& operator=(const Server&) = delete;
@@ -108,7 +107,7 @@ private:
std::uint16_t port_ = 0;
// The directory with the static files to be served.
boost::filesystem::path doc_root_;
fs::path doc_root_;
// The size of the buffer for reading request.
std::size_t buffer_size_ = kBufferSize;
+4 -8
View File
@@ -7,13 +7,9 @@
#include <iostream>
#include <sstream>
#include "boost/filesystem/fstream.hpp"
#include "webcc/string.h"
#include "webcc/version.h"
namespace bfs = boost::filesystem;
namespace webcc {
namespace utility {
@@ -32,18 +28,18 @@ std::string HttpDate() {
return date.str();
}
std::size_t TellSize(const bfs::path& path) {
std::size_t TellSize(const fs::path& path) {
// Flag "ate": seek to the end of stream immediately after open.
bfs::ifstream stream{ path, std::ios::binary | std::ios::ate };
fs::ifstream stream{ path, std::ios::binary | std::ios::ate };
if (stream.fail()) {
return kInvalidLength;
}
return static_cast<std::size_t>(stream.tellg());
}
bool ReadFile(const bfs::path& path, std::string* output) {
bool ReadFile(const fs::path& path, std::string* output) {
// Flag "ate": seek to the end of stream immediately after open.
bfs::ifstream stream{ path, std::ios::binary | std::ios::ate };
fs::ifstream stream{ path, std::ios::binary | std::ios::ate };
if (stream.fail()) {
return false;
}
+3 -3
View File
@@ -5,8 +5,8 @@
#include <string>
#include "boost/asio/ip/tcp.hpp"
#include "boost/filesystem/path.hpp"
#include "webcc/fs.h"
#include "webcc/globals.h"
namespace webcc {
@@ -22,10 +22,10 @@ std::string HttpDate();
// Tell the size in bytes of the given file.
// Return kInvalidLength (-1) on failure.
std::size_t TellSize(const boost::filesystem::path& path);
std::size_t TellSize(const fs::path& path);
// Read entire file into string.
bool ReadFile(const boost::filesystem::path& path, std::string* output);
bool ReadFile(const fs::path& path, std::string* output);
// Dump the string data line by line to achieve more readability.
// Also limit the maximum size of the data to be dumped.