i2pd/libi2pd/FS.cpp

361 lines
8.8 KiB
C++
Raw Normal View History

2016-02-11 03:00:00 +03:00
/*
* Copyright (c) 2013-2024, The PurpleI2P Project
2016-02-11 03:00:00 +03:00
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
#include <algorithm>
#if defined(MAC_OSX)
2024-09-01 22:23:14 +03:00
#if !STD_FILESYSTEM
#include <boost/system/system_error.hpp>
2024-09-01 22:23:14 +03:00
#endif
#include <TargetConditionals.h>
#endif
#ifdef _WIN32
2016-02-22 17:53:26 +03:00
#include <shlobj.h>
#include <windows.h>
#include <codecvt>
2016-02-22 17:53:26 +03:00
#endif
2016-02-11 03:00:00 +03:00
#include "Base.h"
#include "FS.h"
#include "Log.h"
2017-04-03 22:05:10 +03:00
#include "Garlic.h"
2016-02-11 03:00:00 +03:00
2024-08-28 04:49:23 +03:00
#if STD_FILESYSTEM
#include <filesystem>
namespace fs_lib = std::filesystem;
#else
#include <boost/filesystem.hpp>
namespace fs_lib = boost::filesystem;
#endif
2016-02-11 03:00:00 +03:00
namespace i2p {
namespace fs {
std::string appName = "i2pd";
std::string dataDir = "";
std::string certsDir = "";
2016-02-11 03:00:00 +03:00
#ifdef _WIN32
std::string dirSep = "\\";
2016-02-11 03:00:00 +03:00
#else
std::string dirSep = "/";
2016-02-11 03:00:00 +03:00
#endif
const std::string & GetAppName () {
return appName;
}
2016-02-11 03:00:00 +03:00
void SetAppName (const std::string& name) {
appName = name;
}
2016-02-11 03:00:00 +03:00
const std::string & GetDataDir () {
return dataDir;
}
2016-02-11 03:00:00 +03:00
const std::string & GetCertsDir () {
return certsDir;
}
const std::string GetUTF8DataDir () {
#ifdef _WIN32
int size = MultiByteToWideChar(CP_ACP, 0,
dataDir.c_str(), dataDir.size(), nullptr, 0);
std::wstring utf16Str(size, L'\0');
MultiByteToWideChar(CP_ACP, 0,
dataDir.c_str(), dataDir.size(), &utf16Str[0], size);
int utf8Size = WideCharToMultiByte(CP_UTF8, 0,
utf16Str.c_str(), utf16Str.size(), nullptr, 0, nullptr, nullptr);
std::string utf8Str(utf8Size, '\0');
WideCharToMultiByte(CP_UTF8, 0,
utf16Str.c_str(), utf16Str.size(), &utf8Str[0], utf8Size, nullptr, nullptr);
return utf8Str;
#else
return dataDir; // linux, osx, android uses UTF-8 by default
#endif
}
void DetectDataDir(const std::string & cmdline_param, bool isService) {
// with 'datadir' option
if (cmdline_param != "") {
dataDir = cmdline_param;
return;
}
#if !defined(MAC_OSX) && !defined(ANDROID)
// with 'service' option
if (isService) {
#ifdef _WIN32
wchar_t commonAppData[MAX_PATH];
if(SHGetFolderPathW(NULL, CSIDL_COMMON_APPDATA, NULL, 0, commonAppData) != S_OK)
{
#ifdef WIN32_APP
MessageBox(NULL, TEXT("Unable to get common AppData path!"), TEXT("I2Pd: error"), MB_ICONERROR | MB_OK);
#else
fprintf(stderr, "Error: Unable to get common AppData path!");
#endif
exit(1);
}
else
{
2024-08-29 00:52:08 +03:00
#if ((BOOST_VERSION >= 108500) || STD_FILESYSTEM)
2024-08-28 04:49:23 +03:00
dataDir = fs_lib::path(commonAppData).string() + "\\" + appName;
#else
2024-08-28 04:49:23 +03:00
dataDir = fs_lib::wpath(commonAppData).string() + "\\" + appName;
#endif
}
#else
dataDir = "/var/lib/" + appName;
#endif
return;
}
#endif
// detect directory as usual
#ifdef _WIN32
wchar_t localAppData[MAX_PATH];
// check executable directory first
if(!GetModuleFileNameW(NULL, localAppData, MAX_PATH))
{
#ifdef WIN32_APP
MessageBox(NULL, TEXT("Unable to get application path!"), TEXT("I2Pd: error"), MB_ICONERROR | MB_OK);
#else
fprintf(stderr, "Error: Unable to get application path!");
#endif
exit(1);
}
else
{
2024-08-29 00:52:08 +03:00
#if ((BOOST_VERSION >= 108500) || STD_FILESYSTEM)
2024-08-28 04:49:23 +03:00
auto execPath = fs_lib::path(localAppData).parent_path();
#else
2024-08-28 04:49:23 +03:00
auto execPath = fs_lib::wpath(localAppData).parent_path();
#endif
// if config file exists in .exe's folder use it
2024-08-28 04:49:23 +03:00
if(fs_lib::exists(execPath/"i2pd.conf")) // TODO: magic string
{
dataDir = execPath.string ();
} else // otherwise %appdata%
{
if(SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, localAppData) != S_OK)
{
#ifdef WIN32_APP
MessageBox(NULL, TEXT("Unable to get AppData path!"), TEXT("I2Pd: error"), MB_ICONERROR | MB_OK);
#else
fprintf(stderr, "Error: Unable to get AppData path!");
#endif
exit(1);
}
else
{
2024-08-29 00:52:08 +03:00
#if ((BOOST_VERSION >= 108500) || STD_FILESYSTEM)
2024-08-28 04:49:23 +03:00
dataDir = fs_lib::path(localAppData).string() + "\\" + appName;
#else
2024-08-28 04:49:23 +03:00
dataDir = fs_lib::wpath(localAppData).string() + "\\" + appName;
#endif
}
}
}
return;
2016-02-11 03:00:00 +03:00
#elif defined(MAC_OSX)
char *home = getenv("HOME");
dataDir = (home != NULL && strlen(home) > 0) ? home : "";
dataDir += "/Library/Application Support/" + appName;
return;
2023-09-28 22:30:38 +03:00
#elif defined(__HAIKU__)
char *home = getenv("HOME");
if (home != NULL && strlen(home) > 0) {
dataDir = std::string(home) + "/config/settings/" + appName;
} else {
dataDir = "/tmp/" + appName;
}
return;
2016-02-11 03:00:00 +03:00
#else /* other unix */
#if defined(ANDROID)
const char * ext = getenv("EXTERNAL_STORAGE");
if (!ext) ext = "/sdcard";
2024-08-28 04:49:23 +03:00
if (fs_lib::exists(ext))
{
dataDir = std::string (ext) + "/" + appName;
return;
}
#endif // ANDROID
// use /home/user/.i2pd or /tmp/i2pd
char *home = getenv("HOME");
if (home != NULL && strlen(home) > 0) {
dataDir = std::string(home) + "/." + appName;
} else {
dataDir = "/tmp/" + appName;
}
return;
2016-02-11 03:00:00 +03:00
#endif
}
void SetCertsDir(const std::string & cmdline_certsdir) {
if (cmdline_certsdir != "")
{
if (cmdline_certsdir[cmdline_certsdir.length()-1] == '/')
certsDir = cmdline_certsdir.substr(0, cmdline_certsdir.size()-1); // strip trailing slash
else
certsDir = cmdline_certsdir;
}
else
{
certsDir = i2p::fs::DataDirPath("certificates");
}
return;
}
bool Init() {
2024-08-28 04:49:23 +03:00
if (!fs_lib::exists(dataDir))
fs_lib::create_directory(dataDir);
std::string destinations = DataDirPath("destinations");
2024-08-28 04:49:23 +03:00
if (!fs_lib::exists(destinations))
fs_lib::create_directory(destinations);
std::string tags = DataDirPath("tags");
2024-08-28 04:49:23 +03:00
if (!fs_lib::exists(tags))
fs_lib::create_directory(tags);
else
i2p::garlic::CleanUpTagsFiles ();
return true;
}
bool ReadDir(const std::string & path, std::vector<std::string> & files) {
2024-08-28 04:49:23 +03:00
if (!fs_lib::exists(path))
return false;
2024-08-28 04:49:23 +03:00
fs_lib::directory_iterator it(path);
fs_lib::directory_iterator end;
for ( ; it != end; it++) {
2024-08-28 04:49:23 +03:00
if (!fs_lib::is_regular_file(it->status()))
continue;
files.push_back(it->path().string());
}
return true;
}
bool Exists(const std::string & path) {
2024-08-28 04:49:23 +03:00
return fs_lib::exists(path);
}
uint32_t GetLastUpdateTime (const std::string & path)
{
2024-08-28 04:49:23 +03:00
if (!fs_lib::exists(path))
return 0;
2024-08-28 04:49:23 +03:00
#if STD_FILESYSTEM
std::error_code ec;
auto t = std::filesystem::last_write_time (path, ec);
if (ec) return 0;
2024-08-31 00:51:03 +03:00
/*#if __cplusplus >= 202002L // C++ 20 or higher
2024-08-28 18:34:07 +03:00
const auto sctp = std::chrono::clock_cast<std::chrono::system_clock>(t);
2024-08-31 00:51:03 +03:00
#else */ // TODO: wait until implemented
2024-08-28 18:34:07 +03:00
const auto sctp = std::chrono::time_point_cast<std::chrono::system_clock::duration>(
2024-08-28 04:49:23 +03:00
t - decltype(t)::clock::now() + std::chrono::system_clock::now());
2024-08-31 00:51:03 +03:00
/*#endif */
2024-08-28 04:49:23 +03:00
return std::chrono::system_clock::to_time_t(sctp);
#else
boost::system::error_code ec;
auto t = boost::filesystem::last_write_time (path, ec);
return ec ? 0 : t;
2024-08-28 04:49:23 +03:00
#endif
}
bool Remove(const std::string & path) {
2024-08-28 04:49:23 +03:00
if (!fs_lib::exists(path))
return false;
2024-08-28 04:49:23 +03:00
return fs_lib::remove(path);
}
2016-02-11 03:00:00 +03:00
2018-01-06 07:01:44 +03:00
bool CreateDirectory (const std::string& path)
{
2024-08-28 04:49:23 +03:00
if (fs_lib::exists(path) && fs_lib::is_directory (fs_lib::status (path)))
return true;
2024-08-28 04:49:23 +03:00
return fs_lib::create_directory(path);
2016-10-19 19:54:13 +03:00
}
void HashedStorage::SetPlace(const std::string &path) {
root = path + i2p::fs::dirSep + name;
}
bool HashedStorage::Init(const char * chars, size_t count) {
2024-08-28 04:49:23 +03:00
if (!fs_lib::exists(root)) {
fs_lib::create_directories(root);
}
for (size_t i = 0; i < count; i++) {
auto p = root + i2p::fs::dirSep + prefix1 + chars[i];
2024-08-28 04:49:23 +03:00
if (fs_lib::exists(p))
continue;
2024-03-11 21:29:41 +03:00
#if TARGET_OS_SIMULATOR
// ios simulator fs says it is case sensitive, but it is not
boost::system::error_code ec;
2024-08-28 04:49:23 +03:00
if (fs_lib::create_directory(p, ec))
continue;
switch (ec.value()) {
case boost::system::errc::file_exists:
case boost::system::errc::success:
continue;
default:
throw boost::system::system_error( ec, __func__ );
}
#else
2024-08-28 04:49:23 +03:00
if (fs_lib::create_directory(p))
continue; /* ^ throws exception on failure */
#endif
return false;
}
return true;
}
std::string HashedStorage::Path(const std::string & ident) const {
std::string safe_ident = ident;
std::replace(safe_ident.begin(), safe_ident.end(), '/', '-');
std::replace(safe_ident.begin(), safe_ident.end(), '\\', '-');
std::stringstream t("");
t << this->root << i2p::fs::dirSep;
t << prefix1 << safe_ident[0] << i2p::fs::dirSep;
t << prefix2 << safe_ident << "." << suffix;
return t.str();
}
void HashedStorage::Remove(const std::string & ident) {
std::string path = Path(ident);
2024-08-28 04:49:23 +03:00
if (!fs_lib::exists(path))
return;
2024-08-28 04:49:23 +03:00
fs_lib::remove(path);
}
void HashedStorage::Traverse(std::vector<std::string> & files) {
Iterate([&files] (const std::string & fname) {
files.push_back(fname);
});
}
void HashedStorage::Iterate(FilenameVisitor v)
{
2024-08-28 04:49:23 +03:00
fs_lib::path p(root);
fs_lib::recursive_directory_iterator it(p);
fs_lib::recursive_directory_iterator end;
for ( ; it != end; it++) {
2024-08-28 04:49:23 +03:00
if (!fs_lib::is_regular_file( it->status() ))
continue;
const std::string & t = it->path().string();
v(t);
}
}
2016-02-11 03:00:00 +03:00
} // fs
} // i2p