代码拉取完成,页面将自动刷新
同步操作将从 刘枭雄/云备份 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#ifndef __MY_DATA__
#define __MY_DATA__
#include<unordered_map>
#include"util.hpp"
namespace cloud
{
typedef struct _FileInfo
{
std::string filename;
std::string url_path;
std::string real_path;
size_t file_size;
time_t back_time;
bool pack_flag;
std::string pack_path;
}FileInfo;
class DataManager
{
private:
std::string _backup_file = "./backup.dat";
std::unordered_map<std::string, std::string> _backup_map;
public:
static int Split(const std::string& body, const std::string& sep, std::vector<std::string>* arry)
{
int count = 0;
size_t idx = 0, pos = 0;
while (idx < body.size())
{
pos = body.find(sep, idx);
if (pos == std::string::npos)
{
//找不到分隔符
break;
}
if (pos == idx)
{
//防止有两个连续一起的间隔符情况,相当于检索位置起始就是间隔符
idx = pos + sep.size();
continue;
}
//hi.txt=211314\nhello.txt=432244\n
std::string val = body.substr(idx, pos - idx);
arry->push_back(val);
idx = pos + sep.size(); //让下次的检索间隔符起始位置向后偏移
count++;
}
if (idx < body.size())
{
std::string val = body.substr(idx);//从idx位置截断到末尾
arry->push_back(val);
count++;
}
return count;
}
public:
DataManager()
{
InitLoad();
}
bool Storage()
{
std::vector<std::pair<std::string, std::string>>arry;
this->SelectAll(&arry);
std::stringstream ss;
for (auto& a : arry)
{
//因为两个数据中间使用了间隔符,因此实际上需要对文件名中的特殊字符进行转义编码
ss << a.first << "=" << a.second << "\n";
}
std::cout << "into Storage" << std::endl;
std::cout <<"ss.str()=" <<ss.str() << std::endl;
FileUtil(_backup_file).Write(ss.str());
return true;
}
bool InitLoad()
{
std::string body;
if (FileUtil(_backup_file).Read(&body) == false)
{
return false;
}
//hi.txt=18-324322\nhello.txt=21-3244321\n
std::vector<std::string> arry;
Split(body, "\n", &arry);
for (auto& a : arry)
{
std::vector<std::string> tmp;
Split(a, "=", &tmp);
_backup_map[tmp[0]] = tmp[1];
}
return true;
}
public:
std::string FileEtag(const std::string& pathname)
{
size_t fsize = FileUtil(pathname).Size();
time_t mtime = FileUtil(pathname).MTime();
std::stringstream ss;
ss << fsize << "-" << mtime;
return ss.str();
}
bool Insert(const std::string& pathname)
{
std::string etag = FileEtag(pathname);
_backup_map[pathname] = etag;
Storage();
return true;
}
bool Update(const std::string& pathname)
{
std::string etag = FileEtag(pathname);
_backup_map[pathname] = etag;
Storage();
return true;
}
bool SelectAll(std::vector<std::pair<std::string,std::string>> *infos)
{
for (auto it = _backup_map.begin(); it != _backup_map.end(); it++)
{
std::pair<std::string, std::string>info;
info.first = it->first;
info.second = it->second;
infos->push_back(info);
}
return true;
}
bool SelectOne(const std::string& filename, std::string *etag)
{
auto it = _backup_map.find(filename);
if (it == _backup_map.end())
{
return false;
}
*etag = it->second;
return true;
}
bool Delete(const std::string& filename)
{
auto it = _backup_map.find(filename);
if (it == _backup_map.end())
{
return false;
}
_backup_map.erase(it);
Storage();
return true;
}
};
}
#endif
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。