mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-04-07 06:35:40 +12:00
[FS] Implement SetSize
This commit is contained in:
parent
82b986bbc0
commit
075a244dd0
3 changed files with 51 additions and 0 deletions
|
@ -18,6 +18,12 @@
|
||||||
#define _CRT_SECURE_NO_WARNINGS
|
#define _CRT_SECURE_NO_WARNINGS
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
#include <io.h> // For _chsize_s
|
||||||
|
#else
|
||||||
|
#include <unistd.h> // For ftruncate
|
||||||
|
#endif
|
||||||
|
|
||||||
class IOFile {
|
class IOFile {
|
||||||
FILE* handle = nullptr;
|
FILE* handle = nullptr;
|
||||||
static inline std::filesystem::path appData = ""; // Directory for holding app data. AppData on Windows
|
static inline std::filesystem::path appData = ""; // Directory for holding app data. AppData on Windows
|
||||||
|
@ -112,5 +118,19 @@ public:
|
||||||
appData = dir;
|
appData = dir;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sets the size of the file to "size" and returns whether it succeeded or not
|
||||||
|
bool setSize(std::uint64_t size) {
|
||||||
|
if (!isOpen()) return false;
|
||||||
|
bool success;
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
success = _chsize_s(_fileno(handle), size) == 0;
|
||||||
|
#else
|
||||||
|
success = ftruncate(fileno(handle), size) == 0;
|
||||||
|
#endif
|
||||||
|
fflush(handle);
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
static std::filesystem::path getAppData() { return IOFile::appData; }
|
static std::filesystem::path getAppData() { return IOFile::appData; }
|
||||||
};
|
};
|
|
@ -127,6 +127,7 @@ private:
|
||||||
void writeFile(u32 messagePointer, Handle file);
|
void writeFile(u32 messagePointer, Handle file);
|
||||||
void getFileSize(u32 messagePointer, Handle file);
|
void getFileSize(u32 messagePointer, Handle file);
|
||||||
void openLinkFile(u32 messagePointer, Handle file);
|
void openLinkFile(u32 messagePointer, Handle file);
|
||||||
|
void setFileSize(u32 messagePointer, Handle file);
|
||||||
void setFilePriority(u32 messagePointer, Handle file);
|
void setFilePriority(u32 messagePointer, Handle file);
|
||||||
|
|
||||||
// Directory operations
|
// Directory operations
|
||||||
|
|
|
@ -5,6 +5,7 @@ namespace FileOps {
|
||||||
Read = 0x080200C2,
|
Read = 0x080200C2,
|
||||||
Write = 0x08030102,
|
Write = 0x08030102,
|
||||||
GetSize = 0x08040000,
|
GetSize = 0x08040000,
|
||||||
|
SetSize = 0x08050080,
|
||||||
Close = 0x08080000,
|
Close = 0x08080000,
|
||||||
SetPriority = 0x080A0040,
|
SetPriority = 0x080A0040,
|
||||||
OpenLinkFile = 0x080C0000
|
OpenLinkFile = 0x080C0000
|
||||||
|
@ -25,6 +26,7 @@ void Kernel::handleFileOperation(u32 messagePointer, Handle file) {
|
||||||
case FileOps::GetSize: getFileSize(messagePointer, file); break;
|
case FileOps::GetSize: getFileSize(messagePointer, file); break;
|
||||||
case FileOps::OpenLinkFile: openLinkFile(messagePointer, file); break;
|
case FileOps::OpenLinkFile: openLinkFile(messagePointer, file); break;
|
||||||
case FileOps::Read: readFile(messagePointer, file); break;
|
case FileOps::Read: readFile(messagePointer, file); break;
|
||||||
|
case FileOps::SetSize: setFileSize(messagePointer, file); break;
|
||||||
case FileOps::SetPriority: setFilePriority(messagePointer, file); break;
|
case FileOps::SetPriority: setFilePriority(messagePointer, file); break;
|
||||||
case FileOps::Write: writeFile(messagePointer, file); break;
|
case FileOps::Write: writeFile(messagePointer, file); break;
|
||||||
default: Helpers::panic("Unknown file operation: %08X", cmd);
|
default: Helpers::panic("Unknown file operation: %08X", cmd);
|
||||||
|
@ -132,6 +134,34 @@ void Kernel::writeFile(u32 messagePointer, Handle fileHandle) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Kernel::setFileSize(u32 messagePointer, Handle fileHandle) {
|
||||||
|
logFileIO("Setting size of file %X\n", fileHandle);
|
||||||
|
|
||||||
|
const auto p = getObject(fileHandle, KernelObjectType::File);
|
||||||
|
if (p == nullptr) [[unlikely]] {
|
||||||
|
Helpers::panic("Called SetFileSize on non-existent file");
|
||||||
|
}
|
||||||
|
|
||||||
|
FileSession* file = p->getData<FileSession>();
|
||||||
|
if (!file->isOpen) {
|
||||||
|
Helpers::panic("Tried to get size of closed file");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file->fd) {
|
||||||
|
const u64 newSize = mem.read64(messagePointer + 4);
|
||||||
|
IOFile f(file->fd);
|
||||||
|
bool success = f.setSize(newSize);
|
||||||
|
|
||||||
|
if (success) {
|
||||||
|
mem.write32(messagePointer + 4, Result::Success);
|
||||||
|
} else {
|
||||||
|
Helpers::panic("FileOp::SetFileSize failed");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Helpers::panic("Tried to set file size of file without file descriptor");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Kernel::getFileSize(u32 messagePointer, Handle fileHandle) {
|
void Kernel::getFileSize(u32 messagePointer, Handle fileHandle) {
|
||||||
logFileIO("Getting size of file %X\n", fileHandle);
|
logFileIO("Getting size of file %X\n", fileHandle);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue