Remove working directory translations

This commit is contained in:
momo5502
2025-02-08 17:31:53 +01:00
parent 72e88d30d4
commit 7efe75ba97
3 changed files with 11 additions and 44 deletions

View File

@@ -4,28 +4,22 @@
#include <platform/compiler.hpp>
struct working_directory_provider
{
virtual windows_path get_working_directory() = 0;
};
class file_system
{
public:
file_system(std::filesystem::path root, working_directory_provider& working_dir_provider)
: root_(std::move(root)),
working_dir_provider_(&working_dir_provider)
file_system(std::filesystem::path root)
: root_(std::move(root))
{
}
std::filesystem::path translate(const windows_path& win_path) const
{
assert(win_path.is_absolute() && "Path should always be absolute");
const auto& full_path = win_path.is_absolute() //
? win_path
: (this->working_dir_provider_->get_working_directory() / win_path);
if (!win_path.is_absolute())
{
throw std::runtime_error("Only absolute paths can be translated!");
}
const auto mapping = this->mappings_.find(full_path);
const auto mapping = this->mappings_.find(win_path);
if (mapping != this->mappings_.end())
{
return mapping->second;
@@ -34,19 +28,14 @@ class file_system
#ifdef OS_WINDOWS
if (this->root_.empty())
{
return full_path.u16string();
return win_path.u16string();
}
#endif
// TODO: Sanitize path to prevent traversal!
return this->root_ / full_path.to_portable_path();
return this->root_ / win_path.to_portable_path();
}
/*const windows_path& get_working_directory() const
{
return this->working_dir_;
}*/
windows_path local_to_windows_path(const std::filesystem::path& local_path) const
{
const auto absolute_local_path = absolute(local_path);
@@ -84,6 +73,5 @@ class file_system
private:
std::filesystem::path root_{};
working_directory_provider* working_dir_provider_{};
std::unordered_map<windows_path, std::filesystem::path> mappings_{};
};

View File

@@ -866,7 +866,7 @@ windows_emulator::windows_emulator(const emulator_settings& settings, emulator_c
windows_emulator::windows_emulator(const std::filesystem::path& emulation_root, std::unique_ptr<x64_emulator> emu)
: emulation_root_{emulation_root.empty() ? emulation_root : absolute(emulation_root)},
file_sys_(emulation_root_.empty() ? emulation_root_ : emulation_root_ / "filesys", *this),
file_sys_(emulation_root_.empty() ? emulation_root_ : emulation_root_ / "filesys"),
emu_(std::move(emu)),
process_(*emu_, file_sys_)
{
@@ -942,25 +942,6 @@ bool windows_emulator::activate_thread(const uint32_t id)
return switch_to_thread(*this, *thread, true);
}
windows_path windows_emulator::get_working_directory()
{
if (!this->process_.peb)
{
return {};
}
const auto peb = this->process_.peb.read();
if (!peb.ProcessParameters)
{
return {};
}
const auto& emu = this->emu();
const auto process_params = emu.read_memory<RTL_USER_PROCESS_PARAMETERS64>(peb.ProcessParameters);
return read_unicode_string(emu, process_params.CurrentDirectory.DosPath);
}
void windows_emulator::on_instruction_execution(const uint64_t address)
{
auto& process = this->process();

View File

@@ -48,7 +48,7 @@ enum class apiset_location : uint8_t
default_windows_11
};
class windows_emulator : working_directory_provider
class windows_emulator
{
public:
windows_emulator(const std::filesystem::path& emulation_root,
@@ -190,8 +190,6 @@ class windows_emulator : working_directory_provider
return this->emulation_root_;
}
windows_path get_working_directory() override;
private:
std::filesystem::path emulation_root_{};
file_system file_sys_;