Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,8 @@ That operation requires a caller-trusted stable ancestor namespace, permits shar
It refuses links and directories another account can modify, leaves existing permissions intact, and reports the last completed and uncertain component with the original I/O cause while retaining the partial chain for explicit recovery.
On Windows it currently returns `Unsupported` before creation; a file write-through acknowledgement and the legacy directory-sync no-op cannot close this required bootstrap boundary.
The document save in `hide-host/src/save.rs` is exchange-and-verify on top of these and keeps its contract on every system; its checks (a regular file, one name, owned by this account, writable) are answered by `identity::link_count`, `private::handle_owned_by_current_user` and `permissions::Permissions`, so they mean the same on Windows.
`link` makes a link a caller can replace in one step (`replace_link`, the kit's `current`) and ask about (`is_link_to`): a symbolic link on Unix; on Windows a junction for a folder, which needs no privilege and leads only to an absolute path on this machine, and a symbolic link for a file, which needs the privilege or Developer Mode and otherwise fails with `NeedsPrivilege`, a typed answer (`needs_privilege(&error)`) that a caller with an alternative takes.
`link` makes a link a caller can replace in one step (`replace_link`, the kit's `current`) and ask about (`is_link_to`); the name is never missing while it is replaced, and a path opened through it at that moment finds the old target or the new one on macOS and Windows but can answer "not found" on Linux, where the path walk that follows the link can meet the swap.
A link is a symbolic link on Unix; on Windows a junction for a folder, which needs no privilege and leads only to an absolute path on this machine, and a symbolic link for a file, which needs the privilege or Developer Mode and otherwise fails with `NeedsPrivilege`, a typed answer (`needs_privilege(&error)`) that a caller with an alternative takes.
`identity` says which file a name leads to (`FileId`: a device and an inode, or a volume serial and a 128-bit file id), whether two names are one file (`/tmp` and `/private/tmp`, a link and its target), the id of one entry of an open folder (`entry_id`, which the Explorer's "replaced while the prompt was open" check compares on every system), a canonical spelling with the `\\?\` prefix removed where the short form means the same, the path an open file or folder is at now (`path_of`: `F_GETPATH`, `/proc/self/fd`, `GetFinalPathNameByHandleW` with its `\\?\` prefix kept, so hided's root boundary compares a file and its root by prefix at any length), a `Stamp` (length, write time, change time) that tells whether a file changed between two looks, and whether a folder tells `Name` from `name` (asked of the folder at run time, because a Mac or Windows volume may be either).
`private::open_own_file` opens a file only when the name itself is the account's own regular file with no other name (a symbolic link at the name is not followed, a hard link is refused, a pipe does not block), which the label generator's lock, the daemon's locks and the pane credential reads stand on; `fs::open_regular` is the same refusal of links, pipes and devices without the ownership, for a file the operator picked to attach; `private::create_dir_all` makes a chain of private folders.
`private::handle_others_can_modify` checks the permissions of an already-open descriptor, so retirement decides from the same ledger, progress or run-state file it reads; the kit also refuses links at each owned directory before reads and removal.
Expand Down
4 changes: 3 additions & 1 deletion hide-host/src/kit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ fn expand(path: &str, home: &Path) -> HostResult<PathBuf> {
}

/// Points `<root>/current` at `version` through a link made beside it and
/// renamed over it, so the name never leads nowhere in between.
/// renamed over it, so the name is never missing in between. On Linux a
/// program started through `current` at that moment can, rarely, be told it
/// does not exist (see `replace_link`).
fn point_current(root: &Path, version: &str) -> HostResult<()> {
let current = root.join(hide_kit::CURRENT);
if hide_platform::fs::link::is_link_to(&current, Path::new(version)) {
Expand Down
7 changes: 5 additions & 2 deletions hide-platform/src/fs/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ pub fn create_link(target: &Path, link: &Path) -> io::Result<()> {

/// Makes `link` lead to `target`, replacing a link that is already there in
/// one step on every system where the system allows it: a link is made
/// beside it and renamed over it, so the name never leads nowhere in
/// between.
/// beside it and renamed over it, so the name itself is never missing in
/// between and reads as the old target or the new one. Opening a path
/// through the link while it is replaced finds the old target or the new
/// one on macOS and Windows; on Linux that open can, for a moment, find
/// nothing and answer "not found".
pub fn replace_link(target: &Path, link: &Path) -> io::Result<()> {
let name = link
.file_name()
Expand Down
33 changes: 27 additions & 6 deletions hide-platform/tests/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,8 +1134,10 @@ fn replacing_a_link_points_the_same_name_at_the_new_target_with_nothing_left_bes
assert_eq!(names(outer.path()), ["current", "version-1", "version-2"]);
}

#[test]
fn a_link_that_is_replaced_never_leads_nowhere_while_a_reader_follows_it() {
/// Swaps `current` between two versions 40 times while a reader loops on
/// `read`, which answers the version it found; a read can be refused while
/// the swap lands on Windows, and what it must never be is "not found".
fn replace_while_reading(read: impl Fn(&Path) -> std::io::Result<String> + Sync) {
let outer = folder();
for version in ["version-1", "version-2"] {
fs::create_dir(outer.path().join(version)).unwrap();
Expand All @@ -1149,11 +1151,9 @@ fn a_link_that_is_replaced_never_leads_nowhere_while_a_reader_follows_it() {
scope.spawn(|| {
let mut seen = 0;
while !done.load(Ordering::Relaxed) || seen < 20 {
// A read can be refused while the swap lands on Windows;
// what it must never be is "not found".
match fs::read_to_string(current.join("hide")) {
match read(&current) {
Ok(text) => {
assert!(text == "version-1" || text == "version-2");
assert!(text == "version-1" || text == "version-2", "{text:?}");
seen += 1;
}
Err(error) => assert_ne!(error.kind(), ErrorKind::NotFound, "{error}"),
Expand All @@ -1172,6 +1172,27 @@ fn a_link_that_is_replaced_never_leads_nowhere_while_a_reader_follows_it() {
});
}

#[test]
fn a_link_that_is_replaced_is_never_missing_while_a_reader_looks_at_it() {
// A junction reads back as the absolute path it leads to.
replace_while_reading(|current| {
fs::read_link(current).map(|to| {
to.file_name()
.unwrap_or_default()
.to_string_lossy()
.into_owned()
})
});
}

/// Linux does not keep this one: a path walk that follows the link while it
/// is renamed over can find nothing and answer "not found".
#[cfg(not(target_os = "linux"))]
#[test]
fn a_link_that_is_replaced_never_leads_nowhere_while_a_reader_follows_it() {
replace_while_reading(|current| fs::read_to_string(current.join("hide")));
}

#[test]
fn a_dangling_link_is_still_a_link_and_a_plain_file_is_not() {
let outer = folder();
Expand Down
Loading