Skip to content

Commit

Permalink
Generalize bug fix from OpenXRay#1512
Browse files Browse the repository at this point in the history
  • Loading branch information
AMS21 committed Nov 14, 2023
1 parent 8ff7b67 commit 710f951
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
28 changes: 27 additions & 1 deletion src/Common/PlatformLinux.inl
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ inline int _rmdir(const char *path)
}
#define _write write
#define _strupr strupr
#define _read read
#define _read xr_read
#define _set_new_handler std::set_new_handler
#define _finite isfinite
inline int _mkdir(const char *dir) { return mkdir(dir, S_IRWXU); }
Expand Down Expand Up @@ -1108,3 +1108,29 @@ inline tm* localtime_safe(const time_t *time, struct tm* result){ return localti
#define xr_strerror(errno, buffer, bufferSize) strerror_r(errno, buffer, sizeof(buffer))

using xrpid_t = pid_t;

// This is a drop-in replacement for calls to linux 'read' function. We use this because unlike other OSes
// Linux 'read' function can return less then the requested number of bytes and then needs to be called multiple times.
// Apart from this, it behaves exactly as you would expect and matches the other OSes implementation.
// See also: https://www.man7.org/linux/man-pages/man2/read.2.html
inline ssize_t xr_read(int file_handle, void *buffer, size_t count)
{
ssize_t total_r_bytes = 0;
do
{
const ssize_t r_bytes =
read(file_handle, reinterpret_cast<u8*>(buffer) + total_r_bytes, count - total_r_bytes);

// Check for error
if (r_bytes == -1)
return -1;

// Check for EOF otherwise we would loop indefinitely
if (r_bytes == 0)
return total_r_bytes;

total_r_bytes += r_bytes;
} while (static_cast<size_t>(total_r_bytes) < count);

return total_r_bytes;
}
18 changes: 2 additions & 16 deletions src/xrCore/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,8 @@ void* FileDownload(pcstr file_name, const int& file_handle, size_t& file_size)
VERIFY(file_size != 0);
void* buffer = xr_malloc(file_size);

#ifdef XR_PLATFORM_LINUX
size_t total_r_bytes = 0;
do
{
const ssize_t r_bytes =
_read(file_handle, reinterpret_cast<u8*>(buffer) + total_r_bytes, file_size - total_r_bytes);
R_ASSERT3(r_bytes > 0, "Can't read from file : ", file_name);

total_r_bytes += r_bytes;
} while (total_r_bytes < file_size);
#elif defined(XR_PLATFORM_WINDOWS) || defined(XR_PLATFORM_BSD) || defined(XR_PLATFORM_APPLE)
int total_r_bytes = _read(file_handle, buffer, file_size);
#else
# error Select or add implementation for your platform
#endif
R_ASSERT3(total_r_bytes == file_size, "Can't read from file : ", file_name);
const ssize_t r_bytes = _read(file_handle, buffer, file_size);
R_ASSERT3(r_bytes > 0 && static_cast<size_t>(r_bytes) == file_size, "Can't read from file : ", file_name);

// file_size = r_bytes;

Expand Down
6 changes: 3 additions & 3 deletions src/xrCore/LocatorAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,14 @@ IReader* open_chunk(int fd, u32 ID, pcstr archiveName, size_t archiveSize, bool

while (true)
{
read_byte = ::read(fd, &dwType, 4);
read_byte = _read(fd, &dwType, 4);
if (read_byte == -1)
return nullptr;
else if (read_byte == 0)
return nullptr;

u32 tempSize = 0;
read_byte = ::read(fd, &tempSize, 4);
read_byte = _read(fd, &tempSize, 4);
dwSize = tempSize;
if (read_byte == -1)
return nullptr;
Expand All @@ -356,7 +356,7 @@ IReader* open_chunk(int fd, u32 ID, pcstr archiveName, size_t archiveSize, bool
if ((dwType & ~CFS_CompressMark) == ID)
{
u8* src_data = xr_alloc<u8>(dwSize);
read_byte = ::read(fd, src_data, dwSize);
read_byte = _read(fd, src_data, dwSize);

VERIFY(read_byte == dwSize);
if (dwType & CFS_CompressMark)
Expand Down

0 comments on commit 710f951

Please sign in to comment.