Skip to content

Commit

Permalink
[wpiutil] MemoryBuffer: Fix normal read and file type check (wpilibsu…
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJohnson authored and Starlight220 committed Dec 1, 2023
1 parent e9ac9f9 commit 061dcc1
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,30 +230,31 @@ static std::unique_ptr<WritableMemoryBuffer> GetMemoryBufferForStream(
fs::file_t f, std::string_view bufferName, std::error_code& ec) {
constexpr size_t ChunkSize = 4096 * 4;
SmallVector<uint8_t, ChunkSize> buffer;
uint64_t size = 0;
#ifdef _WIN32
DWORD readBytes;
#else
ssize_t readBytes;
#endif
// Read into Buffer until we hit EOF.
do {
size_t prevSize = buffer.size();
buffer.resize_for_overwrite(prevSize + ChunkSize);
buffer.resize_for_overwrite(size + ChunkSize);
#ifdef _WIN32
if (!ReadFile(f, buffer.begin() + prevSize, ChunkSize, &readBytes,
nullptr)) {
if (!ReadFile(f, buffer.begin() + size, ChunkSize, &readBytes, nullptr)) {
ec = mapWindowsError(GetLastError());
return nullptr;
}
#else
readBytes = sys::RetryAfterSignal(-1, ::read, f, buffer.begin() + prevSize,
readBytes = sys::RetryAfterSignal(-1, ::read, f, buffer.begin() + size,
ChunkSize);
if (readBytes == -1) {
ec = std::error_code(errno, std::generic_category());
return nullptr;
}
#endif
size += readBytes;
} while (readBytes != 0);
buffer.truncate(size);

return GetMemBufferCopyImpl(buffer, bufferName, ec);
}
Expand Down Expand Up @@ -370,7 +371,7 @@ static std::unique_ptr<WriteThroughMemoryBuffer> GetReadWriteFile(

// If this not a file or a block device (e.g. it's a named pipe
// or character device), we can't mmap it, so error out.
if (status.st_mode != S_IFREG && status.st_mode != S_IFBLK) {
if (!S_ISREG(status.st_mode) && !S_ISBLK(status.st_mode)) {
ec = make_error_code(errc::invalid_argument);
return nullptr;
}
Expand Down Expand Up @@ -433,7 +434,7 @@ static std::unique_ptr<MB> GetOpenFileImpl(fs::file_t f,
// If this not a file or a block device (e.g. it's a named pipe
// or character device), we can't trust the size. Create the memory
// buffer by copying off the stream.
if (status.st_mode != S_IFREG && status.st_mode != S_IFBLK) {
if (!S_ISREG(status.st_mode) && !S_ISBLK(status.st_mode)) {
return GetMemoryBufferForStream(f, filename, ec);
}

Expand Down

0 comments on commit 061dcc1

Please sign in to comment.