|
|
@ -11,6 +11,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32 |
|
|
|
#ifdef _WIN32 |
|
|
|
#include <shlobj.h> |
|
|
|
#include <shlobj.h> |
|
|
|
|
|
|
|
#include <windows.h> |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#include "Base.h" |
|
|
|
#include "Base.h" |
|
|
@ -20,187 +21,211 @@ |
|
|
|
|
|
|
|
|
|
|
|
namespace i2p { |
|
|
|
namespace i2p { |
|
|
|
namespace fs { |
|
|
|
namespace fs { |
|
|
|
std::string appName = "i2pd"; |
|
|
|
std::string appName = "i2pd"; |
|
|
|
std::string dataDir = ""; |
|
|
|
std::string dataDir = ""; |
|
|
|
#ifdef _WIN32 |
|
|
|
#ifdef _WIN32 |
|
|
|
std::string dirSep = "\\"; |
|
|
|
std::string dirSep = "\\"; |
|
|
|
#else |
|
|
|
#else |
|
|
|
std::string dirSep = "/"; |
|
|
|
std::string dirSep = "/"; |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
const std::string & GetAppName () { |
|
|
|
const std::string & GetAppName () { |
|
|
|
return appName; |
|
|
|
return appName; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SetAppName (const std::string& name) { |
|
|
|
void SetAppName (const std::string& name) { |
|
|
|
appName = name; |
|
|
|
appName = name; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const std::string & GetDataDir () { |
|
|
|
const std::string & GetDataDir () { |
|
|
|
return dataDir; |
|
|
|
return dataDir; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void DetectDataDir(const std::string & cmdline_param, bool isService) { |
|
|
|
void DetectDataDir(const std::string & cmdline_param, bool isService) { |
|
|
|
if (cmdline_param != "") { |
|
|
|
if (cmdline_param != "") { |
|
|
|
dataDir = cmdline_param; |
|
|
|
dataDir = cmdline_param; |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
#if defined(WIN32) || defined(_WIN32) |
|
|
|
#if defined(WIN32) || defined(_WIN32) |
|
|
|
char localAppData[MAX_PATH]; |
|
|
|
char localAppData[MAX_PATH]; |
|
|
|
// check executable directory first
|
|
|
|
|
|
|
|
GetModuleFileName (NULL, localAppData, MAX_PATH); |
|
|
|
// check executable directory first
|
|
|
|
auto execPath = boost::filesystem::path(localAppData).parent_path(); |
|
|
|
if(!GetModuleFileName(NULL, localAppData, MAX_PATH)) |
|
|
|
// if config file exists in .exe's folder use it
|
|
|
|
{ |
|
|
|
if(boost::filesystem::exists(execPath/"i2pd.conf")) // TODO: magic string
|
|
|
|
#if defined(WIN32_APP) |
|
|
|
dataDir = execPath.string (); |
|
|
|
MessageBox(NULL, TEXT("Unable to get application path!"), TEXT("I2Pd: error"), MB_ICONERROR | MB_OK); |
|
|
|
else |
|
|
|
#else |
|
|
|
{ |
|
|
|
fprintf(stderr, "Error: Unable to get application path!"); |
|
|
|
// otherwise %appdata%
|
|
|
|
#endif |
|
|
|
SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, localAppData); |
|
|
|
exit(1); |
|
|
|
dataDir = std::string(localAppData) + "\\" + appName; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
return; |
|
|
|
{ |
|
|
|
|
|
|
|
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%
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, localAppData) != S_OK) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
#if defined(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 |
|
|
|
|
|
|
|
dataDir = std::string(localAppData) + "\\" + appName; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
#elif defined(MAC_OSX) |
|
|
|
#elif defined(MAC_OSX) |
|
|
|
char *home = getenv("HOME"); |
|
|
|
char *home = getenv("HOME"); |
|
|
|
dataDir = (home != NULL && strlen(home) > 0) ? home : ""; |
|
|
|
dataDir = (home != NULL && strlen(home) > 0) ? home : ""; |
|
|
|
dataDir += "/Library/Application Support/" + appName; |
|
|
|
dataDir += "/Library/Application Support/" + appName; |
|
|
|
return; |
|
|
|
return; |
|
|
|
#else /* other unix */ |
|
|
|
#else /* other unix */ |
|
|
|
#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 (boost::filesystem::exists(ext)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
dataDir = std::string (ext) + "/" + appName; |
|
|
|
dataDir = std::string (ext) + "/" + appName; |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
// otherwise use /data/files
|
|
|
|
|
|
|
|
#endif |
|
|
|
#endif |
|
|
|
char *home = getenv("HOME"); |
|
|
|
// otherwise use /data/files
|
|
|
|
if (isService) { |
|
|
|
char *home = getenv("HOME"); |
|
|
|
dataDir = "/var/lib/" + appName; |
|
|
|
if (isService) { |
|
|
|
} else if (home != NULL && strlen(home) > 0) { |
|
|
|
dataDir = "/var/lib/" + appName; |
|
|
|
dataDir = std::string(home) + "/." + appName; |
|
|
|
} else if (home != NULL && strlen(home) > 0) { |
|
|
|
} else { |
|
|
|
dataDir = std::string(home) + "/." + appName; |
|
|
|
dataDir = "/tmp/" + appName; |
|
|
|
} else { |
|
|
|
} |
|
|
|
dataDir = "/tmp/" + appName; |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool Init() { |
|
|
|
bool Init() { |
|
|
|
if (!boost::filesystem::exists(dataDir)) |
|
|
|
if (!boost::filesystem::exists(dataDir)) |
|
|
|
boost::filesystem::create_directory(dataDir); |
|
|
|
boost::filesystem::create_directory(dataDir); |
|
|
|
std::string destinations = DataDirPath("destinations"); |
|
|
|
|
|
|
|
if (!boost::filesystem::exists(destinations)) |
|
|
|
std::string destinations = DataDirPath("destinations"); |
|
|
|
boost::filesystem::create_directory(destinations); |
|
|
|
if (!boost::filesystem::exists(destinations)) |
|
|
|
std::string tags = DataDirPath("tags"); |
|
|
|
boost::filesystem::create_directory(destinations); |
|
|
|
if (!boost::filesystem::exists(tags)) |
|
|
|
|
|
|
|
boost::filesystem::create_directory(tags); |
|
|
|
std::string tags = DataDirPath("tags"); |
|
|
|
else |
|
|
|
if (!boost::filesystem::exists(tags)) |
|
|
|
i2p::garlic::CleanUpTagsFiles (); |
|
|
|
boost::filesystem::create_directory(tags); |
|
|
|
|
|
|
|
else |
|
|
|
return true; |
|
|
|
i2p::garlic::CleanUpTagsFiles (); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
bool ReadDir(const std::string & path, std::vector<std::string> & files) { |
|
|
|
} |
|
|
|
if (!boost::filesystem::exists(path)) |
|
|
|
|
|
|
|
return false; |
|
|
|
bool ReadDir(const std::string & path, std::vector<std::string> & files) { |
|
|
|
boost::filesystem::directory_iterator it(path); |
|
|
|
if (!boost::filesystem::exists(path)) |
|
|
|
boost::filesystem::directory_iterator end; |
|
|
|
return false; |
|
|
|
|
|
|
|
boost::filesystem::directory_iterator it(path); |
|
|
|
for ( ; it != end; it++) { |
|
|
|
boost::filesystem::directory_iterator end; |
|
|
|
if (!boost::filesystem::is_regular_file(it->status())) |
|
|
|
|
|
|
|
continue; |
|
|
|
for ( ; it != end; it++) { |
|
|
|
files.push_back(it->path().string()); |
|
|
|
if (!boost::filesystem::is_regular_file(it->status())) |
|
|
|
} |
|
|
|
continue; |
|
|
|
|
|
|
|
files.push_back(it->path().string()); |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
bool Exists(const std::string & path) { |
|
|
|
} |
|
|
|
return boost::filesystem::exists(path); |
|
|
|
|
|
|
|
} |
|
|
|
bool Exists(const std::string & path) { |
|
|
|
|
|
|
|
return boost::filesystem::exists(path); |
|
|
|
uint32_t GetLastUpdateTime (const std::string & path) |
|
|
|
} |
|
|
|
{ |
|
|
|
|
|
|
|
if (!boost::filesystem::exists(path)) return 0; |
|
|
|
uint32_t GetLastUpdateTime (const std::string & path) |
|
|
|
boost::system::error_code ec; |
|
|
|
{ |
|
|
|
auto t = boost::filesystem::last_write_time (path, ec); |
|
|
|
if (!boost::filesystem::exists(path)) |
|
|
|
return ec ? 0 : t; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
boost::system::error_code ec; |
|
|
|
|
|
|
|
auto t = boost::filesystem::last_write_time (path, ec); |
|
|
|
bool Remove(const std::string & path) { |
|
|
|
return ec ? 0 : t; |
|
|
|
if (!boost::filesystem::exists(path)) |
|
|
|
} |
|
|
|
return false; |
|
|
|
|
|
|
|
return boost::filesystem::remove(path); |
|
|
|
bool Remove(const std::string & path) { |
|
|
|
} |
|
|
|
if (!boost::filesystem::exists(path)) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
return boost::filesystem::remove(path); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool CreateDirectory (const std::string& path) |
|
|
|
bool CreateDirectory (const std::string& path) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (boost::filesystem::exists(path) && |
|
|
|
if (boost::filesystem::exists(path) && boost::filesystem::is_directory (boost::filesystem::status (path))) |
|
|
|
boost::filesystem::is_directory (boost::filesystem::status (path))) return true; |
|
|
|
return true; |
|
|
|
return boost::filesystem::create_directory(path); |
|
|
|
return boost::filesystem::create_directory(path); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void HashedStorage::SetPlace(const std::string &path) { |
|
|
|
void HashedStorage::SetPlace(const std::string &path) { |
|
|
|
root = path + i2p::fs::dirSep + name; |
|
|
|
root = path + i2p::fs::dirSep + name; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool HashedStorage::Init(const char * chars, size_t count) { |
|
|
|
bool HashedStorage::Init(const char * chars, size_t count) { |
|
|
|
if (!boost::filesystem::exists(root)) { |
|
|
|
if (!boost::filesystem::exists(root)) { |
|
|
|
boost::filesystem::create_directories(root); |
|
|
|
boost::filesystem::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 (boost::filesystem::exists(p)) |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
if (boost::filesystem::create_directory(p)) |
|
|
|
if (boost::filesystem::create_directory(p)) |
|
|
|
continue; /* ^ throws exception on failure */ |
|
|
|
continue; /* ^ throws exception on failure */ |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string HashedStorage::Path(const std::string & ident) const { |
|
|
|
std::string HashedStorage::Path(const std::string & ident) const { |
|
|
|
std::string safe_ident = ident; |
|
|
|
std::string safe_ident = ident; |
|
|
|
std::replace(safe_ident.begin(), safe_ident.end(), '/', '-'); |
|
|
|
std::replace(safe_ident.begin(), safe_ident.end(), '/', '-'); |
|
|
|
std::replace(safe_ident.begin(), safe_ident.end(), '\\', '-'); |
|
|
|
std::replace(safe_ident.begin(), safe_ident.end(), '\\', '-'); |
|
|
|
|
|
|
|
|
|
|
|
std::stringstream t(""); |
|
|
|
std::stringstream t(""); |
|
|
|
t << this->root << i2p::fs::dirSep; |
|
|
|
t << this->root << i2p::fs::dirSep; |
|
|
|
t << prefix1 << safe_ident[0] << i2p::fs::dirSep; |
|
|
|
t << prefix1 << safe_ident[0] << i2p::fs::dirSep; |
|
|
|
t << prefix2 << safe_ident << "." << suffix; |
|
|
|
t << prefix2 << safe_ident << "." << suffix; |
|
|
|
|
|
|
|
|
|
|
|
return t.str(); |
|
|
|
return t.str(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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 (!boost::filesystem::exists(path)) |
|
|
|
return; |
|
|
|
return; |
|
|
|
boost::filesystem::remove(path); |
|
|
|
boost::filesystem::remove(path); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void HashedStorage::Traverse(std::vector<std::string> & files) { |
|
|
|
void HashedStorage::Traverse(std::vector<std::string> & files) { |
|
|
|
Iterate([&files] (const std::string & fname) { |
|
|
|
Iterate([&files] (const std::string & fname) { |
|
|
|
files.push_back(fname); |
|
|
|
files.push_back(fname); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void HashedStorage::Iterate(FilenameVisitor v) |
|
|
|
void HashedStorage::Iterate(FilenameVisitor v) |
|
|
|
{ |
|
|
|
{ |
|
|
|
boost::filesystem::path p(root); |
|
|
|
boost::filesystem::path p(root); |
|
|
|
boost::filesystem::recursive_directory_iterator it(p); |
|
|
|
boost::filesystem::recursive_directory_iterator it(p); |
|
|
|
boost::filesystem::recursive_directory_iterator end; |
|
|
|
boost::filesystem::recursive_directory_iterator end; |
|
|
|
|
|
|
|
|
|
|
|
for ( ; it != end; it++) { |
|
|
|
for ( ; it != end; it++) { |
|
|
|
if (!boost::filesystem::is_regular_file( it->status() )) |
|
|
|
if (!boost::filesystem::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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} // fs
|
|
|
|
} // fs
|
|
|
|
} // i2p
|
|
|
|
} // i2p
|
|
|
|