[FS] Better DeleteFile, fclose when file session is closed

This commit is contained in:
wheremyfoodat 2023-05-20 02:32:36 +03:00
parent 9ec2964695
commit 01d16fdfd1
3 changed files with 51 additions and 4 deletions

View file

@ -38,12 +38,27 @@ FSResult ExtSaveDataArchive::deleteFile(const FSPath& path) {
fs::path p = IOFile::getAppData() / backingFolder;
p += fs::path(path.utf16_string).make_preferred();
if (fs::is_directory(p)) {
Helpers::panic("ExtSaveData::DeleteFile: Tried to delete directory");
}
if (!fs::is_regular_file(p)) {
return FSResult::FileNotFound;
}
std::error_code ec;
bool success = fs::remove(p, ec);
return success ? FSResult::Success : FSResult::FileNotFound;
// It might still be possible for fs::remove to fail, if there's eg an open handle to a file being deleted
// In this case, print a warning, but still return success for now
if (!success) {
Helpers::warn("ExtSaveData::DeleteFile: fs::remove failed\n");
}
return FSResult::Success;
}
Helpers::panic("ExtSaveDataArchive::DeleteFile: Failed");
Helpers::panic("ExtSaveDataArchive::DeleteFile: Unknown path type");
return FSResult::Success;
}

View file

@ -31,7 +31,34 @@ FSResult SaveDataArchive::createDirectory(const FSPath& path) {
}
FSResult SaveDataArchive::deleteFile(const FSPath& path) {
Helpers::panic("[SaveData] Unimplemented DeleteFile");
if (path.type == PathType::UTF16) {
if (!isPathSafe<PathType::UTF16>(path))
Helpers::panic("Unsafe path in SaveData::DeleteFile");
fs::path p = IOFile::getAppData() / "SaveData";
p += fs::path(path.utf16_string).make_preferred();
if (fs::is_directory(p)) {
Helpers::panic("SaveData::DeleteFile: Tried to delete directory");
}
if (!fs::is_regular_file(p)) {
return FSResult::FileNotFound;
}
std::error_code ec;
bool success = fs::remove(p, ec);
// It might still be possible for fs::remove to fail, if there's eg an open handle to a file being deleted
// In this case, print a warning, but still return success for now
if (!success) {
Helpers::warn("SaveData::DeleteFile: fs::remove failed\n");
}
return FSResult::Success;
}
Helpers::panic("SaveDataArchive::DeleteFile: Unknown path type");
return FSResult::Success;
}

View file

@ -41,7 +41,12 @@ void Kernel::closeFile(u32 messagePointer, Handle fileHandle) {
Helpers::panic("Called CloseFile on non-existent file");
}
p->getData<FileSession>()->isOpen = false;
FileSession* session = p->getData<FileSession>();
session->isOpen = false;
if (session->fd != nullptr) {
fclose(session->fd);
}
mem.write32(messagePointer + 4, Result::Success);
}