Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cap-primitives] add support for illumos #369

Merged
merged 1 commit into from
Oct 30, 2024
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
4 changes: 2 additions & 2 deletions cap-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ edition = "2021"
[dependencies]
ambient-authority = "0.0.2"
arbitrary = { version = "1.0.0", optional = true, features = ["derive"] }
ipnet = "2.3.0"
ipnet = "2.5.0"
maybe-owned = "0.3.4"
fs-set-times = "0.20.0"
io-extras = "0.18.0"
Expand All @@ -25,7 +25,7 @@ io-lifetimes = { version = "2.0.0", default-features = false }
cap-tempfile = { path = "../cap-tempfile" }

[target.'cfg(not(windows))'.dependencies]
rustix = { version = "0.38.32", features = ["fs", "process", "procfs", "termios", "time"] }
rustix = { version = "0.38.38", features = ["fs", "process", "procfs", "termios", "time"] }

[target.'cfg(windows)'.dependencies]
winx = "0.36.0"
Expand Down
6 changes: 6 additions & 0 deletions cap-primitives/src/fs/dir_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ impl DirEntry {
/// Returns the file type for the file that this entry points at.
///
/// This corresponds to [`std::fs::DirEntry::file_type`].
///
/// # Platform-specific behavior
///
/// On Windows and most Unix platforms this function is free (no extra system calls needed), but
/// some Unix platforms may require the equivalent call to `metadata` to learn about the target
/// file type.
#[inline]
pub fn file_type(&self) -> io::Result<FileType> {
self.inner.file_type()
Expand Down
13 changes: 11 additions & 2 deletions cap-primitives/src/rustix/fs/dir_entry_inner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::fs::{
FileType, FollowSymlinks, ImplFileTypeExt, Metadata, MetadataExt, OpenOptions, ReadDir,
ReadDirInner,
FileType, FollowSymlinks, Metadata, MetadataExt, OpenOptions, ReadDir, ReadDirInner,
};
use rustix::fs::DirEntry;
use std::ffi::{OsStr, OsString};
Expand Down Expand Up @@ -41,9 +40,12 @@ impl DirEntryInner {
self.read_dir.remove_dir(self.file_name_bytes())
}

#[cfg(not(any(target_os = "illumos", target_os = "solaris")))]
#[inline]
#[allow(clippy::unnecessary_wraps)]
pub(crate) fn file_type(&self) -> io::Result<FileType> {
use crate::fs::ImplFileTypeExt;

Ok(match self.rustix.file_type() {
rustix::fs::FileType::Directory => FileType::dir(),
rustix::fs::FileType::RegularFile => FileType::file(),
Expand All @@ -58,6 +60,13 @@ impl DirEntryInner {
})
}

#[cfg(any(target_os = "illumos", target_os = "solaris"))]
#[inline]
pub(crate) fn file_type(&self) -> io::Result<FileType> {
// These platforms don't have the file type on the dirent, so we must lstat the file.
self.metadata().map(|m| m.file_type())
}

#[inline]
pub(crate) fn file_name(&self) -> OsString {
self.file_name_bytes().to_os_string()
Expand Down
2 changes: 2 additions & 0 deletions cap-primitives/src/rustix/fs/dir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ pub(crate) const fn target_o_path() -> OFlags {
target_os = "netbsd",
target_os = "openbsd",
target_os = "wasi",
target_os = "illumos",
target_os = "solaris",
))]
{
OFlags::empty()
Expand Down
10 changes: 7 additions & 3 deletions cap-primitives/src/rustix/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,13 @@ fn tty_path() {
#[cfg(unix)]
use std::os::unix::fs::FileTypeExt;

// On FreeBSD, /dev/{tty,stdin,stdout,stderr} are aliases to different real
// devices.
let paths: &[&str] = if cfg!(target_os = "freebsd") {
// On FreeBSD, /dev/{tty,stdin,stdout,stderr} are aliases to different
// real devices.
&["/dev/ttyv0", "/dev/pts/0"]
} else if cfg!(target_os = "illumos") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure about Solaris here so I left this as illumos. The other bits I'm reasonably sure are okay on Solaris.

// On illumos, /dev/std{in,out,err} only exist if they're open.
&["/dev/tty", "/dev/pts/0"]
} else {
&["/dev/tty", "/dev/stdin", "/dev/stdout", "/dev/stderr"]
};
Expand All @@ -162,7 +165,8 @@ fn tty_path() {
.as_ref()
.map(std::fs::canonicalize)
.map(Result::unwrap),
Some(canonical)
Some(canonical),
"for path {path}, file_path matches canonicalized path"
);
}
}
Expand Down
Loading