2017-10-11 18:48:27 +05:00
|
|
|
#include <nntpchan/storage.hpp>
|
|
|
|
#include <nntpchan/sanitize.hpp>
|
2017-10-13 16:58:41 +05:00
|
|
|
#include <cassert>
|
2017-10-09 20:48:10 +05:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
namespace nntpchan
|
|
|
|
{
|
|
|
|
ArticleStorage::ArticleStorage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ArticleStorage::ArticleStorage(const fs::path & fpath) {
|
|
|
|
SetPath(fpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArticleStorage::~ArticleStorage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleStorage::SetPath(const fs::path & fpath)
|
|
|
|
{
|
|
|
|
basedir = fpath;
|
|
|
|
fs::create_directories(basedir);
|
2017-10-13 16:58:41 +05:00
|
|
|
assert(init_skiplist("posts_skiplist"));
|
2017-10-09 20:48:10 +05:00
|
|
|
}
|
|
|
|
|
2017-10-13 16:58:41 +05:00
|
|
|
bool ArticleStorage::init_skiplist(const std::string & subdir) const
|
|
|
|
{
|
|
|
|
fs::path skiplist = basedir / fs::path(subdir);
|
|
|
|
fs::create_directories(skiplist);
|
|
|
|
const auto subdirs = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
|
|
|
|
for (const auto & s : subdirs)
|
|
|
|
fs::create_directories(skiplist / s);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ArticleStorage::Accept(const std::string& msgid) const
|
2017-10-09 20:48:10 +05:00
|
|
|
{
|
|
|
|
if (!IsValidMessageID(msgid)) return false;
|
|
|
|
auto p = MessagePath(msgid);
|
|
|
|
return !fs::exists(p);
|
|
|
|
}
|
|
|
|
|
2017-10-13 16:58:41 +05:00
|
|
|
fs::path ArticleStorage::MessagePath(const std::string & msgid) const
|
2017-10-09 20:48:10 +05:00
|
|
|
{
|
|
|
|
return basedir / msgid;
|
|
|
|
}
|
|
|
|
|
2017-10-13 16:58:41 +05:00
|
|
|
FileHandle_ptr ArticleStorage::OpenRead(const std::string & msgid) const
|
2017-10-09 20:48:10 +05:00
|
|
|
{
|
|
|
|
return OpenFile(MessagePath(msgid), eRead);
|
|
|
|
}
|
|
|
|
|
2017-10-13 16:58:41 +05:00
|
|
|
FileHandle_ptr ArticleStorage::OpenWrite(const std::string & msgid) const
|
2017-10-09 20:48:10 +05:00
|
|
|
{
|
|
|
|
return OpenFile(MessagePath(msgid), eWrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|