Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys_fs, sys_ss, and sys_usbd: LV2 syscalls enhancements #13840

Merged
merged 4 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 13 additions & 41 deletions Utilities/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "StrFmt.h"
#include "Crypto/sha1.h"

#include <filesystem>
#include <unordered_map>
#include <algorithm>
#include <cstring>
Expand Down Expand Up @@ -384,55 +385,26 @@ shared_ptr<fs::device_base> fs::set_virtual_device(const std::string& name, shar
return get_device_manager().set_device(name, std::move(device));
}

std::string fs::get_parent_dir(std::string_view path, u32 levels)
std::string fs::get_parent_dir(std::string_view path, u32 parent_level)
{
std::string_view result = path;
std::string normalized_path = std::filesystem::path(path).lexically_normal().string();

// Number of path components to remove
usz to_remove = levels;
#ifdef _WIN32
std::replace(normalized_path.begin(), normalized_path.end(), '\\', '/');
#endif

if (normalized_path.back() == '/')
brian218 marked this conversation as resolved.
Show resolved Hide resolved
normalized_path.pop_back();

while (to_remove--)
while (parent_level--)
{
// Trim contiguous delimiters at the end
if (usz sz = result.find_last_not_of(delim) + 1)
{
result = result.substr(0, sz);
}
if (const auto pos = normalized_path.find_last_of('/'); pos != umax)
normalized_path = normalized_path.substr(0, pos);
else
{
return "/";
}

const auto elem = result.substr(result.find_last_of(delim) + 1);

if (elem.empty() || elem.size() == result.size())
{
break;
}

if (elem == ".")
{
to_remove += 1;
}

if (elem == "..")
{
to_remove += 2;
}

result.remove_suffix(elem.size());
}

if (usz sz = result.find_last_not_of(delim) + 1)
{
result = result.substr(0, sz);
}
else
{
return "/";
}

return std::string{result};
return normalized_path.empty() ? "/" : normalized_path;
}

bool fs::stat(const std::string& path, stat_t& info)
Expand Down
4 changes: 2 additions & 2 deletions Utilities/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ namespace fs
// Set virtual device with specified name (nullptr for deletion)
shared_ptr<device_base> set_virtual_device(const std::string& name, shared_ptr<device_base> device);

// Try to get parent directory (returns empty string on failure)
std::string get_parent_dir(std::string_view path, u32 levels = 1);
// Try to get normalized parent directory
std::string get_parent_dir(std::string_view path, u32 parent_level = 1);

// Get file information
bool stat(const std::string& path, stat_t& info);
Expand Down
19 changes: 19 additions & 0 deletions Utilities/StrUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,23 @@ namespace fmt
const u8* buf;
usz len;
};

struct string_hash
{
using hash_type = std::hash<std::string_view>;
using is_transparent = void;

std::size_t operator()(const char* str) const
{
return hash_type{}(str);
}
std::size_t operator()(std::string_view str) const
{
return hash_type{}(str);
}
std::size_t operator()(std::string const& str) const
{
return hash_type{}(str);
}
};
}
10 changes: 6 additions & 4 deletions rpcs3/Emu/Cell/Modules/cellSysCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,19 @@ error_code cellSysCacheMount(vm::ptr<CellSysCacheParam> param)
{
});

std::lock_guard lock0(g_mp_sys_dev_hdd1.mutex);

// Check if can reuse existing cache (won't if cache id is an empty string)
if (param->cacheId[0] && cache_id == cache.cache_id)
{
// Isn't mounted yet on first call to cellSysCacheMount
vfs::mount("/dev_hdd1", new_path);
if (vfs::mount("/dev_hdd1", new_path))
g_fxo->get<lv2_fs_mount_info_map>().add("/dev_hdd1", &g_mp_sys_dev_hdd1);

cellSysutil.success("Mounted existing cache at %s", new_path);
return not_an_error(CELL_SYSCACHE_RET_OK_RELAYED);
}

std::lock_guard lock0(g_mp_sys_dev_hdd1.mutex);

// Clear existing cache
if (!cache.cache_id.empty())
{
Expand All @@ -160,7 +161,8 @@ error_code cellSysCacheMount(vm::ptr<CellSysCacheParam> param)
// Set new cache id
cache.cache_id = std::move(cache_id);
fs::create_dir(new_path);
vfs::mount("/dev_hdd1", new_path);
if (vfs::mount("/dev_hdd1", new_path))
g_fxo->get<lv2_fs_mount_info_map>().add("/dev_hdd1", &g_mp_sys_dev_hdd1);

return not_an_error(CELL_SYSCACHE_RET_OK_CLEARED);
}
Expand Down
2 changes: 1 addition & 1 deletion rpcs3/Emu/Cell/lv2/lv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ const std::array<std::pair<ppu_intrp_func_t, std::string_view>, 1024> g_ppu_sysc
null_func,//BIND_SYSC(sys_usbd_...), //555 (0x22B)
BIND_SYSC(sys_usbd_get_device_speed), //556 (0x22C)
null_func,//BIND_SYSC(sys_usbd_...), //557 (0x22D)
null_func,//BIND_SYSC(sys_usbd_...), //558 (0x22E)
BIND_SYSC(sys_usbd_unregister_extra_ldd), //558 (0x22E)
BIND_SYSC(sys_usbd_register_extra_ldd), //559 (0x22F)
null_func,//BIND_SYSC(sys_...), //560 (0x230) ROOT
null_func,//BIND_SYSC(sys_...), //561 (0x231) ROOT
Expand Down