mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
[wpiutil] MemoryBuffer: Fix normal read and file type check (#5875)
This commit is contained in:
@@ -230,6 +230,7 @@ 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
|
||||
@@ -237,23 +238,23 @@ static std::unique_ptr<WritableMemoryBuffer> GetMemoryBufferForStream(
|
||||
#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);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user