Skip to content

Commit

Permalink
runtime: Replace boost::filesystem with std::filesystem
Browse files Browse the repository at this point in the history
BREAKING CHANGES:

- xdg_ functions take and return std::filesystem::path
  instead of boost::filesystem::path.

- Command takes a std::filesystem::path
  instead of boost::filesystem::path.

For the most part, all you need to do is change the namespace
to migrate.
  • Loading branch information
cassava committed Jun 3, 2024
1 parent e51bd7c commit eb2d595
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 83 deletions.
15 changes: 7 additions & 8 deletions runtime/include/cloe/utility/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@

#pragma once

#include <string> // for string
#include <vector> // for vector<>
#include <filesystem> // for filesystem::path
#include <string> // for string
#include <vector> // for vector<>

#include <boost/filesystem/path.hpp> // for path
#include <fable/schema.hpp> // for Schema, Struct, Variant
#include <fable/schema/boost_path.hpp> // for make_schema, Path
#include <fable/schema.hpp> // for Schema, Struct, Variant

#include <cloe/core.hpp> // for Confable, Schema

Expand Down Expand Up @@ -77,7 +76,7 @@ class Command : public Confable {
});
}

Command(boost::filesystem::path executable, std::initializer_list<std::string> args) {
Command(const std::filesystem::path& executable, std::initializer_list<std::string> args) {
from_conf(Json{
{"executable", executable.native()},
{"args", args},
Expand All @@ -88,7 +87,7 @@ class Command : public Confable {
/**
* Return the executable.
*/
boost::filesystem::path executable() const { return executable_; }
std::filesystem::path executable() const { return executable_; }

/**
* Return the executable arguments.
Expand Down Expand Up @@ -159,7 +158,7 @@ class Command : public Confable {
void from_conf(const Conf& c) override;

private:
boost::filesystem::path executable_;
std::filesystem::path executable_;
std::vector<std::string> args_;
std::string command_;
Mode mode_ = Mode::Sync;
Expand Down
83 changes: 38 additions & 45 deletions runtime/include/cloe/utility/xdg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@

#pragma once

#include <filesystem> // for path
#include <functional> // for function
#include <string> // for string
#include <vector> // for vector<>

#include <boost/filesystem/path.hpp> // for path, operator/

namespace cloe {
namespace utility {
namespace cloe::utility {

/**
* This enum class contains all the exceptions that can be thrown.
Expand Down Expand Up @@ -69,30 +67,28 @@ enum class XdgError {
// The following xdg_* functions are implementation functions, and aren't meant
// to be used directly.
#ifdef __linux__
boost::filesystem::path xdg_temp_dir();
std::filesystem::path xdg_temp_dir();
#endif

boost::filesystem::path xdg_getenv_path(const std::string& env);
boost::filesystem::path xdg_path(const std::string& env,
const boost::filesystem::path& default_path);
std::vector<boost::filesystem::path> xdg_paths(const std::string& env,
const std::string& default_paths);
std::filesystem::path xdg_getenv_path(const std::string& env);
std::filesystem::path xdg_path(const std::string& env, const std::filesystem::path& default_path);
std::vector<std::filesystem::path> xdg_paths(const std::string& env,
const std::string& default_paths);

/*
* Make sure to ask `is_empty()` on the result.
*/
boost::filesystem::path xdg_find(const boost::filesystem::path& file,
const std::vector<boost::filesystem::path>& dirs);
std::filesystem::path xdg_find(const std::filesystem::path& file,
const std::vector<std::filesystem::path>& dirs);

std::vector<boost::filesystem::path> xdg_findall(const boost::filesystem::path& file,
const std::vector<boost::filesystem::path>& dirs);
std::vector<std::filesystem::path> xdg_findall(const std::filesystem::path& file,
const std::vector<std::filesystem::path>& dirs);

void xdg_merge(const boost::filesystem::path& file,
const std::vector<boost::filesystem::path>& dirs, bool reverse,
std::function<bool(const boost::filesystem::path&)> mergefn);
void xdg_merge(const std::filesystem::path& file, const std::vector<std::filesystem::path>& dirs,
bool reverse, const std::function<bool(const std::filesystem::path&)>& mergefn);

/// User configuration base directory, e.g., `~./config`.
inline boost::filesystem::path config_home() {
inline std::filesystem::path config_home() {
#ifdef __linux__
return xdg_path("XDG_CONFIG_HOME", "~/.config");
#elif WIN32
Expand All @@ -101,7 +97,7 @@ inline boost::filesystem::path config_home() {
}

/// User data files base directory, e.g., `~/.local/share`.
inline boost::filesystem::path data_home() {
inline std::filesystem::path data_home() {
#ifdef __linux__
return xdg_path("XDG_DATA_HOME", "~/.local/share");
#elif WIN32
Expand All @@ -110,7 +106,7 @@ inline boost::filesystem::path data_home() {
}

/// User cache files base directory, e.g., `~/.cache`.
inline boost::filesystem::path cache_home() {
inline std::filesystem::path cache_home() {
#ifdef __linux__
return xdg_path("XDG_CACHE_HOME", "~/.cache");
#elif WIN32
Expand All @@ -119,7 +115,7 @@ inline boost::filesystem::path cache_home() {
}

/// User runtime files base directory, e.g., `/run/user/1000`
inline boost::filesystem::path runtime_dir() {
inline std::filesystem::path runtime_dir() {
#ifdef __linux__
return xdg_path("XDG_RUNTIME_DIR", xdg_temp_dir());
#elif WIN32
Expand All @@ -128,7 +124,7 @@ inline boost::filesystem::path runtime_dir() {
}

/// Global configuration directories, e.g., `/etc/xdg`.
inline std::vector<boost::filesystem::path> config_dirs() {
inline std::vector<std::filesystem::path> config_dirs() {
#ifdef __linux__
return xdg_paths("XDG_CONFIG_DIRS", "/etc/xdg");
#elif WIN32
Expand All @@ -137,7 +133,7 @@ inline std::vector<boost::filesystem::path> config_dirs() {
}

/// Global data files directories, e.g., `/usr/local/share`.
inline std::vector<boost::filesystem::path> data_dirs() {
inline std::vector<std::filesystem::path> data_dirs() {
#ifdef __linux__
return xdg_paths("XDG_DATA_DIRS", "/usr/local/share:/usr/share");
#elif WIN32
Expand All @@ -146,14 +142,14 @@ inline std::vector<boost::filesystem::path> data_dirs() {
}

/// User and global configuration directories
inline std::vector<boost::filesystem::path> all_config_dirs() {
inline std::vector<std::filesystem::path> all_config_dirs() {
auto xs = config_dirs();
xs.insert(xs.begin(), config_home());
return xs;
}

/// User and global data directories
inline std::vector<boost::filesystem::path> all_data_dirs() {
inline std::vector<std::filesystem::path> all_data_dirs() {
auto xs = data_dirs();
xs.insert(xs.begin(), data_home());
return xs;
Expand All @@ -164,16 +160,16 @@ inline std::vector<boost::filesystem::path> all_data_dirs() {
*
* This is useful if you want to create a file.
*/
inline boost::filesystem::path user_config(const boost::filesystem::path& file) {
inline std::filesystem::path user_config(const std::filesystem::path& file) {
return config_home() / file;
}
inline boost::filesystem::path user_data(const boost::filesystem::path& file) {
inline std::filesystem::path user_data(const std::filesystem::path& file) {
return data_home() / file;
}
inline boost::filesystem::path user_cache(const boost::filesystem::path& file) {
inline std::filesystem::path user_cache(const std::filesystem::path& file) {
return cache_home() / file;
}
inline boost::filesystem::path user_runtime(const boost::filesystem::path& file) {
inline std::filesystem::path user_runtime(const std::filesystem::path& file) {
return runtime_dir() / file;
}

Expand All @@ -183,17 +179,17 @@ inline boost::filesystem::path user_runtime(const boost::filesystem::path& file)
*
* This is useful if you want to read a file.
*/
inline boost::filesystem::path find_config(const boost::filesystem::path& file) {
inline std::filesystem::path find_config(const std::filesystem::path& file) {
return xdg_find(file, all_config_dirs());
}
inline boost::filesystem::path find_data(const boost::filesystem::path& file) {
inline std::filesystem::path find_data(const std::filesystem::path& file) {
return xdg_find(file, all_data_dirs());
}
inline boost::filesystem::path find_cache(const boost::filesystem::path& file) {
return xdg_find(file, std::vector<boost::filesystem::path>{cache_home()});
inline std::filesystem::path find_cache(const std::filesystem::path& file) {
return xdg_find(file, std::vector<std::filesystem::path>{cache_home()});
}
inline boost::filesystem::path find_runtime(const boost::filesystem::path& file) {
return xdg_find(file, std::vector<boost::filesystem::path>{runtime_dir()});
inline std::filesystem::path find_runtime(const std::filesystem::path& file) {
return xdg_find(file, std::vector<std::filesystem::path>{runtime_dir()});
}

/*
Expand All @@ -202,10 +198,10 @@ inline boost::filesystem::path find_runtime(const boost::filesystem::path& file)
*
* This is useful if you can read multiple instances of the config or data.
*/
inline std::vector<boost::filesystem::path> find_all_config(const boost::filesystem::path& file) {
inline std::vector<std::filesystem::path> find_all_config(const std::filesystem::path& file) {
return xdg_findall(file, all_config_dirs());
}
inline std::vector<boost::filesystem::path> find_all_data(const boost::filesystem::path& file) {
inline std::vector<std::filesystem::path> find_all_data(const std::filesystem::path& file) {
return xdg_findall(file, all_data_dirs());
}

Expand All @@ -217,19 +213,16 @@ inline std::vector<boost::filesystem::path> find_all_data(const boost::filesyste
* Because in merging, the most important file should be loaded last, there
* is a reverse option. If the function returns false, the merging is aborted.
*/
inline void merge_config(const boost::filesystem::path& file,
std::function<bool(const boost::filesystem::path&)>
mergefn,
inline void merge_config(const std::filesystem::path& file,
const std::function<bool(const std::filesystem::path&)>& mergefn,
bool reverse = false) {
xdg_merge(file, all_config_dirs(), reverse, mergefn);
}

inline void merge_data(const boost::filesystem::path& file,
std::function<bool(const boost::filesystem::path&)>
mergefn,
inline void merge_data(const std::filesystem::path& file,
const std::function<bool(const std::filesystem::path&)>& mergefn,
bool reverse = false) {
xdg_merge(file, all_data_dirs(), reverse, mergefn);
}

} // namespace utility
} // namespace cloe
} // namespace cloe::utility
27 changes: 14 additions & 13 deletions runtime/src/cloe/utility/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@

#include <cloe/utility/command.hpp>

#include <cstdlib> // for getenv
#include <string> // for string
#include <cstdlib> // for getenv
#include <filesystem> // for path
#include <optional> // for optional<>
#include <string> // for string

#include <boost/filesystem/path.hpp> // for path
#include <boost/process/search_path.hpp> // for search_path
#include <fable/utility/path.hpp> // for search_path

namespace cloe {
namespace {
Expand All @@ -39,22 +40,22 @@ namespace {
* quickly lead to errors if the user starting Cloe makes use of an alternative
* shell.
*/
boost::filesystem::path shell_executable() {
for (auto shell : {"bash", "dash", "sh", "zsh"}) {
auto result = boost::process::search_path(shell);
if (!result.empty()) {
return result;
std::filesystem::path shell_executable() {
for (const auto* shell : {"sh", "bash", "dash", "zsh"}) {
auto result = fable::search_path(shell);
if (result) {
return *result;
}
}

if (std::getenv("SHELL") != nullptr) {
auto result = boost::process::search_path(std::getenv("SHELL"));
if (!result.empty()) {
return result;
auto result = fable::search_path(std::getenv("SHELL"));
if (result) {
return *result;
}
}

return boost::filesystem::path{};
return {""};
}

} // namespace
Expand Down
30 changes: 13 additions & 17 deletions runtime/src/cloe/utility/xdg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,18 @@

#include <cloe/utility/xdg.hpp>

#include <sstream> // for stringstream
#include <string> // for string
#include <vector> // for vector<>

#include <boost/filesystem/operations.hpp> // for temp_directory_path
#include <boost/filesystem/path.hpp> // for path
#include <filesystem> // for path, temp_directory_path
#include <sstream> // for stringstream
#include <string> // for string
#include <vector> // for vector<>

#ifdef __linux__
#include <unistd.h> // for getuid
#endif

namespace cloe {
namespace utility {
namespace cloe::utility {

using boost::filesystem::path; // NOLINT
using std::filesystem::path; // NOLINT

namespace {

Expand Down Expand Up @@ -72,7 +69,7 @@ std::vector<path> split(const std::string& paths, char delim = '/') {

#ifdef __linux__
path xdg_temp_dir() {
path tmpdir = boost::filesystem::temp_directory_path();
path tmpdir = std::filesystem::temp_directory_path();
return tmpdir / path("xdg-" + std::to_string(getuid()));
}
#endif
Expand Down Expand Up @@ -126,8 +123,8 @@ std::vector<path> xdg_paths(const std::string& env, const std::string& default_p
}

path xdg_find(const path& file, const std::vector<path>& dirs) {
for (auto& dir : dirs) {
if (boost::filesystem::exists(dir / file)) {
for (const auto& dir : dirs) {
if (std::filesystem::exists(dir / file)) {
return dir / file;
}
}
Expand All @@ -136,16 +133,16 @@ path xdg_find(const path& file, const std::vector<path>& dirs) {

std::vector<path> xdg_findall(const path& file, const std::vector<path>& dirs) {
std::vector<path> out;
for (auto& dir : dirs) {
if (boost::filesystem::exists(dir / file)) {
for (const auto& dir : dirs) {
if (std::filesystem::exists(dir / file)) {
out.push_back(dir / file);
}
}
return out;
}

void xdg_merge(const path& file, const std::vector<path>& dirs, bool rev,
std::function<bool(const path&)> mergefn) {
const std::function<bool(const path&)>& mergefn) {
auto files = xdg_findall(file, dirs);
if (rev) {
for (auto it = files.rbegin(); it != files.rend(); it++) {
Expand All @@ -162,5 +159,4 @@ void xdg_merge(const path& file, const std::vector<path>& dirs, bool rev,
}
}

} // namespace utility
} // namespace cloe
} // namespace cloe::utility

0 comments on commit eb2d595

Please sign in to comment.