rpcsx/rpcs3/Emu/Cell/lv2/sys_fs.cpp

874 lines
19 KiB
C++
Raw Normal View History

2015-03-12 20:02:02 +01:00
#include "stdafx.h"
#include "sys_fs.h"
2016-06-02 17:16:01 +02:00
#include <mutex>
#include "Emu/Cell/PPUThread.h"
#include "Crypto/unedat.h"
2016-06-02 17:16:01 +02:00
#include "Emu/VFS.h"
2017-02-04 16:09:02 +01:00
#include "Emu/IdManager.h"
#include "Utilities/StrUtil.h"
2016-06-02 17:16:01 +02:00
namespace vm { using namespace ps3; }
2016-05-13 15:55:34 +02:00
logs::channel sys_fs("sys_fs", logs::level::notice);
2015-03-12 20:02:02 +01:00
2016-06-02 17:16:01 +02:00
struct lv2_fs_mount_point
{
std::mutex mutex;
};
lv2_fs_mount_point g_mp_sys_dev_hdd0;
lv2_fs_mount_point g_mp_sys_dev_hdd1;
lv2_fs_mount_point g_mp_sys_dev_usb;
lv2_fs_mount_point g_mp_sys_dev_bdvd;
lv2_fs_mount_point g_mp_sys_app_home;
lv2_fs_mount_point g_mp_sys_host_root;
bool verify_mself(u32 fd, fs::file const& mself_file)
{
FsMselfHeader mself_header;
if (!mself_file.read<FsMselfHeader>(mself_header))
{
sys_fs.error("verify_mself: Didn't read expected bytes for header.");
return false;
}
if (mself_header.m_magic != 0x4D534600)
{
sys_fs.error("verify_mself: Header magic is incorrect.");
return false;
}
if (mself_header.m_format_version != 1)
{
sys_fs.error("verify_mself: Unexpected header format version.");
return false;
}
// sanity check
if (mself_header.m_entry_size != sizeof(FsMselfEntry))
{
sys_fs.error("verify_mself: Unexpected header entry size.");
return false;
}
return true;
}
2016-06-02 17:16:01 +02:00
lv2_fs_mount_point* lv2_fs_object::get_mp(const char* filename)
{
// TODO
return &g_mp_sys_dev_hdd0;
}
u64 lv2_file::op_read(vm::ps3::ptr<void> buf, u64 size)
{
// Copy data from intermediate buffer (avoid passing vm pointer to a native API)
std::unique_ptr<u8[]> local_buf(new u8[size]);
const u64 result = file.read(local_buf.get(), size);
std::memcpy(buf.get_ptr(), local_buf.get(), result);
return result;
}
u64 lv2_file::op_write(vm::ps3::cptr<void> buf, u64 size)
{
// Copy data to intermediate buffer (avoid passing vm pointer to a native API)
std::unique_ptr<u8[]> local_buf(new u8[size]);
std::memcpy(local_buf.get(), buf.get_ptr(), size);
return file.write(local_buf.get(), size);
}
struct lv2_file::file_view : fs::file_base
{
const std::shared_ptr<lv2_file> m_file;
const u64 m_off;
u64 m_pos;
explicit file_view(const std::shared_ptr<lv2_file>& _file, u64 offset)
: m_file(_file)
, m_off(offset)
, m_pos(0)
{
}
~file_view() override
{
}
fs::stat_t stat() override
{
return m_file->file.stat();
}
bool trunc(u64 length) override
{
return false;
}
u64 read(void* buffer, u64 size) override
{
const u64 old_pos = m_file->file.pos();
const u64 new_pos = m_file->file.seek(m_off + m_pos);
const u64 result = m_file->file.read(buffer, size);
verify(HERE), old_pos == m_file->file.seek(old_pos);
m_pos += result;
return result;
}
u64 write(const void* buffer, u64 size) override
{
return 0;
}
u64 seek(s64 offset, fs::seek_mode whence) override
{
2017-03-23 19:32:59 +01:00
const s64 new_pos =
whence == fs::seek_set ? offset :
whence == fs::seek_cur ? offset + m_pos :
whence == fs::seek_end ? offset + size() :
(fmt::raw_error("lv2_file::file_view::seek(): invalid whence"), 0);
2017-03-23 19:32:59 +01:00
if (new_pos < 0)
{
fs::g_tls_error = fs::error::inval;
return -1;
}
m_pos = new_pos;
return m_pos;
}
u64 size() override
{
return m_file->file.size();
}
};
fs::file lv2_file::make_view(const std::shared_ptr<lv2_file>& _file, u64 offset)
{
fs::file result;
result.reset(std::make_unique<lv2_file::file_view>(_file, offset));
return result;
}
error_code sys_fs_test(u32 arg1, u32 arg2, vm::ptr<u32> arg3, u32 arg4, vm::ptr<char> arg5, u32 arg6)
2015-03-15 01:41:08 +01:00
{
sys_fs.todo("sys_fs_test(arg1=0x%x, arg2=0x%x, arg3=*0x%x, arg4=0x%x, arg5=*0x%x, arg6=0x%x) -> CELL_OK", arg1, arg2, arg3, arg4, arg5, arg6);
2015-03-15 01:41:08 +01:00
return CELL_OK;
}
error_code sys_fs_open(vm::cptr<char> path, s32 flags, vm::ptr<u32> fd, s32 mode, vm::cptr<void> arg, u64 size)
2015-03-12 20:02:02 +01:00
{
2016-08-11 01:29:59 +02:00
sys_fs.warning("sys_fs_open(path=%s, flags=%#o, fd=*0x%x, mode=%#o, arg=*0x%x, size=0x%llx)", path, flags, fd, mode, arg, size);
2015-03-12 20:02:02 +01:00
2015-04-21 17:16:29 +02:00
if (!path[0])
{
2016-08-11 01:29:59 +02:00
sys_fs.error("sys_fs_open(%s) failed: path is invalid", path);
2016-06-02 17:16:01 +02:00
return CELL_EINVAL;
2015-04-21 17:16:29 +02:00
}
2016-04-14 00:23:53 +02:00
const std::string& local_path = vfs::get(path.get_ptr());
2016-06-02 17:16:01 +02:00
2016-04-14 00:23:53 +02:00
if (local_path.empty())
2015-04-21 17:16:29 +02:00
{
2016-08-11 01:29:59 +02:00
sys_fs.error("sys_fs_open(%s) failed: device not mounted", path);
2016-06-02 17:16:01 +02:00
return CELL_ENOTMOUNTED;
2015-04-21 17:16:29 +02:00
}
2015-03-13 16:06:27 +01:00
// TODO: other checks for path
2015-04-24 23:38:11 +02:00
if (fs::is_dir(local_path))
2015-03-13 16:06:27 +01:00
{
2016-08-11 01:29:59 +02:00
sys_fs.error("sys_fs_open(%s) failed: path is a directory", path);
2016-06-02 17:16:01 +02:00
return CELL_EISDIR;
2015-03-13 16:06:27 +01:00
}
2015-03-12 20:02:02 +01:00
2016-08-07 21:01:27 +02:00
bs_t<fs::open_mode> open_mode{};
2015-03-13 21:43:11 +01:00
2015-04-19 15:19:24 +02:00
switch (flags & CELL_FS_O_ACCMODE)
2015-03-13 21:43:11 +01:00
{
2016-04-14 00:23:53 +02:00
case CELL_FS_O_RDONLY: open_mode += fs::read; break;
case CELL_FS_O_WRONLY: open_mode += fs::write; break;
case CELL_FS_O_RDWR: open_mode += fs::read + fs::write; break;
2015-03-13 21:43:11 +01:00
}
2015-04-19 15:19:24 +02:00
if (flags & CELL_FS_O_CREAT)
2015-03-13 21:43:11 +01:00
{
2016-04-14 00:23:53 +02:00
open_mode += fs::create;
2015-03-13 21:43:11 +01:00
}
2015-04-19 15:19:24 +02:00
if (flags & CELL_FS_O_TRUNC)
2015-03-13 21:43:11 +01:00
{
2016-04-14 00:23:53 +02:00
open_mode += fs::trunc;
2015-03-13 21:43:11 +01:00
}
2015-03-12 20:02:02 +01:00
if (flags & CELL_FS_O_APPEND)
{
2016-04-14 00:23:53 +02:00
open_mode += fs::append;
}
2015-04-19 15:19:24 +02:00
if (flags & CELL_FS_O_EXCL)
2015-03-13 16:06:27 +01:00
{
2015-04-20 17:53:31 +02:00
if (flags & CELL_FS_O_CREAT)
2015-03-12 20:02:02 +01:00
{
2016-04-14 00:23:53 +02:00
open_mode += fs::excl;
2015-04-19 15:19:24 +02:00
}
else
{
2016-04-14 00:23:53 +02:00
open_mode = {}; // error
2015-03-12 20:02:02 +01:00
}
2015-03-13 16:06:27 +01:00
}
2015-03-13 21:43:11 +01:00
if (flags & CELL_FS_O_MSELF)
{
open_mode = fs::read;
// mself can be mself or mself | rdonly
if (flags & ~(CELL_FS_O_MSELF | CELL_FS_O_RDONLY))
{
open_mode = {};
}
}
if (flags & ~(CELL_FS_O_ACCMODE | CELL_FS_O_CREAT | CELL_FS_O_TRUNC | CELL_FS_O_APPEND | CELL_FS_O_EXCL | CELL_FS_O_MSELF))
2015-03-13 16:06:27 +01:00
{
2016-04-14 00:23:53 +02:00
open_mode = {}; // error
2015-03-13 16:06:27 +01:00
}
2015-03-12 20:02:02 +01:00
2015-04-19 15:19:24 +02:00
if ((flags & CELL_FS_O_ACCMODE) == CELL_FS_O_ACCMODE)
2015-03-13 16:06:27 +01:00
{
2016-04-14 00:23:53 +02:00
open_mode = {}; // error
2015-03-12 20:02:02 +01:00
}
2015-03-13 21:43:11 +01:00
2016-08-07 21:01:27 +02:00
if (!test(open_mode))
2015-03-12 20:02:02 +01:00
{
2016-08-11 01:29:59 +02:00
fmt::throw_exception("sys_fs_open(%s): Invalid or unimplemented flags: %#o" HERE, path, flags);
2015-03-13 16:06:27 +01:00
}
2015-04-19 15:19:24 +02:00
2016-04-14 00:23:53 +02:00
fs::file file(local_path, open_mode);
2015-03-12 20:02:02 +01:00
2016-04-14 00:23:53 +02:00
if (!file)
2015-03-12 20:02:02 +01:00
{
2016-08-11 01:29:59 +02:00
sys_fs.error("sys_fs_open(%s): failed to open file (flags=%#o, mode=%#o)", path, flags, mode);
2015-04-20 17:53:31 +02:00
2016-08-07 21:01:27 +02:00
if (test(open_mode & fs::excl))
2015-04-20 17:53:31 +02:00
{
2016-06-02 17:16:01 +02:00
return CELL_EEXIST; // approximation
2015-04-20 17:53:31 +02:00
}
2016-06-02 17:16:01 +02:00
return CELL_ENOENT;
2015-03-12 20:02:02 +01:00
}
if ((flags & CELL_FS_O_MSELF) && (!verify_mself(*fd, file)))
return CELL_ENOTMSELF;
const auto casted_arg = vm::static_ptr_cast<const u64>(arg);//static_cast<const be_t<u32> *>(arg.get_ptr());
if (size == 8)
{
// check for sdata
if (*casted_arg == 0x18000000010)
{
// check if the file has the NPD header, or else assume its not encrypted
u32 magic;
file.read<u32>(magic);
file.seek(0);
if (magic == "NPD\0"_u32)
{
auto sdata_file = std::make_unique<EDATADecrypter>(std::move(file));
if (!sdata_file->ReadHeader())
{
sys_fs.error("sys_fs_open(%s): Error reading sdata header!", path);
return CELL_EFSSPECIFIC;
}
file.reset(std::move(sdata_file));
}
}
// edata
else if (*casted_arg == 0x2)
{
// check if the file has the NPD header, or else assume its not encrypted
u32 magic;
file.read<u32>(magic);
file.seek(0);
if (magic == "NPD\0"_u32)
{
auto edatkeys = fxm::get_always<LoadedNpdrmKeys_t>();
auto sdata_file = std::make_unique<EDATADecrypter>(std::move(file), edatkeys->devKlic, edatkeys->rifKey);
if (!sdata_file->ReadHeader())
{
sys_fs.error("sys_fs_open(%s): Error reading edata header!", path);
return CELL_EFSSPECIFIC;
}
file.reset(std::move(sdata_file));
}
}
}
2017-02-04 16:09:02 +01:00
if (const u32 id = idm::make<lv2_fs_object, lv2_file>(path.get_ptr(), std::move(file), mode, flags))
{
2017-02-04 16:09:02 +01:00
*fd = id;
return CELL_OK;
}
2017-02-04 16:09:02 +01:00
// Out of file descriptors
return CELL_EMFILE;
2015-03-12 20:02:02 +01:00
}
error_code sys_fs_read(u32 fd, vm::ptr<void> buf, u64 nbytes, vm::ptr<u64> nread)
2015-03-12 20:02:02 +01:00
{
sys_fs.trace("sys_fs_read(fd=%d, buf=*0x%x, nbytes=0x%llx, nread=*0x%x)", fd, buf, nbytes, nread);
if (!buf)
{
return CELL_EFAULT;
}
2015-03-12 20:02:02 +01:00
2017-01-26 02:12:50 +01:00
const auto file = idm::get<lv2_fs_object, lv2_file>(fd);
2015-03-12 20:02:02 +01:00
2015-04-12 03:36:25 +02:00
if (!file || file->flags & CELL_FS_O_WRONLY)
2015-03-13 16:06:27 +01:00
{
2016-06-02 17:16:01 +02:00
return CELL_EBADF;
2015-03-13 16:06:27 +01:00
}
2015-03-12 20:02:02 +01:00
2016-06-02 17:16:01 +02:00
std::lock_guard<std::mutex> lock(file->mp->mutex);
2015-03-16 01:21:40 +01:00
2016-06-02 17:16:01 +02:00
*nread = file->op_read(buf, nbytes);
2015-03-12 20:02:02 +01:00
return CELL_OK;
}
error_code sys_fs_write(u32 fd, vm::cptr<void> buf, u64 nbytes, vm::ptr<u64> nwrite)
2015-03-12 20:02:02 +01:00
{
sys_fs.trace("sys_fs_write(fd=%d, buf=*0x%x, nbytes=0x%llx, nwrite=*0x%x)", fd, buf, nbytes, nwrite);
2015-03-12 20:02:02 +01:00
2017-01-26 02:12:50 +01:00
const auto file = idm::get<lv2_fs_object, lv2_file>(fd);
2015-03-12 20:02:02 +01:00
2015-04-12 03:36:25 +02:00
if (!file || !(file->flags & CELL_FS_O_ACCMODE))
2015-03-13 16:06:27 +01:00
{
2016-06-02 17:16:01 +02:00
return CELL_EBADF;
2015-03-13 16:06:27 +01:00
}
2015-03-12 20:02:02 +01:00
2016-06-02 17:16:01 +02:00
// TODO: return CELL_EBUSY if locked by stream
2015-03-16 01:21:40 +01:00
2016-06-02 17:16:01 +02:00
std::lock_guard<std::mutex> lock(file->mp->mutex);
2015-03-13 23:05:48 +01:00
2016-06-02 17:16:01 +02:00
*nwrite = file->op_write(buf, nbytes);
2015-03-12 20:02:02 +01:00
return CELL_OK;
}
error_code sys_fs_close(u32 fd)
2015-03-12 20:02:02 +01:00
{
sys_fs.trace("sys_fs_close(fd=%d)", fd);
2015-03-12 20:02:02 +01:00
2017-01-26 02:12:50 +01:00
const auto file = idm::get<lv2_fs_object, lv2_file>(fd);
2015-03-13 16:06:27 +01:00
2015-04-12 03:36:25 +02:00
if (!file)
2015-03-13 16:06:27 +01:00
{
2016-06-02 17:16:01 +02:00
return CELL_EBADF;
2015-03-13 16:06:27 +01:00
}
2016-06-02 17:16:01 +02:00
// TODO: return CELL_EBUSY if locked
2015-03-13 16:06:27 +01:00
2017-01-26 02:12:50 +01:00
idm::remove<lv2_fs_object, lv2_file>(fd);
2015-03-12 20:02:02 +01:00
return CELL_OK;
}
error_code sys_fs_opendir(vm::cptr<char> path, vm::ptr<u32> fd)
2015-03-12 20:02:02 +01:00
{
2016-08-11 01:29:59 +02:00
sys_fs.warning("sys_fs_opendir(path=%s, fd=*0x%x)", path, fd);
2015-03-12 20:02:02 +01:00
2016-06-02 17:16:01 +02:00
const std::string& local_path = vfs::get(path.get_ptr());
if (local_path.empty())
{
2016-08-11 01:29:59 +02:00
sys_fs.error("sys_fs_opendir(%s) failed: device not mounted", path);
2016-06-02 17:16:01 +02:00
return CELL_ENOTMOUNTED;
}
// TODO: other checks for path
if (fs::is_file(local_path))
{
2016-08-11 01:29:59 +02:00
sys_fs.error("sys_fs_opendir(%s) failed: path is a file", path);
2016-06-02 17:16:01 +02:00
return CELL_ENOTDIR;
}
fs::dir dir(local_path);
2015-03-13 16:06:27 +01:00
2016-04-14 00:23:53 +02:00
if (!dir)
2015-03-12 20:02:02 +01:00
{
2016-08-11 01:29:59 +02:00
sys_fs.error("sys_fs_opendir(%s): failed to open directory", path);
2016-06-02 17:16:01 +02:00
return CELL_ENOENT;
2015-03-12 20:02:02 +01:00
}
2017-02-04 16:09:02 +01:00
if (const u32 id = idm::make<lv2_fs_object, lv2_dir>(path.get_ptr(), std::move(dir)))
{
2017-02-04 16:09:02 +01:00
*fd = id;
return CELL_OK;
}
2017-02-04 16:09:02 +01:00
// Out of file descriptors
return CELL_EMFILE;
2015-03-12 20:02:02 +01:00
}
error_code sys_fs_readdir(u32 fd, vm::ptr<CellFsDirent> dir, vm::ptr<u64> nread)
2015-03-12 20:02:02 +01:00
{
sys_fs.warning("sys_fs_readdir(fd=%d, dir=*0x%x, nread=*0x%x)", fd, dir, nread);
2015-03-12 20:02:02 +01:00
2017-01-26 02:12:50 +01:00
const auto directory = idm::get<lv2_fs_object, lv2_dir>(fd);
2015-03-13 16:06:27 +01:00
2015-04-12 03:36:25 +02:00
if (!directory)
2015-03-13 16:06:27 +01:00
{
2016-06-02 17:16:01 +02:00
return CELL_EBADF;
2015-03-13 16:06:27 +01:00
}
2015-03-12 20:02:02 +01:00
2016-04-14 00:23:53 +02:00
fs::dir_entry info;
2015-03-13 16:06:27 +01:00
2016-04-14 00:23:53 +02:00
if (directory->dir.read(info))
2015-03-12 20:02:02 +01:00
{
2016-04-14 00:23:53 +02:00
dir->d_type = info.is_directory ? CELL_FS_TYPE_DIRECTORY : CELL_FS_TYPE_REGULAR;
dir->d_namlen = u8(std::min<size_t>(info.name.size(), CELL_FS_MAX_FS_FILE_NAME_LENGTH));
strcpy_trunc(dir->d_name, info.name);
2015-03-12 20:02:02 +01:00
*nread = sizeof(CellFsDirent);
}
else
{
*nread = 0;
}
return CELL_OK;
}
error_code sys_fs_closedir(u32 fd)
2015-03-12 20:02:02 +01:00
{
2016-08-18 12:27:20 +02:00
sys_fs.warning("sys_fs_closedir(fd=%d)", fd);
2015-03-13 16:06:27 +01:00
2017-01-26 02:12:50 +01:00
const auto directory = idm::get<lv2_fs_object, lv2_dir>(fd);
2015-03-12 20:02:02 +01:00
2015-04-12 03:36:25 +02:00
if (!directory)
2015-03-13 16:06:27 +01:00
{
2016-06-02 17:16:01 +02:00
return CELL_EBADF;
2015-03-13 16:06:27 +01:00
}
2017-01-26 02:12:50 +01:00
idm::remove<lv2_fs_object, lv2_dir>(fd);
2015-03-12 20:02:02 +01:00
return CELL_OK;
}
error_code sys_fs_stat(vm::cptr<char> path, vm::ptr<CellFsStat> sb)
2015-03-12 20:02:02 +01:00
{
2016-08-11 01:29:59 +02:00
sys_fs.warning("sys_fs_stat(path=%s, sb=*0x%x)", path, sb);
2015-03-12 20:02:02 +01:00
2016-04-14 00:23:53 +02:00
const std::string& local_path = vfs::get(path.get_ptr());
2015-03-12 20:02:02 +01:00
2016-04-14 00:23:53 +02:00
if (local_path.empty())
2015-04-21 17:16:29 +02:00
{
2016-08-11 01:29:59 +02:00
sys_fs.warning("sys_fs_stat(%s) failed: not mounted", path);
2016-06-02 17:16:01 +02:00
return CELL_ENOTMOUNTED;
2015-04-21 17:16:29 +02:00
}
2015-03-12 20:02:02 +01:00
2015-04-24 23:38:11 +02:00
fs::stat_t info;
2015-03-12 20:02:02 +01:00
2015-04-25 21:15:53 +02:00
if (!fs::stat(local_path, info))
2015-03-12 20:02:02 +01:00
{
2016-08-11 01:29:59 +02:00
sys_fs.error("sys_fs_stat(%s) failed: not found", path);
2016-06-02 17:16:01 +02:00
return CELL_ENOENT;
2015-03-12 20:02:02 +01:00
}
2015-04-24 23:38:11 +02:00
sb->mode = info.is_directory ? CELL_FS_S_IFDIR | 0777 : CELL_FS_S_IFREG | 0666;
2017-02-08 15:27:13 +01:00
sb->uid = 0; // Always zero
sb->gid = 0; // Always zero
2015-04-19 18:02:35 +02:00
sb->atime = info.atime;
sb->mtime = info.mtime;
sb->ctime = info.ctime;
sb->size = info.size;
2015-04-21 20:18:15 +02:00
sb->blksize = 4096; // ???
2015-03-12 20:02:02 +01:00
2015-04-19 18:02:35 +02:00
return CELL_OK;
2015-03-12 20:02:02 +01:00
}
error_code sys_fs_fstat(u32 fd, vm::ptr<CellFsStat> sb)
2015-03-12 20:02:02 +01:00
{
sys_fs.warning("sys_fs_fstat(fd=%d, sb=*0x%x)", fd, sb);
2015-03-12 20:02:02 +01:00
2017-01-26 02:12:50 +01:00
const auto file = idm::get<lv2_fs_object, lv2_file>(fd);
2015-03-16 17:20:02 +01:00
2015-04-12 03:36:25 +02:00
if (!file)
2015-03-16 17:20:02 +01:00
{
2016-06-02 17:16:01 +02:00
return CELL_EBADF;
2015-03-16 17:20:02 +01:00
}
2015-03-12 20:02:02 +01:00
2016-06-02 17:16:01 +02:00
std::lock_guard<std::mutex> lock(file->mp->mutex);
2015-04-19 19:57:04 +02:00
2016-04-14 00:23:53 +02:00
const fs::stat_t& info = file->file.stat();
2015-04-19 19:57:04 +02:00
2015-04-24 23:38:11 +02:00
sb->mode = info.is_directory ? CELL_FS_S_IFDIR | 0777 : CELL_FS_S_IFREG | 0666;
2017-02-08 15:27:13 +01:00
sb->uid = 0; // Always zero
sb->gid = 0; // Always zero
2015-04-19 19:57:04 +02:00
sb->atime = info.atime;
sb->mtime = info.mtime;
2015-04-21 20:18:15 +02:00
sb->ctime = info.ctime; // ctime may be incorrect
2015-04-19 19:57:04 +02:00
sb->size = info.size;
2015-04-21 20:18:15 +02:00
sb->blksize = 4096; // ???
2015-03-12 20:02:02 +01:00
return CELL_OK;
}
error_code sys_fs_mkdir(vm::cptr<char> path, s32 mode)
2015-03-12 20:02:02 +01:00
{
2016-08-11 01:29:59 +02:00
sys_fs.warning("sys_fs_mkdir(path=%s, mode=%#o)", path, mode);
2015-03-12 20:02:02 +01:00
2016-04-14 00:23:53 +02:00
const std::string& local_path = vfs::get(path.get_ptr());
2015-03-12 20:02:02 +01:00
2016-04-14 00:23:53 +02:00
if (fs::is_dir(local_path))
2015-03-16 17:20:02 +01:00
{
2016-06-02 17:16:01 +02:00
return CELL_EEXIST;
2015-03-16 17:20:02 +01:00
}
2015-03-12 20:02:02 +01:00
2016-04-14 00:23:53 +02:00
if (!fs::create_path(local_path))
2015-03-16 17:20:02 +01:00
{
2016-06-02 17:16:01 +02:00
return CELL_EIO; // ???
2015-03-16 17:20:02 +01:00
}
2015-03-12 20:02:02 +01:00
2016-08-11 01:29:59 +02:00
sys_fs.notice("sys_fs_mkdir(): directory %s created", path);
2015-03-12 20:02:02 +01:00
return CELL_OK;
}
error_code sys_fs_rename(vm::cptr<char> from, vm::cptr<char> to)
2015-03-12 20:02:02 +01:00
{
2016-08-11 01:29:59 +02:00
sys_fs.warning("sys_fs_rename(from=%s, to=%s)", from, to);
2015-03-12 20:02:02 +01:00
2016-04-14 00:23:53 +02:00
if (!fs::rename(vfs::get(from.get_ptr()), vfs::get(to.get_ptr())))
2015-03-12 20:02:02 +01:00
{
2016-06-02 17:16:01 +02:00
return CELL_ENOENT; // ???
2015-03-12 20:02:02 +01:00
}
2016-08-11 01:29:59 +02:00
sys_fs.notice("sys_fs_rename(): %s renamed to %s", from, to);
return CELL_OK;
2015-03-12 20:02:02 +01:00
}
error_code sys_fs_rmdir(vm::cptr<char> path)
2015-03-12 20:02:02 +01:00
{
2016-08-11 01:29:59 +02:00
sys_fs.warning("sys_fs_rmdir(path=%s)", path);
2015-03-12 20:02:02 +01:00
2016-04-14 00:23:53 +02:00
if (!fs::remove_dir(vfs::get(path.get_ptr())))
2015-03-16 17:20:02 +01:00
{
2016-05-13 15:55:34 +02:00
switch (auto error = fs::g_tls_error)
2016-04-14 00:23:53 +02:00
{
2016-06-02 17:16:01 +02:00
case fs::error::noent: return CELL_ENOENT;
default: sys_fs.error("sys_fs_rmdir(): unknown error %s", error);
2016-04-14 00:23:53 +02:00
}
2015-03-12 20:02:02 +01:00
2016-06-02 17:16:01 +02:00
return CELL_EIO; // ???
2015-04-20 17:53:31 +02:00
}
2016-08-11 01:29:59 +02:00
sys_fs.notice("sys_fs_rmdir(): directory %s removed", path);
2015-03-12 20:02:02 +01:00
return CELL_OK;
}
error_code sys_fs_unlink(vm::cptr<char> path)
2015-03-12 20:02:02 +01:00
{
2016-08-11 01:29:59 +02:00
sys_fs.warning("sys_fs_unlink(path=%s)", path);
2015-03-12 20:02:02 +01:00
2016-04-14 00:23:53 +02:00
if (!fs::remove_file(vfs::get(path.get_ptr())))
2015-03-16 17:20:02 +01:00
{
2016-05-13 15:55:34 +02:00
switch (auto error = fs::g_tls_error)
2016-04-14 00:23:53 +02:00
{
2016-06-02 17:16:01 +02:00
case fs::error::noent: return CELL_ENOENT;
default: sys_fs.error("sys_fs_unlink(): unknown error %s", error);
2016-04-14 00:23:53 +02:00
}
2015-03-12 20:02:02 +01:00
2016-06-02 17:16:01 +02:00
return CELL_EIO; // ???
2015-04-20 17:53:31 +02:00
}
2016-08-11 01:29:59 +02:00
sys_fs.notice("sys_fs_unlink(): file %s deleted", path);
2015-03-12 20:02:02 +01:00
return CELL_OK;
}
error_code sys_fs_fcntl(u32 fd, u32 op, vm::ptr<void> _arg, u32 _size)
2015-03-15 01:41:08 +01:00
{
2016-06-02 17:16:01 +02:00
sys_fs.trace("sys_fs_fcntl(fd=%d, op=0x%x, arg=*0x%x, size=0x%x)", fd, op, _arg, _size);
switch (op)
{
case 0x8000000A: // Read with offset
case 0x8000000B: // Write with offset
{
const auto arg = vm::static_ptr_cast<lv2_file_op_rw>(_arg);
if (_size < arg.size())
{
return CELL_EINVAL;
}
2017-01-26 02:12:50 +01:00
const auto file = idm::get<lv2_fs_object, lv2_file>(fd);
2016-06-02 17:16:01 +02:00
if (!file)
{
return CELL_EBADF;
}
if (op == 0x8000000A && file->flags & CELL_FS_O_WRONLY)
{
return CELL_EBADF;
}
if (op == 0x8000000B && !(file->flags & CELL_FS_O_ACCMODE))
{
return CELL_EBADF;
}
std::lock_guard<std::mutex> lock(file->mp->mutex);
const u64 old_pos = file->file.pos();
const u64 new_pos = file->file.seek(arg->offset);
arg->out_size = op == 0x8000000A
? file->op_read(arg->buf, arg->size)
: file->op_write(arg->buf, arg->size);
2016-08-15 02:11:49 +02:00
verify(HERE), old_pos == file->file.seek(old_pos);
2016-06-02 17:16:01 +02:00
arg->out_code = CELL_OK;
break;
}
case 0x80000009: // cellFsSdataOpenByFd
{
const auto arg = vm::static_ptr_cast<lv2_file_op_09>(_arg);
if (_size < arg.size())
{
return CELL_EINVAL;
}
const auto file = idm::get<lv2_fs_object, lv2_file>(fd);
if (!file)
{
return CELL_EBADF;
}
auto sdata_file = std::make_unique<EDATADecrypter>(lv2_file::make_view(file, arg->offset));
if (!sdata_file->ReadHeader())
{
return CELL_EFSSPECIFIC;
}
fs::file stream;
stream.reset(std::move(sdata_file));
if (const u32 id = idm::make<lv2_fs_object, lv2_file>(file->mp, std::move(stream), file->mode, file->flags))
{
arg->out_code = CELL_OK;
arg->out_fd = id;
return CELL_OK;
}
// Out of file descriptors
return CELL_EMFILE;
}
2017-04-22 15:01:24 +02:00
case 0xc0000006: // Unknown
{
const auto arg = vm::static_ptr_cast<lv2_file_c0000006>(_arg);
sys_fs.warning("0xc0000006: 0x%x, 0x%x, 0x%x, %s, 0x%x, 0x%x, 0x%x", arg->size, arg->_x4, arg->_x8, arg->name, arg->_x14, arg->_x18, arg->_x1c);
return CELL_OK;
}
2017-04-22 14:29:20 +02:00
case 0xe0000012: // cellFsGetDirectoryEntries
{
const auto arg = vm::static_ptr_cast<lv2_file_op_dir::dir_info>(_arg);
if (_size < arg.size())
{
return CELL_EINVAL;
}
const auto directory = idm::get<lv2_fs_object, lv2_dir>(fd);
if (!directory)
{
return CELL_EBADF;
}
for (; arg->_size < arg->max; arg->_size++)
{
fs::dir_entry info;
if (directory->dir.read(info))
{
auto& entry = arg->ptr[arg->_size];
entry.attribute.mode = info.is_directory ? CELL_FS_S_IFDIR | 0777 : CELL_FS_S_IFREG | 0666;
entry.attribute.uid = 0;
entry.attribute.gid = 0;
entry.attribute.atime = info.atime;
entry.attribute.mtime = info.mtime;
entry.attribute.ctime = info.ctime;
entry.attribute.size = info.size;
entry.attribute.blksize = 4096; // ???
entry.entry_name.d_type = info.is_directory ? CELL_FS_TYPE_DIRECTORY : CELL_FS_TYPE_REGULAR;
entry.entry_name.d_namlen = u8(std::min<size_t>(info.name.size(), CELL_FS_MAX_FS_FILE_NAME_LENGTH));
strcpy_trunc(entry.entry_name.d_name, info.name);
}
else
{
break;
}
}
arg->_code = CELL_OK;
return CELL_OK;
}
2016-06-02 17:16:01 +02:00
default:
{
sys_fs.todo("sys_fs_fcntl(): Unknown operation 0x%08x (fd=%d, arg=*0x%x, size=0x%x)", op, fd, _arg, _size);
}
}
2015-03-15 01:41:08 +01:00
return CELL_OK;
}
error_code sys_fs_lseek(u32 fd, s64 offset, s32 whence, vm::ptr<u64> pos)
2015-03-12 20:02:02 +01:00
{
sys_fs.trace("sys_fs_lseek(fd=%d, offset=0x%llx, whence=0x%x, pos=*0x%x)", fd, offset, whence, pos);
2015-03-12 20:02:02 +01:00
2015-04-19 15:19:24 +02:00
if (whence >= 3)
2015-03-12 20:02:02 +01:00
{
2016-04-14 00:23:53 +02:00
sys_fs.error("sys_fs_lseek(): invalid seek whence (%d)", whence);
2016-06-02 17:16:01 +02:00
return CELL_EINVAL;
2015-03-12 20:02:02 +01:00
}
2017-01-26 02:12:50 +01:00
const auto file = idm::get<lv2_fs_object, lv2_file>(fd);
2015-03-16 17:20:02 +01:00
2015-04-12 03:36:25 +02:00
if (!file)
2015-03-16 17:20:02 +01:00
{
2016-06-02 17:16:01 +02:00
return CELL_EBADF;
2015-03-16 17:20:02 +01:00
}
2015-03-12 20:02:02 +01:00
2016-06-02 17:16:01 +02:00
std::lock_guard<std::mutex> lock(file->mp->mutex);
2015-03-16 17:20:02 +01:00
2017-03-23 19:32:59 +01:00
const u64 result = file->file.seek(offset, static_cast<fs::seek_mode>(whence));
if (result == -1)
{
switch (auto error = fs::g_tls_error)
{
case fs::error::inval: return CELL_EINVAL;
default: sys_fs.error("sys_fs_lseek(): unknown error %s", error);
}
return CELL_EIO; // ???
}
2015-03-16 17:20:02 +01:00
2017-03-23 19:32:59 +01:00
*pos = result;
2015-03-12 20:02:02 +01:00
return CELL_OK;
}
error_code sys_fs_fget_block_size(u32 fd, vm::ptr<u64> sector_size, vm::ptr<u64> block_size, vm::ptr<u64> arg4, vm::ptr<u64> arg5)
2015-03-12 20:02:02 +01:00
{
sys_fs.todo("sys_fs_fget_block_size(fd=%d, sector_size=*0x%x, block_size=*0x%x, arg4=*0x%x, arg5=*0x%x)", fd, sector_size, block_size, arg4, arg5);
2015-03-12 20:02:02 +01:00
2017-01-26 02:12:50 +01:00
const auto file = idm::get<lv2_fs_object, lv2_file>(fd);
2015-03-16 17:20:02 +01:00
2015-04-12 03:36:25 +02:00
if (!file)
2015-03-16 17:20:02 +01:00
{
2016-06-02 17:16:01 +02:00
return CELL_EBADF;
2015-03-16 17:20:02 +01:00
}
2015-03-12 20:02:02 +01:00
*sector_size = 4096; // ?
*block_size = 4096; // ?
return CELL_OK;
}
error_code sys_fs_get_block_size(vm::cptr<char> path, vm::ptr<u64> sector_size, vm::ptr<u64> block_size, vm::ptr<u64> arg4)
2015-03-12 20:02:02 +01:00
{
2016-08-11 01:29:59 +02:00
sys_fs.todo("sys_fs_get_block_size(path=%s, sector_size=*0x%x, block_size=*0x%x, arg4=*0x%x, arg5=*0x%x)", path, sector_size, block_size, arg4);
2015-03-12 20:02:02 +01:00
*sector_size = 4096; // ?
*block_size = 4096; // ?
return CELL_OK;
}
error_code sys_fs_truncate(vm::cptr<char> path, u64 size)
2015-03-12 20:02:02 +01:00
{
2016-08-11 01:29:59 +02:00
sys_fs.warning("sys_fs_truncate(path=%s, size=0x%llx)", path, size);
2015-03-12 20:02:02 +01:00
2016-04-14 00:23:53 +02:00
if (!fs::truncate_file(vfs::get(path.get_ptr()), size))
2015-03-12 20:02:02 +01:00
{
2016-05-13 15:55:34 +02:00
switch (auto error = fs::g_tls_error)
2016-04-14 00:23:53 +02:00
{
2016-06-02 17:16:01 +02:00
case fs::error::noent: return CELL_ENOENT;
default: sys_fs.error("sys_fs_truncate(): unknown error %s", error);
2016-04-14 00:23:53 +02:00
}
2015-03-12 20:02:02 +01:00
2016-06-02 17:16:01 +02:00
return CELL_EIO; // ???
2015-03-12 20:02:02 +01:00
}
return CELL_OK;
}
error_code sys_fs_ftruncate(u32 fd, u64 size)
2015-03-12 20:02:02 +01:00
{
sys_fs.warning("sys_fs_ftruncate(fd=%d, size=0x%llx)", fd, size);
2015-03-12 20:02:02 +01:00
2017-01-26 02:12:50 +01:00
const auto file = idm::get<lv2_fs_object, lv2_file>(fd);
2015-03-16 17:20:02 +01:00
if (!file || !(file->flags & CELL_FS_O_ACCMODE))
2015-03-16 17:20:02 +01:00
{
2016-06-02 17:16:01 +02:00
return CELL_EBADF;
2015-03-16 17:20:02 +01:00
}
2015-03-12 20:02:02 +01:00
2016-06-02 17:16:01 +02:00
std::lock_guard<std::mutex> lock(file->mp->mutex);
2015-04-12 03:36:25 +02:00
2016-04-14 00:23:53 +02:00
if (!file->file.trunc(size))
{
2016-05-13 15:55:34 +02:00
switch (auto error = fs::g_tls_error)
2016-04-14 00:23:53 +02:00
{
2016-05-13 15:55:34 +02:00
case fs::error::ok:
default: sys_fs.error("sys_fs_ftruncate(): unknown error %s", error);
2016-04-14 00:23:53 +02:00
}
2016-06-02 17:16:01 +02:00
return CELL_EIO; // ???
}
2015-03-12 20:02:02 +01:00
return CELL_OK;
}
error_code sys_fs_chmod(vm::cptr<char> path, s32 mode)
2015-03-12 20:02:02 +01:00
{
2016-08-11 01:29:59 +02:00
sys_fs.todo("sys_fs_chmod(path=%s, mode=%#o) -> CELL_OK", path, mode);
2015-03-12 20:02:02 +01:00
return CELL_OK;
}
2017-01-26 13:01:21 +01:00
error_code sys_fs_utime(vm::ps3::cptr<char> path, vm::ps3::cptr<CellFsUtimbuf> timep)
{
sys_fs.warning("sys_fs_utime(path=%s, timep=*0x%x)", path, timep);
if (!fs::utime(vfs::get(path.get_ptr()), timep->actime, timep->modtime))
{
switch (auto error = fs::g_tls_error)
{
case fs::error::noent: return CELL_ENOENT;
default: sys_fs.error("sys_fs_utime(): unknown error %s", error);
}
return CELL_EIO; // ???
}
return CELL_OK;
}