Skip to content

Commit d1fa735

Browse files
committed
Fix stat when the last path component is a symlink to ...
Replace `stat_via_parent` with `stat_manually` which similarly to `open_manually`. Factor the `open_manually` implemenation to allow parts to be reused.
1 parent 87e4836 commit d1fa735

13 files changed

+447
-246
lines changed

cap-primitives/src/fs/canonicalize_manually.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Manual path canonicalization, one component at a time, with manual symlink
22
//! resolution, in order to enforce sandboxing.
33
4-
use crate::fs::{canonicalize_options, open_manually, FollowSymlinks, MaybeOwnedFile};
4+
use crate::fs::{canonicalize_options, open_manually_impl, FollowSymlinks, MaybeOwnedFile};
55
use std::{
66
fs, io,
77
path::{Path, PathBuf},
@@ -28,7 +28,7 @@ pub(crate) fn canonicalize_manually(
2828
let mut canonical_path = PathBuf::new();
2929
let start = MaybeOwnedFile::borrowed(start);
3030

31-
if let Err(e) = open_manually(
31+
if let Err(e) = open_manually_impl(
3232
start,
3333
path,
3434
canonicalize_options().follow(follow),

cap-primitives/src/fs/metadata.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,35 @@ impl Metadata {
2727
/// Constructs a new instance of `Self` from the given `std::fs::Metadata`.
2828
#[inline]
2929
pub fn from_std(std: fs::Metadata) -> Self {
30+
// TODO: Initialize `created` on Linux with `std.created().ok()` once yanix
31+
// has `statx` and we make use of it.
3032
Self {
3133
file_type: FileType::from_std(std.file_type()),
3234
len: std.len(),
3335
permissions: Permissions::from_std(std.permissions()),
3436
modified: std.modified().ok(),
3537
accessed: std.accessed().ok(),
38+
39+
#[cfg(any(
40+
target_os = "freebsd",
41+
target_os = "openbsd",
42+
target_os = "macos",
43+
target_os = "ios",
44+
target_os = "netbsd",
45+
windows,
46+
))]
3647
created: std.created().ok(),
48+
49+
#[cfg(not(any(
50+
target_os = "freebsd",
51+
target_os = "openbsd",
52+
target_os = "macos",
53+
target_os = "ios",
54+
target_os = "netbsd",
55+
windows,
56+
)))]
57+
created: None,
58+
3759
ext: MetadataExt::from_std(std),
3860
}
3961
}

cap-primitives/src/fs/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ mod set_permissions;
4242
#[cfg(not(target_os = "linux"))] // doesn't work reliably on linux
4343
mod set_permissions_via_parent;
4444
mod stat;
45-
mod stat_via_parent;
4645
mod symlink;
4746
mod symlink_via_parent;
4847
mod unlink;
@@ -70,7 +69,6 @@ pub(crate) use rename_via_parent::*;
7069
pub(crate) use rmdir_via_parent::*;
7170
#[cfg(not(target_os = "linux"))] // doesn't work reliably on linux
7271
pub(crate) use set_permissions_via_parent::*;
73-
pub(crate) use stat_via_parent::*;
7472
pub(crate) use symlink_via_parent::*;
7573
pub(crate) use unlink_via_parent::*;
7674

cap-primitives/src/fs/open_entry_manually.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::fs::{
2-
open_manually, open_unchecked, readlink_one, FollowSymlinks, MaybeOwnedFile, OpenOptions,
2+
open_manually_impl, open_unchecked, readlink_one, FollowSymlinks, MaybeOwnedFile, OpenOptions,
33
OpenUncheckedError,
44
};
55
use std::{ffi::OsStr, fs, io};
@@ -19,7 +19,7 @@ pub(crate) fn open_entry_manually(
1919
let mut symlink_count = 0;
2020
let destination = readlink_one(start, path, &mut symlink_count)?;
2121
let maybe = MaybeOwnedFile::borrowed(start);
22-
open_manually(maybe, &destination, options, &mut symlink_count, None)
22+
open_manually_impl(maybe, &destination, options, &mut symlink_count, None)
2323
.map(MaybeOwnedFile::unwrap_owned)
2424
}
2525
Err(OpenUncheckedError::NotFound(err)) | Err(OpenUncheckedError::Other(err)) => Err(err),

0 commit comments

Comments
 (0)