目录
🌳.文件信息管理模块介绍
🌳.FileInfo类的设计
🌳. FileInfoManger类的设计(文件信息管理类)
🍎.FileInfoManger类的成员变量
🍎.成员函数
FileInfo类的变量:
FileInfo类的构造函数,使用配置模块和文件工具类初始化FileInfo中的成员变量
#include"Config.hpp" class FileInfo{ public: bool pack_sign; size_t file_size; time_t access_time; time_t modify_time; std::string back_path; std::string pack_path; std::string url; public: FileInfo(const std::string& backpath){ AddFileInfo(backpath); } FileInfo(){} bool AddFileInfo(const std::string& backpath){ sjp::FileUtil fu(backpath.c_str()); file_size=fu.GetFileSize(); pack_sign=false; access_time=fu.GetFileAccessTime(); modify_time=fu.GetFileModfityTime(); back_path=sjp::Config::GetInstant()->GetBackDir(); back_path+=fu.GetFilename(); pack_path=sjp::Config::GetInstant()->GetPackDir(); pack_path+=fu.GetFilename(); pack_path+=sjp::Config::GetInstant()->GetPackFileSuffix(); url="/download/"; url+=fu.GetFilename(); return true; } };
InitLoad函数的功能加载backfileinfo文件中的所有的文件信息加载到FileInfoManger对象里面。
//将文件上的信息加载到内存中bool InitLoad(){/** 1.打开备份文件的存储信息2.将存储信息读取到body中3.将body进行反序列化为root;4.将各个文件信息加载到table中*/sjp::FileUtil fu(file_info_path.c_str()); std::string body;fu.GetContent(body);Json::Value root;sjp::JsonUtil::UnSerialize(root,body);
W> for(int i=0;i
UpdateFileInfo的功能是查看backdir下的文件信息是否全都存储在backfileinfo,如果某些文件没有存储,则将该文件的信息存储进backfileinfo里面。
实现思路:
bool UpdateFileInfo(){std::string path=sjp::Config::GetInstant()->GetBackDir();sjp::FileUtil fu(path);std::vector arry;fu.GetPathInDir(arry);W> for(int i=0;i //没有将该文件信息存储到信息系统中sjp::FileInfo fileinfo(arry[i]); Insert(fileinfo);}}Storage();
W> }
Storage函数的功能将_table中的所有信息持久化存储到backfileinfo文件里面。
实现思路:
//将table上的所有信息存储持久化到文件上bool Storage(){//将文件的备份信息反序列化字符并组织到body上std::string filebody;Json::Value root; std::string body;auto it=_table.begin();int i=0;while(it!=_table.end()){root[i]["AccessTime"]=(Json::Int)it->second.access_time; root[i]["ModifyTime"]=(Json::Int)it->second.modify_time; root[i]["FileSize"]=(Json::Int)it->second.file_size; root[i]["Url"]=it->second.url; root[i]["PackSign"]=it->second.pack_sign; root[i]["BackPath"]=it->second.back_path; root[i]["PackPath"]=it->second.pack_path;i++;it++;}sjp::JsonUtil::Serialize(root,body);sjp::FileUtil fu(file_info_path.c_str());if(!fu.SetContent(body)){return false;}return true;}
UpdatePackSign函数的功能是将packdir中的下文件信息中的压缩标志为false,则更改为true,表示该文件已经被压缩。
bool UpdatePackSign(){cout<<"Updatepacksign"<second.pack_path);if(fu.Exist()){it->second.pack_sign=true;Insert(it->second);}it++;}Storage();
W> }
FileInfoManger(){file_info_path=Config::GetInstant()->GetBackFileInfo();pthread_rwlock_init(&_rwlock,NULL);InitLoad(); UpdateFileInfo();UpdatePackSign();}
#pragma once #include"Config.hpp" #include #include namespace sjp{ class FileInfoManger{ private:std::string file_info_path;//存储文件信息的持久化文件std::unordered_map _table; //backpath-infopthread_rwlock_t _rwlock;//读写锁private:static FileInfoManger* instant;static std::mutex lock;private:FileInfoManger(FileInfoManger&)=delete;public:static FileInfoManger* GetInstant(){ if(instant==nullptr){ lock.lock(); if(instant==nullptr){ instant=new FileInfoManger(); } lock.unlock(); } return instant; } //插入信息bool Insert(const FileInfo& info){pthread_rwlock_wrlock(&_rwlock);_table[info.back_path]=info;pthread_rwlock_unlock(&_rwlock);return true;}//更新信息bool update(const FileInfo& info){pthread_rwlock_wrlock(&_rwlock);_table[info.back_path]=info;pthread_rwlock_unlock(&_rwlock);return true;} //通过backpath判断该文件是否存在bool Exist(const std::string& backpath){auto it=_table.find(backpath);if(it!=_table.end()){return true;}return false;}//通过url获取文件信息bool GetoneByURL(const std::string& url,FileInfo& info){sjp::FileUtil fu(url); std::string backpath=sjp::Config::GetInstant()->GetBackDir();backpath+=fu.GetFilename();return GetOneByRealPath(backpath,info);}//通过相对路径获取文件信息bool GetOneByRealPath(const std::string& backpath,FileInfo& info){auto it=_table.find(backpath);if(it!=_table.end()){info=it->second;return true;}return false;}//获取所有备份文件的文件信息bool GetAllInfo( std::vector& arry){pthread_rwlock_wrlock(&_rwlock);auto it=_table.begin();while(it!=_table.end()){arry.push_back(it->second);it++;}pthread_rwlock_unlock(&_rwlock);return true;}~FileInfoManger(){Storage();}};FileInfoManger* FileInfoManger::instant=nullptr;std::mutex FileInfoManger::lock;}