Bounded Windows read coverage and sharing documentation (#9) #25
No files matched your search
@@ -288,6 +288,18 @@ private:
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// Read up to `options.max_bytes` from `path` cleanly with typed errors.
|
/// Read up to `options.max_bytes` from `path` cleanly with typed errors.
|
||||||
|
///
|
||||||
|
/// The file is opened once and read through that single handle. The size is
|
||||||
|
/// checked before allocation and every chunk is checked again while reading,
|
||||||
|
/// so a file that grows after the initial size check still fails with
|
||||||
|
/// `limit_exceeded` and never returns partial contents.
|
||||||
|
///
|
||||||
|
/// Sharing: POSIX opens read-only; Windows opens with
|
||||||
|
/// `FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE`, so another process
|
||||||
|
/// may append, rename or replace the path while a background read is pending.
|
||||||
|
/// The read continues from the already opened handle and sees one coherent
|
||||||
|
/// version of the file (the one that was opened), not a mixture with the
|
||||||
|
/// replacement. On both platforms the path is resolved once at open time.
|
||||||
[[nodiscard]] inline Void::Result::Result<std::string, Void::Result::Error>
|
[[nodiscard]] inline Void::Result::Result<std::string, Void::Result::Error>
|
||||||
read_file_bounded(const std::filesystem::path& path,
|
read_file_bounded(const std::filesystem::path& path,
|
||||||
ReadFileOptions options = {})
|
ReadFileOptions options = {})
|
||||||
|
|||||||
@@ -414,3 +414,79 @@ Void::Testing::Test replace_file_concurrent_writers_are_atomic()
|
|||||||
std::string::npos);
|
std::string::npos);
|
||||||
Void::Testing::expect_eq(entry_count(dir.path), std::size_t{1});
|
Void::Testing::expect_eq(entry_count(dir.path), std::size_t{1});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Issue #9: read_file_bounded uses one handle, checks the cap before
|
||||||
|
// allocation and again on every chunk, and handles empty/binary/Unicode paths.
|
||||||
|
Void::Testing::Test read_file_bounded_handles_empty_binary_and_limits()
|
||||||
|
{
|
||||||
|
ScopedTestDir dir{"bounded"};
|
||||||
|
|
||||||
|
const auto empty = dir.path / "empty.bin";
|
||||||
|
seed(empty, "");
|
||||||
|
auto empty_result = Void::FileSystem::read_file_bounded(empty);
|
||||||
|
Void::Testing::require(empty_result.has_value());
|
||||||
|
Void::Testing::expect(empty_result->empty());
|
||||||
|
|
||||||
|
const std::string binary{"\x00\x01\x02\xff\x00", 5};
|
||||||
|
const auto binary_path = dir.path / "binary.bin";
|
||||||
|
seed(binary_path, binary);
|
||||||
|
auto binary_result = Void::FileSystem::read_file_bounded(binary_path);
|
||||||
|
Void::Testing::require(binary_result.has_value());
|
||||||
|
Void::Testing::expect_eq(*binary_result, binary);
|
||||||
|
|
||||||
|
const auto unicode = dir.path / std::filesystem::path{u8"данные-δ.bin"};
|
||||||
|
seed(unicode, "unicode payload");
|
||||||
|
auto unicode_result = Void::FileSystem::read_file_bounded(unicode);
|
||||||
|
Void::Testing::require(unicode_result.has_value());
|
||||||
|
Void::Testing::expect_eq(*unicode_result, std::string{"unicode payload"});
|
||||||
|
|
||||||
|
const auto data = dir.path / "data.bin";
|
||||||
|
seed(data, std::string(16, 'x'));
|
||||||
|
|
||||||
|
// Exactly at the limit is accepted.
|
||||||
|
auto exact = Void::FileSystem::read_file_bounded(data, {.max_bytes = 16});
|
||||||
|
Void::Testing::require(exact.has_value());
|
||||||
|
Void::Testing::expect_eq(exact->size(), std::size_t{16});
|
||||||
|
|
||||||
|
// One byte over is rejected by the pre-allocation size check.
|
||||||
|
auto over = Void::FileSystem::read_file_bounded(data, {.max_bytes = 15});
|
||||||
|
Void::Testing::require(!over.has_value());
|
||||||
|
Void::Testing::expect(over.error().message().find("limit") !=
|
||||||
|
std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
// A stream that keeps producing data after open must be cut off by the
|
||||||
|
// per-chunk check even though it has no initial size to validate.
|
||||||
|
Void::Testing::Test read_file_bounded_rejects_growth_past_limit()
|
||||||
|
{
|
||||||
|
ScopedTestDir dir{"bounded_growth"};
|
||||||
|
const auto fifo = dir.path / "stream";
|
||||||
|
Void::Testing::require(::mkfifo(fifo.c_str(), 0600) == 0);
|
||||||
|
|
||||||
|
std::thread writer{[&fifo] {
|
||||||
|
const int fd = ::open(fifo.c_str(), O_WRONLY);
|
||||||
|
if (fd < 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const std::string payload(64, 'z');
|
||||||
|
std::size_t offset = 0;
|
||||||
|
while (offset < payload.size()) {
|
||||||
|
const ssize_t written =
|
||||||
|
::write(fd, payload.data() + offset, payload.size() - offset);
|
||||||
|
if (written <= 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
offset += static_cast<std::size_t>(written);
|
||||||
|
}
|
||||||
|
(void)::close(fd);
|
||||||
|
}};
|
||||||
|
|
||||||
|
auto result = Void::FileSystem::read_file_bounded(fifo, {.max_bytes = 8});
|
||||||
|
writer.join();
|
||||||
|
|
||||||
|
Void::Testing::require(!result.has_value());
|
||||||
|
Void::Testing::expect(result.error().message().find("limit") !=
|
||||||
|
std::string::npos);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
Reference in new issue
Block a user