Skip to main content

windows_namespace_request_sys/
lib.rs

1// Copyright (c) Mike Grier.
2
3//! Owned, marshalable parameter sets for synchronous Win32 namespace calls.
4//!
5//! Win32's namespace and metadata surface -- opening, querying, closing -- is
6//! synchronous-only. A call that blocks on a dead network path blocks the thread
7//! that made it, and no overlapped form exists. This crate makes such a call
8//! **capturable as a value**: an owned parameter set built on one thread and
9//! executed faithfully on another.
10//!
11//! It schedules nothing. It is the catalogue-plus-faithful-execution layer,
12//! testable with no ring, no pool, and no async anywhere near it.
13//!
14//! # What a request is, and is not
15//!
16//! A request carries call **parameters**. It does not carry the impersonation
17//! token or any other thread-scoped state the call runs under -- that belongs to
18//! [`windows-thread-ambient-sys`][ambient], which this crate does not depend on.
19//! The two are siblings rather than a stack: a request can be executed with no
20//! captured context at all, and a context is useful to work that never opens a
21//! file. Whoever owns both pairs them at the submission site.
22//!
23//! A request also chooses **no delivery model**. An opened handle comes back
24//! plain and unassociated, because associating it with a completion port
25//! irreversibly forecloses `IoRing` use, and that choice belongs to a layer that
26//! knows the handle's destination.
27//!
28//! [ambient]: https://docs.rs/windows-thread-ambient-sys
29//!
30//! # Faithful means unaltered
31//!
32//! An entry reports the raw Win32 outcome. `ERROR_FILE_NOT_FOUND` means a
33//! missing directory from an open, an empty directory from a first query, and a
34//! genuine failure from a later one; only a consumer can tell those apart, so
35//! nothing here normalises or reclassifies.
36//!
37//! The code is also snapshotted before any cleanup can overwrite it, because
38//! `GetLastError` is volatile thread state that a `Drop` or a buffer release
39//! will happily clobber. That guarantee is a primitive rather than a rule each
40//! entry remembers: see [`outcome`].
41//!
42//! # A path is copied; a handle is duplicated
43//!
44//! Several entries take a handle rather than a path, and a request owns a
45//! **duplicate** of any handle it names. The distinction matters and is easy to
46//! get backwards: a path is a value and is copied, while a handle is a reference
47//! to a kernel object, so duplicating it *shares that object* rather than
48//! cloning it.
49//!
50//! A request is therefore self-contained with respect to **lifetime** -- it
51//! cannot be left pointing at a handle its originator closed -- and is **not**
52//! isolated with respect to **state**. Measured: a duplicated handle continues
53//! the source's directory enumeration rather than starting its own, while
54//! closing the duplicate leaves the source usable and single-shot metadata
55//! queries disturb nothing. An independent traversal needs a fresh open, not a
56//! duplicate.
57//!
58//! # Scope
59//!
60//! One entry per Win32 call; a consumer needing two makes two requests and
61//! sequences them itself. The round-one entry list is audited from three real
62//! consumers rather than chosen by taste, and its omissions are deliberate and
63//! written down. Volume and disk operations are not namespace operations and are in
64//! `win-volume-sys`, not here. See `DESIGN-NOTES.md` in the crate
65//! root.
66//!
67//! # Example
68//!
69//! Capture the parameters on the submitting thread, where a failure is still
70//! the caller's to see and the process current directory still means what the
71//! caller thinks it means, then use them on a worker that saw none of it:
72//!
73//! ```
74//! use std::fs;
75//! use std::os::windows::io::AsHandle;
76//! use std::thread;
77//!
78//! use windows_namespace_request_sys::{CapturedHandle, OpenFile};
79//! use wtf_string::Wtf16String;
80//!
81//! let path = std::env::temp_dir().join(format!("wnrs-doc-{}.tmp", std::process::id()));
82//! fs::write(&path, b"example")?;
83//!
84//! // Built here, not on the worker: a relative path is fixed to the process
85//! // current directory now, which is shared mutable state that any thread can
86//! // change in between. This one is absolute, so it is handed over as given.
87//! let text = path.to_str().expect("a temporary path is valid UTF-8");
88//! let request = OpenFile::new(Wtf16String::from(text))?;
89//! assert_eq!(request.win32_path().to_string_lossy(), text);
90//!
91//! // An owned duplicate, so the captured parameters cannot be left pointing
92//! // at a handle the caller has since closed.
93//! let file = fs::File::open(&path)?;
94//! let captured = CapturedHandle::capture(file.as_handle())?;
95//! drop(file);
96//!
97//! let length = thread::spawn(move || {
98//!     fs::File::from(captured.into_owned_handle()).metadata().map(|m| m.len())
99//! })
100//! .join()
101//! .expect("the worker did not panic")?;
102//!
103//! assert_eq!(length, b"example".len() as u64);
104//! # fs::remove_file(&path)?;
105//! # Ok::<(), Box<dyn std::error::Error>>(())
106//! ```
107
108#![cfg(windows)]
109#![forbid(unsafe_op_in_unsafe_fn)]
110#![warn(missing_docs)]
111
112mod anchored;
113pub mod close;
114pub mod file_info;
115pub mod final_path;
116pub mod full_path;
117pub mod handle;
118pub mod open;
119pub mod open_by_id;
120pub mod outcome;
121pub mod query;
122pub mod reopen;
123pub mod request;
124pub mod security;
125pub mod volume;
126pub mod watch;
127
128pub use anchored::PathCaptureError;
129pub use close::{CloseFn, CloseRequest};
130pub use file_info::QueryFileInformationByHandle;
131pub use final_path::{FinalPathError, FinalPathFlags, QueryFinalPath};
132pub use full_path::{FullPathError, ResolveFullPath};
133pub use handle::{CapturedHandle, HandleCaptureError, HandleCaptureFailure};
134pub use io_buffers::AlignedBuf;
135pub use open::OpenFile;
136pub use open_by_id::{FileIdentifier, OpenFileByIdentifier};
137pub use outcome::{Outcome, Win32Error};
138pub use query::{FileInformationClass, QueryFileInformation};
139pub use reopen::ReOpenFile;
140pub use request::{ConsumingRequest, Request};
141/// Compiles the README's examples, so a contract change breaks the build
142/// rather than silently teaching the old answer.
143#[cfg(doctest)]
144#[doc = include_str!("../README.md")]
145struct ReadmeDoctests;
146
147#[cfg(test)]
148mod tests;
149
150pub use security::{
151    AclState, SecurityAttributes, SecurityCaptureError, SecurityCaptureFailure, SecurityDescriptor,
152    SecurityInformation,
153};
154pub use volume::{QueryVolumeInformation, VolumeInformation};
155pub use watch::{ChangeNotification, NotifyFilter, WatchDirectory};