66 lines
1.8 KiB
C++
66 lines
1.8 KiB
C++
#include "ClientFile.h"
|
|
#include <wx/dir.h>
|
|
|
|
ClientFile::ClientFile()
|
|
{
|
|
}
|
|
|
|
bool ClientFile::GetDirFiles(const std::wstring& path, DirFileInfoVec& vec)
|
|
{
|
|
if (!fs::exists(path)) {
|
|
lastErr_ = wxString::Format(_("Path does not exist: %s"), path);
|
|
return false;
|
|
}
|
|
|
|
if (!fs::is_directory(path)) {
|
|
lastErr_ = wxString::Format(_("Not a directory: %s"), path);
|
|
return false;
|
|
}
|
|
|
|
vec.vec.clear();
|
|
|
|
try {
|
|
// 遍历目录
|
|
for (const auto& entry : fs::directory_iterator(path)) {
|
|
DirFileInfo info;
|
|
const auto& path = entry.path();
|
|
|
|
// 设置基本信息
|
|
info.fullPath = path.wstring();
|
|
info.name = path.filename().wstring();
|
|
|
|
// 设置类型
|
|
if (entry.is_directory()) {
|
|
info.type = Dir;
|
|
info.size = 0;
|
|
} else if (entry.is_regular_file()) {
|
|
info.type = File;
|
|
info.size = entry.file_size();
|
|
} else {
|
|
// 跳过符号链接、设备文件等
|
|
continue;
|
|
}
|
|
|
|
// 获取最后修改时间
|
|
auto ftime = entry.last_write_time();
|
|
auto sctp = std::chrono::time_point_cast<std::chrono::system_clock::duration>(
|
|
ftime - fs::file_time_type::clock::now() + std::chrono::system_clock::now());
|
|
info.lastModifyTime = std::chrono::system_clock::to_time_t(sctp);
|
|
vec.vec.push_back(info);
|
|
}
|
|
} catch (const fs::filesystem_error& e) {
|
|
lastErr_ = wxString::FromUTF8(e.what());
|
|
return false;
|
|
} catch (const std::exception& e) {
|
|
lastErr_ = wxString::FromUTF8(e.what());
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
wxString ClientFile::GetLastErr() const
|
|
{
|
|
return lastErr_;
|
|
}
|