Base: Refine directory permissions test

This commit is contained in:
Chris Hennes
2025-09-21 13:00:36 -05:00
parent 15adc6f2c8
commit 25269bbf73

View File

@@ -26,6 +26,7 @@
#include <algorithm>
#include <codecvt>
#include <cstring>
#include <fstream>
#include <iostream>
#include <system_error>
#ifdef FC_OS_WIN32
@@ -304,12 +305,37 @@ bool FileInfo::isReadable() const
return (perms & fs::perms::owner_read) == fs::perms::owner_read;
}
bool directoryIsWritable(const fs::path& dir)
{
try {
if (!fs::exists(dir) || !fs::is_directory(dir)) {
return false;
}
fs::path test_file = dir / (".fs_perm_test_" + std::to_string(std::rand()) + ".tmp");
{
std::ofstream ofs(test_file);
if (!ofs) {
return false;
}
}
std::error_code ec;
fs::remove(test_file, ec);
return true;
}
catch (...) {
return false;
}
}
bool FileInfo::isWritable() const
{
fs::path path = stringToPath(FileName);
if (!fs::exists(path)) {
return false;
}
if (fs::is_directory(path)) {
return directoryIsWritable(path);
}
fs::file_status stat = fs::status(path);
fs::perms perms = stat.permissions();
return (perms & fs::perms::owner_write) == fs::perms::owner_write;