2016-02-11 00:00:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2016, The PurpleI2P Project
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
2016-03-09 08:47:35 +00:00
|
|
|
#ifdef _WIN32
|
2016-02-22 14:53:26 +00:00
|
|
|
#include <shlobj.h>
|
|
|
|
#endif
|
|
|
|
|
2016-02-11 00:00:00 +00:00
|
|
|
#include "Base.h"
|
|
|
|
#include "FS.h"
|
|
|
|
#include "Log.h"
|
2017-04-03 19:05:10 +00:00
|
|
|
#include "Garlic.h"
|
2016-02-11 00:00:00 +00:00
|
|
|
|
|
|
|
namespace i2p {
|
|
|
|
namespace fs {
|
|
|
|
std::string appName = "i2pd";
|
|
|
|
std::string dataDir = "";
|
|
|
|
#ifdef _WIN32
|
|
|
|
std::string dirSep = "\\";
|
|
|
|
#else
|
|
|
|
std::string dirSep = "/";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const std::string & GetAppName () {
|
|
|
|
return appName;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetAppName (const std::string& name) {
|
|
|
|
appName = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string & GetDataDir () {
|
|
|
|
return dataDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DetectDataDir(const std::string & cmdline_param, bool isService) {
|
|
|
|
if (cmdline_param != "") {
|
|
|
|
dataDir = cmdline_param;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#if defined(WIN32) || defined(_WIN32)
|
2016-10-31 18:00:31 +00:00
|
|
|
char localAppData[MAX_PATH];
|
|
|
|
// check executable directory first
|
|
|
|
GetModuleFileName (NULL, localAppData, MAX_PATH);
|
|
|
|
auto execPath = boost::filesystem::path(localAppData).parent_path();
|
|
|
|
// if config file exists in .exe's folder use it
|
|
|
|
if(boost::filesystem::exists(execPath/"i2pd.conf")) // TODO: magic string
|
|
|
|
dataDir = execPath.string ();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// otherwise %appdata%
|
2017-06-11 06:29:31 +00:00
|
|
|
SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, localAppData);
|
2016-10-31 18:00:31 +00:00
|
|
|
dataDir = std::string(localAppData) + "\\" + appName;
|
2016-10-19 16:54:13 +00:00
|
|
|
}
|
2016-02-11 00:00:00 +00:00
|
|
|
return;
|
|
|
|
#elif defined(MAC_OSX)
|
|
|
|
char *home = getenv("HOME");
|
|
|
|
dataDir = (home != NULL && strlen(home) > 0) ? home : "";
|
|
|
|
dataDir += "/Library/Application Support/" + appName;
|
|
|
|
return;
|
|
|
|
#else /* other unix */
|
2016-06-19 13:58:29 +00:00
|
|
|
#if defined(ANDROID)
|
2016-10-31 18:00:31 +00:00
|
|
|
const char * ext = getenv("EXTERNAL_STORAGE");
|
|
|
|
if (!ext) ext = "/sdcard";
|
|
|
|
if (boost::filesystem::exists(ext))
|
2016-10-19 16:54:13 +00:00
|
|
|
{
|
2016-10-31 22:10:33 +00:00
|
|
|
dataDir = std::string (ext) + "/" + appName;
|
2016-06-19 13:58:29 +00:00
|
|
|
return;
|
2016-10-19 16:54:13 +00:00
|
|
|
}
|
|
|
|
// otherwise use /data/files
|
|
|
|
#endif
|
2016-02-11 00:00:00 +00:00
|
|
|
char *home = getenv("HOME");
|
|
|
|
if (isService) {
|
|
|
|
dataDir = "/var/lib/" + appName;
|
|
|
|
} else if (home != NULL && strlen(home) > 0) {
|
|
|
|
dataDir = std::string(home) + "/." + appName;
|
|
|
|
} else {
|
|
|
|
dataDir = "/tmp/" + appName;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Init() {
|
2016-03-10 17:05:28 +00:00
|
|
|
if (!boost::filesystem::exists(dataDir))
|
2016-02-11 00:00:00 +00:00
|
|
|
boost::filesystem::create_directory(dataDir);
|
|
|
|
std::string destinations = DataDirPath("destinations");
|
2016-03-10 17:05:28 +00:00
|
|
|
if (!boost::filesystem::exists(destinations))
|
2016-02-11 00:00:00 +00:00
|
|
|
boost::filesystem::create_directory(destinations);
|
2017-04-03 19:05:10 +00:00
|
|
|
std::string tags = DataDirPath("tags");
|
|
|
|
if (!boost::filesystem::exists(tags))
|
|
|
|
boost::filesystem::create_directory(tags);
|
|
|
|
else
|
|
|
|
i2p::garlic::CleanUpTagsFiles ();
|
2016-02-11 00:00:00 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReadDir(const std::string & path, std::vector<std::string> & files) {
|
|
|
|
if (!boost::filesystem::exists(path))
|
|
|
|
return false;
|
|
|
|
boost::filesystem::directory_iterator it(path);
|
|
|
|
boost::filesystem::directory_iterator end;
|
|
|
|
|
|
|
|
for ( ; it != end; it++) {
|
|
|
|
if (!boost::filesystem::is_regular_file(it->status()))
|
|
|
|
continue;
|
|
|
|
files.push_back(it->path().string());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Exists(const std::string & path) {
|
|
|
|
return boost::filesystem::exists(path);
|
|
|
|
}
|
|
|
|
|
2017-04-03 19:05:10 +00:00
|
|
|
uint32_t GetLastUpdateTime (const std::string & path)
|
|
|
|
{
|
|
|
|
if (!boost::filesystem::exists(path)) return 0;
|
|
|
|
boost::system::error_code ec;
|
|
|
|
auto t = boost::filesystem::last_write_time (path, ec);
|
|
|
|
return ec ? 0 : t;
|
|
|
|
}
|
|
|
|
|
2016-02-11 00:00:00 +00:00
|
|
|
bool Remove(const std::string & path) {
|
|
|
|
if (!boost::filesystem::exists(path))
|
|
|
|
return false;
|
|
|
|
return boost::filesystem::remove(path);
|
|
|
|
}
|
|
|
|
|
2016-03-14 20:05:57 +00:00
|
|
|
bool CreateDirectory (const std::string& path)
|
|
|
|
{
|
2016-10-19 16:54:13 +00:00
|
|
|
if (boost::filesystem::exists(path) &&
|
2016-03-14 20:05:57 +00:00
|
|
|
boost::filesystem::is_directory (boost::filesystem::status (path))) return true;
|
|
|
|
return boost::filesystem::create_directory(path);
|
2016-10-19 16:54:13 +00:00
|
|
|
}
|
2016-03-14 20:05:57 +00:00
|
|
|
|
2016-02-18 00:00:00 +00:00
|
|
|
void HashedStorage::SetPlace(const std::string &path) {
|
2016-02-11 00:00:00 +00:00
|
|
|
root = path + i2p::fs::dirSep + name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HashedStorage::Init(const char * chars, size_t count) {
|
|
|
|
if (!boost::filesystem::exists(root)) {
|
2016-03-08 22:57:53 +00:00
|
|
|
boost::filesystem::create_directories(root);
|
2016-02-11 00:00:00 +00:00
|
|
|
}
|
2016-10-19 16:54:13 +00:00
|
|
|
|
2016-02-11 00:00:00 +00:00
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
auto p = root + i2p::fs::dirSep + prefix1 + chars[i];
|
|
|
|
if (boost::filesystem::exists(p))
|
|
|
|
continue;
|
|
|
|
if (boost::filesystem::create_directory(p))
|
|
|
|
continue; /* ^ throws exception on failure */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-18 00:00:00 +00:00
|
|
|
std::string HashedStorage::Path(const std::string & ident) const {
|
2016-02-11 00:00:00 +00:00
|
|
|
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);
|
|
|
|
if (!boost::filesystem::exists(path))
|
|
|
|
return;
|
|
|
|
boost::filesystem::remove(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HashedStorage::Traverse(std::vector<std::string> & files) {
|
2016-08-29 18:16:29 +00:00
|
|
|
Iterate([&files] (const std::string & fname) {
|
|
|
|
files.push_back(fname);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void HashedStorage::Iterate(FilenameVisitor v)
|
|
|
|
{
|
2016-02-11 00:00:00 +00:00
|
|
|
boost::filesystem::path p(root);
|
|
|
|
boost::filesystem::recursive_directory_iterator it(p);
|
|
|
|
boost::filesystem::recursive_directory_iterator end;
|
|
|
|
|
|
|
|
for ( ; it != end; it++) {
|
|
|
|
if (!boost::filesystem::is_regular_file( it->status() ))
|
|
|
|
continue;
|
|
|
|
const std::string & t = it->path().string();
|
2016-08-29 18:16:29 +00:00
|
|
|
v(t);
|
2016-02-11 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // fs
|
|
|
|
} // i2p
|