rpcsx/rpcs3/Emu/FS/VFS.cpp

577 lines
12 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "VFS.h"
#include "vfsDir.h"
#include "vfsFile.h"
2014-08-25 20:09:48 +02:00
#include "vfsDirBase.h"
2013-11-19 11:30:58 +01:00
#include "Emu/HDD/HDD.h"
#include "vfsDeviceLocalFile.h"
2014-07-11 13:59:13 +02:00
#include "Ini.h"
2014-11-29 14:16:53 +01:00
#include "Emu/System.h"
#include "Utilities/Log.h"
2014-11-29 14:16:53 +01:00
std::vector<std::string> simplify_path_blocks(const std::string& path)
{
2015-01-08 23:17:26 +01:00
// fmt::tolower() removed
std::vector<std::string> path_blocks = std::move(fmt::split(path, { "/", "\\" }));
2014-11-29 14:16:53 +01:00
2015-04-24 02:35:42 +02:00
for (s32 i = 0; i < path_blocks.size(); ++i)
2014-11-29 14:16:53 +01:00
{
2015-04-24 02:35:42 +02:00
if (path_blocks[i] == "." || (i > 0 && path_blocks[i].empty()))
2014-11-29 14:16:53 +01:00
{
2015-04-24 02:35:42 +02:00
path_blocks.erase(path_blocks.begin() + i);
i--;
2014-11-29 14:16:53 +01:00
}
2015-04-24 02:35:42 +02:00
else if (i > 0 && path_blocks[i] == "..")
2014-11-29 14:16:53 +01:00
{
path_blocks.erase(path_blocks.begin() + (i - 1), path_blocks.begin() + (i + 1));
i--;
}
}
return path_blocks;
}
2015-01-08 23:17:26 +01:00
std::string simplify_path(const std::string& path, bool is_dir, bool is_ps3)
2014-11-29 14:16:53 +01:00
{
std::vector<std::string> path_blocks = simplify_path_blocks(path);
if (path_blocks.empty())
2014-12-21 00:32:19 +01:00
return "";
2014-11-29 14:16:53 +01:00
2014-12-21 00:32:19 +01:00
std::string result = fmt::merge(path_blocks, "/");
2015-01-08 23:17:26 +01:00
#ifdef _WIN32
if (is_ps3)
#endif
{
result = "/" + result;
}
if (is_dir) result = result + "/";
2014-11-29 14:16:53 +01:00
2015-01-08 23:17:26 +01:00
return result;
2014-11-29 14:16:53 +01:00
}
VFS::~VFS()
{
UnMountAll();
}
void VFS::Mount(const std::string& ps3_path, const std::string& local_path, vfsDevice* device)
{
2015-01-08 23:17:26 +01:00
std::string simpl_ps3_path = simplify_path(ps3_path, true, true);
2014-11-29 14:16:53 +01:00
UnMount(simpl_ps3_path);
2015-01-08 23:17:26 +01:00
device->SetPath(simpl_ps3_path, simplify_path(local_path, true, false));
m_devices.push_back(device);
2014-11-29 14:16:53 +01:00
if (m_devices.size() > 1)
{
std::sort(m_devices.begin(), m_devices.end(), [](vfsDevice *a, vfsDevice *b) { return b->GetPs3Path().length() < a->GetPs3Path().length(); });
}
}
void VFS::Link(const std::string& mount_point, const std::string& ps3_path)
{
links[simplify_path_blocks(mount_point)] = simplify_path_blocks(ps3_path);
}
2015-01-08 23:17:26 +01:00
std::string VFS::GetLinked(const std::string& ps3_path) const
{
2015-01-08 23:17:26 +01:00
// fmt::tolower removed
auto path_blocks = fmt::split(ps3_path, { "/", "\\" });
for (auto link : links)
{
if (path_blocks.size() < link.first.size())
continue;
bool is_ok = true;
for (size_t i = 0; i < link.first.size(); ++i)
{
if (link.first[i] != path_blocks[i])
{
is_ok = false;
break;
}
}
if (is_ok)
return fmt::merge({ link.second, std::vector<std::string>(path_blocks.begin() + link.first.size(), path_blocks.end()) }, "/");
}
return ps3_path;
}
void VFS::UnMount(const std::string& ps3_path)
{
2015-01-08 23:17:26 +01:00
std::string simpl_ps3_path = simplify_path(ps3_path, true, true);
2014-11-29 14:16:53 +01:00
for (u32 i = 0; i < m_devices.size(); ++i)
{
2014-11-29 16:15:26 +01:00
if (!strcmp(m_devices[i]->GetPs3Path().c_str(), simpl_ps3_path.c_str()))
{
delete m_devices[i];
2014-11-29 14:16:53 +01:00
m_devices.erase(m_devices.begin() +i);
return;
}
}
}
void VFS::UnMountAll()
{
for(u32 i=0; i<m_devices.size(); ++i)
{
delete m_devices[i];
}
m_devices.clear();
}
2015-04-19 15:19:24 +02:00
vfsFileBase* VFS::OpenFile(const std::string& ps3_path, u32 mode) const
{
std::string path;
2015-04-18 19:18:23 +02:00
2014-11-29 14:16:53 +01:00
if (vfsDevice* dev = GetDevice(ps3_path, path))
{
2014-11-29 14:16:53 +01:00
if (vfsFileBase* res = dev->GetNewFileStream())
{
res->Open(path, mode);
return res;
}
}
return nullptr;
}
vfsDirBase* VFS::OpenDir(const std::string& ps3_path) const
{
std::string path;
2014-11-29 14:16:53 +01:00
if (vfsDevice* dev = GetDevice(ps3_path, path))
{
2014-11-29 14:16:53 +01:00
if (vfsDirBase* res = dev->GetNewDirStream())
{
res->Open(path);
return res;
}
}
return nullptr;
}
bool VFS::CreateDir(const std::string& ps3_path) const
{
std::string path;
2015-04-18 19:18:23 +02:00
2014-11-29 14:16:53 +01:00
if (vfsDevice* dev = GetDevice(ps3_path, path))
{
// return dev->create_dir(path);
return fs::create_dir(path);
}
return false;
}
2015-04-20 17:53:31 +02:00
bool VFS::CreatePath(const std::string& ps3_path) const
{
std::string path;
if (vfsDevice* dev = GetDevice(ps3_path, path))
{
// return dev->create_path(path);
2015-04-24 23:38:11 +02:00
return fs::create_path(path);
2015-04-20 17:53:31 +02:00
}
return false;
}
bool VFS::RemoveFile(const std::string& ps3_path) const
{
std::string path;
2015-04-18 19:18:23 +02:00
2014-11-29 14:16:53 +01:00
if (vfsDevice* dev = GetDevice(ps3_path, path))
{
// return dev->remove_file(path);
return fs::remove_file(path);
}
return false;
}
bool VFS::RemoveDir(const std::string& ps3_path) const
{
std::string path;
2015-04-18 19:18:23 +02:00
2014-11-29 14:16:53 +01:00
if (vfsDevice* dev = GetDevice(ps3_path, path))
{
// return dev->remove_dir(path);
return fs::remove_dir(path);
}
return false;
}
void VFS::DeleteAll(const std::string& ps3_path) const
{
// Delete directory and all its contents recursively
for (const auto entry : vfsDir(ps3_path))
{
if (entry->name == "." || entry->name == "..")
{
continue;
}
2014-02-16 16:37:32 +01:00
if (entry->flags & DirEntry_TypeFile)
{
RemoveFile(ps3_path + "/" + entry->name);
}
if (entry->flags & DirEntry_TypeDir)
{
DeleteAll(ps3_path + "/" + entry->name);
}
}
}
u64 VFS::GetDirSize(const std::string& ps3_path) const
{
u64 result = 0;
for (const auto entry : vfsDir(ps3_path))
{
if (entry->name == "." || entry->name == "..")
{
continue;
}
if (entry->flags & DirEntry_TypeFile)
{
result += entry->size;
}
if (entry->flags & DirEntry_TypeDir)
{
result += GetDirSize(ps3_path + "/" + entry->name);
}
}
return result;
}
bool VFS::ExistsFile(const std::string& ps3_path) const
{
std::string path;
2015-04-18 19:18:23 +02:00
2014-11-29 14:16:53 +01:00
if (vfsDevice* dev = GetDevice(ps3_path, path))
{
// return dev->is_file(path);
return fs::is_file(path);
}
return false;
}
bool VFS::ExistsDir(const std::string& ps3_path) const
{
std::string path;
2015-04-18 19:18:23 +02:00
2014-11-29 14:16:53 +01:00
if (vfsDevice* dev = GetDevice(ps3_path, path))
{
// return dev->is_dir(path);
return fs::is_dir(path);
}
return false;
}
bool VFS::Exists(const std::string& ps3_path) const
{
std::string path;
2014-02-16 16:37:32 +01:00
if (vfsDevice* dev = GetDevice(ps3_path, path))
2015-03-16 17:20:02 +01:00
{
// return dev->exists(path);
return fs::exists(path);
}
return false;
}
bool VFS::Rename(const std::string& ps3_path_from, const std::string& ps3_path_to) const
{
2015-03-16 17:20:02 +01:00
std::string path_from, path_to;
2014-02-16 16:37:32 +01:00
2015-03-16 17:20:02 +01:00
if (vfsDevice* dev = GetDevice(ps3_path_from, path_from))
{
if (vfsDevice* dev_ = GetDevice(ps3_path_to, path_to))
{
// return dev->rename(dev_, path_from, path_to);
return fs::rename(path_from, path_to);
}
}
return false;
}
2015-04-13 16:46:10 +02:00
bool VFS::CopyFile(const std::string& ps3_path_from, const std::string& ps3_path_to, bool overwrite) const
{
std::string path_from, path_to;
if (vfsDevice* dev = GetDevice(ps3_path_from, path_from))
{
if (vfsDevice* dev_ = GetDevice(ps3_path_to, path_to))
{
// return dev->copy_file(dev_, path_from, path_to, overwrite);
2015-04-24 23:38:11 +02:00
return fs::copy_file(path_from, path_to, overwrite);
2015-04-13 16:46:10 +02:00
}
}
return false;
}
2015-04-18 15:38:42 +02:00
bool VFS::TruncateFile(const std::string& ps3_path, u64 length) const
{
std::string path;
if (vfsDevice* dev = GetDevice(ps3_path, path))
{
// return dev->truncate_file(path, length);
2015-04-24 23:38:11 +02:00
return fs::truncate_file(path, length);
2015-04-18 15:38:42 +02:00
}
return false;
}
vfsDevice* VFS::GetDevice(const std::string& ps3_path, std::string& path) const
{
auto try_get_device = [this, &path](const std::string& ps3_path) -> vfsDevice*
{
std::vector<std::string> ps3_path_blocks = simplify_path_blocks(ps3_path);
2014-11-29 14:16:53 +01:00
size_t max_eq = 0;
int max_i = -1;
2014-11-29 14:16:53 +01:00
for (u32 i = 0; i < m_devices.size(); ++i)
{
2014-11-29 14:16:53 +01:00
std::vector<std::string> dev_ps3_path_blocks = simplify_path_blocks(m_devices[i]->GetPs3Path());
if (ps3_path_blocks.size() < dev_ps3_path_blocks.size())
continue;
size_t eq = 0;
for (; eq < dev_ps3_path_blocks.size(); ++eq)
{
2014-11-29 16:15:26 +01:00
if (strcmp(ps3_path_blocks[eq].c_str(), dev_ps3_path_blocks[eq].c_str()))
2014-11-29 14:16:53 +01:00
{
break;
}
}
if (eq > max_eq)
{
max_eq = eq;
max_i = i;
}
}
2014-11-29 14:16:53 +01:00
if (max_i < 0)
return nullptr;
path = m_devices[max_i]->GetLocalPath();
2015-01-25 17:23:24 +01:00
for (size_t i = max_eq; i < ps3_path_blocks.size(); i++)
2014-11-29 14:16:53 +01:00
{
path += "/" + ps3_path_blocks[i];
}
2015-01-08 23:17:26 +01:00
path = simplify_path(path, false, false);
2014-11-29 14:16:53 +01:00
return m_devices[max_i];
};
2015-01-08 23:17:26 +01:00
if (!ps3_path.size() || ps3_path[0] != '/')
{
return nullptr;
}
2014-11-29 14:16:53 +01:00
2015-01-08 23:17:26 +01:00
return try_get_device(GetLinked(ps3_path));
2014-11-29 14:16:53 +01:00
2015-01-08 23:17:26 +01:00
// What is it? cwd is real path, ps3_path is ps3 path, but GetLinked accepts ps3 path
//if (auto res = try_get_device(GetLinked(cwd + ps3_path)))
// return res;
}
vfsDevice* VFS::GetDeviceLocal(const std::string& local_path, std::string& path) const
{
int max_eq = -1;
2014-11-29 14:16:53 +01:00
int max_i = -1;
2014-11-29 14:16:53 +01:00
std::vector<std::string> local_path_blocks = simplify_path_blocks(local_path);
2014-11-29 14:16:53 +01:00
for (u32 i = 0; i < m_devices.size(); ++i)
{
std::vector<std::string> dev_local_path_blocks = simplify_path_blocks(m_devices[i]->GetLocalPath());
if (local_path_blocks.size() < dev_local_path_blocks.size())
2014-11-29 14:16:53 +01:00
continue;
int dev_blocks = dev_local_path_blocks.size();
2014-11-29 14:16:53 +01:00
bool prefix_equal = std::equal(
std::begin(dev_local_path_blocks),
std::end(dev_local_path_blocks),
std::begin(local_path_blocks),
[](const std::string& a, const std::string& b){ return strcmp(a.c_str(), b.c_str()) == 0; }
);
if (prefix_equal && dev_blocks > max_eq)
{
max_eq = dev_blocks;
max_i = i;
}
}
2014-11-29 14:16:53 +01:00
if (max_i < 0)
return nullptr;
path = m_devices[max_i]->GetPs3Path();
2015-01-25 17:23:24 +01:00
for (size_t i = max_eq; i < local_path_blocks.size(); i++)
2014-11-29 14:16:53 +01:00
{
path += "/" + local_path_blocks[i];
}
2015-01-08 23:17:26 +01:00
path = simplify_path(path, false, true);
return m_devices[max_i];
}
void VFS::Init(const std::string& path)
{
2015-01-08 23:17:26 +01:00
cwd = simplify_path(path, true, false);
2014-11-29 14:16:53 +01:00
UnMountAll();
2014-03-27 05:34:55 +01:00
std::vector<VFSManagerEntry> entries;
SaveLoadDevices(entries, true);
2014-03-27 05:34:55 +01:00
for(const VFSManagerEntry& entry : entries)
{
vfsDevice* dev;
2014-03-27 05:34:55 +01:00
switch(entry.device)
{
case vfsDevice_LocalFile:
dev = new vfsDeviceLocalFile();
break;
case vfsDevice_HDD:
2014-03-27 05:34:55 +01:00
dev = new vfsDeviceHDD(entry.device_path);
break;
default:
continue;
}
std::string mpath = entry.path;
// TODO: This shouldn't use current dir
// If no value assigned to SysEmulationDirPath in INI, use the path that with executable.
if (Ini.SysEmulationDirPathEnable.GetValue())
{
fmt::Replace(mpath, "$(EmulatorDir)", Ini.SysEmulationDirPath.GetValue());
}
else
{
fmt::Replace(mpath, "$(EmulatorDir)", Emu.GetEmulatorPath());
}
2014-11-29 14:16:53 +01:00
fmt::Replace(mpath, "$(GameDir)", cwd);
Mount(entry.mount, mpath, dev);
}
2014-11-30 20:23:51 +01:00
Link("/app_home/", "/host_root/" + cwd);
}
2014-03-27 05:34:55 +01:00
void VFS::SaveLoadDevices(std::vector<VFSManagerEntry>& res, bool is_load)
{
IniEntry<int> entries_count;
entries_count.Init("count", "VFSManager");
int count = 0;
2014-11-29 14:16:53 +01:00
if (is_load)
{
count = entries_count.LoadValue(count);
2014-11-29 14:16:53 +01:00
if (!count)
{
2014-03-27 05:34:55 +01:00
res.emplace_back(vfsDevice_LocalFile, "$(EmulatorDir)/dev_hdd0/", "/dev_hdd0/");
res.emplace_back(vfsDevice_LocalFile, "$(EmulatorDir)/dev_hdd1/", "/dev_hdd1/");
res.emplace_back(vfsDevice_LocalFile, "$(EmulatorDir)/dev_flash/", "/dev_flash/");
res.emplace_back(vfsDevice_LocalFile, "$(EmulatorDir)/dev_usb000/", "/dev_usb000/");
res.emplace_back(vfsDevice_LocalFile, "$(EmulatorDir)/dev_usb000/", "/dev_usb/");
res.emplace_back(vfsDevice_LocalFile, "", "/host_root/");
return;
}
2014-03-27 05:34:55 +01:00
res.resize(count);
}
else
{
2014-08-30 20:35:18 +02:00
count = (int)res.size();
entries_count.SaveValue(count);
}
// Custom EmulationDir
if (Ini.SysEmulationDirPathEnable.GetValue())
{
std::string dir = Ini.SysEmulationDirPath.GetValue();
if (dir.empty())
{
Ini.SysEmulationDirPath.SetValue(Emu.GetEmulatorPath());
}
2015-04-24 23:38:11 +02:00
if (!fs::is_dir(dir))
{
2015-04-24 23:38:11 +02:00
LOG_ERROR(GENERAL, "Custom EmulationDir: directory '%s' not found", dir);
}
else
{
LOG_NOTICE(GENERAL, "Custom EmulationDir: $(EmulatorDir) bound to '%s'", dir);
}
}
for(int i=0; i<count; ++i)
{
IniEntry<std::string> entry_path;
IniEntry<std::string> entry_device_path;
IniEntry<std::string> entry_mount;
IniEntry<int> entry_device;
2015-08-12 20:38:17 +02:00
entry_path.Init(fmt::format("path[%d]", i), "VFSManager");
entry_device_path.Init(fmt::format("device_path[%d]", i), "VFSManager");
entry_mount.Init(fmt::format("mount[%d]", i), "VFSManager");
entry_device.Init(fmt::format("device[%d]", i), "VFSManager");
2014-11-29 14:16:53 +01:00
if (is_load)
{
2014-03-27 05:34:55 +01:00
res[i] = VFSManagerEntry();
res[i].path = entry_path.LoadValue("");
res[i].device_path = entry_device_path.LoadValue("");
res[i].mount = entry_mount.LoadValue("");
res[i].device = (vfsDeviceType)entry_device.LoadValue(vfsDevice_LocalFile);
}
else
{
2013-11-27 20:16:19 +01:00
entry_path.SaveValue(res[i].path);
entry_device_path.SaveValue(res[i].device_path);
entry_mount.SaveValue(res[i].mount);
entry_device.SaveValue(res[i].device);
}
}
2013-11-19 11:30:58 +01:00
}