fix: sanitize paths on Windows (gh #241)

This commit is contained in:
Marcus Holland-Moritz 2024-10-20 22:20:09 +02:00
parent 09d4748e87
commit 75300dbfc6
6 changed files with 10 additions and 9 deletions

View File

@ -124,7 +124,7 @@ file_stat::file_stat(fs::path const& path) {
status.type() == fs::file_type::not_found status.type() == fs::file_type::not_found
? "file not found" ? "file not found"
: "unknown file type", : "unknown file type",
u8string_to_string(path.u8string()))); path_to_utf8_string_sanitized(path)));
} }
valid_fields_ = file_stat::mode_valid; valid_fields_ = file_stat::mode_valid;

View File

@ -94,7 +94,7 @@ class incompressible_categorizer_job_ : public sequential_categorizer_job {
auto stats = [this] { auto stats = [this] {
return fmt::format("{} -> incompressible blocks: {}/{}, overall " return fmt::format("{} -> incompressible blocks: {}/{}, overall "
"compression ratio: {:.2f}%", "compression ratio: {:.2f}%",
u8string_to_string(path_.u8string()), path_to_utf8_string_sanitized(path_),
incompressible_blocks_, total_blocks_, incompressible_blocks_, total_blocks_,
100.0 * total_output_size_ / total_input_size_); 100.0 * total_output_size_ / total_input_size_);
}; };

View File

@ -101,7 +101,7 @@ fs::path entry::fs_path() const {
} }
std::string entry::path_as_string() const { std::string entry::path_as_string() const {
return u8string_to_string(fs_path().u8string()); return path_to_utf8_string_sanitized(fs_path());
} }
std::string entry::dpath() const { std::string entry::dpath() const {
@ -409,7 +409,7 @@ void dir::remove_empty_dirs(progress& prog) {
} }
std::shared_ptr<entry> dir::find(fs::path const& path) { std::shared_ptr<entry> dir::find(fs::path const& path) {
auto name = u8string_to_string(path.filename().u8string()); auto name = path_to_utf8_string_sanitized(path.filename());
if (!lookup_ && entries_.size() >= 16) { if (!lookup_ && entries_.size() >= 16) {
populate_lookup_table(); populate_lookup_table();
@ -449,7 +449,7 @@ const std::string& link::linkname() const { return link_; }
void link::accept(entry_visitor& v, bool) { v.visit(this); } void link::accept(entry_visitor& v, bool) { v.visit(this); }
void link::scan(os_access const& os, progress& prog) { void link::scan(os_access const& os, progress& prog) {
link_ = u8string_to_string(os.read_symlink(fs_path()).u8string()); link_ = path_to_utf8_string_sanitized(os.read_symlink(fs_path()));
prog.original_size += size(); prog.original_size += size();
prog.symlink_size += size(); prog.symlink_size += size();
} }

View File

@ -351,7 +351,7 @@ class inode_ : public inode {
if (mm) { if (mm) {
if (auto size = mm->size(); size >= min_size) { if (auto size = mm->size(); size >= min_size) {
return prog.create_context<scanner_progress>( return prog.create_context<scanner_progress>(
context, u8string_to_string(mm->path().u8string()), size); context, path_to_utf8_string_sanitized(mm->path()), size);
} }
} }
return nullptr; return nullptr;

View File

@ -189,7 +189,7 @@ template <typename LoggerPolicy>
void rule_based_entry_filter_<LoggerPolicy>::set_root_path( void rule_based_entry_filter_<LoggerPolicy>::set_root_path(
fs::path const& path) { fs::path const& path) {
// TODO: this whole thing needs to be windowsized // TODO: this whole thing needs to be windowsized
root_path_ = u8string_to_string(path.u8string()); root_path_ = path_to_utf8_string_sanitized(path);
if constexpr (kLocalPathSeparator != '/') { if constexpr (kLocalPathSeparator != '/') {
// Both '/' and '\\' are, surprisingly, valid path separators on Windows, // Both '/' and '\\' are, surprisingly, valid path separators on Windows,

View File

@ -467,7 +467,7 @@ scanner_<LoggerPolicy>::add_entry(std::filesystem::path const& name,
default: default:
LOG_ERROR << "unsupported entry type: " << int(pe->type()) << " (" LOG_ERROR << "unsupported entry type: " << int(pe->type()) << " ("
<< pe->fs_path() << ")"; << pe->path_as_string() << ")";
prog.errors++; prog.errors++;
break; break;
} }
@ -552,7 +552,8 @@ scanner_<LoggerPolicy>::scan_tree(std::filesystem::path const& path,
prog.dirs_scanned++; prog.dirs_scanned++;
} catch (const std::system_error& e) { } catch (const std::system_error& e) {
LOG_ERROR << "cannot read directory `" << ppath LOG_ERROR << "cannot read directory `"
<< path_to_utf8_string_sanitized(ppath)
<< "`: " << exception_str(e); << "`: " << exception_str(e);
prog.errors++; prog.errors++;
} }