Skip to content

Commit

Permalink
Switch to std::filesystem
Browse files Browse the repository at this point in the history
Change-Id: Idd88815c8971a1dd6a592ac9d3f46d5925afd21b
  • Loading branch information
Pesa committed Dec 17, 2024
1 parent d04267f commit b071654
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 68 deletions.
1 change: 0 additions & 1 deletion .jenkins.d/00-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ APT_PKGS=(
libboost-chrono-dev
libboost-date-time-dev
libboost-dev
libboost-filesystem-dev
libboost-iostreams-dev
libboost-log-dev
libboost-program-options-dev
Expand Down
2 changes: 1 addition & 1 deletion .waf-tools/default-compiler-flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def getDebugFlags(self, conf):
return {
'CXXFLAGS': [],
'LINKFLAGS': [],
'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
'DEFINES': ['BOOST_ASIO_NO_DEPRECATED'],
}

def getOptimizedFlags(self, conf):
Expand Down
63 changes: 28 additions & 35 deletions src/conf-file-processor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2023, The University of Memphis,
* Copyright (c) 2014-2024, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
Expand Down Expand Up @@ -29,16 +29,16 @@
#include <ndn-cxx/util/io.hpp>

#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/property_tree/info_parser.hpp>

#include <filesystem>
#include <fstream>
#include <iostream>

namespace bf = boost::filesystem;

namespace nlsr {

namespace fs = std::filesystem;

template <class T>
class ConfigurationVariable
{
Expand Down Expand Up @@ -330,14 +330,14 @@ ConfFileProcessor::processConfSectionGeneral(const ConfigSection& section)
return false;
}

// state-dir
try {
std::string stateDir = section.get<std::string>("state-dir");
if (bf::exists(stateDir)) {
if (bf::is_directory(stateDir)) {
fs::path stateDir(section.get<std::string>("state-dir"));
if (fs::exists(stateDir)) {
if (fs::is_directory(stateDir)) {
// copying nlsr.conf file to a user-defined directory for possible modification
std::string conFileDynamic = (bf::path(stateDir) / "nlsr.conf").string();

if (m_confFileName == conFileDynamic) {
auto conFileDynamic = stateDir / "nlsr.conf";
if (m_confFileName == conFileDynamic.string()) {
std::cerr << "Please use nlsr.conf stored at another location "
<< "or change the state-dir in the configuration." << std::endl;
std::cerr << "The file at " << conFileDynamic <<
Expand All @@ -347,45 +347,39 @@ ConfFileProcessor::processConfSectionGeneral(const ConfigSection& section)
return false;
}

m_confParam.setConfFileNameDynamic(conFileDynamic);
m_confParam.setConfFileNameDynamic(conFileDynamic.string());
try {
bf::copy_file(m_confFileName, conFileDynamic,
#if BOOST_VERSION >= 107400
bf::copy_options::overwrite_existing
#else
bf::copy_option::overwrite_if_exists
#endif
);
fs::copy_file(m_confFileName, conFileDynamic, fs::copy_options::overwrite_existing);
}
catch (const bf::filesystem_error& e) {
std::cerr << "Error copying conf file to the state directory: " << e.what() << std::endl;
catch (const fs::filesystem_error& e) {
std::cerr << "Error copying conf file to state-dir: " << e.what() << std::endl;
return false;
}

std::string testFileName = (bf::path(stateDir) / "test.seq").string();
std::ofstream testOutFile(testFileName);
if (testOutFile) {
m_confParam.setStateFileDir(stateDir);
auto testFilePath = stateDir / "test.seq";
std::ofstream testFile(testFilePath);
if (testFile) {
m_confParam.setStateFileDir(stateDir.string());
}
else {
std::cerr << "NLSR does not have read/write permission on the state directory" << std::endl;
std::cerr << "NLSR does not have read/write permission on state-dir" << std::endl;
return false;
}
testOutFile.close();
remove(testFileName.c_str());
testFile.close();
fs::remove(testFilePath);
}
else {
std::cerr << "Provided path '" << stateDir << "' is not a directory" << std::endl;
std::cerr << "Provided state-dir " << stateDir << " is not a directory" << std::endl;
return false;
}
}
else {
std::cerr << "Provided state directory '" << stateDir << "' does not exist" << std::endl;
std::cerr << "Provided state-dir " << stateDir << " does not exist" << std::endl;
return false;
}
}
catch (const std::exception& ex) {
std::cerr << "You must configure state directory" << std::endl;
std::cerr << "You must configure state-dir" << std::endl;
std::cerr << ex.what() << std::endl;
return false;
}
Expand Down Expand Up @@ -654,20 +648,19 @@ ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section)
return false;
}

std::string file = it->second.data();
bf::path certfilePath = absolute(file, bf::path(m_confFileName).parent_path());
std::ifstream ifs(certfilePath.string());
fs::path certPath = fs::canonical(fs::path(m_confFileName).parent_path() / it->second.data());
std::ifstream ifs(certPath);

ndn::security::Certificate idCert;
try {
idCert = ndn::io::loadTlv<ndn::security::Certificate>(ifs);
}
catch (const std::exception& e) {
std::cerr << "Error: Cannot load cert-to-publish '" << file << "': " << e.what() << std::endl;
std::cerr << "Error: Cannot load cert-to-publish " << certPath << ": " << e.what() << std::endl;
return false;
}

m_confParam.addCertPath(certfilePath.string());
m_confParam.addCertPath(certPath.string());
m_confParam.loadCertToValidator(idCert);
}
}
Expand Down
7 changes: 4 additions & 3 deletions tests/key-chain-fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

#include <ndn-cxx/util/io.hpp>

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

namespace nlsr::tests {

Expand All @@ -40,9 +41,9 @@ KeyChainFixture::KeyChainFixture()

KeyChainFixture::~KeyChainFixture()
{
boost::system::error_code ec;
std::error_code ec;
for (const auto& certFile : m_certFiles) {
boost::filesystem::remove(certFile, ec); // ignore error
std::filesystem::remove(certFile, ec); // ignore error
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/security/test-certificate-store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
#include "tests/io-key-chain-fixture.hpp"
#include "tests/test-common.hpp"

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/property_tree/info_parser.hpp>

#include <filesystem>

namespace nlsr::tests {

class CertificateStoreFixture : public IoKeyChainFixture
Expand All @@ -50,7 +50,7 @@ class CertificateStoreFixture : public IoKeyChainFixture
, nlsr(face, m_keyChain, conf)
, lsdb(nlsr.getLsdb())
, certStore(face, conf, lsdb)
, ROOT_CERT_PATH(boost::filesystem::current_path() / "root.cert")
, ROOT_CERT_PATH(std::filesystem::current_path() / "root.cert")
{
rootId = m_keyChain.createIdentity(rootIdName);
siteIdentity = addSubCertificate(siteIdentityName, rootId);
Expand Down Expand Up @@ -112,7 +112,7 @@ class CertificateStoreFixture : public IoKeyChainFixture
ndn::security::Certificate certificate;
ndn::Name certificateKey;
security::CertificateStore certStore;
const boost::filesystem::path ROOT_CERT_PATH;
const std::filesystem::path ROOT_CERT_PATH;
};

BOOST_FIXTURE_TEST_SUITE(TestCertificateStore, CertificateStoreFixture)
Expand Down
8 changes: 4 additions & 4 deletions tests/test-lsa-rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
#include "tests/io-key-chain-fixture.hpp"
#include "tests/test-common.hpp"

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <boost/property_tree/ptree.hpp>

#include <filesystem>

namespace nlsr::tests {

using namespace ndn;
Expand All @@ -48,7 +48,7 @@ class LsaRuleFixture : public IoKeyChainFixture
, confProcessor(confParam, SyncProtocol::PSYNC, HYPERBOLIC_STATE_OFF,
"/ndn/", "/edu/test-site", "/%C1.Router/router1")
, lsdb(face, m_keyChain, confParam)
, ROOT_CERT_PATH(boost::filesystem::current_path() / std::string("root.cert"))
, ROOT_CERT_PATH(std::filesystem::current_path() / "root.cert")
{
rootId = m_keyChain.createIdentity(rootIdName);
siteIdentity = addSubCertificate(siteIdentityName, rootId);
Expand Down Expand Up @@ -92,7 +92,7 @@ class LsaRuleFixture : public IoKeyChainFixture
DummyConfFileProcessor confProcessor;
Lsdb lsdb;

const boost::filesystem::path ROOT_CERT_PATH;
const std::filesystem::path ROOT_CERT_PATH;
};

BOOST_FIXTURE_TEST_SUITE(TestLsaDataValidation, LsaRuleFixture)
Expand Down
20 changes: 9 additions & 11 deletions tests/test-sequencing-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@

#include "tests/boost-test.hpp"

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

namespace nlsr::tests {

Expand All @@ -33,22 +34,17 @@ using namespace ndn;
class SequencingManagerFixture
{
public:
SequencingManagerFixture()
: m_seqManager("/tmp", HYPERBOLIC_STATE_OFF)
{
}

~SequencingManagerFixture()
{
boost::filesystem::remove(seqFile);
std::error_code ec;
std::filesystem::remove(m_seqFile, ec); // ignore error
}

void
writeToFile(const std::string& testSeq)
{
std::ofstream outputFile(seqFile, std::ofstream::trunc);
std::ofstream outputFile(m_seqFile, std::ofstream::trunc);
outputFile << testSeq;
outputFile.close();
}

void
Expand All @@ -66,8 +62,10 @@ class SequencingManagerFixture
}

public:
std::string seqFile = "/tmp/nlsrSeqNo.txt";
SequencingManager m_seqManager;
SequencingManager m_seqManager{"/tmp", HYPERBOLIC_STATE_OFF};

private:
std::filesystem::path m_seqFile{"/tmp/nlsrSeqNo.txt"};
};

BOOST_FIXTURE_TEST_SUITE(TestSequencingManager, SequencingManagerFixture)
Expand Down
8 changes: 4 additions & 4 deletions tests/update/test-prefix-update-processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
#include <ndn-cxx/security/interest-signer.hpp>
#include <ndn-cxx/security/signing-helpers.hpp>

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <boost/property_tree/ptree.hpp>

#include <filesystem>

namespace nlsr::tests {

using namespace ndn;
Expand All @@ -50,7 +50,7 @@ class PrefixUpdateFixture : public IoKeyChainFixture
, confProcessor(conf)
, nlsr(face, m_keyChain, conf)
, namePrefixList(conf.getNamePrefixList())
, SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert"))
, SITE_CERT_PATH(std::filesystem::current_path() / "site.cert")
{
// Site cert
siteIdentity = m_keyChain.createIdentity(siteIdentityName);
Expand Down Expand Up @@ -139,7 +139,7 @@ class PrefixUpdateFixture : public IoKeyChainFixture
Nlsr nlsr;
NamePrefixList& namePrefixList;

const boost::filesystem::path SITE_CERT_PATH;
const std::filesystem::path SITE_CERT_PATH;
};

BOOST_FIXTURE_TEST_SUITE(TestPrefixUpdateProcessor, PrefixUpdateFixture)
Expand Down
9 changes: 5 additions & 4 deletions tests/update/test-save-delete-prefix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
#include <ndn-cxx/security/interest-signer.hpp>
#include <ndn-cxx/security/signing-helpers.hpp>

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

#include <filesystem>

namespace nlsr::tests {

namespace bpt = boost::property_tree;
Expand All @@ -49,7 +49,7 @@ class PrefixSaveDeleteFixture : public IoKeyChainFixture
, conf(face, m_keyChain, testConfFile)
, confProcessor(conf)
, nlsr(face, m_keyChain, conf)
, SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert"))
, SITE_CERT_PATH(std::filesystem::current_path() / "site.cert")
, counter(0)
{
std::ifstream source("nlsr.conf", std::ios::binary);
Expand All @@ -69,6 +69,7 @@ class PrefixSaveDeleteFixture : public IoKeyChainFixture
std::ifstream inputFile;
inputFile.open(testConfFile);
BOOST_REQUIRE(inputFile.is_open());

bpt::ptree pt;
bpt::read_info(inputFile, pt);
// Loads section and file name
Expand Down Expand Up @@ -164,7 +165,7 @@ class PrefixSaveDeleteFixture : public IoKeyChainFixture
ConfParameter conf;
DummyConfFileProcessor confProcessor;
Nlsr nlsr;
const boost::filesystem::path SITE_CERT_PATH;
const std::filesystem::path SITE_CERT_PATH;
ndn::Name sessionTime;
int counter;
};
Expand Down
2 changes: 1 addition & 1 deletion wscript
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def configure(conf):
conf.check_cfg(package='libndn-cxx', args=['libndn-cxx >= 0.9.0', '--cflags', '--libs'],
uselib_store='NDN_CXX', pkg_config_path=pkg_config_path)

conf.check_boost(lib='filesystem', mt=True)
conf.check_boost()
if conf.env.BOOST_VERSION_NUMBER < 107100:
conf.fatal('The minimum supported version of Boost is 1.71.0.\n'
'Please upgrade your distribution or manually install a newer version of Boost.\n'
Expand Down

0 comments on commit b071654

Please sign in to comment.