use bytes::Buf;
use std::fs::File;
use std::io;
#[cfg(any(unix, windows, target_os = "wasi"))]
use std::mem::MaybeUninit;
use std::path::{Path, PathBuf};
use crate::io::PartBuffer;
#[cfg(any(unix, windows, target_os = "wasi"))]
fn read_exact_at_loop(
mut dst: &mut [MaybeUninit<u8>],
mut offset: u64,
mut read: impl FnMut(&mut [MaybeUninit<u8>], u64) -> io::Result<usize>,
) -> io::Result<()> {
while !dst.is_empty() {
match read(dst, offset) {
Ok(0) => {
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"positioned read reached end of file",
));
}
Ok(count) if count <= dst.len() => {
offset = offset
.checked_add(count as u64)
.ok_or_else(|| io::Error::other("file read offset overflowed"))?;
let current = dst;
dst = &mut current[count..];
}
Ok(_) => {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"positioned read reported more bytes than requested",
));
}
Err(error) if error.kind() == io::ErrorKind::Interrupted => {}
Err(error) => return Err(error),
}
}
Ok(())
}
#[cfg(any(unix, windows, target_os = "wasi"))]
fn write_all_at_loop<B>(
buf: &mut B,
mut offset: u64,
mut write: impl FnMut(&B, u64) -> io::Result<usize>,
) -> io::Result<()>
where
B: Buf,
{
while buf.has_remaining() {
let remaining = buf.remaining();
match write(buf, offset) {
Ok(0) => {
return Err(io::Error::new(
io::ErrorKind::WriteZero,
"positioned write made no progress",
));
}
Ok(count) if count <= remaining => {
buf.advance(count);
offset = offset
.checked_add(count as u64)
.ok_or_else(|| io::Error::other("file write offset overflowed"))?;
}
Ok(_) => {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"positioned write reported more bytes than provided",
));
}
Err(error) if error.kind() == io::ErrorKind::Interrupted => {}
Err(error) => return Err(error),
}
}
Ok(())
}
#[cfg(any(unix, windows, target_os = "wasi"))]
fn validate_range(offset: u64, len: usize, max: u64, max_len: usize) -> io::Result<()> {
if len == 0 {
return Ok(());
}
if len > max_len {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"file range exceeds the platform count limit",
));
}
let last = offset
.checked_add(len as u64 - 1)
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "file range overflowed"))?;
if last > max {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"file range exceeds the platform offset limit",
));
}
Ok(())
}
#[cfg(any(unix, target_os = "wasi"))]
fn validate_off_t_range(offset: u64, len: usize) -> io::Result<()> {
#[cfg(unix)]
let off_t_max: u64 = libc::off_t::MAX as u64;
#[cfg(not(unix))] let off_t_max: u64 = i64::MAX as u64;
#[cfg(unix)]
let max_len: usize = libc::ssize_t::MAX as usize;
#[cfg(not(unix))]
let max_len: usize = usize::MAX;
validate_range(offset, len, off_t_max, max_len)
}
#[cfg(unix)]
fn read_exact_at_unix(file: &File, dst: &mut [MaybeUninit<u8>], offset: u64) -> io::Result<()> {
#[cfg(any(
all(target_os = "linux", not(target_env = "musl")),
target_os = "android"
))]
use libc::{off64_t, pread64};
#[cfg(not(any(
all(target_os = "linux", not(target_env = "musl")),
target_os = "android"
)))]
use libc::{off_t as off64_t, pread as pread64};
use std::os::unix::io::AsRawFd;
validate_range(
offset,
dst.len(),
off64_t::MAX as u64,
libc::ssize_t::MAX as usize,
)?;
let fd = file.as_raw_fd();
read_exact_at_loop(dst, offset, |dst, offset| {
let offset = offset as off64_t;
let n = unsafe { pread64(fd, dst.as_mut_ptr().cast(), dst.len(), offset) };
match n {
0 => Ok(0),
n if n > 0 => Ok(n as usize),
_ => Err(io::Error::last_os_error()),
}
})
}
pub(crate) fn read_exact_at(file: &File, dst: &mut PartBuffer, mut offset: u64) -> io::Result<()> {
while dst.writable() != 0 {
let uninitialized = unsafe { dst.chunk_mut().as_uninit_slice_mut() };
let count = uninitialized.len();
if count == 0 {
return Err(io::Error::other(
"pooled file buffer exposed no writable range",
));
}
sys::read_exact_at(file, uninitialized, offset)?;
unsafe {
dst.advance_mut(count);
}
offset = offset
.checked_add(count as u64)
.ok_or_else(|| io::Error::other("file read offset overflowed"))?;
}
Ok(())
}
pub(crate) fn write_all_at(file: &File, buf: &mut impl Buf, offset: u64) -> io::Result<()> {
sys::write_all_at(file, buf, offset)
}
pub(crate) fn preallocate(file: &File, len: u64) -> io::Result<()> {
sys::preallocate(file, len)
}
pub(crate) fn is_append_only(file: &File) -> io::Result<bool> {
sys::is_append_only(file)
}
pub(crate) fn create_first_new(
candidates: impl IntoIterator<Item = PathBuf>,
) -> io::Result<(File, PathBuf)> {
let mut tried = 0;
for path in candidates {
tried += 1;
match File::options().write(true).create_new(true).open(&path) {
Ok(file) => return Ok((file, path)),
Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {}
Err(error) => return Err(error),
}
}
Err(io::Error::new(
io::ErrorKind::AlreadyExists,
format!("every candidate path already exists ({tried} tried)"),
))
}
#[cfg(any(unix, windows, target_os = "wasi"))]
pub(crate) async fn create_first_new_async(
candidates: Vec<PathBuf>,
) -> io::Result<(File, PathBuf)> {
run_blocking(move || create_first_new(candidates)).await
}
#[cfg(windows)]
pub(crate) fn is_disk_file(file: &File) -> bool {
use std::os::windows::io::AsRawHandle;
use windows_sys::Win32::Storage::FileSystem::{GetFileType, FILE_TYPE_DISK};
unsafe { GetFileType(file.as_raw_handle()) == FILE_TYPE_DISK }
}
#[cfg(all(unix, not(miri)))]
pub(crate) fn open_without_blocking(path: &Path) -> io::Result<File> {
use std::os::unix::fs::OpenOptionsExt;
File::options()
.read(true)
.custom_flags(libc::O_NONBLOCK)
.open(path)
}
#[cfg(all(unix, not(miri)))]
pub(crate) fn restore_blocking(file: &File) -> io::Result<()> {
use nix::fcntl::{fcntl, FcntlArg, OFlag};
use std::os::unix::io::AsFd;
let flags =
OFlag::from_bits_truncate(fcntl(file.as_fd(), FcntlArg::F_GETFL).map_err(io::Error::from)?);
fcntl(file.as_fd(), FcntlArg::F_SETFL(flags - OFlag::O_NONBLOCK)).map_err(io::Error::from)?;
Ok(())
}
#[cfg(not(all(unix, not(miri))))]
pub(crate) fn open_without_blocking(path: &Path) -> io::Result<File> {
File::open(path)
}
#[cfg(not(all(unix, not(miri))))]
pub(crate) fn restore_blocking(_file: &File) -> io::Result<()> {
Ok(())
}
#[cfg(not(target_family = "wasm"))]
pub(crate) async fn rename_async(from: &Path, to: &Path) -> io::Result<()> {
tokio::fs::rename(from, to).await
}
#[cfg(target_family = "wasm")]
pub(crate) async fn rename_async(from: &Path, to: &Path) -> io::Result<()> {
std::fs::rename(from, to)
}
#[cfg(not(target_family = "wasm"))]
pub(crate) async fn remove_file_async(path: &Path) -> io::Result<()> {
tokio::fs::remove_file(path).await
}
#[cfg(target_family = "wasm")]
pub(crate) async fn remove_file_async(path: &Path) -> io::Result<()> {
std::fs::remove_file(path)
}
#[cfg(not(target_family = "wasm"))]
async fn run_blocking<T: Send + 'static>(
operation: impl FnOnce() -> io::Result<T> + Send + 'static,
) -> io::Result<T> {
tokio::task::spawn_blocking(operation).await?
}
#[cfg(all(target_family = "wasm", not(target_os = "unknown")))]
async fn run_blocking<T>(operation: impl FnOnce() -> io::Result<T>) -> io::Result<T> {
operation()
}
#[cfg(not(target_os = "wasi"))]
pub(crate) use same_file::Handle;
#[cfg(target_os = "wasi")]
pub(crate) use sys::Handle;
#[cfg(all(unix, not(miri)))]
mod sys {
use bytes::Buf;
use std::fs::File;
use std::io::{self, IoSlice};
use std::os::unix::io::AsFd;
const MAX_IO_SLICES: usize = 128;
pub(super) use super::read_exact_at_unix as read_exact_at;
pub(super) fn write_all_at(file: &File, buf: &mut impl Buf, offset: u64) -> io::Result<()> {
let fd = file.as_fd();
super::validate_off_t_range(offset, buf.remaining())?;
super::write_all_at_loop(buf, offset, |buf, offset| {
let mut slices = [IoSlice::new(&[]); MAX_IO_SLICES];
let n = buf.chunks_vectored(&mut slices);
let offset = libc::off_t::try_from(offset)
.expect("validated Unix file offset must fit in off_t");
nix::sys::uio::pwritev(fd, &slices[..n], offset).map_err(io::Error::from)
})
}
#[cfg(target_os = "linux")]
pub(super) fn preallocate(file: &File, len: u64) -> io::Result<()> {
if len == 0 {
return file.set_len(0);
}
let len = libc::off_t::try_from(len).map_err(|_| {
io::Error::new(
io::ErrorKind::InvalidInput,
"file length exceeds the platform limit",
)
})?;
nix::fcntl::posix_fallocate(file.as_fd(), 0, len).map_err(io::Error::from)
}
#[cfg(not(target_os = "linux"))]
pub(super) fn preallocate(file: &File, len: u64) -> io::Result<()> {
file.set_len(len)
}
pub(super) fn is_append_only(file: &File) -> io::Result<bool> {
use nix::fcntl::{fcntl, FcntlArg, OFlag};
let flags = fcntl(file.as_fd(), FcntlArg::F_GETFL).map_err(io::Error::from)?;
Ok(OFlag::from_bits_truncate(flags).contains(OFlag::O_APPEND))
}
}
#[cfg(all(unix, miri))]
mod sys {
use bytes::Buf;
use std::fs::File;
use std::io;
use std::os::unix::fs::FileExt;
pub(super) use super::read_exact_at_unix as read_exact_at;
pub(super) fn write_all_at(file: &File, buf: &mut impl Buf, offset: u64) -> io::Result<()> {
super::validate_off_t_range(offset, buf.remaining())?;
super::write_all_at_loop(buf, offset, |buf, offset| {
file.write_at(buf.chunk(), offset)
})
}
pub(super) fn preallocate(file: &File, len: u64) -> io::Result<()> {
file.set_len(len)
}
pub(super) fn is_append_only(_file: &File) -> io::Result<bool> {
Ok(false)
}
}
#[cfg(windows)]
mod sys {
use bytes::Buf;
use std::fs::File;
use std::io;
use std::mem::MaybeUninit;
use std::os::windows::fs::FileExt;
pub(super) fn read_exact_at(
file: &File,
dst: &mut [MaybeUninit<u8>],
offset: u64,
) -> io::Result<()> {
use std::os::windows::io::AsRawHandle;
use windows_sys::Win32::Foundation::ERROR_HANDLE_EOF;
use windows_sys::Win32::Storage::FileSystem::ReadFile;
use windows_sys::Win32::System::IO::{OVERLAPPED, OVERLAPPED_0, OVERLAPPED_0_0};
super::validate_range(offset, dst.len(), i64::MAX as u64, usize::MAX)?;
super::read_exact_at_loop(dst, offset, |dst, offset| {
let count = dst.len().min(u32::MAX as usize) as u32;
let mut overlapped = OVERLAPPED {
Internal: 0,
InternalHigh: 0,
Anonymous: OVERLAPPED_0 {
Anonymous: OVERLAPPED_0_0 {
Offset: offset as u32,
OffsetHigh: (offset >> 32) as u32,
},
},
hEvent: std::ptr::null_mut(),
};
let mut bytes_read: u32 = 0;
let ok = unsafe {
ReadFile(
file.as_raw_handle(),
dst.as_mut_ptr().cast(),
count,
&mut bytes_read,
&mut overlapped,
)
};
if ok == 0 {
let err = io::Error::last_os_error();
if err.raw_os_error() == Some(ERROR_HANDLE_EOF as i32) {
Ok(0)
} else {
Err(err)
}
} else {
Ok(bytes_read as usize)
}
})
}
pub(super) fn write_all_at(file: &File, buf: &mut impl Buf, offset: u64) -> io::Result<()> {
super::write_all_at_loop(buf, offset, |buf, offset| {
file.seek_write(buf.chunk(), offset)
})
}
pub(super) fn preallocate(file: &File, len: u64) -> io::Result<()> {
file.set_len(len)
}
pub(super) fn is_append_only(file: &File) -> io::Result<bool> {
use std::mem::MaybeUninit;
use std::os::windows::io::AsRawHandle;
use windows_sys::Wdk::Storage::FileSystem::{
FileAccessInformation, NtQueryInformationFile, FILE_ACCESS_INFORMATION,
};
use windows_sys::Win32::Foundation::{RtlNtStatusToDosError, STATUS_SUCCESS};
use windows_sys::Win32::Storage::FileSystem::{FILE_APPEND_DATA, FILE_WRITE_DATA};
use windows_sys::Win32::System::IO::IO_STATUS_BLOCK;
let mut io_status = MaybeUninit::<IO_STATUS_BLOCK>::uninit();
let mut info = FILE_ACCESS_INFORMATION { AccessFlags: 0 };
let status = unsafe {
NtQueryInformationFile(
file.as_raw_handle(),
io_status.as_mut_ptr(),
std::ptr::from_mut(&mut info).cast(),
std::mem::size_of::<FILE_ACCESS_INFORMATION>() as u32,
FileAccessInformation,
)
};
if status != STATUS_SUCCESS {
let code = unsafe { RtlNtStatusToDosError(status) };
return Err(io::Error::from_raw_os_error(code as i32));
}
let access = info.AccessFlags;
Ok((access & FILE_APPEND_DATA) != 0 && (access & FILE_WRITE_DATA) == 0)
}
}
#[cfg(target_os = "wasi")]
mod sys {
use bytes::Buf;
use std::fs::File;
use std::io;
use std::mem::MaybeUninit;
use std::path::Path;
#[derive(Debug)]
pub(crate) struct Handle {
_file: File,
dev: u64,
ino: u64,
}
impl PartialEq for Handle {
fn eq(&self, other: &Self) -> bool {
(self.dev, self.ino) == (other.dev, other.ino)
}
}
impl Eq for Handle {}
impl Handle {
pub(crate) fn from_path(path: impl AsRef<Path>) -> io::Result<Self> {
let file = File::open(path)?;
let stat = rustix::fs::fstat(&file)?;
Ok(Self {
_file: file,
dev: stat.st_dev,
ino: stat.st_ino,
})
}
}
pub(super) fn read_exact_at(
file: &File,
dst: &mut [MaybeUninit<u8>],
offset: u64,
) -> io::Result<()> {
super::validate_off_t_range(offset, dst.len())?;
super::read_exact_at_loop(dst, offset, |dst, offset| {
let (initialized, _) = rustix::io::pread(file, dst, offset)?;
Ok(initialized.len())
})
}
pub(super) fn write_all_at(file: &File, buf: &mut impl Buf, offset: u64) -> io::Result<()> {
super::validate_off_t_range(offset, buf.remaining())?;
super::write_all_at_loop(buf, offset, |buf, offset| {
Ok(rustix::io::pwrite(file, buf.chunk(), offset)?)
})
}
pub(super) fn preallocate(file: &File, len: u64) -> io::Result<()> {
file.set_len(len)
}
pub(super) fn is_append_only(file: &File) -> io::Result<bool> {
let flags = rustix::fs::fcntl_getfl(file)?;
Ok(flags.contains(rustix::fs::OFlags::APPEND))
}
}
#[cfg(not(any(unix, windows, target_os = "wasi")))]
mod sys {
use bytes::Buf;
use std::fs::File;
use std::io;
use std::mem::MaybeUninit;
pub(super) fn read_exact_at(
_file: &File,
_dst: &mut [MaybeUninit<u8>],
_offset: u64,
) -> io::Result<()> {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"positioned reads not supported on this platform",
))
}
pub(super) fn write_all_at(_file: &File, _buf: &mut impl Buf, _offset: u64) -> io::Result<()> {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"positioned writes not supported on this platform",
))
}
pub(super) fn preallocate(_file: &File, _len: u64) -> io::Result<()> {
Ok(())
}
pub(super) fn is_append_only(_file: &File) -> io::Result<bool> {
Ok(false)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::ByteSize;
use std::io::IoSlice;
use std::path::Path;
use std::task::{Context, Waker};
use bytes::Bytes;
use bytes_utils::SegmentedBuf;
use crate::config::MemoryBudgetConfig;
use crate::io::PartBuffer;
use crate::memory::BufferPool;
fn test_pool() -> BufferPool {
BufferPool::builder()
.memory_budget(MemoryBudgetConfig::Limit(ByteSize::new(1024 * 1024)))
.build()
.unwrap()
}
fn buf_prefix(buf: &impl Buf, limit: usize) -> Vec<u8> {
let mut slices = [IoSlice::new(&[]); 8];
let count = buf.chunks_vectored(&mut slices);
let mut remaining = limit.min(buf.remaining());
let mut out = Vec::with_capacity(remaining);
for slice in &slices[..count] {
let count = remaining.min(slice.len());
out.extend_from_slice(&slice[..count]);
remaining -= count;
if remaining == 0 {
break;
}
}
out
}
#[test]
fn read_exact_loop_retries_interrupts_and_short_reads() {
let source = b"0123456789";
let mut dst = [MaybeUninit::<u8>::uninit(); 5];
let mut calls = 0usize;
let mut offsets = Vec::new();
read_exact_at_loop(&mut dst, 3, |dst, offset| {
offsets.push(offset);
calls += 1;
if calls == 1 {
return Err(io::Error::from(io::ErrorKind::Interrupted));
}
let count = match calls {
2 => 2,
3 => 1,
_ => dst.len(),
};
let start = offset as usize;
for (i, byte) in dst[..count].iter_mut().enumerate() {
byte.write(source[start + i]);
}
Ok(count)
})
.unwrap();
let read = dst.map(|byte| unsafe { byte.assume_init() });
assert_eq!(&read, b"34567");
assert_eq!(offsets, [3, 3, 5, 6]);
}
#[test]
fn read_exact_loop_reports_unexpected_eof_after_partial_progress() {
let mut dst = [MaybeUninit::<u8>::uninit(); 4];
let mut calls = 0usize;
let error = read_exact_at_loop(&mut dst, 0, |dst, _| {
calls += 1;
if calls == 1 {
dst[0].write(b'a');
dst[1].write(b'b');
Ok(2)
} else {
Ok(0)
}
})
.unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::UnexpectedEof);
assert_eq!(calls, 2);
assert_eq!(unsafe { dst[0].assume_init() }, b'a');
assert_eq!(unsafe { dst[1].assume_init() }, b'b');
}
#[test]
fn write_all_loop_retries_interrupts_and_advances_segmented_input() {
let mut input = SegmentedBuf::new();
input.push(Bytes::from_static(b"ab"));
input.push(Bytes::from_static(b"cdef"));
input.push(Bytes::from_static(b"ghi"));
let mut output = vec![0u8; 4];
let mut calls = 0usize;
let mut offsets = Vec::new();
write_all_at_loop(&mut input, 4, |buf, offset| {
offsets.push(offset);
calls += 1;
if calls == 1 {
return Err(io::Error::from(io::ErrorKind::Interrupted));
}
let limit = match calls {
2 => 2,
3 => 3,
_ => usize::MAX,
};
let bytes = buf_prefix(buf, limit);
let start = offset as usize;
output.resize(start + bytes.len(), 0);
output[start..].copy_from_slice(&bytes);
Ok(bytes.len())
})
.unwrap();
assert_eq!(input.remaining(), 0);
assert_eq!(&output[..4], &[0; 4]);
assert_eq!(&output[4..], b"abcdefghi");
assert_eq!(offsets, [4, 4, 6, 9]);
}
#[test]
fn write_all_loop_preserves_partial_progress_on_error() {
let mut input = Bytes::from_static(b"abcdef");
let mut calls = 0usize;
let mut offsets = Vec::new();
let error = write_all_at_loop(&mut input, 10, |buf, offset| {
offsets.push(offset);
calls += 1;
if calls == 1 {
assert_eq!(buf_prefix(buf, 2), b"ab");
Ok(2)
} else {
Err(io::Error::other("injected write failure"))
}
})
.unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::Other);
assert_eq!(input.as_ref(), b"cdef");
assert_eq!(offsets, [10, 12]);
}
#[test]
fn write_all_loop_rejects_zero_progress() {
let mut input = Bytes::from_static(b"payload");
let error = write_all_at_loop(&mut input, 0, |_, _| Ok(0)).unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::WriteZero);
assert_eq!(input.as_ref(), b"payload");
}
#[cfg(unix)]
#[test]
fn write_all_at_rejects_unrepresentable_unix_offset() {
let tmp = tempfile::NamedTempFile::new().unwrap();
let mut input = Bytes::from_static(b"x");
let error = write_all_at(tmp.as_file(), &mut input, u64::MAX).unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
assert_eq!(input.as_ref(), b"x");
assert_eq!(tmp.as_file().metadata().unwrap().len(), 0);
}
#[cfg(any(unix, windows))]
#[cfg_attr(miri, ignore)] #[test]
fn is_append_only_detects_append_mode() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("out.bin");
let append = File::options()
.append(true)
.create(true)
.open(&path)
.unwrap();
assert!(is_append_only(&append).unwrap());
let mut write_append = File::options();
write_append.write(true);
let write_append = write_append.append(true).open(&path).unwrap();
assert!(is_append_only(&write_append).unwrap());
}
#[cfg(unix)]
#[cfg_attr(miri, ignore)] #[test]
fn is_append_only_detects_append_flag_set_after_open() {
use nix::fcntl::{fcntl, FcntlArg, OFlag};
use std::os::unix::io::AsFd;
let dir = tempfile::tempdir().unwrap();
let file = File::create(dir.path().join("out.bin")).unwrap();
assert!(!is_append_only(&file).unwrap());
let flags = OFlag::from_bits_truncate(fcntl(file.as_fd(), FcntlArg::F_GETFL).unwrap());
fcntl(file.as_fd(), FcntlArg::F_SETFL(flags | OFlag::O_APPEND)).unwrap();
assert!(is_append_only(&file).unwrap());
}
#[cfg(any(unix, windows))]
#[cfg_attr(miri, ignore)] #[test]
fn is_append_only_is_false_for_positional_handles() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("out.bin");
let created = File::create(&path).unwrap();
assert!(!is_append_only(&created).unwrap());
let read_write = File::options().read(true).write(true).open(&path).unwrap();
assert!(!is_append_only(&read_write).unwrap());
let read_only = File::open(&path).unwrap();
assert!(!is_append_only(&read_only).unwrap());
}
#[test]
fn read_exact_at_initializes_pooled_runs() {
let pool = test_pool();
let part_size = pool.carrier_size() * 2 + 37;
let data = (0..part_size)
.map(|index| (index % 251) as u8)
.collect::<Vec<_>>();
let mut tmp = tempfile::NamedTempFile::new().unwrap();
std::io::Write::write_all(&mut tmp, &data).unwrap();
let mut buffer = PartBuffer::new(&pool, part_size);
let waker = Waker::noop();
let mut cx = Context::from_waker(waker);
assert!(buffer.poll_acquire(&mut cx, part_size).is_ready());
read_exact_at(tmp.as_file(), &mut buffer, 0).unwrap();
let mut frozen = buffer.freeze();
assert_eq!(frozen.copy_to_bytes(frozen.remaining()).as_ref(), data);
assert_eq!(pool.metrics().in_use_bytes(), 0);
}
#[cfg(any(unix, windows, target_os = "wasi"))]
#[test]
fn positioned_read_fills_uninitialized_memory_and_reports_eof() {
let data = (0..64u8).collect::<Vec<_>>();
let mut tmp = tempfile::NamedTempFile::new().unwrap();
std::io::Write::write_all(&mut tmp, &data).unwrap();
let mut dst = [MaybeUninit::<u8>::uninit(); 40];
sys::read_exact_at(tmp.as_file(), &mut dst, 11).unwrap();
let read = dst.map(|byte| unsafe { byte.assume_init() });
assert_eq!(&read[..], &data[11..51]);
let mut dst = [MaybeUninit::<u8>::uninit(); 40];
let error = sys::read_exact_at(tmp.as_file(), &mut dst, 29).unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::UnexpectedEof);
}
#[cfg(unix)]
#[test]
fn read_exact_at_rejects_unrepresentable_unix_offset() {
let tmp = tempfile::NamedTempFile::new().unwrap();
let mut dst = [MaybeUninit::<u8>::uninit(); 1];
let error = sys::read_exact_at(tmp.as_file(), &mut dst, u64::MAX).unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
}
#[cfg(windows)]
#[test]
fn read_exact_at_rejects_unrepresentable_windows_offset() {
let tmp = tempfile::NamedTempFile::new().unwrap();
let mut dst = [MaybeUninit::<u8>::uninit(); 1];
let error = sys::read_exact_at(tmp.as_file(), &mut dst, u64::MAX).unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
}
#[test]
fn write_all_at_single_segment() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test.bin");
let file = File::create(&path).unwrap();
let data = Bytes::from_static(b"hello world");
let mut seg = SegmentedBuf::new();
seg.push(data);
write_all_at(&file, &mut seg, 0).unwrap();
drop(file);
let contents = std::fs::read(&path).unwrap();
assert_eq!(contents, b"hello world");
}
#[test]
fn write_all_at_multiple_segments() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test.bin");
let file = File::create(&path).unwrap();
let mut seg = SegmentedBuf::new();
seg.push(Bytes::from_static(b"aaa"));
seg.push(Bytes::from_static(b"bbb"));
seg.push(Bytes::from_static(b"ccc"));
write_all_at(&file, &mut seg, 0).unwrap();
drop(file);
let contents = std::fs::read(&path).unwrap();
assert_eq!(contents, b"aaabbbccc");
}
#[test]
fn write_all_at_nonzero_offset() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test.bin");
let file = File::create(&path).unwrap();
let mut seg0 = SegmentedBuf::new();
seg0.push(Bytes::from_static(b"AAAA"));
write_all_at(&file, &mut seg0, 0).unwrap();
let mut seg1 = SegmentedBuf::new();
seg1.push(Bytes::from_static(b"BBBB"));
write_all_at(&file, &mut seg1, 4).unwrap();
drop(file);
let contents = std::fs::read(&path).unwrap();
assert_eq!(contents, b"AAAABBBB");
}
#[test]
fn write_all_at_out_of_order() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test.bin");
let file = File::create(&path).unwrap();
let mut seg1 = SegmentedBuf::new();
seg1.push(Bytes::from_static(b"BBBB"));
write_all_at(&file, &mut seg1, 4).unwrap();
let mut seg0 = SegmentedBuf::new();
seg0.push(Bytes::from_static(b"AAAA"));
write_all_at(&file, &mut seg0, 0).unwrap();
drop(file);
let contents = std::fs::read(&path).unwrap();
assert_eq!(contents, b"AAAABBBB");
}
#[test]
fn write_all_at_empty_buf() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test.bin");
let file = File::create(&path).unwrap();
let mut seg = SegmentedBuf::<Bytes>::new();
write_all_at(&file, &mut seg, 0).unwrap();
drop(file);
let contents = std::fs::read(&path).unwrap();
assert!(contents.is_empty());
}
#[test]
fn write_all_at_exceeds_io_slice_limit() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test.bin");
let file = File::create(&path).unwrap();
let num_segments = 192;
let segment_size = 4096;
let mut seg = SegmentedBuf::new();
let mut expected = Vec::with_capacity(num_segments * segment_size);
for i in 0..num_segments {
let byte = (i % 256) as u8;
let data = vec![byte; segment_size];
expected.extend_from_slice(&data);
seg.push(Bytes::from(data));
}
write_all_at(&file, &mut seg, 0).unwrap();
drop(file);
let contents = std::fs::read(&path).unwrap();
assert_eq!(contents.len(), expected.len());
assert_eq!(contents, expected);
}
#[cfg_attr(miri, ignore)] #[test]
fn write_all_at_large_parts_at_offsets() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test.bin");
let file = File::create(&path).unwrap();
let part_size: usize = 8 * 1024 * 1024;
let segment_size: usize = 16 * 1024;
let num_segments = part_size / segment_size;
let mut seg0 = SegmentedBuf::new();
for _ in 0..num_segments {
seg0.push(Bytes::from(vec![0xAA; segment_size]));
}
write_all_at(&file, &mut seg0, 0).unwrap();
let mut seg1 = SegmentedBuf::new();
for _ in 0..num_segments {
seg1.push(Bytes::from(vec![0xBB; segment_size]));
}
write_all_at(&file, &mut seg1, part_size as u64).unwrap();
drop(file);
let contents = std::fs::read(&path).unwrap();
assert_eq!(contents.len(), part_size * 2);
assert!(contents[..part_size].iter().all(|&b| b == 0xAA));
assert!(contents[part_size..].iter().all(|&b| b == 0xBB));
}
#[test]
fn preallocate_sets_file_size() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("prealloc.bin");
let file = File::create(&path).unwrap();
let len = 1024 * 1024; preallocate(&file, len).unwrap();
let meta = std::fs::metadata(&path).unwrap();
assert_eq!(meta.len(), len);
}
fn assert_handle_writes_path(mut file: std::fs::File, path: &Path) {
std::io::Write::write_all(&mut file, b"object").unwrap();
drop(file);
assert_eq!(std::fs::read(path).unwrap(), b"object");
}
#[test]
fn create_first_new_leaves_existing_file_and_uses_next_candidate() {
let dir = tempfile::tempdir().unwrap();
let existing = dir.path().join("taken");
std::fs::write(&existing, b"CUSTOMER").unwrap();
let free = dir.path().join("free");
let (file, path) =
create_first_new([existing.clone(), existing.clone(), free.clone()]).unwrap();
assert_eq!(path, free);
assert_handle_writes_path(file, &path);
assert_eq!(std::fs::read(&existing).unwrap(), b"CUSTOMER");
}
#[cfg(unix)]
#[test]
fn create_first_new_does_not_follow_symlink_at_candidate() {
let dir = tempfile::tempdir().unwrap();
let outside = tempfile::tempdir().unwrap();
let target = outside.path().join("victim.txt");
std::fs::write(&target, b"VICTIM").unwrap();
let link = dir.path().join("link");
std::os::unix::fs::symlink(&target, &link).unwrap();
let free = dir.path().join("free");
let (file, path) = create_first_new([link.clone(), free.clone()]).unwrap();
assert_eq!(path, free);
assert_handle_writes_path(file, &path);
assert_eq!(std::fs::read(&target).unwrap(), b"VICTIM");
assert!(link.symlink_metadata().unwrap().file_type().is_symlink());
}
#[test]
fn create_first_new_fails_when_every_candidate_exists() {
let dir = tempfile::tempdir().unwrap();
let taken: Vec<PathBuf> = (0u8..3)
.map(|i| {
let path = dir.path().join(format!("taken-{i}"));
std::fs::write(&path, [i]).unwrap();
path
})
.collect();
let error = create_first_new(taken.clone()).unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::AlreadyExists);
assert!(error.to_string().contains("(3 tried)"), "{error}");
for (i, path) in taken.iter().enumerate() {
assert_eq!(std::fs::read(path).unwrap(), [i as u8]);
}
}
}