Browse Source

use std::filesystem for C++17

pull/2096/head
orignal 3 months ago
parent
commit
ae309ca632
  1. 96
      libi2pd/FS.cpp
  2. 6
      libi2pd/FS.h
  3. 17
      libi2pd_client/AddressBook.cpp

96
libi2pd/FS.cpp

@ -7,7 +7,6 @@
*/ */
#include <algorithm> #include <algorithm>
#include <boost/filesystem.hpp>
#if defined(MAC_OSX) #if defined(MAC_OSX)
#include <boost/system/system_error.hpp> #include <boost/system/system_error.hpp>
@ -25,6 +24,14 @@
#include "Log.h" #include "Log.h"
#include "Garlic.h" #include "Garlic.h"
#if STD_FILESYSTEM
#include <filesystem>
namespace fs_lib = std::filesystem;
#else
#include <boost/filesystem.hpp>
namespace fs_lib = boost::filesystem;
#endif
namespace i2p { namespace i2p {
namespace fs { namespace fs {
std::string appName = "i2pd"; std::string appName = "i2pd";
@ -55,13 +62,13 @@ namespace fs {
const std::string GetUTF8DataDir () { const std::string GetUTF8DataDir () {
#ifdef _WIN32 #ifdef _WIN32
#if (BOOST_VERSION >= 108500) #if (BOOST_VERSION >= 108500)
boost::filesystem::path path (dataDir); fs_lib::path path (dataDir);
#else #else
boost::filesystem::wpath path (dataDir); fs_lib::wpath path (dataDir);
#endif #endif
auto loc = boost::filesystem::path::imbue(std::locale( std::locale(), new std::codecvt_utf8_utf16<wchar_t>() ) ); // convert path to UTF-8 auto loc = fs_lib::path::imbue(std::locale( std::locale(), new std::codecvt_utf8_utf16<wchar_t>() ) ); // convert path to UTF-8
auto dataDirUTF8 = path.string(); auto dataDirUTF8 = path.string();
boost::filesystem::path::imbue(loc); // Return locale settings back fs_lib::path::imbue(loc); // Return locale settings back
return dataDirUTF8; return dataDirUTF8;
#else #else
return dataDir; // linux, osx, android uses UTF-8 by default return dataDir; // linux, osx, android uses UTF-8 by default
@ -92,9 +99,9 @@ namespace fs {
else else
{ {
#if (BOOST_VERSION >= 108500) #if (BOOST_VERSION >= 108500)
dataDir = boost::filesystem::path(commonAppData).string() + "\\" + appName; dataDir = fs_lib::path(commonAppData).string() + "\\" + appName;
#else #else
dataDir = boost::filesystem::wpath(commonAppData).string() + "\\" + appName; dataDir = fs_lib::wpath(commonAppData).string() + "\\" + appName;
#endif #endif
} }
#else #else
@ -121,13 +128,13 @@ namespace fs {
else else
{ {
#if (BOOST_VERSION >= 108500) #if (BOOST_VERSION >= 108500)
auto execPath = boost::filesystem::path(localAppData).parent_path(); auto execPath = fs_lib::path(localAppData).parent_path();
#else #else
auto execPath = boost::filesystem::wpath(localAppData).parent_path(); auto execPath = fs_lib::wpath(localAppData).parent_path();
#endif #endif
// if config file exists in .exe's folder use it // if config file exists in .exe's folder use it
if(boost::filesystem::exists(execPath/"i2pd.conf")) // TODO: magic string if(fs_lib::exists(execPath/"i2pd.conf")) // TODO: magic string
{ {
dataDir = execPath.string (); dataDir = execPath.string ();
} else // otherwise %appdata% } else // otherwise %appdata%
@ -144,9 +151,9 @@ namespace fs {
else else
{ {
#if (BOOST_VERSION >= 108500) #if (BOOST_VERSION >= 108500)
dataDir = boost::filesystem::path(localAppData).string() + "\\" + appName; dataDir = fs_lib::path(localAppData).string() + "\\" + appName;
#else #else
dataDir = boost::filesystem::wpath(localAppData).string() + "\\" + appName; dataDir = fs_lib::wpath(localAppData).string() + "\\" + appName;
#endif #endif
} }
} }
@ -169,7 +176,7 @@ namespace fs {
#if defined(ANDROID) #if defined(ANDROID)
const char * ext = getenv("EXTERNAL_STORAGE"); const char * ext = getenv("EXTERNAL_STORAGE");
if (!ext) ext = "/sdcard"; if (!ext) ext = "/sdcard";
if (boost::filesystem::exists(ext)) if (fs_lib::exists(ext))
{ {
dataDir = std::string (ext) + "/" + appName; dataDir = std::string (ext) + "/" + appName;
return; return;
@ -202,16 +209,16 @@ namespace fs {
} }
bool Init() { bool Init() {
if (!boost::filesystem::exists(dataDir)) if (!fs_lib::exists(dataDir))
boost::filesystem::create_directory(dataDir); fs_lib::create_directory(dataDir);
std::string destinations = DataDirPath("destinations"); std::string destinations = DataDirPath("destinations");
if (!boost::filesystem::exists(destinations)) if (!fs_lib::exists(destinations))
boost::filesystem::create_directory(destinations); fs_lib::create_directory(destinations);
std::string tags = DataDirPath("tags"); std::string tags = DataDirPath("tags");
if (!boost::filesystem::exists(tags)) if (!fs_lib::exists(tags))
boost::filesystem::create_directory(tags); fs_lib::create_directory(tags);
else else
i2p::garlic::CleanUpTagsFiles (); i2p::garlic::CleanUpTagsFiles ();
@ -219,13 +226,13 @@ namespace fs {
} }
bool ReadDir(const std::string & path, std::vector<std::string> & files) { bool ReadDir(const std::string & path, std::vector<std::string> & files) {
if (!boost::filesystem::exists(path)) if (!fs_lib::exists(path))
return false; return false;
boost::filesystem::directory_iterator it(path); fs_lib::directory_iterator it(path);
boost::filesystem::directory_iterator end; fs_lib::directory_iterator end;
for ( ; it != end; it++) { for ( ; it != end; it++) {
if (!boost::filesystem::is_regular_file(it->status())) if (!fs_lib::is_regular_file(it->status()))
continue; continue;
files.push_back(it->path().string()); files.push_back(it->path().string());
} }
@ -234,29 +241,38 @@ namespace fs {
} }
bool Exists(const std::string & path) { bool Exists(const std::string & path) {
return boost::filesystem::exists(path); return fs_lib::exists(path);
} }
uint32_t GetLastUpdateTime (const std::string & path) uint32_t GetLastUpdateTime (const std::string & path)
{ {
if (!boost::filesystem::exists(path)) if (!fs_lib::exists(path))
return 0; return 0;
#if STD_FILESYSTEM
std::error_code ec;
auto t = std::filesystem::last_write_time (path, ec);
if (ec) return 0;
auto sctp = std::chrono::time_point_cast<std::chrono::system_clock::duration>(
t - decltype(t)::clock::now() + std::chrono::system_clock::now());
return std::chrono::system_clock::to_time_t(sctp);
#else
boost::system::error_code ec; boost::system::error_code ec;
auto t = boost::filesystem::last_write_time (path, ec); auto t = boost::filesystem::last_write_time (path, ec);
return ec ? 0 : t; return ec ? 0 : t;
#endif
} }
bool Remove(const std::string & path) { bool Remove(const std::string & path) {
if (!boost::filesystem::exists(path)) if (!fs_lib::exists(path))
return false; return false;
return boost::filesystem::remove(path); return fs_lib::remove(path);
} }
bool CreateDirectory (const std::string& path) bool CreateDirectory (const std::string& path)
{ {
if (boost::filesystem::exists(path) && boost::filesystem::is_directory (boost::filesystem::status (path))) if (fs_lib::exists(path) && fs_lib::is_directory (fs_lib::status (path)))
return true; return true;
return boost::filesystem::create_directory(path); return fs_lib::create_directory(path);
} }
void HashedStorage::SetPlace(const std::string &path) { void HashedStorage::SetPlace(const std::string &path) {
@ -264,18 +280,18 @@ namespace fs {
} }
bool HashedStorage::Init(const char * chars, size_t count) { bool HashedStorage::Init(const char * chars, size_t count) {
if (!boost::filesystem::exists(root)) { if (!fs_lib::exists(root)) {
boost::filesystem::create_directories(root); fs_lib::create_directories(root);
} }
for (size_t i = 0; i < count; i++) { for (size_t i = 0; i < count; i++) {
auto p = root + i2p::fs::dirSep + prefix1 + chars[i]; auto p = root + i2p::fs::dirSep + prefix1 + chars[i];
if (boost::filesystem::exists(p)) if (fs_lib::exists(p))
continue; continue;
#if TARGET_OS_SIMULATOR #if TARGET_OS_SIMULATOR
// ios simulator fs says it is case sensitive, but it is not // ios simulator fs says it is case sensitive, but it is not
boost::system::error_code ec; boost::system::error_code ec;
if (boost::filesystem::create_directory(p, ec)) if (fs_lib::create_directory(p, ec))
continue; continue;
switch (ec.value()) { switch (ec.value()) {
case boost::system::errc::file_exists: case boost::system::errc::file_exists:
@ -285,7 +301,7 @@ namespace fs {
throw boost::system::system_error( ec, __func__ ); throw boost::system::system_error( ec, __func__ );
} }
#else #else
if (boost::filesystem::create_directory(p)) if (fs_lib::create_directory(p))
continue; /* ^ throws exception on failure */ continue; /* ^ throws exception on failure */
#endif #endif
return false; return false;
@ -308,9 +324,9 @@ namespace fs {
void HashedStorage::Remove(const std::string & ident) { void HashedStorage::Remove(const std::string & ident) {
std::string path = Path(ident); std::string path = Path(ident);
if (!boost::filesystem::exists(path)) if (!fs_lib::exists(path))
return; return;
boost::filesystem::remove(path); fs_lib::remove(path);
} }
void HashedStorage::Traverse(std::vector<std::string> & files) { void HashedStorage::Traverse(std::vector<std::string> & files) {
@ -321,12 +337,12 @@ namespace fs {
void HashedStorage::Iterate(FilenameVisitor v) void HashedStorage::Iterate(FilenameVisitor v)
{ {
boost::filesystem::path p(root); fs_lib::path p(root);
boost::filesystem::recursive_directory_iterator it(p); fs_lib::recursive_directory_iterator it(p);
boost::filesystem::recursive_directory_iterator end; fs_lib::recursive_directory_iterator end;
for ( ; it != end; it++) { for ( ; it != end; it++) {
if (!boost::filesystem::is_regular_file( it->status() )) if (!fs_lib::is_regular_file( it->status() ))
continue; continue;
const std::string & t = it->path().string(); const std::string & t = it->path().string();
v(t); v(t);

6
libi2pd/FS.h

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2020, The PurpleI2P Project * Copyright (c) 2013-2024, The PurpleI2P Project
* *
* This file is part of Purple i2pd project and licensed under BSD3 * This file is part of Purple i2pd project and licensed under BSD3
* *
@ -15,6 +15,10 @@
#include <sstream> #include <sstream>
#include <functional> #include <functional>
#if (!defined(_WIN32) && !TARGET_OS_SIMULATOR && (__cplusplus >= 201703L)) // C++ 17 or higher
# define STD_FILESYSTEM 1
#endif
namespace i2p { namespace i2p {
namespace fs { namespace fs {
extern std::string dirSep; extern std::string dirSep;

17
libi2pd_client/AddressBook.cpp

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2023, The PurpleI2P Project * Copyright (c) 2013-2024, The PurpleI2P Project
* *
* This file is part of Purple i2pd project and licensed under BSD3 * This file is part of Purple i2pd project and licensed under BSD3
* *
@ -15,7 +15,6 @@
#include <condition_variable> #include <condition_variable>
#include <openssl/rand.h> #include <openssl/rand.h>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include "Base.h" #include "Base.h"
#include "util.h" #include "util.h"
#include "Identity.h" #include "Identity.h"
@ -27,6 +26,14 @@
#include "AddressBook.h" #include "AddressBook.h"
#include "Config.h" #include "Config.h"
#if STD_FILESYSTEM
#include <filesystem>
namespace fs_lib = std::filesystem;
#else
#include <boost/filesystem.hpp>
namespace fs_lib = boost::filesystem;
#endif
namespace i2p namespace i2p
{ {
namespace client namespace client
@ -266,11 +273,11 @@ namespace client
void AddressBookFilesystemStorage::ResetEtags () void AddressBookFilesystemStorage::ResetEtags ()
{ {
LogPrint (eLogError, "Addressbook: Resetting eTags"); LogPrint (eLogError, "Addressbook: Resetting eTags");
for (boost::filesystem::directory_iterator it (etagsPath); it != boost::filesystem::directory_iterator (); ++it) for (fs_lib::directory_iterator it (etagsPath); it != fs_lib::directory_iterator (); ++it)
{ {
if (!boost::filesystem::is_regular_file (it->status ())) if (!fs_lib::is_regular_file (it->status ()))
continue; continue;
boost::filesystem::remove (it->path ()); fs_lib::remove (it->path ());
} }
} }

Loading…
Cancel
Save