#include "stdafx.h" #include "Emu/Memory/Memory.h" #include "Emu/System.h" #include "Emu/IdManager.h" #include "Emu/SysCalls/SysCalls.h" #ifdef _WIN32 #include #undef CreateFile #else #include #include #endif #include "Emu/FS/VFS.h" #include "Emu/FS/vfsFile.h" #include "Emu/FS/vfsDir.h" #include "sys_fs.h" SysCallBase sys_fs("sys_fs"); s32 sys_fs_test(u32 arg1, u32 arg2, vm::ptr arg3, u32 arg4, vm::ptr arg5, u32 arg6) { 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); return CELL_OK; } s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, s32 mode, vm::ptr arg, u64 size) { sys_fs.Warning("sys_fs_open(path=*0x%x, flags=%#o, fd=*0x%x, mode=%#o, arg=*0x%x, size=0x%llx)", path, flags, fd, mode, arg, size); sys_fs.Warning("*** path = '%s'", path.get_ptr()); // TODO: other checks for path if (Emu.GetVFS().ExistsDir(path.get_ptr())) { sys_fs.Error("sys_fs_open(): '%s' is a directory", path.get_ptr()); return CELL_FS_EISDIR; } u32 open_mode = 0; switch (flags & CELL_FS_O_ACCMODE) { case CELL_FS_O_RDONLY: open_mode |= o_read; break; case CELL_FS_O_WRONLY: open_mode |= o_write; break; case CELL_FS_O_RDWR: open_mode |= o_read | o_write; break; } if (flags & CELL_FS_O_CREAT) { open_mode |= o_create; } if (flags & CELL_FS_O_TRUNC) { open_mode |= o_trunc; } if (flags & CELL_FS_O_EXCL) { if ((flags & CELL_FS_O_CREAT) && !(flags & CELL_FS_O_TRUNC)) { open_mode |= o_excl; } else { open_mode = 0; // error } } if (flags & ~(CELL_FS_O_ACCMODE | CELL_FS_O_CREAT | CELL_FS_O_TRUNC | CELL_FS_O_EXCL)) { open_mode = 0; // error } if ((flags & CELL_FS_O_ACCMODE) == CELL_FS_O_ACCMODE) { open_mode = 0; // error } if (!open_mode) { sys_fs.Error("sys_fs_open(): invalid or unimplemented flags (%#o)", flags); return CELL_FS_EINVAL; } std::shared_ptr file(Emu.GetVFS().OpenFile(path.get_ptr(), open_mode)); if (!file || !file->IsOpened()) { sys_fs.Error("sys_fs_open(): failed to open '%s' (flags=%#o, mode=%#o)", path.get_ptr(), flags, mode); return CELL_FS_ENOENT; } std::shared_ptr file_handler(new fs_file_t(file, mode, flags)); *fd = Emu.GetIdManager().GetNewID(file_handler, TYPE_FS_FILE); return CELL_OK; } s32 sys_fs_read(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nread) { sys_fs.Log("sys_fs_read(fd=0x%x, buf=0x%x, nbytes=0x%llx, nread=0x%x)", fd, buf, nbytes, nread); const auto file = Emu.GetIdManager().GetIDData(fd); if (!file || file->flags & CELL_FS_O_WRONLY) { return CELL_FS_EBADF; } std::lock_guard lock(file->mutex); *nread = file->file->Read(buf.get_ptr(), nbytes); return CELL_OK; } s32 sys_fs_write(u32 fd, vm::ptr buf, u64 nbytes, vm::ptr nwrite) { sys_fs.Log("sys_fs_write(fd=0x%x, buf=*0x%x, nbytes=0x%llx, nwrite=*0x%x)", fd, buf, nbytes, nwrite); const auto file = Emu.GetIdManager().GetIDData(fd); if (!file || !(file->flags & CELL_FS_O_ACCMODE)) { return CELL_FS_EBADF; } // TODO: return CELL_FS_EBUSY if locked by stream std::lock_guard lock(file->mutex); *nwrite = file->file->Write(buf.get_ptr(), nbytes); return CELL_OK; } s32 sys_fs_close(u32 fd) { sys_fs.Log("sys_fs_close(fd=0x%x)", fd); const auto file = Emu.GetIdManager().GetIDData(fd); if (!file) { return CELL_FS_EBADF; } // TODO: return CELL_FS_EBUSY if locked Emu.GetIdManager().RemoveID(fd); return CELL_OK; } s32 sys_fs_opendir(vm::ptr path, vm::ptr fd) { sys_fs.Warning("sys_fs_opendir(path=*0x%x, fd=*0x%x)", path, fd); sys_fs.Warning("*** path = '%s'", path.get_ptr()); std::shared_ptr directory(Emu.GetVFS().OpenDir(path.get_ptr())); if (!directory || !directory->IsOpened()) { sys_fs.Error("sys_fs_opendir(): failed to open '%s'", path.get_ptr()); return CELL_FS_ENOENT; } *fd = Emu.GetIdManager().GetNewID(directory, TYPE_FS_DIR); return CELL_OK; } s32 sys_fs_readdir(u32 fd, vm::ptr dir, vm::ptr nread) { sys_fs.Warning("sys_fs_readdir(fd=0x%x, dir=*0x%x, nread=*0x%x)", fd, dir, nread); const auto directory = Emu.GetIdManager().GetIDData(fd); if (!directory) { return CELL_FS_EBADF; } const DirEntryInfo* info = directory->Read(); if (info) { dir->d_type = (info->flags & DirEntry_TypeFile) ? CELL_FS_TYPE_REGULAR : CELL_FS_TYPE_DIRECTORY; dir->d_namlen = u8(std::min(info->name.length(), CELL_FS_MAX_FS_FILE_NAME_LENGTH)); strcpy_trunc(dir->d_name, info->name); *nread = sizeof(CellFsDirent); } else { *nread = 0; } return CELL_OK; } s32 sys_fs_closedir(u32 fd) { sys_fs.Log("sys_fs_closedir(fd=0x%x)", fd); const auto directory = Emu.GetIdManager().GetIDData(fd); if (!directory) { return CELL_FS_EBADF; } Emu.GetIdManager().RemoveID(fd); return CELL_OK; } s32 sys_fs_stat(vm::ptr path, vm::ptr sb) { sys_fs.Warning("sys_fs_stat(path=*0x%x, sb=*0x%x)", path, sb); sys_fs.Warning("*** path = '%s'", path.get_ptr()); std::string local_path; Emu.GetVFS().GetDevice(path.get_ptr(), local_path); FileInfo info; if (!get_file_info(local_path, info) || !info.exists) { sys_fs.Error("sys_fs_stat(): '%s' not found or get_file_info() failed", path.get_ptr()); return CELL_FS_ENOENT; } s32 mode = CELL_FS_S_IRUSR | CELL_FS_S_IRGRP | CELL_FS_S_IROTH; if (info.isWritable) { mode |= CELL_FS_S_IWUSR | CELL_FS_S_IWGRP | CELL_FS_S_IWOTH; } if (info.isDirectory) { mode |= CELL_FS_S_IFDIR; mode |= CELL_FS_S_IXUSR | CELL_FS_S_IXGRP | CELL_FS_S_IXOTH; // ??? } else { mode |= CELL_FS_S_IFREG; } sb->uid = 1; // ??? sb->gid = 1; // ??? sb->atime = info.atime; sb->mtime = info.mtime; sb->ctime = info.ctime; sb->size = info.size; sb->blksize = 4096; // ??? return CELL_OK; } s32 sys_fs_fstat(u32 fd, vm::ptr sb) { sys_fs.Error("sys_fs_fstat(fd=0x%x, sb=*0x%x)", fd, sb); const auto file = Emu.GetIdManager().GetIDData(fd); if (!file) { return CELL_FS_EBADF; } sb->mode = CELL_FS_S_IRUSR | CELL_FS_S_IWUSR | CELL_FS_S_IXUSR | CELL_FS_S_IRGRP | CELL_FS_S_IWGRP | CELL_FS_S_IXGRP | CELL_FS_S_IROTH | CELL_FS_S_IWOTH | CELL_FS_S_IXOTH; sb->mode |= CELL_FS_S_IFREG; sb->uid = 1; // ??? sb->gid = 1; // ??? sb->atime = 0; // TODO sb->mtime = 0; // TODO sb->ctime = 0; // TODO sb->size = file->file->GetSize(); sb->blksize = 4096; return CELL_OK; } s32 sys_fs_mkdir(vm::ptr path, s32 mode) { sys_fs.Warning("sys_fs_mkdir(path=*0x%x, mode=%#o)", path, mode); sys_fs.Warning("*** path = '%s'", path.get_ptr()); const std::string _path = path.get_ptr(); if (Emu.GetVFS().ExistsDir(_path)) { return CELL_FS_EEXIST; } if (!Emu.GetVFS().CreateDir(_path)) { return CELL_FS_EIO; // ??? } sys_fs.Notice("sys_fs_mkdir(): directory '%s' created", path.get_ptr()); return CELL_OK; } s32 sys_fs_rename(vm::ptr from, vm::ptr to) { sys_fs.Warning("sys_fs_rename(from=*0x%x, to=*0x%x)", from, to); sys_fs.Warning("*** from = '%s'", from.get_ptr()); sys_fs.Warning("*** to = '%s'", to.get_ptr()); std::string _from = from.get_ptr(); std::string _to = to.get_ptr(); if (Emu.GetVFS().ExistsDir(_from)) { if (!Emu.GetVFS().RenameDir(_from, _to)) { return CELL_FS_EIO; // ??? } sys_fs.Notice("sys_fs_rename(): directory '%s' renamed to '%s'", from.get_ptr(), to.get_ptr()); return CELL_OK; } if (Emu.GetVFS().ExistsFile(_from)) { if (!Emu.GetVFS().RenameFile(_from, _to)) { return CELL_FS_EIO; // ??? } sys_fs.Notice("sys_fs_rename(): file '%s' renamed to '%s'", from.get_ptr(), to.get_ptr()); return CELL_OK; } return CELL_FS_ENOENT; } s32 sys_fs_rmdir(vm::ptr path) { sys_fs.Warning("sys_fs_rmdir(path=*0x%x)", path); sys_fs.Warning("*** path = '%s'", path.get_ptr()); std::string _path = path.get_ptr(); if (!Emu.GetVFS().RemoveDir(_path)) { if (Emu.GetVFS().ExistsDir(_path)) { return CELL_FS_EIO; // ??? } return CELL_FS_ENOENT; } sys_fs.Notice("sys_fs_rmdir(): directory '%s' removed", path.get_ptr()); return CELL_OK; } s32 sys_fs_unlink(vm::ptr path) { sys_fs.Warning("sys_fs_unlink(path=*0x%x)", path); sys_fs.Warning("*** path = '%s'", path.get_ptr()); std::string _path = path.get_ptr(); if (!Emu.GetVFS().RemoveFile(_path)) { if (Emu.GetVFS().ExistsFile(_path)) { return CELL_FS_EIO; // ??? } return CELL_FS_ENOENT; } sys_fs.Notice("sys_fs_unlink(): file '%s' deleted", path.get_ptr()); return CELL_OK; } s32 sys_fs_fcntl(u32 fd, s32 flags, u32 addr, u32 arg4, u32 arg5, u32 arg6) { sys_fs.Todo("sys_fs_fcntl(fd=0x%x, flags=0x%x, addr=*0x%x, arg4=0x%x, arg5=0x%x, arg6=0x%x) -> CELL_OK", fd, flags, addr, arg4, arg5, arg6); return CELL_OK; } s32 sys_fs_lseek(u32 fd, s64 offset, s32 whence, vm::ptr pos) { sys_fs.Log("sys_fs_lseek(fd=0x%x, offset=0x%llx, whence=0x%x, pos=*0x%x)", fd, offset, whence, pos); if (whence >= 3) { sys_fs.Error("sys_fs_lseek(fd=0x%x): unknown seek whence (%d)", fd, whence); return CELL_FS_EINVAL; } const auto file = Emu.GetIdManager().GetIDData(fd); if (!file) { return CELL_FS_EBADF; } std::lock_guard lock(file->mutex); *pos = file->file->Seek(offset, whence); return CELL_OK; } s32 sys_fs_fget_block_size(u32 fd, vm::ptr sector_size, vm::ptr block_size, vm::ptr arg4, vm::ptr arg5) { sys_fs.Todo("sys_fs_fget_block_size(fd=0x%x, sector_size=*0x%x, block_size=*0x%x, arg4=*0x%x, arg5=*0x%x)", fd, sector_size, block_size, arg4, arg5); const auto file = Emu.GetIdManager().GetIDData(fd); if (!file) { return CELL_FS_EBADF; } *sector_size = 4096; // ? *block_size = 4096; // ? return CELL_OK; } s32 sys_fs_get_block_size(vm::ptr path, vm::ptr sector_size, vm::ptr block_size, vm::ptr arg4) { sys_fs.Todo("sys_fs_get_block_size(path=*0x%x, sector_size=*0x%x, block_size=*0x%x, arg4=*0x%x, arg5=*0x%x)", path, sector_size, block_size, arg4); sys_fs.Todo("*** path = '%s'", path.get_ptr()); *sector_size = 4096; // ? *block_size = 4096; // ? return CELL_OK; } s32 sys_fs_truncate(vm::ptr path, u64 size) { sys_fs.Warning("sys_fs_truncate(path=*0x%x, size=0x%llx)", path, size); sys_fs.Warning("*** path = '%s'", path.get_ptr()); const std::string filename = path.get_ptr(); if (!Emu.GetVFS().ExistsFile(filename)) { return CELL_FS_ENOENT; } if (!Emu.GetVFS().TruncateFile(filename, size)) { return CELL_FS_EIO; // ??? } return CELL_OK; } s32 sys_fs_ftruncate(u32 fd, u64 size) { sys_fs.Todo("sys_fs_ftruncate(fd=0x%x, size=0x%llx)", fd, size); const auto file = Emu.GetIdManager().GetIDData(fd); if (!file) { return CELL_FS_EBADF; } std::lock_guard lock(file->mutex); // must use rfile_t::trunc() return CELL_OK; } s32 sys_fs_chmod(vm::ptr path, s32 mode) { sys_fs.Todo("sys_fs_chmod(path=*0x%x, mode=%#o) -> CELL_OK", path, mode); sys_fs.Todo("*** path = '%s'", path.get_ptr()); return CELL_OK; }