419 lines
8.6 KiB
C++
419 lines
8.6 KiB
C++
#include "io/dl_file.h"
|
||
|
||
#include <filesystem>
|
||
#include <regex>
|
||
|
||
#include "io/dl_log.h"
|
||
#include "dl_algorithm.h"
|
||
#include "memory/dl_code_cvt.h"
|
||
#include "memory/dl_stream_input.h"
|
||
#include "memory/dl_stream_output.h"
|
||
#include "dl_string.h"
|
||
|
||
namespace dl
|
||
{
|
||
|
||
|
||
|
||
std::u8string_view CastU8StringView(std::string_view str)
|
||
{
|
||
return { (char8_t*)str.data(), str.size() };
|
||
}
|
||
|
||
std::string_view CastStringView(std::u8string_view str)
|
||
{
|
||
return { (char*)str.data(), str.size() };
|
||
}
|
||
|
||
std::string ToString(std::u8string_view str)
|
||
{
|
||
return { (char*)str.data(), str.size() };
|
||
}
|
||
|
||
Buffer File::GetBuffer(std::ifstream& ifs)
|
||
{
|
||
Buffer ret;
|
||
|
||
ifs.seekg(0, std::ios::end);
|
||
ret.resize(ifs.tellg());
|
||
ifs.seekg(0, std::ios::beg);
|
||
|
||
|
||
ifs.read((char*)ret.data(), ret.size());
|
||
ifs.seekg(0, std::ios::beg);
|
||
return ret;
|
||
}
|
||
|
||
|
||
Buffer File::GetBuffer(std::string_view path_name)
|
||
{
|
||
std::ifstream ifs = Get(path_name, true);
|
||
if (!ifs.is_open())
|
||
{
|
||
log_err("文件打开失败: {}", path_name );
|
||
return {};
|
||
}
|
||
return GetBuffer(ifs);
|
||
}
|
||
//
|
||
//
|
||
//bool File::HashBlake2b(string_view path_name, Crypto::Blake2b& blake2b)
|
||
//{
|
||
// Buffer buf = GetBuffer(path_name);
|
||
// if (buf.Empty())
|
||
// return false;
|
||
//
|
||
// blake2b = Crypto::HashBlake2b(buf);
|
||
// buf.Delete();
|
||
//
|
||
// return true;
|
||
//}
|
||
//
|
||
//
|
||
//bool File::HashBlake2bCheck(string_view path_name, const Crypto::Blake2b& digest)
|
||
//{
|
||
// Buffer buf = GetBuffer(path_name);
|
||
// if (buf.Empty())
|
||
// return false;
|
||
//
|
||
// bool ret = Crypto::HashBlake2bCheck(buf, digest);
|
||
// buf.Delete();
|
||
//
|
||
// return ret;
|
||
//}
|
||
//
|
||
//
|
||
//uint32_t File::GetCrc32(string_view path_name)
|
||
//{
|
||
// Buffer buf = GetBuffer(path_name);
|
||
// if (buf.Empty())
|
||
// return 0;
|
||
//
|
||
// uint32_t ret = Crypto::HashCrc32(buf);
|
||
// buf.Delete();
|
||
// return ret;
|
||
//}
|
||
|
||
|
||
void File::GetPathFileAll(std::string_view path, std::vector<std::string>& ret,
|
||
std::string_view regex, bool recursion)
|
||
{
|
||
//构造path后是wstring,最后返回再转为utf-8
|
||
|
||
using directory_iterator = std::filesystem::directory_iterator;
|
||
std::list<directory_iterator> vec_path;
|
||
|
||
directory_iterator path_iter{ CastU8StringView(path) };
|
||
vec_path.push_back(path_iter);
|
||
|
||
bool no_reg = true;
|
||
std::regex reg;
|
||
if (!regex.empty())
|
||
{
|
||
reg = std::string{ regex };
|
||
no_reg = false;
|
||
}
|
||
|
||
operate_front(vec_path, [&](const directory_iterator& iter_path)
|
||
{
|
||
for (auto& fe : iter_path)
|
||
{
|
||
if (fe.status().type() == std::filesystem::file_type::directory)
|
||
{
|
||
if(recursion)
|
||
vec_path.emplace_back(directory_iterator{ fe });
|
||
}
|
||
else
|
||
{
|
||
std::string path_name = fe.path().string();
|
||
//String::CvtPathSlash(path_name);
|
||
|
||
if (no_reg || std::regex_search(path_name, reg))
|
||
ret.emplace_back(path_name);
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
|
||
FILE* File::GetFile(std::string_view path_name, bool is_binary /*= true*/)
|
||
{
|
||
//fp = nullptr;
|
||
//fopen_s(&fp, CodeCvt::cvt_u8_mb(path_name).c_str(), is_binary ? "rb" : "r");
|
||
//if (fp == nullptr)
|
||
// return 0;
|
||
//return 1;
|
||
FILE* ret = fopen(std::string{ path_name }.c_str(), is_binary ? "rb" : "r");
|
||
return ret;
|
||
}
|
||
|
||
std::ifstream File::Get(std::string_view path_name, bool is_binary /*= true*/)
|
||
{
|
||
//CodeCvt::cvt_u8_mb(path_name)
|
||
//ifs.open(, is_binary ? (ifstream::binary | ifstream::in) : ifstream::in);
|
||
std::ifstream ret;
|
||
ret.open(path_name, is_binary ? (std::ifstream::binary | std::ifstream::in) : std::ifstream::in);
|
||
return ret;
|
||
}
|
||
|
||
|
||
bool File::ConvertMb2U8(std::string_view path_name, std::string_view out)
|
||
{
|
||
std::ifstream ifs = Get(path_name, false);
|
||
if (!ifs.is_open())
|
||
{
|
||
log_err("转换文件编码,文件加载失败:{}", path_name);
|
||
return false;
|
||
}
|
||
|
||
|
||
std::ofstream ofs;
|
||
ofs.open(out);
|
||
if (!ofs.is_open())
|
||
{
|
||
log_err("转换文件编码,文件创建失败:{}", out );
|
||
return false;
|
||
}
|
||
|
||
std::istreambuf_iterator<char> beg(ifs), end;
|
||
std::string str(beg, end);
|
||
ifs.close();
|
||
|
||
std::string str_u8 = CodeCvt::MultiByteToUtf8(str);
|
||
ofs << str_u8;
|
||
|
||
return true;
|
||
}
|
||
|
||
time_t File::GetFileLastWriteTime(std::string_view path_name)
|
||
{
|
||
struct _stat64 fileInfo;
|
||
std::string p = CodeCvt::Utf8ToMultiByte(path_name);
|
||
if (_stat64(p.c_str(), &fileInfo) != 0)
|
||
{
|
||
log_err("获取文件信息失败: {}", path_name);
|
||
}
|
||
return fileInfo.st_mtime;
|
||
}
|
||
|
||
|
||
bool File::CheckPathFileModified(std::string_view path_name, std::string_view cache, bool update)
|
||
{
|
||
log(fmt::format("尝试对文件夹进行改动检查:{},cache:{}", path_name, cache));
|
||
|
||
|
||
constexpr uint32_t VERSION = 0;
|
||
struct SL
|
||
{
|
||
uint32_t _crc32;//32
|
||
char _pathName[520];//992
|
||
};
|
||
|
||
if (!IsExist(cache))
|
||
{//cache文件不存在
|
||
|
||
//按遍历加入即可
|
||
std::list<SL> list_sl;
|
||
|
||
std::vector<std::string> mul_file;
|
||
GetPathFileAll(path_name, mul_file);
|
||
|
||
//遍历文件夹所有文件,记录{path_name, write_time}
|
||
for (auto& iter : mul_file)
|
||
{
|
||
log("开始读取文件:" + iter);
|
||
|
||
//
|
||
uint32_t crc32 = GetCrc32(iter);
|
||
SL sl;
|
||
sl._crc32 = crc32;
|
||
strcpy(sl._pathName, iter.c_str());
|
||
|
||
list_sl.emplace_back(sl);
|
||
}
|
||
|
||
//写入cache文件
|
||
StreamOutput s;
|
||
|
||
s.Write(&VERSION);
|
||
s.Write(&list_sl);
|
||
|
||
//
|
||
s.Save(cache);
|
||
|
||
}
|
||
else
|
||
{//cache文件存在
|
||
StreamInput s;
|
||
|
||
if (!s.LoadFromFile(cache))
|
||
{
|
||
log_err("输入流从cache加载失败:{}", cache);
|
||
return true;
|
||
}
|
||
|
||
//
|
||
uint32_t version;
|
||
s.Read(&version);
|
||
if (version != VERSION)
|
||
{
|
||
log_err("VERSION值不匹配!");
|
||
return true;
|
||
}
|
||
|
||
std::list<SL> list_sl;
|
||
s.Read(&list_sl);
|
||
|
||
//读取到cache文件数据后,再与现有文件比对
|
||
bool not_match = false;
|
||
|
||
std::vector<std::string> mul_file;
|
||
GetPathFileAll(path_name, mul_file);
|
||
|
||
//数量不一致
|
||
if (list_sl.size() != mul_file.size())
|
||
{
|
||
not_match = true;
|
||
}
|
||
else
|
||
{
|
||
auto iter = list_sl.begin();
|
||
//遍历文件夹所有文件
|
||
for (auto& iter_file : mul_file)
|
||
{
|
||
//已经数据不够,说明新增加了文件
|
||
if (iter == list_sl.end())
|
||
{
|
||
not_match = true;
|
||
break;
|
||
}
|
||
|
||
//比较操作
|
||
uint32_t crc32 = GetCrc32(iter_file);
|
||
|
||
if (crc32 != iter->_crc32
|
||
|| iter_file != iter->_pathName)
|
||
{
|
||
log_msg("此文件发生了修改(或新增):{},crc32:{} -> {}",
|
||
iter_file, iter->_crc32, crc32);
|
||
not_match = true;
|
||
break;
|
||
}
|
||
|
||
++iter;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
if (not_match && update)
|
||
{
|
||
//删除
|
||
Delete(cache);
|
||
|
||
//再重建
|
||
if (CheckPathFileModified(path_name, cache, false) == false)
|
||
log_err("此处应该返回真。");
|
||
}
|
||
return not_match;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
void File::DeleteContent(std::string_view path_name)
|
||
{
|
||
std::filesystem::path p{ CastU8StringView(path_name) };
|
||
if (!exists(p))
|
||
return;
|
||
for (auto& de : std::filesystem::directory_iterator(p))
|
||
{
|
||
std::filesystem::remove_all(de.path());
|
||
}
|
||
}
|
||
|
||
bool File::Copy(std::string_view source, std::string_view target)
|
||
{
|
||
std::filesystem::path p0{ CastU8StringView(source) };
|
||
std::filesystem::path p1{ CastU8StringView(target) };
|
||
std::error_code e;
|
||
copy(p0, p1, e);
|
||
if (e)
|
||
{
|
||
log_err0(to_string(e));
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
std::string File::GetExtension(std::string_view path_name, bool has_dot /*= false*/)
|
||
{
|
||
std::filesystem::path p{ CastU8StringView(path_name) };
|
||
std::string ret = ToString(p.extension().u8string());
|
||
if (!has_dot && !ret.empty())
|
||
return std::string{ ret.begin() + 1, ret.end() };
|
||
return ret;
|
||
}
|
||
|
||
std::string File::GetFilename(std::string_view path_name)
|
||
{
|
||
std::filesystem::path p{ CastU8StringView(path_name) };
|
||
return ToString(p.filename().u8string());
|
||
}
|
||
|
||
std::string File::GetStem(std::string_view path_name)
|
||
{
|
||
std::filesystem::path p{ CastU8StringView(path_name) };
|
||
return ToString(p.stem().u8string());
|
||
}
|
||
|
||
bool File::CreateDirectory(std::string_view path)
|
||
{
|
||
//create_directory要求父目录存在??
|
||
std::filesystem::path p{ CastU8StringView(path) };
|
||
return create_directories(p);
|
||
}
|
||
|
||
std::string File::GetPathCurrent()
|
||
{
|
||
std::filesystem::path p = std::filesystem::current_path();
|
||
std::string ret = ToString(p.u8string());
|
||
ret += '/';
|
||
String::CvtPathSlash(ret);
|
||
return ret;
|
||
}
|
||
|
||
std::string File::GetPathTemp()
|
||
{
|
||
std::filesystem::path p = std::filesystem::temp_directory_path();
|
||
std::string ret = ToString(p.u8string());
|
||
String::CvtPathSlash(ret);
|
||
return ret;
|
||
}
|
||
|
||
bool File::IsExist(std::string_view path_name)
|
||
{
|
||
std::filesystem::path p{ CastU8StringView(path_name) };
|
||
return exists(p);
|
||
}
|
||
|
||
bool File::Delete(std::string_view path_name, bool all /*= false*/)
|
||
{
|
||
std::filesystem::path p{ CastU8StringView(path_name) };
|
||
return all ? remove_all(p) : remove(p);
|
||
}
|
||
|
||
bool File::SaveBuffer(const Buffer& buf, std::string_view path_name)
|
||
{
|
||
std::ofstream ofs(CodeCvt::Utf8ToMultiByte(path_name), std::ios::binary);
|
||
if (!ofs.is_open())
|
||
return false;
|
||
ofs.write((char*)buf.data(), buf.size());
|
||
return true;
|
||
}
|
||
|
||
uint32_t File::GetCrc32(std::string_view path_name)
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
} |