Skip to content

Commit

Permalink
Merge pull request #4218 from Sonicadvance1/fix_file_loading
Browse files Browse the repository at this point in the history
Utils/FileLoading: Fix LoadFileImpl
  • Loading branch information
lioncash authored Dec 14, 2024
2 parents c902b88 + 38fa866 commit 527752c
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions FEXCore/Source/Utils/FileLoading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,28 @@ static bool LoadFileImpl(T& Data, const fextl::string& Filepath, size_t FixedSiz
FileSize = FixedSize;
}

ssize_t CurrentOffset = 0;
ssize_t Read = -1;
bool LoadedFile {};
if (FileSize) {
// File size is known upfront
Data.resize(FileSize);
Read = pread(FD, &Data.at(0), FileSize, 0);
while (CurrentOffset != FileSize && (Read = pread(FD, &Data.at(CurrentOffset), FileSize, 0)) > 0) {
CurrentOffset += Read;
}

LoadedFile = Read == FileSize;
LoadedFile = CurrentOffset == FileSize && Read != -1;
} else {
// The file is either empty or its size is unknown (e.g. procfs data).
// Try reading in chunks instead
ssize_t CurrentOffset = 0;
constexpr size_t READ_SIZE = 4096;
Data.resize(READ_SIZE);

while ((Read = pread(FD, &Data.at(CurrentOffset), READ_SIZE, CurrentOffset)) == READ_SIZE) {
while ((Read = pread(FD, &Data.at(CurrentOffset), READ_SIZE, CurrentOffset)) > 0) {
CurrentOffset += Read;
Data.resize(CurrentOffset + Read);
if ((CurrentOffset + READ_SIZE) > Data.size()) {
Data.resize(CurrentOffset + READ_SIZE);
}
}

if (Read == -1) {
Expand Down

0 comments on commit 527752c

Please sign in to comment.