use std::collections::VecDeque;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::io;
use std::os::windows::ffi::{OsStrExt as _, OsStringExt as _};
use std::os::windows::fs::OpenOptionsExt as _;
use std::os::windows::io::{AsHandle, AsRawHandle as _, BorrowedHandle, FromRawHandle as _};
use std::path::{Component, Path};
use std::sync::Arc;
#[cfg(test)]
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};
use tracing::{debug, error, warn};
use windows_sys::Wdk::Foundation::OBJECT_ATTRIBUTES;
use windows_sys::Wdk::Storage::FileSystem::{
FILE_CREATE, FILE_DIRECTORY_FILE, FILE_INFORMATION_CLASS, FILE_NON_DIRECTORY_FILE, FILE_OPEN,
FILE_OPEN_FOR_BACKUP_INTENT, FILE_OPEN_IF, FILE_OPEN_REPARSE_POINT,
FILE_RENAME_POSIX_SEMANTICS, FILE_RENAME_REPLACE_IF_EXISTS, FILE_SYNCHRONOUS_IO_NONALERT,
FileBasicInformation, FileDispositionInformation, FileFsDeviceInformation,
FileFullDirectoryInformation, FileNormalizedNameInformation, FileRenameInformation,
FileRenameInformationEx, NtCreateFile, NtQueryDirectoryFile, NtQueryInformationFile,
NtQueryVolumeInformationFile, NtSetInformationFile,
};
use windows_sys::Win32::Foundation::{
GENERIC_READ, GENERIC_WRITE, HANDLE, NTSTATUS, OBJ_CASE_INSENSITIVE, STATUS_ACCESS_DENIED,
STATUS_CANNOT_DELETE, STATUS_INVALID_INFO_CLASS, STATUS_INVALID_PARAMETER,
STATUS_NOT_SUPPORTED, STATUS_OBJECT_NAME_INVALID, UNICODE_STRING,
};
use windows_sys::Win32::Storage::FileSystem::{
BY_HANDLE_FILE_INFORMATION, DELETE, FILE_ATTRIBUTE_DIRECTORY, FILE_ATTRIBUTE_HIDDEN,
FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_REPARSE_POINT,
FILE_ATTRIBUTE_TAG_INFO, FILE_BASIC_INFO, FILE_DISPOSITION_INFO, FILE_FLAG_BACKUP_SEMANTICS,
FILE_ID_INFO, FILE_LIST_DIRECTORY, FILE_READ_ATTRIBUTES, FILE_RENAME_INFO, FILE_SHARE_DELETE,
FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_TRAVERSE, FILE_WRITE_ATTRIBUTES, FileAttributeTagInfo,
FileIdInfo, GetFileInformationByHandle, GetFileInformationByHandleEx, SYNCHRONIZE,
};
use windows_sys::Win32::System::IO::{DeviceIoControl, IO_STATUS_BLOCK};
use windows_sys::Win32::System::Ioctl::FSCTL_GET_REPARSE_POINT;
use super::local::{filetime_to_system_time, is_hidden, special_file};
pub(crate) use crate::host::{
ERROR_INVALID_FUNCTION, ERROR_INVALID_PARAMETER, ERROR_NOT_SUPPORTED, nt_error, refuses_posix,
};
use crate::host::{delete_posix, dos_error};
use crate::utils::utf16le_to_units;
pub(crate) const SHARE_ALL: u32 = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
const HOLD_ACCESS: u32 = FILE_TRAVERSE | FILE_READ_ATTRIBUTES;
const HOLD_SHARE: u32 = FILE_SHARE_READ | FILE_SHARE_WRITE;
const MAX_LINKS: u32 = 40;
const IO_REPARSE_TAG_MOUNT_POINT: u32 = 0xA000_0003;
const IO_REPARSE_TAG_SYMLINK: u32 = 0xA000_000C;
const NAME_SURROGATE: u32 = 0x2000_0000;
const SYMLINK_FLAG_RELATIVE: u32 = 1;
const MAX_REPARSE: usize = 16 << 10;
const LISTING: usize = 64 << 10;
const DIR_CREATION_TIME: usize = 8;
const DIR_LAST_ACCESS_TIME: usize = 16;
const DIR_LAST_WRITE_TIME: usize = 24;
const DIR_END_OF_FILE: usize = 40;
const DIR_ATTRIBUTES: usize = 56;
const DIR_NAME_LENGTH: usize = 60;
const DIR_EA_SIZE: usize = 64;
const DIR_NAME: usize = 68;
const SETTABLE_ATTRIBUTES: u32 = 0x31A7;
const WALK_OPTIONS: u32 =
FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_REPARSE_POINT | FILE_OPEN_FOR_BACKUP_INTENT;
const STATUS_NO_MORE_FILES: NTSTATUS = 0x8000_0006_u32 as NTSTATUS;
const STATUS_NO_SUCH_FILE: NTSTATUS = 0xC000_000F_u32 as NTSTATUS;
pub(crate) const FILE_CREATED: usize = 2;
const FILE_REMOTE_DEVICE: u32 = 0x10;
const MAX_PATH_UNITS: usize = 32767;
pub(crate) const RENAME_WAIT: Duration = Duration::from_secs(1);
const ERROR_ACCESS_DENIED: i32 = 5;
pub(crate) const ERROR_SHARING_VIOLATION: i32 = 32;
pub(crate) const ERROR_DIR_NOT_EMPTY: i32 = 145;
const ERROR_DIRECTORY: i32 = 267;
const ERROR_TOO_MANY_LINKS: i32 = 1142;
fn nt_create(
root: HANDLE,
mut name: Vec<u16>,
access: u32,
share: u32,
disposition: u32,
options: u32,
attributes: u32,
) -> Result<(fs::File, usize), NTSTATUS> {
let bytes = u16::try_from(name.len() * 2).map_err(|_| STATUS_OBJECT_NAME_INVALID)?;
let unicode = UNICODE_STRING {
Length: bytes,
MaximumLength: bytes,
Buffer: name.as_mut_ptr(),
};
let object = OBJECT_ATTRIBUTES {
Length: std::mem::size_of::<OBJECT_ATTRIBUTES>() as u32,
RootDirectory: root,
ObjectName: &unicode,
Attributes: OBJ_CASE_INSENSITIVE,
SecurityDescriptor: std::ptr::null(),
SecurityQualityOfService: std::ptr::null(),
};
let mut handle: HANDLE = std::ptr::null_mut();
let mut status_block = IO_STATUS_BLOCK::default();
let status = unsafe {
NtCreateFile(
&mut handle,
access | SYNCHRONIZE,
&object,
&mut status_block,
std::ptr::null(),
attributes,
share,
disposition,
options,
std::ptr::null(),
0,
)
};
if status < 0 {
return Err(status);
}
let file = unsafe { fs::File::from_raw_handle(handle as _) };
Ok((file, status_block.Information))
}
pub(crate) fn nt_open(
root: HANDLE,
name: Vec<u16>,
access: u32,
share: u32,
disposition: u32,
options: u32,
) -> io::Result<(fs::File, usize)> {
nt_create(
root,
name,
access,
share,
disposition,
options,
FILE_ATTRIBUTE_NORMAL,
)
.map_err(nt_error)
}
fn file_id(file: &fs::File) -> io::Result<(u64, [u8; 16])> {
let mut info = FILE_ID_INFO::default();
let ok = unsafe {
GetFileInformationByHandleEx(
file.as_raw_handle(),
FileIdInfo,
(&raw mut info).cast(),
std::mem::size_of::<FILE_ID_INFO>() as u32,
)
};
if ok == 0 {
return Err(io::Error::last_os_error());
}
Ok((info.VolumeSerialNumber, info.FileId.Identifier))
}
fn file_index_of(file: &fs::File) -> io::Result<(u32, u64)> {
let md = Metadata::from_file(file)?;
match (md.volume_serial_number, md.file_index) {
(Some(volume), Some(index)) => Ok((volume, index)),
_ => Err(io::ErrorKind::Unsupported.into()),
}
}
pub(crate) fn same_file(a: &fs::File, b: &fs::File) -> io::Result<bool> {
if let (Ok(a), Ok(b)) = (file_id(a), file_id(b)) {
return Ok(a == b);
}
Ok(file_index_of(a)? == file_index_of(b)?)
}
pub(crate) fn moved() -> io::Error {
io::Error::new(
io::ErrorKind::NotFound,
"the file is no longer under the name it was opened by",
)
}
fn reopen_checked(file: &fs::File, access: u32, share: u32, options: u32) -> io::Result<fs::File> {
let (new, _) = nt_open(
file.as_raw_handle(),
Vec::new(),
access,
share,
FILE_OPEN,
options,
)?;
if !same_file(file, &new)? {
return Err(moved());
}
Ok(new)
}
pub(crate) fn reopen(file: &fs::File, access: u32) -> io::Result<fs::File> {
reopen_checked(
file,
access | FILE_READ_ATTRIBUTES,
SHARE_ALL,
FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_REPARSE_POINT,
)
}
pub(crate) fn aligned(len: usize) -> Vec<u64> {
vec![0; len.div_ceil(8)]
}
pub(crate) fn bytes_of(words: &[u64], len: usize) -> Vec<u8> {
let mut out: Vec<u8> = words.iter().flat_map(|w| w.to_ne_bytes()).collect();
out.truncate(len);
out
}
fn u16_at(buf: &[u8], at: usize) -> Option<u16> {
let bytes = buf.get(at..at.checked_add(2)?)?;
Some(u16::from_le_bytes(bytes.try_into().ok()?))
}
fn u32_at(buf: &[u8], at: usize) -> Option<u32> {
let bytes = buf.get(at..at.checked_add(4)?)?;
Some(u32::from_le_bytes(bytes.try_into().ok()?))
}
fn u64_at(buf: &[u8], at: usize) -> Option<u64> {
let bytes = buf.get(at..at.checked_add(8)?)?;
Some(u64::from_le_bytes(bytes.try_into().ok()?))
}
fn attribute_tag(file: &fs::File) -> io::Result<(u32, u32)> {
let mut info = FILE_ATTRIBUTE_TAG_INFO::default();
let ok = unsafe {
GetFileInformationByHandleEx(
file.as_raw_handle(),
FileAttributeTagInfo,
(&raw mut info).cast(),
std::mem::size_of::<FILE_ATTRIBUTE_TAG_INFO>() as u32,
)
};
if ok == 0 {
return Err(io::Error::last_os_error());
}
let tag = if info.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT != 0 {
info.ReparseTag
} else {
0
};
Ok((info.FileAttributes, tag))
}
fn links(attributes: u32, tag: u32) -> bool {
attributes & FILE_ATTRIBUTE_REPARSE_POINT != 0 && tag & NAME_SURROGATE != 0
}
pub(crate) fn is_link(file: &fs::File) -> io::Result<bool> {
let (attributes, tag) = attribute_tag(file)?;
Ok(links(attributes, tag))
}
fn link_target(link: &fs::File) -> io::Result<OsString> {
let mut buf = aligned(MAX_REPARSE);
let mut returned = 0u32;
let ok = unsafe {
DeviceIoControl(
link.as_raw_handle(),
FSCTL_GET_REPARSE_POINT,
std::ptr::null(),
0,
buf.as_mut_ptr().cast(),
MAX_REPARSE as u32,
&mut returned,
std::ptr::null_mut(),
)
};
if ok == 0 {
return Err(io::Error::last_os_error());
}
let data = bytes_of(&buf, returned as usize);
let invalid = || io::Error::new(io::ErrorKind::InvalidData, "a link without a target");
match u32_at(&data, 0).ok_or_else(invalid)? {
IO_REPARSE_TAG_SYMLINK => {}
IO_REPARSE_TAG_MOUNT_POINT => return Err(escape()),
_ => return Err(special_file()),
}
if u32_at(&data, 16).ok_or_else(invalid)? & SYMLINK_FLAG_RELATIVE == 0 {
return Err(escape());
}
let offset = 20 + usize::from(u16_at(&data, 8).ok_or_else(invalid)?);
let len = usize::from(u16_at(&data, 10).ok_or_else(invalid)?);
let name = data.get(offset..offset + len).ok_or_else(invalid)?;
let units = utf16le_to_units(name).ok_or_else(invalid)?;
Ok(OsString::from_wide(&units))
}
fn set_info<T>(file: &fs::File, class: FILE_INFORMATION_CLASS, info: &T) -> Result<(), NTSTATUS> {
let mut status_block = IO_STATUS_BLOCK::default();
let status = unsafe {
NtSetInformationFile(
file.as_raw_handle(),
&mut status_block,
std::ptr::from_ref(info).cast(),
std::mem::size_of::<T>() as u32,
class,
)
};
if status < 0 { Err(status) } else { Ok(()) }
}
fn set_attributes(file: &fs::File, attributes: u32) -> Result<(), NTSTATUS> {
let attributes = attributes & SETTABLE_ATTRIBUTES;
let info = FILE_BASIC_INFO {
CreationTime: 0,
LastAccessTime: 0,
LastWriteTime: 0,
ChangeTime: 0,
FileAttributes: if attributes == 0 {
FILE_ATTRIBUTE_NORMAL
} else {
attributes
},
};
set_info(file, FileBasicInformation, &info)
}
pub(crate) fn delete_on_close(file: &fs::File) -> Result<(), NTSTATUS> {
let mark = || {
set_info(
file,
FileDispositionInformation,
&FILE_DISPOSITION_INFO { DeleteFile: true },
)
};
match mark() {
Err(STATUS_CANNOT_DELETE) => {}
marked => return marked,
}
let Ok((attributes, _)) = attribute_tag(file) else {
return Err(STATUS_CANNOT_DELETE);
};
if attributes & FILE_ATTRIBUTE_READONLY == 0
|| set_attributes(file, attributes & !FILE_ATTRIBUTE_READONLY).is_err()
{
return Err(STATUS_CANNOT_DELETE);
}
mark().inspect_err(|_| {
if let Err(status) = set_attributes(file, attributes) {
debug!(error = %nt_error(status), "READONLY not set back on a file that was not deleted");
}
})
}
pub(crate) fn open_to_delete(dir: &fs::File, name: &OsStr) -> Result<fs::File, NTSTATUS> {
let (file, _) = nt_create(
dir.as_raw_handle(),
name.encode_wide().collect(),
DELETE | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
SHARE_ALL,
FILE_OPEN,
WALK_OPTIONS,
FILE_ATTRIBUTE_NORMAL,
)?;
Ok(file)
}
fn set_name(
file: &fs::File,
dir: HANDLE,
name: &[u16],
class: FILE_INFORMATION_CLASS,
flags: u32,
) -> Result<(), NTSTATUS> {
let offset = std::mem::offset_of!(FILE_RENAME_INFO, FileName);
let size = offset + (name.len() + 1) * 2;
let name_bytes = u32::try_from(name.len() * 2).map_err(|_| STATUS_OBJECT_NAME_INVALID)?;
let mut buf = aligned(size);
let info = buf.as_mut_ptr().cast::<FILE_RENAME_INFO>();
unsafe {
(*info).Anonymous.Flags = flags;
(*info).RootDirectory = dir;
(*info).FileNameLength = name_bytes;
std::ptr::copy_nonoverlapping(
name.as_ptr(),
(&raw mut (*info).FileName).cast::<u16>(),
name.len(),
);
}
let mut status_block = IO_STATUS_BLOCK::default();
let status = unsafe {
NtSetInformationFile(
file.as_raw_handle(),
&mut status_block,
buf.as_ptr().cast(),
size as u32,
class,
)
};
if status < 0 { Err(status) } else { Ok(()) }
}
pub(crate) fn random_name() -> io::Result<String> {
let mut id = [0u8; 16];
crate::utils::fill_random(&mut id).map_err(|e| io::Error::other(e.to_string()))?;
Ok(id.iter().map(|b| format!("{b:02x}")).collect())
}
pub(crate) fn upcase(c: char) -> char {
let mut upper = c.to_uppercase();
match (upper.next(), upper.next()) {
(Some(u), None) => u,
_ => c,
}
}
fn same_name(a: &[u16], b: &[u16]) -> bool {
let chars = |units: &[u16]| {
char::decode_utf16(units.iter().copied())
.map(|c| c.map(upcase))
.collect::<Vec<_>>()
};
chars(a) == chars(b)
}
pub(crate) fn last_component(path: &[u16]) -> &[u16] {
path.rsplit(|&unit| unit == u16::from(b'\\'))
.next()
.unwrap_or(path)
}
#[cfg(test)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Step {
Check,
Reopen,
Aside,
Requested,
Restore,
}
#[cfg(test)]
type SettleHook = Box<dyn Fn(Step, &[u16]) -> Option<io::Error> + Send>;
#[cfg(test)]
pub(crate) static SETTLE: std::sync::Mutex<Option<SettleHook>> = std::sync::Mutex::new(None);
#[cfg(test)]
fn settle_hook(step: Step, name: &[u16]) -> Option<io::Error> {
SETTLE
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.as_ref()
.and_then(|f| f(step, name))
}
macro_rules! step {
($step:ident, $name:expr, $run:expr) => {{
#[cfg(test)]
let refused = settle_hook(Step::$step, $name);
#[cfg(not(test))]
let refused: Option<io::Error> = None;
match refused {
Some(e) => Err(e),
None => $run,
}
}};
}
#[repr(C)]
#[derive(Default)]
struct DeviceInfo {
device_type: u32,
characteristics: u32,
}
fn is_remote(file: &fs::File) -> io::Result<bool> {
let mut info = DeviceInfo::default();
let mut status_block = IO_STATUS_BLOCK::default();
let status = unsafe {
NtQueryVolumeInformationFile(
file.as_raw_handle(),
&mut status_block,
(&raw mut info).cast(),
std::mem::size_of::<DeviceInfo>() as u32,
FileFsDeviceInformation,
)
};
if status < 0 {
return Err(nt_error(status));
}
Ok(info.characteristics & FILE_REMOTE_DEVICE != 0)
}
fn normalized_name(file: &fs::File) -> io::Result<Option<Vec<u16>>> {
let len = 4 + MAX_PATH_UNITS * 2;
let mut buf = aligned(len);
let mut status_block = IO_STATUS_BLOCK::default();
let status = unsafe {
NtQueryInformationFile(
file.as_raw_handle(),
&mut status_block,
buf.as_mut_ptr().cast(),
len as u32,
FileNormalizedNameInformation,
)
};
match status {
STATUS_NOT_SUPPORTED | STATUS_INVALID_INFO_CLASS | STATUS_INVALID_PARAMETER => {
return Ok(None);
}
s if s < 0 => return Err(nt_error(s)),
_ => {}
}
let data = bytes_of(&buf, len);
let invalid = || io::Error::new(io::ErrorKind::InvalidData, "a name without its length");
let name_len = u32_at(&data, 0).ok_or_else(invalid)? as usize;
let name = data.get(4..4 + name_len).ok_or_else(invalid)?;
Ok(Some(utf16le_to_units(name).ok_or_else(invalid)?))
}
fn listed_name(dir: &fs::File, name: &[u16]) -> io::Result<Vec<u16>> {
for entry in Entries::new(reopen(dir, FILE_LIST_DIRECTORY)?, name.to_vec()) {
let listed: Vec<u16> = entry?.name.encode_wide().collect();
if same_name(&listed, name) {
return Ok(listed);
}
}
Err(io::ErrorKind::NotFound.into())
}
pub(crate) struct RootVolume {
remote: bool,
no_normalized_names: AtomicBool,
no_posix_renames: AtomicBool,
#[cfg(test)]
corrections: AtomicUsize,
#[cfg(test)]
before_rename: std::sync::OnceLock<RenameHook>,
}
#[cfg(test)]
pub(crate) type RenameHook = Box<dyn Fn(&Path, &OsStr, bool) -> Option<fs::File> + Send + Sync>;
impl RootVolume {
fn of(root: &fs::File) -> io::Result<Self> {
Ok(Self {
remote: is_remote(root)?,
no_normalized_names: AtomicBool::new(false),
no_posix_renames: AtomicBool::new(false),
#[cfg(test)]
corrections: AtomicUsize::new(0),
#[cfg(test)]
before_rename: std::sync::OnceLock::new(),
})
}
#[cfg(test)]
pub(crate) fn before_rename(&self, hook: RenameHook) {
assert!(
self.before_rename.set(hook).is_ok(),
"a rename hook is in place already"
);
}
pub(crate) fn is_remote(&self) -> bool {
self.remote
}
#[cfg(test)]
pub(crate) fn corrections(&self) -> usize {
self.corrections.load(Ordering::Relaxed)
}
pub(crate) fn normalized(&self, file: &fs::File) -> io::Result<Option<Vec<u16>>> {
if self.no_normalized_names.load(Ordering::Relaxed) {
return Ok(None);
}
let name = normalized_name(file)?;
if name.is_none() {
self.no_normalized_names.store(true, Ordering::Relaxed);
}
Ok(name)
}
fn stored_name(&self, file: &fs::File, dir: &fs::File, name: &[u16]) -> io::Result<Vec<u16>> {
match self.normalized(file)? {
Some(path) => Ok(last_component(&path).to_vec()),
None => listed_name(dir, name),
}
}
fn rename(
&self,
file: &fs::File,
dir: HANDLE,
name: &[u16],
replace: bool,
) -> Result<(), NTSTATUS> {
let flags = if replace {
FILE_RENAME_REPLACE_IF_EXISTS
} else {
0
};
let mut wait = Wait::default();
loop {
let refused = match set_name(file, dir, name, FileRenameInformation, flags) {
Err(status) if dos_error(status) == ERROR_ACCESS_DENIED => status,
renamed => return renamed,
};
if !self.no_posix_renames.load(Ordering::Relaxed) {
let posix = set_name(
file,
dir,
name,
FileRenameInformationEx,
flags | FILE_RENAME_POSIX_SEMANTICS,
);
match posix {
Ok(()) => return Ok(()),
Err(status) if dos_error(status) == ERROR_DIR_NOT_EMPTY => return Err(status),
Err(status) if refuses_posix(status) => {
self.no_posix_renames.store(true, Ordering::Relaxed);
}
Err(_) => {}
}
}
if refused != STATUS_ACCESS_DENIED || !wait.again() {
return Err(refused);
}
}
}
pub(crate) fn settle(
&self,
names: &dyn Names,
made: Made,
requested: &[u16],
file: &fs::File,
) -> io::Result<()> {
let stored = match step!(Check, requested, names.stored(file)) {
Ok(stored) if stored == requested => return Ok(()),
Ok(stored) => stored,
Err(e) => return unsettled(made, requested, None, e),
};
let handle = match step!(Reopen, requested, names.reopen()) {
Ok(handle) => handle,
Err(e) => return unsettled(made, requested, Some(&stored), e),
};
let aside: Vec<u16> = match random_name() {
Ok(hex) => format!("~smb-{hex}").encode_utf16().collect(),
Err(e) => return unsettled(made, requested, Some(&stored), e),
};
if let Err(e) = step!(Aside, requested, names.rename(&handle, &aside)) {
return unsettled(made, requested, Some(&stored), e);
}
if let Err(e) = step!(Requested, requested, names.rename(&handle, requested)) {
if made == Made::Created && e.kind() == io::ErrorKind::AlreadyExists {
return Err(io::Error::other(Collided { aside }));
}
return match step!(Restore, requested, names.rename(&handle, &stored)) {
Ok(()) => unsettled(made, requested, Some(&stored), e),
Err(back) => {
error!(
requested = %String::from_utf16_lossy(requested),
left = %String::from_utf16_lossy(&aside),
error = %e,
back = %back,
"an entry was left under a temporary name"
);
Err(e)
}
};
}
match names.stored(&handle) {
Ok(now) if now == requested => {
#[cfg(test)]
self.corrections.fetch_add(1, Ordering::Relaxed);
Ok(())
}
Ok(now) => unsettled(made, requested, Some(&now), nt_error(STATUS_ACCESS_DENIED)),
Err(e) => unsettled(made, requested, None, e),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Made {
Created,
Moved,
CaseOnly,
}
fn unsettled(
made: Made,
requested: &[u16],
stored: Option<&[u16]>,
e: io::Error,
) -> io::Result<()> {
if made == Made::CaseOnly {
return Err(e);
}
warn!(
requested = %String::from_utf16_lossy(requested),
stored = %stored.map(String::from_utf16_lossy).unwrap_or_default(),
error = %e,
"a name keeps the spelling the server stored"
);
Ok(())
}
pub(crate) trait Names {
fn stored(&self, file: &fs::File) -> io::Result<Vec<u16>>;
fn reopen(&self) -> io::Result<fs::File>;
fn rename(&self, file: &fs::File, name: &[u16]) -> io::Result<()>;
}
struct EntryNames<'a> {
volume: &'a RootVolume,
dir: &'a fs::File,
requested: &'a [u16],
made: &'a fs::File,
deletable: bool,
}
impl Names for EntryNames<'_> {
fn stored(&self, file: &fs::File) -> io::Result<Vec<u16>> {
self.volume.stored_name(file, self.dir, self.requested)
}
fn reopen(&self) -> io::Result<fs::File> {
if self.deletable {
return self.made.try_clone();
}
let mut wait = Wait::default();
loop {
match reopen_checked(
self.made,
DELETE | FILE_READ_ATTRIBUTES,
SHARE_ALL,
FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_REPARSE_POINT,
) {
Err(e) if e.raw_os_error() == Some(ERROR_SHARING_VIOLATION) && wait.again() => {}
reopened => return reopened,
}
}
}
fn rename(&self, file: &fs::File, name: &[u16]) -> io::Result<()> {
self.volume
.rename(file, self.dir.as_raw_handle(), name, false)
.map_err(nt_error)
}
}
#[derive(Debug)]
pub(crate) struct Collided {
pub(crate) aside: Vec<u16>,
}
impl std::fmt::Display for Collided {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("another entry took the name while it was corrected")
}
}
impl std::error::Error for Collided {}
pub(crate) fn collided(e: &io::Error) -> Option<&Collided> {
e.get_ref()?.downcast_ref::<Collided>()
}
#[derive(Default)]
pub(crate) struct Wait {
first: Option<Instant>,
pauses: u32,
}
impl Wait {
pub(crate) fn again(&mut self) -> bool {
let first = *self.first.get_or_insert_with(Instant::now);
let Some(left) = RENAME_WAIT
.checked_sub(first.elapsed())
.filter(|left| !left.is_zero())
else {
return false;
};
let pause = match self.pauses {
n @ 0..=5 => Duration::from_millis(1 << n),
_ => Duration::from_millis(50),
};
self.pauses += 1;
std::thread::sleep(pause.min(left));
true
}
}
fn escape() -> io::Error {
io::Error::new(
io::ErrorKind::PermissionDenied,
"a path leads outside the share",
)
}
fn no_name() -> io::Error {
io::Error::new(io::ErrorKind::InvalidInput, "the path has no name")
}
struct Walk<'a> {
start: &'a Dir,
pin: Option<fs::File>,
opened: Vec<fs::File>,
pending: VecDeque<OsString>,
links: u32,
}
impl<'a> Walk<'a> {
fn new(start: &'a Dir, path: &Path) -> io::Result<Self> {
let pin = if start.held {
None
} else {
Some(reopen_checked(
&start.file,
HOLD_ACCESS,
HOLD_SHARE,
WALK_OPTIONS,
)?)
};
let mut walk = Self {
start,
pin,
opened: Vec::new(),
pending: VecDeque::new(),
links: 0,
};
walk.push_front(path)?;
Ok(walk)
}
fn dir_file(&self) -> &fs::File {
self.opened
.last()
.or(self.pin.as_ref())
.unwrap_or(&self.start.file)
}
fn dir(&self) -> HANDLE {
self.dir_file().as_raw_handle()
}
fn at_start(&self) -> bool {
self.opened.is_empty()
}
fn push_front(&mut self, path: &Path) -> io::Result<()> {
let mut names: Vec<OsString> = Vec::new();
for component in path.components() {
match component {
Component::Prefix(_) | Component::RootDir => return Err(escape()),
Component::CurDir => {}
Component::ParentDir if names.last().is_some_and(|name| name != "..") => {
names.pop();
}
Component::ParentDir => names.push("..".into()),
Component::Normal(name) => names.push(name.to_os_string()),
}
}
for name in names.into_iter().rev() {
self.pending.push_front(name);
}
Ok(())
}
fn follow(&mut self, link: &fs::File) -> io::Result<()> {
self.links += 1;
if self.links > MAX_LINKS {
return Err(io::Error::from_raw_os_error(ERROR_TOO_MANY_LINKS));
}
let target = link_target(link)?;
self.push_front(Path::new(&target))
}
fn reach_last(&mut self) -> io::Result<Option<OsString>> {
while let Some(name) = self.pending.pop_front() {
if name == ".." {
self.opened.pop().ok_or_else(escape)?;
continue;
}
if self.at_start() && self.start.reserves(&name) {
return Err(io::ErrorKind::NotFound.into());
}
if self.pending.is_empty() {
return Ok(Some(name));
}
let (next, _) = nt_open(
self.dir(),
name.encode_wide().collect(),
HOLD_ACCESS,
HOLD_SHARE,
FILE_OPEN,
WALK_OPTIONS,
)?;
let (attributes, tag) = attribute_tag(&next)?;
if links(attributes, tag) {
self.follow(&next)?;
} else if attributes & FILE_ATTRIBUTE_DIRECTORY == 0 {
return Err(io::Error::from_raw_os_error(ERROR_DIRECTORY));
} else {
self.opened.push(next);
#[cfg(test)]
if let Some(f) = lock_hook().as_ref() {
f(&name);
}
}
}
Ok(None)
}
fn reach_name(&mut self) -> io::Result<Vec<u16>> {
let name = self.reach_last()?.ok_or_else(no_name)?;
Ok(name.encode_wide().collect())
}
fn open(
&mut self,
access: u32,
disposition: u32,
options: u32,
follow: bool,
) -> io::Result<(fs::File, bool)> {
loop {
let name = self.reach_last()?;
let wide: Vec<u16> = match &name {
Some(name) => name.encode_wide().collect(),
None => Vec::new(),
};
let (file, done) = nt_open(
self.dir(),
wide.clone(),
access,
SHARE_ALL,
disposition,
options | FILE_OPEN_REPARSE_POINT,
)?;
if follow && name.is_some() && is_link(&file)? {
self.follow(&file)?;
continue;
}
let volume = &self.start.volume;
if done == FILE_CREATED && volume.remote && name.is_some() {
let names = EntryNames {
volume,
dir: self.dir_file(),
requested: &wide,
made: &file,
deletable: false,
};
volume.settle(&names, Made::Created, &wide, &file)?;
}
return Ok((file, name.is_none() && self.at_start()));
}
}
}
#[cfg(test)]
type Hook = Box<dyn Fn(&OsStr) + Send>;
#[cfg(test)]
pub(crate) static AFTER_COMPONENT: std::sync::Mutex<Option<Hook>> = std::sync::Mutex::new(None);
#[cfg(test)]
fn lock_hook() -> std::sync::MutexGuard<'static, Option<Hook>> {
AFTER_COMPONENT
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
pub(crate) struct AmbientAuthority;
pub(crate) fn ambient_authority() -> AmbientAuthority {
AmbientAuthority
}
pub(crate) struct Dir {
file: fs::File,
held: bool,
reserved: Option<(&'static str, bool)>,
volume: Arc<RootVolume>,
}
impl Dir {
pub(crate) fn open_ambient_dir(
path: impl AsRef<Path>,
_authority: AmbientAuthority,
) -> io::Result<Self> {
let file = fs::OpenOptions::new()
.read(true)
.custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
.share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE)
.open(path)?;
if !file.metadata()?.is_dir() {
return Err(io::Error::from_raw_os_error(ERROR_DIRECTORY));
}
let volume = Arc::new(RootVolume::of(&file)?);
Ok(Self {
file,
held: true,
reserved: None,
volume,
})
}
pub(crate) fn volume(&self) -> &RootVolume {
&self.volume
}
pub(crate) fn reserve(self, name: &'static str, case_sensitive: bool) -> Self {
Self {
reserved: Some((name, case_sensitive)),
..self
}
}
fn reserves(&self, name: &OsStr) -> bool {
self.reserved.is_some_and(|(reserved, case_sensitive)| {
name.to_str()
.is_some_and(|name| is_hidden(name, reserved, case_sensitive))
})
}
pub(crate) fn pin(&self) -> io::Result<Dir> {
let file = if self.held {
self.file.try_clone()?
} else {
reopen_checked(&self.file, HOLD_ACCESS, HOLD_SHARE, WALK_OPTIONS)?
};
Ok(Self {
file,
held: true,
reserved: self.reserved,
volume: Arc::clone(&self.volume),
})
}
pub(crate) fn dir_metadata(&self) -> io::Result<Metadata> {
Metadata::from_file(&self.file)
}
pub(crate) fn metadata(&self, path: impl AsRef<Path>) -> io::Result<Metadata> {
self.stat(path.as_ref(), true)
}
pub(crate) fn symlink_metadata(&self, path: impl AsRef<Path>) -> io::Result<Metadata> {
self.stat(path.as_ref(), false)
}
fn stat(&self, path: &Path, follow: bool) -> io::Result<Metadata> {
let (file, _) = Walk::new(self, path)?.open(
FILE_READ_ATTRIBUTES,
FILE_OPEN,
FILE_SYNCHRONOUS_IO_NONALERT,
follow,
)?;
Metadata::from_file(&file)
}
pub(crate) fn open_with(
&self,
path: impl AsRef<Path>,
options: &OpenOptions,
) -> io::Result<File> {
let (file, _) = self.open_file(path.as_ref(), options)?;
Ok(File(file))
}
pub(crate) fn open_dir(
&self,
path: impl AsRef<Path>,
options: &OpenOptions,
) -> io::Result<Dir> {
let (file, at_start) = self.open_file(path.as_ref(), options)?;
if !Metadata::from_file(&file)?.is_dir() {
return Err(io::ErrorKind::NotADirectory.into());
}
Ok(Self {
file,
held: false,
reserved: self.reserved.filter(|_| at_start),
volume: Arc::clone(&self.volume),
})
}
fn open_file(&self, path: &Path, options: &OpenOptions) -> io::Result<(fs::File, bool)> {
let access = options.access()?;
let disposition = options.disposition()?;
let mut create = FILE_SYNCHRONOUS_IO_NONALERT;
if options.flags & FILE_FLAG_BACKUP_SEMANTICS == 0 {
create |= FILE_NON_DIRECTORY_FILE;
} else if access & GENERIC_READ != 0 {
create |= FILE_OPEN_FOR_BACKUP_INTENT;
}
Walk::new(self, path)?.open(access | FILE_READ_ATTRIBUTES, disposition, create, true)
}
pub(crate) fn create_dir(&self, path: impl AsRef<Path>) -> io::Result<()> {
let mut walk = Walk::new(self, path.as_ref())?;
let Some(name) = walk.reach_last()? else {
return Err(io::ErrorKind::AlreadyExists.into());
};
let name: Vec<u16> = name.encode_wide().collect();
let remote = self.volume.remote;
let access = if remote {
FILE_LIST_DIRECTORY | DELETE | FILE_READ_ATTRIBUTES
} else {
FILE_LIST_DIRECTORY
};
let (made, _) = nt_open(
walk.dir(),
name.clone(),
access,
FILE_SHARE_READ | FILE_SHARE_WRITE,
FILE_CREATE,
FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT,
)?;
if remote {
let names = EntryNames {
volume: &self.volume,
dir: walk.dir_file(),
requested: &name,
made: &made,
deletable: true,
};
self.volume.settle(&names, Made::Created, &name, &made)?;
}
Ok(())
}
pub(crate) fn hold_dir(&self, name: &str) -> io::Result<fs::File> {
let (file, _) = nt_create(
self.file.as_raw_handle(),
name.encode_utf16().collect(),
HOLD_ACCESS | FILE_LIST_DIRECTORY,
HOLD_SHARE,
FILE_OPEN_IF,
FILE_DIRECTORY_FILE | WALK_OPTIONS,
FILE_ATTRIBUTE_HIDDEN,
)
.map_err(nt_error)?;
let (attributes, _) = attribute_tag(&file)?;
if attributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)
!= FILE_ATTRIBUTE_DIRECTORY
{
return Err(io::Error::from_raw_os_error(ERROR_DIRECTORY));
}
Ok(file)
}
pub(crate) fn open_removal(
&self,
path: impl AsRef<Path>,
is_dir: bool,
) -> io::Result<Removal<'_>> {
let mut walk = Walk::new(self, path.as_ref())?;
let name = walk.reach_name()?;
let (access, kind) = if is_dir {
(
DELETE | FILE_READ_ATTRIBUTES | FILE_LIST_DIRECTORY,
FILE_DIRECTORY_FILE,
)
} else {
(
DELETE | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
FILE_NON_DIRECTORY_FILE,
)
};
let (file, _) = nt_open(
walk.dir(),
name.clone(),
access,
SHARE_ALL,
FILE_OPEN,
kind | WALK_OPTIONS,
)?;
Ok(Removal {
walk,
name,
file,
is_dir,
})
}
pub(crate) fn rename(
&self,
from: impl AsRef<Path>,
to_dir: &Dir,
to: impl AsRef<Path>,
) -> io::Result<()> {
let mut source = Walk::new(self, from.as_ref())?;
let from_name = source.reach_name()?;
let mut target = Walk::new(to_dir, to.as_ref())?;
let to_name = target.reach_name()?;
let remote = self.volume.remote;
let in_place = remote
&& same_name(&from_name, &to_name)
&& (source.dir() == target.dir() || same_file(source.dir_file(), target.dir_file())?);
#[cfg(test)]
let _held = match self.volume.before_rename.get() {
Some(hook) if remote => {
let to = to.as_ref();
let parent = to.parent().unwrap_or(Path::new(""));
hook(parent, &OsString::from_wide(&to_name), in_place)
}
_ => None,
};
let (file, _) = nt_open(
source.dir(),
from_name.clone(),
DELETE | FILE_READ_ATTRIBUTES,
SHARE_ALL,
FILE_OPEN,
WALK_OPTIONS,
)?;
self.volume
.rename(&file, target.dir(), &to_name, true)
.map_err(nt_error)?;
if !remote {
return Ok(());
}
let made = if in_place {
Made::CaseOnly
} else {
Made::Moved
};
let names = EntryNames {
volume: &self.volume,
dir: target.dir_file(),
requested: &to_name,
made: &file,
deletable: true,
};
self.volume.settle(&names, made, &to_name, &file)
}
pub(crate) fn entries(&self) -> io::Result<Entries> {
self.entries_matching(None)
}
pub(crate) fn entries_matching(&self, pattern: Option<&str>) -> io::Result<Entries> {
Ok(Entries::new(
reopen(&self.file, FILE_LIST_DIRECTORY)?,
pattern.unwrap_or("").encode_utf16().collect(),
))
}
}
impl AsHandle for Dir {
fn as_handle(&self) -> BorrowedHandle<'_> {
self.file.as_handle()
}
}
pub(crate) struct Removal<'a> {
walk: Walk<'a>,
name: Vec<u16>,
file: fs::File,
is_dir: bool,
}
impl Removal<'_> {
pub(crate) fn delete_posix(&self) -> Result<(), NTSTATUS> {
delete_posix(&self.file, !self.is_dir)
}
pub(crate) fn delete_on_close(&self) -> Result<(), NTSTATUS> {
delete_on_close(&self.file)
}
pub(crate) fn move_to(&self, dir: &fs::File, name: &[u16]) -> Result<(), NTSTATUS> {
set_name(
&self.file,
dir.as_raw_handle(),
name,
FileRenameInformation,
0,
)
}
pub(crate) fn has_entries(&self) -> io::Result<bool> {
Ok(entries_of(&self.file)?.next().transpose()?.is_some())
}
pub(crate) fn stored_name(&self) -> io::Result<Vec<u16>> {
self.walk
.start
.volume
.stored_name(&self.file, self.walk.dir_file(), &self.name)
}
pub(crate) fn move_back(&self, name: &[u16]) -> Result<(), NTSTATUS> {
set_name(&self.file, self.walk.dir(), name, FileRenameInformation, 0)
}
}
pub(crate) fn entries_of(dir: &fs::File) -> io::Result<Entries> {
Ok(Entries::new(dir.try_clone()?, Vec::new()))
}
pub(crate) struct File(fs::File);
impl File {
pub(crate) fn into_std(self) -> fs::File {
self.0
}
}
impl std::ops::Deref for File {
type Target = fs::File;
fn deref(&self) -> &fs::File {
&self.0
}
}
#[derive(Default)]
pub(crate) struct OpenOptions {
read: bool,
write: bool,
create_new: bool,
flags: u32,
access: Option<u32>,
}
impl OpenOptions {
pub(crate) fn new() -> Self {
Self::default()
}
pub(crate) fn read(&mut self, read: bool) -> &mut Self {
self.read = read;
self
}
pub(crate) fn write(&mut self, write: bool) -> &mut Self {
self.write = write;
self
}
pub(crate) fn create_new(&mut self, create_new: bool) -> &mut Self {
self.create_new = create_new;
self
}
pub(crate) fn custom_flags(&mut self, flags: u32) -> &mut Self {
self.flags = flags;
self
}
pub(crate) fn access_mode(&mut self, access: u32) -> &mut Self {
self.access = Some(access);
self
}
fn access(&self) -> io::Result<u32> {
match (self.access, self.read, self.write) {
(Some(access), ..) => Ok(access),
(None, true, false) => Ok(GENERIC_READ),
(None, false, true) => Ok(GENERIC_WRITE),
(None, true, true) => Ok(GENERIC_READ | GENERIC_WRITE),
(None, false, false) => Err(io::Error::from_raw_os_error(ERROR_INVALID_PARAMETER)),
}
}
fn disposition(&self) -> io::Result<u32> {
match (self.create_new, self.write) {
(false, _) => Ok(FILE_OPEN),
(true, true) => Ok(FILE_CREATE),
(true, false) => Err(io::Error::from_raw_os_error(ERROR_INVALID_PARAMETER)),
}
}
}
#[derive(Clone, Copy)]
pub(crate) struct FileType {
attributes: u32,
reparse_tag: u32,
}
impl FileType {
pub(crate) fn is_dir(&self) -> bool {
!self.is_symlink() && self.attributes & FILE_ATTRIBUTE_DIRECTORY != 0
}
pub(crate) fn is_symlink(&self) -> bool {
links(self.attributes, self.reparse_tag)
}
}
pub(crate) struct SystemTime(std::time::SystemTime);
impl SystemTime {
pub(crate) fn into_std(self) -> std::time::SystemTime {
self.0
}
}
pub(crate) struct Permissions {
readonly: bool,
}
impl Permissions {
pub(crate) fn readonly(&self) -> bool {
self.readonly
}
}
pub(crate) struct Metadata {
attributes: u32,
reparse_tag: u32,
len: u64,
created: u64,
accessed: u64,
written: u64,
volume_serial_number: Option<u32>,
file_index: Option<u64>,
number_of_links: Option<u32>,
}
impl Metadata {
pub(crate) fn from_file(file: &fs::File) -> io::Result<Self> {
let mut info = BY_HANDLE_FILE_INFORMATION::default();
let ok = unsafe { GetFileInformationByHandle(file.as_raw_handle(), &mut info) };
if ok == 0 {
return Err(io::Error::last_os_error());
}
let reparse_tag = if info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT != 0 {
attribute_tag(file)?.1
} else {
0
};
let ticks = |t: windows_sys::Win32::Foundation::FILETIME| {
(u64::from(t.dwHighDateTime) << 32) | u64::from(t.dwLowDateTime)
};
Ok(Self {
attributes: info.dwFileAttributes,
reparse_tag,
len: (u64::from(info.nFileSizeHigh) << 32) | u64::from(info.nFileSizeLow),
created: ticks(info.ftCreationTime),
accessed: ticks(info.ftLastAccessTime),
written: ticks(info.ftLastWriteTime),
volume_serial_number: Some(info.dwVolumeSerialNumber),
file_index: Some(
(u64::from(info.nFileIndexHigh) << 32) | u64::from(info.nFileIndexLow),
),
number_of_links: Some(info.nNumberOfLinks),
})
}
pub(crate) fn file_type(&self) -> FileType {
FileType {
attributes: self.attributes,
reparse_tag: self.reparse_tag,
}
}
pub(crate) fn is_dir(&self) -> bool {
self.file_type().is_dir()
}
pub(crate) fn len(&self) -> u64 {
self.len
}
pub(crate) fn permissions(&self) -> Permissions {
Permissions {
readonly: self.attributes & FILE_ATTRIBUTE_READONLY != 0,
}
}
pub(crate) fn modified(&self) -> io::Result<SystemTime> {
time(self.written)
}
pub(crate) fn accessed(&self) -> io::Result<SystemTime> {
time(self.accessed)
}
pub(crate) fn created(&self) -> io::Result<SystemTime> {
time(self.created)
}
pub(crate) fn file_attributes(&self) -> u32 {
self.attributes
}
pub(crate) fn volume_serial_number(&self) -> Option<u32> {
self.volume_serial_number
}
pub(crate) fn file_index(&self) -> Option<u64> {
self.file_index
}
pub(crate) fn number_of_links(&self) -> Option<u32> {
self.number_of_links
}
}
fn time(ft: u64) -> io::Result<SystemTime> {
filetime_to_system_time(ft)
.map(SystemTime)
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "a time out of range"))
}
pub(crate) struct DirEntry {
name: OsString,
attributes: u32,
ea_size: u32,
len: u64,
created: u64,
accessed: u64,
written: u64,
}
impl DirEntry {
pub(crate) fn file_name(&self) -> OsString {
self.name.clone()
}
pub(crate) fn file_type(&self) -> io::Result<FileType> {
Ok(FileType {
attributes: self.attributes,
reparse_tag: self.reparse_tag(),
})
}
pub(crate) fn metadata(&self) -> io::Result<Metadata> {
Ok(Metadata {
attributes: self.attributes,
reparse_tag: self.reparse_tag(),
len: self.len,
created: self.created,
accessed: self.accessed,
written: self.written,
volume_serial_number: None,
file_index: None,
number_of_links: None,
})
}
pub(crate) fn ea_size(&self) -> Option<u32> {
(self.attributes & FILE_ATTRIBUTE_REPARSE_POINT == 0).then_some(self.ea_size)
}
fn reparse_tag(&self) -> u32 {
if self.attributes & FILE_ATTRIBUTE_REPARSE_POINT == 0 {
0
} else {
self.ea_size
}
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Listing {
Unstarted,
Reading,
Finished,
}
pub(crate) struct Entries {
handle: fs::File,
filter: Vec<u16>,
buf: Vec<u64>,
listed: Vec<u8>,
at: Option<usize>,
state: Listing,
}
impl Entries {
fn new(handle: fs::File, filter: Vec<u16>) -> Self {
Self {
handle,
filter,
buf: aligned(LISTING),
listed: Vec::new(),
at: None,
state: Listing::Unstarted,
}
}
fn fill(&mut self) -> io::Result<bool> {
let bytes =
u16::try_from(self.filter.len() * 2).map_err(|_| io::ErrorKind::InvalidFilename)?;
let filter = UNICODE_STRING {
Length: bytes,
MaximumLength: bytes,
Buffer: self.filter.as_mut_ptr(),
};
let mut status_block = IO_STATUS_BLOCK::default();
let first = self.state == Listing::Unstarted;
let status = unsafe {
NtQueryDirectoryFile(
self.handle.as_raw_handle(),
std::ptr::null_mut(),
None,
std::ptr::null(),
&mut status_block,
self.buf.as_mut_ptr().cast(),
LISTING as u32,
FileFullDirectoryInformation,
false,
if first && bytes != 0 {
&filter
} else {
std::ptr::null()
},
first,
)
};
self.state = Listing::Reading;
match status {
STATUS_NO_MORE_FILES | STATUS_NO_SUCH_FILE => Ok(false),
s if s < 0 => Err(nt_error(s)),
_ if status_block.Information == 0 => Ok(false),
_ => {
self.listed = bytes_of(&self.buf, status_block.Information.min(LISTING));
self.at = Some(0);
Ok(true)
}
}
}
fn entry_at(&self, at: usize) -> Option<(DirEntry, Option<usize>)> {
let listed = &self.listed;
let field = |offset: usize| u32_at(listed, at.checked_add(offset)?);
let quad = |offset: usize| u64_at(listed, at.checked_add(offset)?);
let next = field(0)? as usize;
let name_at = at.checked_add(DIR_NAME)?;
let name_len = field(DIR_NAME_LENGTH)? as usize;
let name = listed.get(name_at..name_at.checked_add(name_len)?)?;
let units = utf16le_to_units(name)?;
let entry = DirEntry {
name: OsString::from_wide(&units),
attributes: field(DIR_ATTRIBUTES)?,
ea_size: field(DIR_EA_SIZE)?,
len: quad(DIR_END_OF_FILE)?,
created: quad(DIR_CREATION_TIME)?,
accessed: quad(DIR_LAST_ACCESS_TIME)?,
written: quad(DIR_LAST_WRITE_TIME)?,
};
let after = if next == 0 {
None
} else {
at.checked_add(next)
};
Some((entry, after))
}
}
impl Iterator for Entries {
type Item = io::Result<DirEntry>;
fn next(&mut self) -> Option<io::Result<DirEntry>> {
loop {
if let Some(at) = self.at {
let parsed = self.entry_at(at);
self.at = parsed.as_ref().and_then(|(_, after)| *after);
match parsed {
Some((entry, _)) if entry.name != "." && entry.name != ".." => {
return Some(Ok(entry));
}
_ => continue,
}
}
if self.state == Listing::Finished {
return None;
}
match self.fill() {
Ok(true) => {}
Ok(false) => {
self.state = Listing::Finished;
return None;
}
Err(e) => {
self.state = Listing::Finished;
return Some(Err(e));
}
}
}
}
}
#[cfg(test)]
pub(super) mod tests;