mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
5.1 KiB
162 lines
5.1 KiB
![]()
9 years ago
|
/*
|
||
|
* 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
|
||
|
*/
|
||
|
|
||
![]()
9 years ago
|
#ifndef FS_H__
|
||
|
#define FS_H__
|
||
|
|
||
![]()
9 years ago
|
#include <vector>
|
||
|
#include <string>
|
||
|
#include <iostream>
|
||
|
#include <sstream>
|
||
![]()
8 years ago
|
#include <functional>
|
||
![]()
9 years ago
|
|
||
|
namespace i2p {
|
||
|
namespace fs {
|
||
|
extern std::string dirSep;
|
||
|
|
||
|
/**
|
||
|
* @brief Class to work with NetDb & Router profiles
|
||
|
*
|
||
|
* Usage:
|
||
|
*
|
||
|
* const char alphabet[8] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
|
||
|
* auto h = HashedStorage("name", "y", "z-", ".txt");
|
||
![]()
9 years ago
|
* h.SetPlace("/tmp/hs-test");
|
||
![]()
9 years ago
|
* h.GetName() -> gives "name"
|
||
|
* h.GetRoot() -> gives "/tmp/hs-test/name"
|
||
![]()
9 years ago
|
* h.Init(alphabet, 8); <- creates needed dirs, 8 is size of alphabet
|
||
|
* h.Path("abcd"); <- returns /tmp/hs-test/name/ya/z-abcd.txt
|
||
|
* h.Remove("abcd"); <- removes /tmp/hs-test/name/ya/z-abcd.txt, if it exists
|
||
|
* std::vector<std::string> files;
|
||
|
* h.Traverse(files); <- finds all files in storage and saves in given vector
|
||
|
*/
|
||
|
class HashedStorage {
|
||
|
protected:
|
||
![]()
9 years ago
|
std::string root; /**< path to storage with it's name included */
|
||
|
std::string name; /**< name of the storage */
|
||
|
std::string prefix1; /**< hashed directory prefix */
|
||
|
std::string prefix2; /**< prefix of file in storage */
|
||
|
std::string suffix; /**< suffix of file in storage (extension) */
|
||
![]()
9 years ago
|
|
||
|
public:
|
||
![]()
8 years ago
|
typedef std::function<void(const std::string &)> FilenameVisitor;
|
||
![]()
9 years ago
|
HashedStorage(const char *n, const char *p1, const char *p2, const char *s):
|
||
|
name(n), prefix1(p1), prefix2(p2), suffix(s) {};
|
||
|
|
||
![]()
9 years ago
|
/** create subdirs in storage */
|
||
![]()
9 years ago
|
bool Init(const char* chars, size_t cnt);
|
||
![]()
9 years ago
|
const std::string & GetRoot() const { return root; }
|
||
|
const std::string & GetName() const { return name; }
|
||
![]()
9 years ago
|
/** set directory where to place storage directory */
|
||
![]()
9 years ago
|
void SetPlace(const std::string & path);
|
||
![]()
9 years ago
|
/** path to file with given ident */
|
||
![]()
9 years ago
|
std::string Path(const std::string & ident) const;
|
||
![]()
9 years ago
|
/** remove file by ident */
|
||
![]()
9 years ago
|
void Remove(const std::string & ident);
|
||
![]()
9 years ago
|
/** find all files in storage and store list in provided vector */
|
||
![]()
9 years ago
|
void Traverse(std::vector<std::string> & files);
|
||
![]()
8 years ago
|
/** visit every file in this storage with a visitor */
|
||
|
void Iterate(FilenameVisitor v);
|
||
![]()
9 years ago
|
};
|
||
|
|
||
|
/** @brief Returns current application name, default 'i2pd' */
|
||
|
const std::string & GetAppName ();
|
||
|
/** @brief Set applicaton name, affects autodetection of datadir */
|
||
|
void SetAppName (const std::string& name);
|
||
|
|
||
|
/** @brief Returns datadir path */
|
||
|
const std::string & GetDataDir();
|
||
|
|
||
|
/**
|
||
|
* @brief Set datadir either from cmdline option or using autodetection
|
||
|
* @param cmdline_param Value of cmdline parameter --datadir=<something>
|
||
|
* @param isService Value of cmdline parameter --service
|
||
|
*
|
||
|
* Examples of autodetected paths:
|
||
|
*
|
||
|
* Windows < Vista: C:\Documents and Settings\Username\Application Data\i2pd\
|
||
|
* Windows >= Vista: C:\Users\Username\AppData\Roaming\i2pd\
|
||
|
* Mac: /Library/Application Support/i2pd/ or ~/Library/Application Support/i2pd/
|
||
|
* Unix: /var/lib/i2pd/ (system=1) >> ~/.i2pd/ or /tmp/i2pd/
|
||
|
*/
|
||
|
void DetectDataDir(const std::string & cmdline_datadir, bool isService = false);
|
||
|
|
||
|
/**
|
||
|
* @brief Create subdirectories inside datadir
|
||
|
*/
|
||
|
bool Init();
|
||
|
|
||
|
/**
|
||
|
* @brief Get list of files in directory
|
||
|
* @param path Path to directory
|
||
|
* @param files Vector to store found files
|
||
|
* @return true on success and false if directory not exists
|
||
|
*/
|
||
![]()
8 years ago
|
bool ReadDir(const std::string & path, std::vector<std::string> & files);
|
||
![]()
9 years ago
|
|
||
|
/**
|
||
|
* @brief Remove file with given path
|
||
|
* @param path Absolute path to file
|
||
|
* @return true on success, false if file not exists, throws exception on error
|
||
|
*/
|
||
|
bool Remove(const std::string & path);
|
||
|
|
||
|
/**
|
||
|
* @brief Check existence of file
|
||
|
* @param path Absolute path to file
|
||
|
* @return true if file exists, false otherwise
|
||
|
*/
|
||
|
bool Exists(const std::string & path);
|
||
![]()
8 years ago
|
|
||
|
uint32_t GetLastUpdateTime (const std::string & path); // seconds since epoch
|
||
![]()
9 years ago
|
|
||
|
bool CreateDirectory (const std::string& path);
|
||
![]()
9 years ago
|
|
||
|
template<typename T>
|
||
|
void _ExpandPath(std::stringstream & path, T c) {
|
||
|
path << i2p::fs::dirSep << c;
|
||
|
}
|
||
|
|
||
|
template<typename T, typename ... Other>
|
||
|
void _ExpandPath(std::stringstream & path, T c, Other ... other) {
|
||
|
_ExpandPath(path, c);
|
||
|
_ExpandPath(path, other ...);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @brief Get path relative to datadir
|
||
|
*
|
||
|
* Examples (with datadir = "/tmp/i2pd"):
|
||
|
*
|
||
|
* i2p::fs::Path("test") -> '/tmp/i2pd/test'
|
||
|
* i2p::fs::Path("test", "file.txt") -> '/tmp/i2pd/test/file.txt'
|
||
|
*/
|
||
|
template<typename ... Other>
|
||
|
std::string DataDirPath(Other ... components) {
|
||
|
std::stringstream s("");
|
||
|
s << i2p::fs::GetDataDir();
|
||
|
_ExpandPath(s, components ...);
|
||
|
|
||
|
return s.str();
|
||
|
}
|
||
![]()
9 years ago
|
|
||
|
template<typename Storage, typename... Filename>
|
||
|
std::string StorageRootPath (const Storage& storage, Filename... filenames)
|
||
|
{
|
||
|
std::stringstream s("");
|
||
|
s << storage.GetRoot ();
|
||
|
_ExpandPath(s, filenames...);
|
||
|
|
||
|
return s.str();
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
} // fs
|
||
|
} // i2p
|
||
![]()
9 years ago
|
|
||
|
#endif // /* FS_H__ */
|