#include "stdafx.h" #include "SC_FileSystem.h" #include "Emu/SysCalls/SysCalls.h" extern Module sys_fs; int cellFsOpen(u32 path_addr, int flags, mem32_t fd, mem32_t arg, u64 size) { const wxString& path = wxString(Memory.ReadString(path_addr), wxConvUTF8); sys_fs.Log("cellFsOpen(path: %s, flags: 0x%x, fd_addr: 0x%x, arg_addr: 0x%x, size: 0x%llx)", path.mb_str(), flags, fd.GetAddr(), arg.GetAddr(), size); const wxString& ppath = path; //ConLog.Warning("path: %s [%s]", ppath, path); s32 _oflags = flags; if(flags & CELL_O_CREAT) { _oflags &= ~CELL_O_CREAT; /* //create path for(uint p=1;pIsOpened()) { sys_fs.Error("'%s' not found! flags: 0x%08x", ppath.mb_str(), flags); delete stream; return CELL_ENOENT; } fd = sys_fs.GetNewId(stream, flags); ConLog.Warning("*** cellFsOpen(path: %s): fd=%d", path.mb_str(), fd.GetValue()); return CELL_OK; } int cellFsRead(u32 fd, u32 buf_addr, u64 nbytes, mem64_t nread) { sys_fs.Log("cellFsRead(fd: %d, buf_addr: 0x%x, nbytes: 0x%llx, nread_addr: 0x%x)", fd, buf_addr, nbytes, nread.GetAddr()); vfsStream* file; if(!sys_fs.CheckId(fd, file)) return CELL_ESRCH; if(Memory.IsGoodAddr(buf_addr) && !Memory.IsGoodAddr(buf_addr, nbytes)) { MemoryBlock& block = Memory.GetMemByAddr(buf_addr); nbytes = block.GetSize() - (buf_addr - block.GetStartAddr()); } const u64 res = nbytes ? file->Read(Memory.GetMemFromAddr(buf_addr), nbytes) : 0; if(nread.IsGood()) nread = res; return CELL_OK; } int cellFsWrite(u32 fd, u32 buf_addr, u64 nbytes, mem64_t nwrite) { sys_fs.Log("cellFsWrite(fd: %d, buf_addr: 0x%x, nbytes: 0x%llx, nwrite_addr: 0x%x)", fd, buf_addr, nbytes, nwrite.GetAddr()); vfsStream* file; if(!sys_fs.CheckId(fd, file)) return CELL_ESRCH; if(Memory.IsGoodAddr(buf_addr) && !Memory.IsGoodAddr(buf_addr, nbytes)) { MemoryBlock& block = Memory.GetMemByAddr(buf_addr); nbytes = block.GetSize() - (buf_addr - block.GetStartAddr()); } const u64 res = nbytes ? file->Write(Memory.GetMemFromAddr(buf_addr), nbytes) : 0; if(nwrite.IsGood()) nwrite = res; return CELL_OK; } int cellFsClose(u32 fd) { sys_fs.Warning("cellFsClose(fd: %d)", fd); if(!Emu.GetIdManager().RemoveID(fd)) return CELL_ESRCH; return CELL_OK; } int cellFsOpendir(u32 path_addr, mem32_t fd) { const wxString& path = Memory.ReadString(path_addr); sys_fs.Error("cellFsOpendir(path_addr: 0x%x(%s), fd_addr: 0x%x)", path_addr, path.mb_str(), fd.GetAddr()); if(!Memory.IsGoodAddr(path_addr) || !fd.IsGood()) return CELL_EFAULT; return CELL_OK; } int cellFsReaddir(u32 fd, u32 dir_addr, mem64_t nread) { sys_fs.Error("cellFsReaddir(fd: %d, dir_addr: 0x%x, nread_addr: 0x%x)", fd, dir_addr, nread.GetAddr()); return CELL_OK; } int cellFsClosedir(u32 fd) { sys_fs.Error("cellFsClosedir(fd: %d)", fd); return CELL_OK; } int cellFsStat(const u32 path_addr, mem_ptr_t sb) { const wxString& path = Memory.ReadString(path_addr); sys_fs.Log("cellFsStat(path: %s, sb_addr: 0x%x)", path.mb_str(), sb.GetAddr()); sb->st_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->st_uid = 0; sb->st_gid = 0; sb->st_atime_ = 0; //TODO sb->st_mtime_ = 0; //TODO sb->st_ctime_ = 0; //TODO sb->st_blksize = 4096; // Check if path is a mount point. (TODO: Add information in sb_addr) for(u32 i=0; ist_mode |= CELL_FS_S_IFDIR; return CELL_OK; } } // TODO: Temporary solution until vfsDir is implemented wxString real_path; Emu.GetVFS().GetDevice(path, real_path); struct stat s; if(stat(real_path.c_str(), &s) == 0) { if(s.st_mode & S_IFDIR) { sb->st_mode |= CELL_FS_S_IFDIR; } else if(s.st_mode & S_IFREG) { vfsFile f(path); sb->st_mode |= CELL_FS_S_IFREG; sb->st_size = f.GetSize(); } } else { sys_fs.Warning("cellFsStat: '%s' not found.", path.mb_str()); return CELL_ENOENT; } return CELL_OK; } int cellFsFstat(u32 fd, mem_ptr_t sb) { sys_fs.Log("cellFsFstat(fd: %d, sb_addr: 0x%x)", fd, sb.GetAddr()); vfsStream* file; if(!sys_fs.CheckId(fd, file)) return CELL_ESRCH; sb->st_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->st_mode |= CELL_FS_S_IFREG; //TODO: dir CELL_FS_S_IFDIR sb->st_uid = 0; sb->st_gid = 0; sb->st_atime_ = 0; //TODO sb->st_mtime_ = 0; //TODO sb->st_ctime_ = 0; //TODO sb->st_size = file->GetSize(); sb->st_blksize = 4096; return CELL_OK; } int cellFsMkdir(u32 path_addr, u32 mode) { const wxString& ps3_path = Memory.ReadString(path_addr); wxString path; Emu.GetVFS().GetDevice(ps3_path, path); sys_fs.Log("cellFsMkdir(path: %s, mode: 0x%x)", path.mb_str(), mode); if(wxDirExists(path)) return CELL_EEXIST; if(!wxMkdir(path)) return CELL_EBUSY; return CELL_OK; } int cellFsRename(u32 from_addr, u32 to_addr) { const wxString& ps3_from = Memory.ReadString(from_addr); const wxString& ps3_to = Memory.ReadString(to_addr); wxString from; wxString to; Emu.GetVFS().GetDevice(ps3_from, from); Emu.GetVFS().GetDevice(ps3_to, to); sys_fs.Log("cellFsRename(from: %s, to: %s)", from.mb_str(), to.mb_str()); if(!wxFileExists(from)) return CELL_ENOENT; if(wxFileExists(to)) return CELL_EEXIST; if(!wxRenameFile(from, to)) return CELL_EBUSY; // (TODO: RenameFile(a,b) = CopyFile(a,b) + RemoveFile(a), therefore file "a" will not be removed if it is opened) return CELL_OK; } int cellFsRmdir(u32 path_addr) { const wxString& ps3_path = Memory.ReadString(path_addr); wxString path; Emu.GetVFS().GetDevice(ps3_path, path); sys_fs.Log("cellFsRmdir(path: %s)", path.mb_str()); if(!wxDirExists(path)) return CELL_ENOENT; if(!wxRmdir(path)) return CELL_EBUSY; // (TODO: Under certain conditions it is not able to delete the folder) return CELL_OK; } int cellFsUnlink(u32 path_addr) { const wxString& ps3_path = Memory.ReadString(path_addr); wxString path; Emu.GetVFS().GetDevice(ps3_path, path); sys_fs.Error("cellFsUnlink(path: %s)", path.mb_str()); return CELL_OK; } int cellFsLseek(u32 fd, s64 offset, u32 whence, mem64_t pos) { vfsSeekMode seek_mode; sys_fs.Log("cellFsLseek(fd: %d, offset: 0x%llx, whence: %d, pos_addr: 0x%x)", fd, offset, whence, pos.GetAddr()); switch(whence) { case CELL_SEEK_SET: seek_mode = vfsSeekSet; break; case CELL_SEEK_CUR: seek_mode = vfsSeekCur; break; case CELL_SEEK_END: seek_mode = vfsSeekEnd; break; default: sys_fs.Error(fd, "Unknown seek whence! (%d)", whence); return CELL_EINVAL; } vfsStream* file; if(!sys_fs.CheckId(fd, file)) return CELL_ESRCH; pos = file->Seek(offset, seek_mode); return CELL_OK; } int cellFsFtruncate(u32 fd, u64 size) { sys_fs.Log("cellFsFtruncate(fd: %d, size: %lld)", fd, size); vfsStream* file; if(!sys_fs.CheckId(fd, file)) return CELL_ESRCH; u64 initialSize = file->GetSize(); if (initialSize < size) { u64 last_pos = file->Tell(); file->Seek(0, vfsSeekEnd); static const char nullbyte = 0; file->Seek(size-initialSize-1, vfsSeekCur); file->Write(&nullbyte, sizeof(char)); file->Seek(last_pos, vfsSeekSet); } if (initialSize > size) { // (TODO) } return CELL_OK; } int cellFsTruncate(u32 path_addr, u64 size) { const wxString& path = Memory.ReadString(path_addr); sys_fs.Log("cellFsTruncate(path: %s, size: %lld)", path.mb_str(), size); vfsFile f(path, vfsReadWrite); if(!f.IsOpened()) { sys_fs.Warning("cellFsTruncate: '%s' not found.", path.mb_str()); return CELL_ENOENT; } u64 initialSize = f.GetSize(); if (initialSize < size) { u64 last_pos = f.Tell(); f.Seek(0, vfsSeekEnd); static const char nullbyte = 0; f.Seek(size-initialSize-1, vfsSeekCur); f.Write(&nullbyte, sizeof(char)); f.Seek(last_pos, vfsSeekSet); } if (initialSize > size) { // (TODO) } return CELL_OK; } int cellFsFGetBlockSize(u32 fd, mem64_t sector_size, mem64_t block_size) { sys_fs.Log("cellFsFGetBlockSize(fd: %d, sector_size_addr: 0x%x, block_size_addr: 0x%x)", fd, sector_size.GetAddr(), block_size.GetAddr()); sector_size = 4096; // ? block_size = 4096; // ? return CELL_OK; }