Skip to content

Commit

Permalink
Replace Boost.filesystem with std::filesystem
Browse files Browse the repository at this point in the history
n.b. These changes don't actually remove Boost.filesystem dependency from the CMake build.

Re ECFLOW-1922
  • Loading branch information
marcosbento committed Oct 26, 2023
1 parent 07b347e commit f268433
Show file tree
Hide file tree
Showing 126 changed files with 633 additions and 807 deletions.
7 changes: 2 additions & 5 deletions ACore/src/EcfPortLock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
#include <iostream>
#include <sstream>

#include <boost/filesystem.hpp>
#include <boost/filesystem/operations.hpp>

#include "Converter.hpp"
#include "File.hpp"

Expand All @@ -33,7 +30,7 @@ class EcfPortLock {
public:
static bool is_free(int port, bool debug = false) {
std::string the_port = ecf::convert_to<std::string>(port);
if (boost::filesystem::exists(port_file(the_port))) {
if (fs::exists(port_file(the_port))) {
if (debug)
std::cout << " EcfPortLock::is_free(" << port << ") returning FALSE\n ";
return false;
Expand All @@ -59,7 +56,7 @@ class EcfPortLock {
std::string the_file = port_file(the_port);
// std::cout << "EcfPortLock::remove " << the_file << "
// --------------------------------------------------\n";
boost::filesystem::remove(the_file);
fs::remove(the_file);
}

private:
Expand Down
41 changes: 19 additions & 22 deletions ACore/src/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#include <fstream>
#include <stdexcept>

#include <boost/filesystem.hpp>

#include "Ecf.hpp"
#include "Log.hpp"
#include "NodePath.hpp"
Expand All @@ -31,7 +29,6 @@

using namespace std;
using namespace boost;
namespace fs = boost::filesystem;

// #define DEBUG_SERVER_PATH 1
// #define DEBUG_CLIENT_PATH 1
Expand Down Expand Up @@ -353,9 +350,9 @@ std::string File::stream_error_condition(const std::ios& stream) {
return msg;
}

bool File::find(const boost::filesystem::path& dir_path, // from this directory downwards,
const std::string& file_name, // search for this name,
boost::filesystem::path& path_found // placing path here if found
bool File::find(const fs::path& dir_path, // from this directory downwards,
const std::string& file_name, // search for this name,
fs::path& path_found // placing path here if found
) {
// std::cout << "Searching '" << dir_path << "' for " << file_name << "\n";
if (!fs::exists(dir_path))
Expand All @@ -378,9 +375,9 @@ bool File::find(const boost::filesystem::path& dir_path, // from this directory
return false;
}

void File::findAll(const boost::filesystem::path& dir_path, // from this directory downwards
const std::string& file_name, // search for this name,
std::vector<boost::filesystem::path>& paths_found // placing path here if found
void File::findAll(const fs::path& dir_path, // from this directory downwards
const std::string& file_name, // search for this name,
std::vector<fs::path>& paths_found // placing path here if found
) {
if (!fs::exists(dir_path))
return;
Expand All @@ -399,9 +396,9 @@ void File::findAll(const boost::filesystem::path& dir_path, // from thi
}
}

void File::find_files_with_extn(const boost::filesystem::path& dir_path, // In this directory
const std::string& extn, // find files matching this extension
std::vector<boost::filesystem::path>& paths_found // placing path here if found
void File::find_files_with_extn(const fs::path& dir_path, // In this directory
const std::string& extn, // find files matching this extension
std::vector<fs::path>& paths_found // placing path here if found
) {
if (!fs::exists(dir_path)) {
return;
Expand All @@ -417,9 +414,9 @@ void File::find_files_with_extn(const boost::filesystem::path& dir_path,
}
}

std::string File::findPath(const boost::filesystem::path& dir_path, // from this directory downwards
const std::string& file_name, // search for this name,
const std::string& leafDir // path must contain this string
std::string File::findPath(const fs::path& dir_path, // from this directory downwards
const std::string& file_name, // search for this name,
const std::string& leafDir // path must contain this string
) {
std::vector<fs::path> paths;
File::findAll(dir_path, file_name, paths);
Expand All @@ -435,9 +432,9 @@ std::string File::findPath(const boost::filesystem::path& dir_path, // from this
return std::string();
}

std::string File::findPath(const boost::filesystem::path& dir_path, // from this directory downwards
const std::string& file_name, // search for this name,
const std::vector<std::string>& tokens // path must contain all these tokens
std::string File::findPath(const fs::path& dir_path, // from this directory downwards
const std::string& file_name, // search for this name,
const std::vector<std::string>& tokens // path must contain all these tokens
) {
std::vector<fs::path> paths;
File::findAll(dir_path, file_name, paths);
Expand Down Expand Up @@ -806,7 +803,7 @@ std::string File::forwardSearch(const std::string& rootPath, const std::string&
}

// Remove a directory recursively ****
bool File::removeDir(const boost::filesystem::path& p) {
bool File::removeDir(const fs::path& p) {
try {
fs::directory_iterator end;
for (fs::directory_iterator it(p); it != end; ++it) {
Expand Down Expand Up @@ -837,9 +834,9 @@ bool File::removeDir(const boost::filesystem::path& p) {

static std::string bjam_workspace_dir() {
// We need the *SAME* location so that different process find the same file. Get to the workspace directory
boost::filesystem::path current_path = boost::filesystem::current_path();
std::string stem = current_path.stem().string();
int count = 0;
auto current_path = fs::current_path();
std::string stem = current_path.stem().string();
int count = 0;
while (stem.find("ecflow") == std::string::npos) {
current_path = current_path.parent_path();
stem = current_path.stem().string();
Expand Down
35 changes: 18 additions & 17 deletions ACore/src/File.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#include <vector>

#include <boost/core/noncopyable.hpp>
#include <boost/filesystem/path.hpp>

#include "Filesystem.hpp"

namespace ecf {

Expand Down Expand Up @@ -75,34 +76,34 @@ class File : private boost::noncopyable {
/// recursively look for a file, given a starting directory
/// Return the first file that matches
/// return true if file found false otherwise
static bool find(const boost::filesystem::path& dir_path, // from this directory downwards
const std::string& file_name, // search for this name,
boost::filesystem::path& path_found // placing path here if found
static bool find(const fs::path& dir_path, // from this directory downwards
const std::string& file_name, // search for this name,
fs::path& path_found // placing path here if found
);

/// recursively look for a file, given a starting directory
/// Returns _ALL_ files that match
static void findAll(const boost::filesystem::path& dir_path, // from this directory downwards
const std::string& file_name, // search for this name,
std::vector<boost::filesystem::path>& paths_found // placing path here if found
static void findAll(const fs::path& dir_path, // from this directory downwards
const std::string& file_name, // search for this name,
std::vector<fs::path>& paths_found // placing path here if found
);

/// Find all files with given extension must include leading .
static void find_files_with_extn(const boost::filesystem::path& dir_path, // In this directory
const std::string& extn, // find files matching this extension
std::vector<boost::filesystem::path>& paths_found // placing path here if found
static void find_files_with_extn(const fs::path& dir_path, // In this directory
const std::string& extn, // find files matching this extension
std::vector<fs::path>& paths_found // placing path here if found
);

/// recursively look for a file, given a starting directory and path token
/// Returns the first match found
static std::string findPath(const boost::filesystem::path& dir_path, // from this directory downwards
const std::string& file_name, // search for this name,
const std::string& leafDir // path must contain this string
static std::string findPath(const fs::path& dir_path, // from this directory downwards
const std::string& file_name, // search for this name,
const std::string& leafDir // path must contain this string
);

static std::string findPath(const boost::filesystem::path& dir_path, // from this directory downwards
const std::string& file_name, // search for this name,
const std::vector<std::string>& tokens // path must contain all these tokens
static std::string findPath(const fs::path& dir_path, // from this directory downwards
const std::string& file_name, // search for this name,
const std::vector<std::string>& tokens // path must contain all these tokens
);

/// Create missing directories. This is *NOT* the same as boost::create_directories
Expand Down Expand Up @@ -149,7 +150,7 @@ class File : private boost::noncopyable {
forwardSearch(const std::string& rootPath, const std::string& nodePath, const std::string& fileExtn);

// Remove a directory recursively ****
static bool removeDir(const boost::filesystem::path& p);
static bool removeDir(const fs::path& p);

// Locate the path to the server exe
static std::string find_ecf_server_path();
Expand Down
49 changes: 49 additions & 0 deletions ACore/src/Filesystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2023- ECMWF.
*
* This software is licensed under the terms of the Apache Licence version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation
* nor does it submit to any jurisdiction.
*/

#include "Filesystem.hpp"

#include <random>

namespace ecf {

namespace details {
struct FilenameGenerator
{
std::string generate(std::string pattern, char placeholder = '%') {
for (auto& c : pattern) {
if (c == placeholder) {
c = generate_random_hex_digit();
}
}
return pattern;
}

private:
char generate_random_hex_digit() {
auto idx = distribution_(rng_);
return hex_[idx];
}

static constexpr const char* hex_ = "0123456789abcdef";

std::random_device dev_{};
std::mt19937 rng_{dev_()};
std::uniform_int_distribution<std::mt19937::result_type> distribution_{0, 15};
};

} // namespace details

fs::path ecf::unique_path(const fs::path& source) {
details::FilenameGenerator fg;
return fg.generate(source);
}

} // namespace ecf
38 changes: 38 additions & 0 deletions ACore/src/Filesystem.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2023- ECMWF.
*
* This software is licensed under the terms of the Apache Licence version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation
* nor does it submit to any jurisdiction.
*/

#ifndef ECFLOW_CORE_FILESYSTEM_HPP
#define ECFLOW_CORE_FILESYSTEM_HPP

#include <cassert>
#include <filesystem>

namespace fs = std::filesystem;

namespace ecf {

namespace details {

template <typename Clock, typename Duration>
inline time_t as_time_t(const std::chrono::time_point<Clock, Duration>& tp) {
return std::chrono::duration_cast<std::chrono::seconds>(tp.time_since_epoch()).count();
}

} // namespace details

inline time_t last_write_time_as_time_t(const fs::path& p) {
return details::as_time_t(fs::last_write_time(fs::path(p)));
}

fs::path unique_path(const fs::path& source);

} // namespace ecf

#endif
5 changes: 1 addition & 4 deletions ACore/src/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@
#include <stdexcept>
#include <vector>

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>

#include "File.hpp"
#include "Filesystem.hpp"
#include "Indentor.hpp"
#include "Str.hpp"
#include "TimeStamp.hpp"

using namespace std;
namespace fs = boost::filesystem;

namespace ecf {

Expand Down
3 changes: 1 addition & 2 deletions ACore/test/TestCereal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@

#include <iostream>

#include <boost/filesystem/operations.hpp>
#include <boost/test/unit_test.hpp>

#include "Filesystem.hpp"
#include "SerializationTest.hpp"

using namespace ecf;
using namespace boost;
using namespace std;
namespace fs = boost::filesystem;

class MyClass {
public:
Expand Down
3 changes: 1 addition & 2 deletions ACore/test/TestCerealOptionalNVP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@

#include <iostream>

#include <boost/filesystem/operations.hpp>
#include <boost/test/unit_test.hpp>

#include "Filesystem.hpp"
#include "SerializationTest.hpp"

using namespace ecf;
using namespace boost;
using namespace std;
namespace fs = boost::filesystem;

class Base {
public:
Expand Down
3 changes: 1 addition & 2 deletions ACore/test/TestCerealWithHierarchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
// Description
//============================================================================

#include <boost/filesystem/operations.hpp>
#include <boost/test/unit_test.hpp>

#include "Filesystem.hpp"
#include "Serialization.hpp"

using namespace ecf;
using namespace boost;
using namespace std;
namespace fs = boost::filesystem;

// ======================================================================================

Expand Down
6 changes: 2 additions & 4 deletions ACore/test/TestFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <iostream>
#include <string>

#include <boost/filesystem/operations.hpp>
#include <boost/test/unit_test.hpp>

#include "Converter.hpp"
Expand All @@ -35,7 +34,6 @@
using namespace boost;
using namespace std;
using namespace ecf;
namespace fs = boost::filesystem;

BOOST_AUTO_TEST_SUITE(CoreTestSuite)

Expand Down Expand Up @@ -540,14 +538,14 @@ BOOST_AUTO_TEST_CASE(test_get_all_files_by_extension) {
cout << "ACore:: ...test_get_all_files_by_extension\n";
{
std::string rootPath = File::test_data("ACore/test/data/badPasswdFiles", "ACore");
std::vector<boost::filesystem::path> vec;
std::vector<fs::path> vec;
File::find_files_with_extn(rootPath, ".passwd", vec);
// for(auto& file: vec) std::cout << file << "\n";
BOOST_REQUIRE_MESSAGE(vec.size() == 6, "Expected 6 files in directory " << rootPath);
}
{
std::string rootPath = File::test_data("ACore/test/data/badWhiteListFiles", "ACore");
std::vector<boost::filesystem::path> vec;
std::vector<fs::path> vec;
File::find_files_with_extn(rootPath, ".lists", vec);
// for(auto& file: vec) std::cout << file << "\n";
BOOST_REQUIRE_MESSAGE(vec.size() == 7, "Expected 7 files in directory " << rootPath);
Expand Down
Loading

0 comments on commit f268433

Please sign in to comment.