Use HAS_STD_FILESYSTEM_PATH in filesystem.h (#950)

This commit is contained in:
An Tao 2021-07-25 23:17:32 +08:00 committed by GitHub
parent 64f9f8b87f
commit da87c124ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 21 additions and 51 deletions

View File

@ -139,6 +139,7 @@ if (DROGON_CXX_STANDARD EQUAL 14)
target_link_libraries(${PROJECT_NAME} PUBLIC Boost::boost)
target_link_libraries(${PROJECT_NAME} PUBLIC Boost::filesystem)
list(APPEND INCLUDE_DIRS_FOR_DYNAMIC_VIEW ${Boost_INCLUDE_DIR})
option(HAS_STD_FILESYSTEM_PATH "use boost::filesystem" OFF)
elseif (DROGON_CXX_STANDARD EQUAL 17)
# With C++17, use Boost if std::filesystem::path is missing
message(STATUS "use c++17")
@ -153,6 +154,7 @@ elseif (DROGON_CXX_STANDARD EQUAL 17)
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
target_link_libraries(${PROJECT_NAME} PUBLIC stdc++fs)
endif()
option(HAS_STD_FILESYSTEM_PATH "use std::filesystem" ON)
else()
find_package(Boost 1.49.0 COMPONENTS filesystem system REQUIRED)
message(STATUS "Using Boost filesytem::path")
@ -161,9 +163,11 @@ elseif (DROGON_CXX_STANDARD EQUAL 17)
message(STATUS "Boost libraries: " ${Boost_LIBRARIES})
target_link_libraries(${PROJECT_NAME} PUBLIC Boost::filesystem Boost::system)
list(APPEND INCLUDE_DIRS_FOR_DYNAMIC_VIEW ${Boost_INCLUDE_DIR})
option(HAS_STD_FILESYSTEM_PATH "use boost::filesystem" OFF)
endif()
else ()
message(STATUS "use c++20")
option(HAS_STD_FILESYSTEM_PATH "use std::filesystem" ON)
endif ()
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules/)

View File

@ -4,6 +4,7 @@
#cmakedefine01 LIBPQ_SUPPORTS_BATCH_MODE
#cmakedefine01 USE_MYSQL
#cmakedefine01 USE_SQLITE3
#cmakedefine01 HAS_STD_FILESYSTEM_PATH
#cmakedefine OpenSSL_FOUND
#cmakedefine Boost_FOUND

View File

@ -14,17 +14,10 @@
#include "HttpFileImpl.h"
#include "HttpAppFrameworkImpl.h"
#include "filesystem.h"
#include <drogon/MultiPart.h>
#include <fstream>
#include <iostream>
// Switch between native c++17 or boost for c++14
#ifdef HAS_STD_FILESYSTEM_PATH
#include <system_error>
namespace stl = std;
#else // HAS_STD_FILESYSTEM_PATH
#include <boost/system/error_code.hpp>
namespace stl = boost::system;
#endif // HAS_STD_FILESYSTEM_PATH
using namespace drogon;
@ -64,7 +57,7 @@ int HttpFileImpl::saveAs(const std::string &fileName) const
}
if (fsFileName.has_parent_path())
{
stl::error_code err;
drogon::error_code err;
filesystem::create_directories(fsFileName.parent_path(), err);
if (err)
return -1;

View File

@ -23,12 +23,6 @@
#include <cstdio>
#include <sys/stat.h>
#include <trantor/utils/Logger.h>
// Switch between native c++17 or boost for c++14
#ifdef HAS_STD_FILESYSTEM_PATH
namespace stl = std;
#else
namespace stl = boost::system;
#endif
using namespace trantor;
using namespace drogon;
@ -350,7 +344,7 @@ void HttpResponseImpl::makeHeaderString(trantor::MsgBuffer &buffer)
}
else
{
stl::error_code err;
drogon::error_code err;
filesystem::path fsSendfile(utils::toNativePath(sendfileName_));
auto fileSize = filesystem::file_size(fsSendfile, err);
if (err)

View File

@ -30,13 +30,6 @@
#include <sys/stat.h>
// Switch between native c++17 or boost for c++14
#include "filesystem.h"
#ifdef HAS_STD_FILESYSTEM_PATH
#include <system_error>
namespace stl = std;
#else
#include <boost/system/error_code.hpp>
namespace stl = boost::system;
#endif
using namespace drogon;
@ -147,7 +140,7 @@ void StaticFileRouter::route(
location.realLocation_ +
std::string{restOfThePath.data(), restOfThePath.length()};
filesystem::path fsFilePath(utils::toNativePath(filePath));
stl::error_code err;
drogon::error_code err;
if (!filesystem::exists(fsFilePath, err))
{
defaultHandler_(req, std::move(callback));
@ -226,7 +219,7 @@ void StaticFileRouter::route(
std::string directoryPath =
HttpAppFrameworkImpl::instance().getDocumentRoot() + path;
filesystem::path fsDirectoryPath(utils::toNativePath(directoryPath));
stl::error_code err;
drogon::error_code err;
if (filesystem::exists(fsDirectoryPath, err))
{
if (filesystem::is_directory(fsDirectoryPath, err))
@ -379,7 +372,7 @@ void StaticFileRouter::sendStaticFileResponse(
if (!fileExists)
{
filesystem::path fsFilePath(utils::toNativePath(filePath));
stl::error_code err;
drogon::error_code err;
if (!filesystem::exists(fsFilePath, err) ||
!filesystem::is_regular_file(fsFilePath, err))
{
@ -402,7 +395,7 @@ void StaticFileRouter::sendStaticFileResponse(
// Find compressed file first.
auto brFileName = filePath + ".br";
filesystem::path fsBrFile(utils::toNativePath(brFileName));
stl::error_code err;
drogon::error_code err;
if (filesystem::exists(fsBrFile, err) &&
filesystem::is_regular_file(fsBrFile, err))
{
@ -419,7 +412,7 @@ void StaticFileRouter::sendStaticFileResponse(
// Find compressed file first.
auto gzipFileName = filePath + ".gz";
filesystem::path fsGzipFile(utils::toNativePath(gzipFileName));
stl::error_code err;
drogon::error_code err;
if (filesystem::exists(fsGzipFile, err) &&
filesystem::is_regular_file(fsGzipFile, err))
{

View File

@ -52,15 +52,6 @@
#include <fcntl.h>
#include <stdarg.h>
// Switch between native c++17 or boost for c++14
#ifdef HAS_STD_FILESYSTEM_PATH
#include <system_error>
namespace stl = std;
#else
#include <boost/system/error_code.hpp>
namespace stl = boost::system;
#endif
#ifdef _WIN32
char *strptime(const char *s, const char *f, struct tm *tm)
{
@ -1061,7 +1052,7 @@ int createPath(const std::string &path)
if (osPath.back() != filesystem::path::preferred_separator)
osPath.push_back(filesystem::path::preferred_separator);
filesystem::path fsPath(osPath);
stl::error_code err;
drogon::error_code err;
filesystem::create_directories(fsPath, err);
if (err)
{

View File

@ -11,31 +11,25 @@
#pragma once
/* Check of std::filesystem::path availability:
* - OS X: depends on the target OSX version (>= 10.15 Catalina)
* - Windows: Visual Studio >= 2019 (c++20)
* - Others: should already have it in c++17
*/
#if (defined(__APPLE__) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 101500L) || \
(defined(_WIN32) && defined(_MSC_VER) && _MSC_VER >= 1920) || \
(!defined(__APPLE__) && !defined(_WIN32) && __cplusplus >= 201703L)
#define HAS_STD_FILESYSTEM_PATH
#endif
#include <drogon/config.h>
#include <trantor/utils/LogStream.h>
#ifdef HAS_STD_FILESYSTEM_PATH
#if HAS_STD_FILESYSTEM_PATH
#include <filesystem>
#include <system_error>
#else
#include <boost/filesystem.hpp>
#include <boost/system/error_code.hpp>
#endif
namespace drogon
{
#ifdef HAS_STD_FILESYSTEM_PATH
#if HAS_STD_FILESYSTEM_PATH
namespace filesystem = std::filesystem;
using std::error_code;
#else
namespace filesystem = boost::filesystem;
using boost::system::error_code;
#endif
} // namespace drogon