-
Notifications
You must be signed in to change notification settings - Fork 31
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
Getting the path of a Dir #373
Comments
See https://docs.rs/cap-std-ext/latest/cap_std_ext/cmdext/trait.CapStdExtCommandExt.html
This is OS specific but possible, see https://github.com/rust-lang/rust/blob/c24e166527cd46d3b4033b0c061d02657e3c3cbf/library/std/src/sys/pal/unix/fs.rs#L1508
Same as above. |
Yes. I think this makes sense to add to cap-primitives already has an internal implementation of this ( |
Thanks to you both for responding! I tried sunfishcode's suggestion, however the Here are the changes I tried: For diff --git a/cap-primitives/src/rustix/darwin/fs/file_path.rs b/cap-primitives/src/rustix/darwin/fs/file_path.rs
index 377309b..8b0f182 100644
--- a/cap-primitives/src/rustix/darwin/fs/file_path.rs
+++ b/cap-primitives/src/rustix/darwin/fs/file_path.rs
@@ -12,7 +12,7 @@ use std::os::unix::ffi::OsStringExt;
use std::os::wasi::ffi::OsStringExt;
use std::path::PathBuf;
-pub(crate) fn file_path(file: &fs::File) -> Option<PathBuf> {
+pub fn file_path(file: &fs::File) -> Option<PathBuf> {
if let Ok(path) = getpath(file) {
return Some(OsString::from_vec(path.into_bytes()).into());
}
diff --git a/cap-primitives/src/rustix/linux/fs/file_path.rs b/cap-primitives/src/rustix/linux/fs/file_path.rs
index 430f10a..4fe53e5 100644
--- a/cap-primitives/src/rustix/linux/fs/file_path.rs
+++ b/cap-primitives/src/rustix/linux/fs/file_path.rs
@@ -2,7 +2,7 @@ use super::procfs::get_path_from_proc_self_fd;
use std::fs;
use std::path::PathBuf;
-pub(crate) fn file_path(file: &fs::File) -> Option<PathBuf> {
+pub fn file_path(file: &fs::File) -> Option<PathBuf> {
use std::os::unix::fs::MetadataExt;
// Ignore paths that don't start with '/', which are things like
diff --git a/cap-primitives/src/windows/fs/mod.rs b/cap-primitives/src/windows/fs/mod.rs
index 0de735a..d804b4f 100644
--- a/cap-primitives/src/windows/fs/mod.rs
+++ b/cap-primitives/src/windows/fs/mod.rs
@@ -81,7 +81,7 @@ pub(crate) use symlink_unchecked::*;
// <https://docs.microsoft.com/en-us/windows/win32/fileio/reparse-points>
pub(crate) const MAX_SYMLINK_EXPANSIONS: u8 = 63;
-pub(crate) fn file_path(file: &std::fs::File) -> Option<std::path::PathBuf> {
+pub fn file_path(file: &std::fs::File) -> Option<std::path::PathBuf> {
get_path::get_path(file).ok()
} I'm also not sure whether these architecture-specific functions should be made public and exposed individually, or wrapped in an architecture-independent function which is exposed? |
Within cap-fs-ext implementations, you can use |
Sometimes I'd like to be able to get the path for a
Dir
.For example,
For example, in a personal project, I'm using the
git2
wrapper aroundlibgit2
. In order to callRepository::clone(url, path)
, I need the path of the cache directory. I could use gitoxide, a pure rust implementation of git, however that would have the same problem. Maybe in the future we can make gitoxide use cap-std, but for now that's not an option.Yes, some of these examples break the capability model, however, from a pragmatic point of view, that may be acceptable.
I'm not sure what the best approach here is, but in my personal project I'm using a wrapper like
with a constructor like
and an
open_dir
method that concatenates the path:One issue with maintaining my own wrapper is that I can't use e.g
ProjectDirs
fromcap-directories
because its methods returnDir
s, so the paths are unknown. I would have to wrapdirectories_next
directly.I wonder if it would be useful for other people if something like this was added to cap-std (with appropriate warnings with respect to it breaking the capability model)?
The text was updated successfully, but these errors were encountered: