Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace boost::filesystem usage with std::filesystem #169

Merged
merged 11 commits into from
Mar 23, 2023
16 changes: 11 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ option (DO_INCLUDE_SDK "Build subproject sdk-cpp" OFF)
option (DO_BUILD_TESTS "Set DO_BUILD_TESTS to OFF to skip building tests." ON)
option (DO_BUILD_FOR_SNAP "Enable DO Snap build option" OFF)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Get verbose output from cmake generation and build steps
set(CMAKE_VERBOSE_MAKEFILE ON)

Expand Down Expand Up @@ -62,6 +58,16 @@ else()
message(FATAL_ERROR "Unknown platform for client")
endif()

# C++17 is required to use std::filesystem but Edge builds on Windows is not ready to move up yet.
# Luckily, we do not need std::filesystem on Windows.
if (DO_INCLUDE_SDK AND DO_PLATFORM_WINDOWS)
set(CMAKE_CXX_STANDARD 14)
else ()
set(CMAKE_CXX_STANDARD 17)
endif ()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# PIC (Position Independent Code) ensures .a files can be linked to executables that have PIE enabled
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

Expand All @@ -85,7 +91,7 @@ if (DO_PLATFORM_LINUX)
# not relevant for our usage or they are symlinks to /usr/lib and /usr/bin.
if (NOT CMAKE_PREFIX_PATH)
set(CMAKE_PREFIX_PATH "/usr")
endif ()
endif ()
endif (DO_PLATFORM_LINUX)

if (DO_PLATFORM_WINDOWS AND DO_INCLUDE_SDK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ steps:
inputs:
targetType: 'filePath'
filePath: 'build/scripts/check_binary_size.sh'
arguments: '363928 /tmp/build-deliveryoptimization-agent/linux-${{parameters.config}}/client-lite/deliveryoptimization-agent'
arguments: '523672 /tmp/build-deliveryoptimization-agent/linux-${{parameters.config}}/client-lite/deliveryoptimization-agent'
displayName: 'Limit binary size increase'

- task: CmdLine@2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ steps:
inputs:
targetType: 'filePath'
filePath: 'build/scripts/check_binary_size.sh'
arguments: '431184 /tmp/build-deliveryoptimization-sdk/linux-${{parameters.config}}/sdk-cpp/libdeliveryoptimization.so.*.*.?'
arguments: '596080 /tmp/build-deliveryoptimization-sdk/linux-${{parameters.config}}/sdk-cpp/libdeliveryoptimization.so.*.*.?'
displayName: 'Limit binary size increase'

- task: CmdLine@2
Expand Down
2 changes: 1 addition & 1 deletion build/scripts/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function installBuildDependencies
apt-get install -y make build-essential g++ gdb gdbserver gcc git wget
apt-get install -y python3 ninja-build
apt-get install -y cmake libmsgsl-dev
apt-get install -y libboost-system-dev libboost-filesystem-dev libboost-program-options-dev
apt-get install -y libboost-program-options-dev
apt-get install -y libproxy-dev libssl-dev uuid-dev libcurl4-openssl-dev

rm -rf /tmp/gtest
Expand Down
1 change: 0 additions & 1 deletion build/scripts/install-vcpkg-deps.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ git checkout 2021.05.12
.\vcpkg integrate install
.\vcpkg update
.\vcpkg install gtest:x64-windows
.\vcpkg install boost-filesystem:x64-windows
.\vcpkg install boost-program-options:x64-windows
3 changes: 0 additions & 3 deletions build/scripts/install-vcpkg-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ git checkout 2021.05.12
./vcpkg integrate install

./vcpkg install ms-gsl
./vcpkg install boost-log
./vcpkg install boost-beast
./vcpkg install boost-iostreams
./vcpkg install boost-uuid
./vcpkg install boost-program-options
./vcpkg install gtest
Expand Down
18 changes: 10 additions & 8 deletions client-lite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ project (${DOSVC_BIN_NAME} VERSION 1.0.0)

option (DO_PROXY_SUPPORT "Set DO_PROXY_SUPPORT to OFF to turn off proxy support for downloads and thus remove dependency on libproxy." ON)

add_definitions(-DBOOST_ALL_DYN_LINK=1)

# -Wno-noexcept-type, the offending function is SetResultLoggingCallback, this warning is fixed in C++17 because exception specification
# is part of a function type. Since the offending function is not public when compiled into docs_common just add the compiler flag here
# to disable the warning.
Expand All @@ -29,7 +27,7 @@ function (target_link_dl_lib target)
endfunction ()

# Include external libraries here:
find_package(Boost COMPONENTS filesystem REQUIRED)
find_package(Boost REQUIRED)
find_package(CURL REQUIRED)
# g++ requires explicit specification of the thread library to be used
find_package(Threads REQUIRED)
Expand All @@ -46,6 +44,8 @@ if (GSL_INCLUDE_DIR STREQUAL "GSL_INCLUDE_DIR-NOTFOUND")
message(FATAL_ERROR "Could not find MS Guidelines Support Library.")
endif()

try_set_filesystem_lib()

set (docs_common_includes
${include_directories_for_arm}
${GSL_INCLUDE_DIR}
Expand Down Expand Up @@ -90,13 +90,15 @@ endif ()

add_do_version_lib(${PROJECT_NAME} ${PROJECT_VERSION})

# Build product files into a lib for use by other targets
# Build product files into a lib for use by other targets.
add_library(docs_common STATIC ${files_docs_common})
target_compile_definitions(docs_common
PRIVATE DO_CONFIG_DIRECTORY_PATH="${docs_svc_config_dir_path}"
DO_AGENT_LOG_DIRECTORY_PATH="${docs_svc_log_dir_path}"
DO_RUN_DIRECTORY_PATH="${docs_svc_run_dir_path}"
PRIVATE
DO_CONFIG_DIRECTORY_PATH="${docs_svc_config_dir_path}"
DO_AGENT_LOG_DIRECTORY_PATH="${docs_svc_log_dir_path}"
DO_RUN_DIRECTORY_PATH="${docs_svc_run_dir_path}"
)
add_boost_definitions(docs_common PUBLIC)
if (DO_DEV_DEBUG)
target_compile_definitions(docs_common PUBLIC DO_DEV_DEBUG)
endif ()
Expand Down Expand Up @@ -124,8 +126,8 @@ add_platform_interface_definitions(${DOSVC_BIN_NAME})
target_include_directories(${DOSVC_BIN_NAME} PRIVATE ${docs_common_includes})
target_link_libraries(${DOSVC_BIN_NAME}
docs_common
${Boost_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
${CXX_FILESYSTEM_LIBS}
)

if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
Expand Down
30 changes: 30 additions & 0 deletions client-lite/src/include/do_filesystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#ifndef _DELIVERY_OPTIMIZATION_DO_FILESYSTEM_H
#define _DELIVERY_OPTIMIZATION_DO_FILESYSTEM_H

#if defined(__cpp_lib_filesystem)
#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 0
#elif defined(__cpp_lib_experimental_filesystem)
#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1
#elif !defined(__has_include)
// Cannot check if headers exist, assume experimental
#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1
#elif __has_include(<filesystem>)
#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 0
#elif __has_include(<experimental/filesystem>)
#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1
#else
#error Could not find system header "<filesystem>" or "<experimental/filesystem>"
#endif

#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif

#endif // _DELIVERY_OPTIMIZATION_DO_FILESYSTEM_H
8 changes: 4 additions & 4 deletions client-lite/src/ipc/rest_port_advertiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <fcntl.h> // open, write
#include <unistd.h> // getpid
#include <sstream>
#include <boost/filesystem.hpp>
#include <gsl/gsl_util>
#include "do_filesystem.h"
#include "do_persistence.h"
#include "error_macros.h"

Expand Down Expand Up @@ -60,13 +60,13 @@ class RestPortAdvertiser
void _DeleteOlderPortFiles() try
{
auto& runtimeDirectory = docli::GetRuntimeDirectory();
for (boost::filesystem::directory_iterator itr(runtimeDirectory); itr != boost::filesystem::directory_iterator(); ++itr)
for (fs::directory_iterator itr(runtimeDirectory); itr != fs::directory_iterator(); ++itr)
{
auto& dirEntry = itr->path();
if (dirEntry.filename().string().find(_restPortFileNamePrefix) != std::string::npos)
{
boost::system::error_code ec;
boost::filesystem::remove(dirEntry, ec);
std::error_code ec;
fs::remove(dirEntry, ec);
if (ec)
{
DoLogWarning("Failed to delete old port file (%d, %s) %s", ec.value(), ec.message().data(), dirEntry.string().data());
Expand Down
6 changes: 3 additions & 3 deletions client-lite/src/util/do_json_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "do_common.h"
#include "do_json_parser.h"

#include <boost/filesystem.hpp>
#include "do_filesystem.h"
#include <boost/property_tree/json_parser.hpp>

std::chrono::seconds JsonParser::RefreshInterval = std::chrono::seconds(60);
Expand All @@ -13,7 +13,7 @@ std::chrono::seconds JsonParser::RefreshInterval = std::chrono::seconds(60);
JsonParser::JsonParser(const std::string& jsonFilePath, bool alwaysCreateFile) :
_jsonFilePath(jsonFilePath)
{
if (alwaysCreateFile && !(boost::filesystem::exists(_jsonFilePath)))
if (alwaysCreateFile && !(fs::exists(_jsonFilePath)))
{
DoLogInfo("json file not found at %s, creating file", _jsonFilePath.data());
boost::property_tree::ptree json;
Expand All @@ -33,7 +33,7 @@ void JsonParser::_TryRefresh(bool force)
return;
}

if (boost::filesystem::exists(_jsonFilePath))
if (fs::exists(_jsonFilePath))
{
try
{
Expand Down
9 changes: 4 additions & 5 deletions client-lite/src/util/proc_launch_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
#include <pwd.h>
#include <grp.h>

#include <boost/filesystem.hpp>

#include "do_common.h"
#include "do_filesystem.h"
#include "do_persistence.h"

inline gid_t GetGroupIdByName(const char *name)
Expand Down Expand Up @@ -54,11 +53,11 @@ inline void SetDOPathPermissions(const std::string& path, mode_t mode)

inline void InitializePath(const std::string& path, mode_t mode = 0) try
{
boost::filesystem::path dirPath(path);
if (!boost::filesystem::exists(dirPath))
fs::path dirPath(path);
if (!fs::exists(dirPath))
{
DoLogInfo("Creating directories for %s", path.c_str());
boost::filesystem::create_directories(dirPath);
fs::create_directories(dirPath);

if (mode != 0)
{
Expand Down
10 changes: 3 additions & 7 deletions client-lite/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

# Unit tests for DOCS

# Only need program_options here but have to include others required by docs_common.
# TODO: docs_common should declare what it needs via target_link_libraries(PUBLIC).
find_package(Boost COMPONENTS filesystem program_options REQUIRED)
find_package(Boost COMPONENTS program_options REQUIRED)
find_package(GTest REQUIRED)

file (GLOB files_docs_tests
*.cpp)
add_executable (deliveryoptimization-agent-tests ${files_docs_tests})
add_executable(deliveryoptimization-agent-tests ${files_docs_tests})
add_platform_interface_definitions(deliveryoptimization-agent-tests)
target_link_libraries(deliveryoptimization-agent-tests
docs_common
dotestutil
stdc++fs
${Boost_LIBRARIES}
GTest::GTest
${CXX_FILESYSTEM_LIBS}
)
4 changes: 2 additions & 2 deletions client-lite/test/do_log_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ TEST_F(DOLoggerTests, BasicWriteToFile)
DOLog::Close();

UINT nLogFilesFound = 0;
for (cppfs::recursive_directory_iterator itr(g_testTempDir); itr != cppfs::recursive_directory_iterator{}; ++itr)
for (fs::recursive_directory_iterator itr(g_testTempDir); itr != fs::recursive_directory_iterator{}; ++itr)
{
++nLogFilesFound;
const auto filePath = itr->path();
ASSERT_NE(strstr(filePath.c_str(), "do-agent."), nullptr);
ASSERT_GT(cppfs::file_size(filePath), 0);
ASSERT_GT(fs::file_size(filePath), 0);

std::ifstream fs;
fs.open(filePath.string());
Expand Down
4 changes: 2 additions & 2 deletions client-lite/test/docs_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <boost/program_options.hpp>
#include "test_data.h"

const cppfs::path g_testTempDir = "/tmp/docs_test_scratch";
const fs::path g_testTempDir = "/tmp/docs_test_scratch";

int main(int argc, char** argv)
{
Expand All @@ -15,7 +15,7 @@ int main(int argc, char** argv)
// TODO(shishirb) enable console only logging

std::error_code ec;
cppfs::create_directories(g_testTempDir, ec);
fs::create_directories(g_testTempDir, ec);
if (ec)
{
printf("Failed to create test dir: %s\n", g_testTempDir.string().data());
Expand Down
2 changes: 1 addition & 1 deletion client-lite/test/download_manager_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ TEST_F(DownloadManagerTests, FileDownloadFatal404)
ASSERT_EQ(status.BytesTotal, 0);
VerifyError(status, HTTP_E_STATUS_NOT_FOUND);
VerifyDownloadHttpStatus(*DownloadForId(manager, id), 404);
ASSERT_TRUE(cppfs::exists(destFile));
ASSERT_TRUE(fs::exists(destFile));
manager.AbortDownload(id);
VerifyFileNotFound(destFile);
}
Expand Down
12 changes: 6 additions & 6 deletions client-lite/test/json_parser_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

const cppfs::path g_jsonTestFilePath = g_testTempDir / "docs_config.json";
const fs::path g_jsonTestFilePath = g_testTempDir / "docs_config.json";

const std::map<std::string, std::string> g_testData =
{
Expand Down Expand Up @@ -70,9 +70,9 @@ class JsonParserTests : public ::testing::Test
void SetUp() override
{
JsonParser::RefreshInterval = std::chrono::seconds(10); // for faster test times
if (cppfs::exists(g_jsonTestFilePath))
if (fs::exists(g_jsonTestFilePath))
{
cppfs::remove(g_jsonTestFilePath);
fs::remove(g_jsonTestFilePath);
}
}
};
Expand Down Expand Up @@ -111,7 +111,7 @@ TEST_F(JsonParserTests, ReadConfigsWithUpdates)
VerifyItemFound(reader, "newkey", "newvalue");

// Delete config file and verify
cppfs::remove(g_jsonTestFilePath);
fs::remove(g_jsonTestFilePath);
VerifyItemFound(reader, "newkey", "newvalue"); // within refresh interval
VerifyTestData(reader, true);

Expand All @@ -122,9 +122,9 @@ TEST_F(JsonParserTests, ReadConfigsWithUpdates)

TEST_F(JsonParserTests, ReadConfigsWithFileCreatedLater)
{
if (cppfs::exists(g_jsonTestFilePath))
if (fs::exists(g_jsonTestFilePath))
{
cppfs::remove(g_jsonTestFilePath);
fs::remove(g_jsonTestFilePath);
}

JsonParser reader(g_jsonTestFilePath);
Expand Down
Loading