IOS/FS: Add a scoped FD class to make it harder to leak FDs

This changes FileSystemProxy::Open to return a file descriptor wrapper
that will ensure the FD is closed when it goes out of scope.

By using such a wrapper we make it more difficult to forget to close
file descriptors.

This fixes a leak in ReadBootContent. I should have added such a class
from the beginning... In practice, I don't think this would have caused
any obvious issue because ReadBootContent is only called after an IOS
relaunch -- which clears all FDs -- and most titles do not get close
to the FD limit.
This commit is contained in:
Léo Lam
2021-04-01 12:26:13 +02:00
parent 06439a2d40
commit 391644dbb5
7 changed files with 68 additions and 41 deletions

View File

@ -346,16 +346,16 @@ u16 Kernel::GetGidForPPC() const
static std::vector<u8> ReadBootContent(FSDevice* fs, const std::string& path, size_t max_size,
Ticks ticks = {})
{
const s64 fd = fs->Open(0, 0, path, FS::Mode::Read, {}, ticks);
if (fd < 0)
const auto fd = fs->Open(0, 0, path, FS::Mode::Read, {}, ticks);
if (fd.Get() < 0)
return {};
const size_t file_size = fs->GetFileStatus(fd, ticks)->size;
const size_t file_size = fs->GetFileStatus(fd.Get(), ticks)->size;
if (max_size != 0 && file_size > max_size)
return {};
std::vector<u8> buffer(file_size);
if (!fs->Read(fd, buffer.data(), buffer.size(), ticks))
if (!fs->Read(fd.Get(), buffer.data(), buffer.size(), ticks))
return {};
return buffer;
}