mirror of
https://github.com/xenia-project/xenia.git
synced 2025-12-06 07:12:03 +01:00
[filesystem] Moved generic methods to platform independent code
This commit is contained in:
parent
753698ea20
commit
d90c320dda
|
|
@ -24,5 +24,29 @@ bool CreateParentFolder(const std::filesystem::path& path) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<FileInfo> ListDirectories(const std::filesystem::path& path) {
|
||||||
|
std::vector<FileInfo> files = ListFiles(path);
|
||||||
|
std::vector<FileInfo> directories = {};
|
||||||
|
|
||||||
|
std::copy_if(files.cbegin(), files.cend(), std::back_inserter(directories),
|
||||||
|
[](const FileInfo& file) {
|
||||||
|
return file.type == FileInfo::Type::kDirectory;
|
||||||
|
});
|
||||||
|
|
||||||
|
return std::move(directories);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<FileInfo> FilterByName(const std::vector<FileInfo>& files,
|
||||||
|
const std::regex pattern) {
|
||||||
|
std::vector<FileInfo> filtered_entries = {};
|
||||||
|
|
||||||
|
std::copy_if(
|
||||||
|
files.cbegin(), files.cend(), std::back_inserter(filtered_entries),
|
||||||
|
[pattern](const FileInfo& file) {
|
||||||
|
return std::regex_match(file.name.filename().string(), pattern);
|
||||||
|
});
|
||||||
|
return std::move(filtered_entries);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace filesystem
|
} // namespace filesystem
|
||||||
} // namespace xe
|
} // namespace xe
|
||||||
|
|
|
||||||
|
|
@ -192,20 +192,26 @@ std::unique_ptr<FileHandle> FileHandle::OpenExisting(
|
||||||
return std::make_unique<PosixFileHandle>(path, handle);
|
return std::make_unique<PosixFileHandle>(path, handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GetInfo(const std::filesystem::path& path, FileInfo* out_info) {
|
std::optional<FileInfo> GetInfo(const std::filesystem::path& path) {
|
||||||
|
FileInfo info{};
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (stat(path.c_str(), &st) == 0) {
|
if (stat(path.c_str(), &st) == 0) {
|
||||||
if (S_ISDIR(st.st_mode)) {
|
if (S_ISDIR(st.st_mode)) {
|
||||||
out_info->type = FileInfo::Type::kDirectory;
|
info.type = FileInfo::Type::kDirectory;
|
||||||
|
// On Linux st.st_size can have non-zero size (generally 4096) so make 0
|
||||||
|
info.total_size = 0;
|
||||||
} else {
|
} else {
|
||||||
out_info->type = FileInfo::Type::kFile;
|
info.type = FileInfo::Type::kFile;
|
||||||
|
info.total_size = st.st_size;
|
||||||
}
|
}
|
||||||
out_info->create_timestamp = convertUnixtimeToWinFiletime(st.st_ctime);
|
info.path = path.parent_path();
|
||||||
out_info->access_timestamp = convertUnixtimeToWinFiletime(st.st_atime);
|
info.name = path.filename();
|
||||||
out_info->write_timestamp = convertUnixtimeToWinFiletime(st.st_mtime);
|
info.create_timestamp = convertUnixtimeToWinFiletime(st.st_ctime);
|
||||||
return true;
|
info.access_timestamp = convertUnixtimeToWinFiletime(st.st_atime);
|
||||||
|
info.write_timestamp = convertUnixtimeToWinFiletime(st.st_mtime);
|
||||||
|
return std::move(info);
|
||||||
}
|
}
|
||||||
return false;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<FileInfo> ListFiles(const std::filesystem::path& path) {
|
std::vector<FileInfo> ListFiles(const std::filesystem::path& path) {
|
||||||
|
|
@ -240,7 +246,7 @@ std::vector<FileInfo> ListFiles(const std::filesystem::path& path) {
|
||||||
result.push_back(info);
|
result.push_back(info);
|
||||||
}
|
}
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
return result;
|
return std::move(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SetAttributes(const std::filesystem::path& path, uint64_t attributes) {
|
bool SetAttributes(const std::filesystem::path& path, uint64_t attributes) {
|
||||||
|
|
|
||||||
|
|
@ -263,30 +263,6 @@ std::vector<FileInfo> ListFiles(const std::filesystem::path& path) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<FileInfo> ListDirectories(const std::filesystem::path& path) {
|
|
||||||
std::vector<FileInfo> files = ListFiles(path);
|
|
||||||
std::vector<FileInfo> directories = {};
|
|
||||||
|
|
||||||
std::copy_if(
|
|
||||||
files.cbegin(), files.cend(), std::back_inserter(directories),
|
|
||||||
[](FileInfo file) { return file.type == FileInfo::Type::kDirectory; });
|
|
||||||
|
|
||||||
return directories;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<FileInfo> FilterByName(const std::vector<FileInfo>& files,
|
|
||||||
const std::regex pattern) {
|
|
||||||
std::vector<FileInfo> filtered_entries = {};
|
|
||||||
|
|
||||||
std::copy_if(files.cbegin(), files.cend(),
|
|
||||||
std::back_inserter(filtered_entries), [pattern](FileInfo file) {
|
|
||||||
return std::regex_match(file.name.filename().string(),
|
|
||||||
pattern);
|
|
||||||
});
|
|
||||||
|
|
||||||
return filtered_entries;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SetAttributes(const std::filesystem::path& path, uint64_t attributes) {
|
bool SetAttributes(const std::filesystem::path& path, uint64_t attributes) {
|
||||||
return SetFileAttributes(path.c_str(), static_cast<DWORD>(attributes));
|
return SetFileAttributes(path.c_str(), static_cast<DWORD>(attributes));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue