-
Notifications
You must be signed in to change notification settings - Fork 13.4k
dirfd: preliminary unix and windows implementations #139514
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
base: master
Are you sure you want to change the base?
Changes from all commits
bcd769d
05f87c5
bdeb57e
0a82f16
228d994
c58a2d8
c1c4fb1
4890e60
f4d6ffc
02f9073
9ae1e46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,6 +132,33 @@ pub enum TryLockError { | |
WouldBlock, | ||
} | ||
|
||
/// An object providing access to a directory on the filesystem. | ||
/// | ||
/// Files are automatically closed when they go out of scope. Errors detected | ||
/// on closing are ignored by the implementation of `Drop`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// Opens a directory and then a file inside it. | ||
/// | ||
/// ```no_run | ||
/// #![feature(dirfd)] | ||
/// use std::{fs::Dir, io::Read}; | ||
/// | ||
/// fn main() -> std::io::Result<()> { | ||
/// let dir = Dir::new("foo")?; | ||
/// let mut file = dir.open("bar.txt")?; | ||
/// let mut s = String::new(); | ||
/// file.read_to_string(&mut s)?; | ||
/// println!("{}", s); | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
#[unstable(feature = "dirfd", issue = "120426")] | ||
pub struct Dir { | ||
inner: fs_imp::Dir, | ||
} | ||
|
||
/// Metadata information about a file. | ||
/// | ||
/// This structure is returned from the [`metadata`] or | ||
|
@@ -1453,6 +1480,223 @@ impl Seek for Arc<File> { | |
} | ||
} | ||
|
||
impl Dir { | ||
/// Attempts to open a directory at `path` in read-only mode. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if we should guarantee read-only. Directories aren't entirely consistent across unixes. That kind of access can be sufficient for Either we can try read access and fallback to traversal-only or always try traversal-only and require the use of Currently There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have tips to find which flag it is for each unix? I think requiring Might there also be potential for confusion when refactoring from Finally, is this blocking? It seems more important that I finish the DirEntry api, and I suspect this is only one of several aspects that face bikeshedding before or during an FCP. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Generally by looking at their manpages.
Well, if you don't want to change the implementation in this PR at least leave an internal note and relax the documentation. |
||
/// | ||
/// See [`new_with`] for more options. | ||
/// | ||
/// # Errors | ||
/// | ||
/// This function will return an error if `path` does not point to an existing directory. | ||
/// Other errors may also be returned according to [`OpenOptions::open`]. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// #![feature(dirfd)] | ||
/// use std::{fs::Dir, io::Read}; | ||
/// | ||
/// fn main() -> std::io::Result<()> { | ||
/// let dir = Dir::new("foo")?; | ||
/// let mut f = dir.open("bar.txt")?; | ||
/// let mut data = vec![]; | ||
/// f.read_to_end(&mut data)?; | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
/// | ||
/// [`new_with`]: Dir::new_with | ||
#[unstable(feature = "dirfd", issue = "120426")] | ||
pub fn new<P: AsRef<Path>>(path: P) -> io::Result<Self> { | ||
Ok(Self { inner: fs_imp::Dir::new(path)? }) | ||
} | ||
|
||
/// Attempts to open a directory at `path` with the options specified by `opts`. | ||
/// | ||
/// # Errors | ||
/// | ||
/// This function may return an error according to [`OpenOptions::open`]. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// #![feature(dirfd)] | ||
/// use std::fs::{Dir, OpenOptions}; | ||
/// | ||
/// fn main() -> std::io::Result<()> { | ||
/// let dir = Dir::new_with("foo", OpenOptions::new().write(true))?; | ||
/// let mut f = dir.remove_file("bar.txt")?; | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
#[unstable(feature = "dirfd", issue = "120426")] | ||
pub fn new_with<P: AsRef<Path>>(path: P, opts: &OpenOptions) -> io::Result<Self> { | ||
Ok(Self { inner: fs_imp::Dir::new_with(path, &opts.0)? }) | ||
} | ||
|
||
/// Attempts to open a file in read-only mode relative to this directory. | ||
/// | ||
/// # Errors | ||
/// | ||
/// This function will return an error if `path` does not point to an existing file. | ||
/// Other errors may also be returned according to [`OpenOptions::open`]. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// #![feature(dirfd)] | ||
/// use std::{fs::Dir, io::Read}; | ||
/// | ||
/// fn main() -> std::io::Result<()> { | ||
/// let dir = Dir::new("foo")?; | ||
/// let mut f = dir.open("bar.txt")?; | ||
/// let mut data = vec![]; | ||
/// f.read_to_end(&mut data)?; | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
#[unstable(feature = "dirfd", issue = "120426")] | ||
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This gives us a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be in addition to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
self.inner.open(path).map(|f| File { inner: f }) | ||
} | ||
|
||
/// Attempts to open a file relative to this directory with the options specified by `opts`. | ||
/// | ||
/// # Errors | ||
/// | ||
/// This function may return an error according to [`OpenOptions::open`]. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// #![feature(dirfd)] | ||
/// use std::{fs::{Dir, OpenOptions}, io::Read}; | ||
/// | ||
/// fn main() -> std::io::Result<()> { | ||
/// let dir = Dir::new("foo")?; | ||
/// let mut f = dir.open_with("bar.txt", OpenOptions::new().read(true))?; | ||
/// let mut data = vec![]; | ||
/// f.read_to_end(&mut data)?; | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
#[unstable(feature = "dirfd", issue = "120426")] | ||
pub fn open_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<File> { | ||
self.inner.open_with(path, &opts.0).map(|f| File { inner: f }) | ||
} | ||
|
||
/// Attempts to create a directory relative to this directory. | ||
/// | ||
/// # Errors | ||
/// | ||
/// This function will return an error if `path` points to an existing file or directory. | ||
/// Other errors may also be returned according to [`OpenOptions::open`]. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// #![feature(dirfd)] | ||
/// use std::{fs::{Dir, OpenOptions}, io::Read}; | ||
/// | ||
/// fn main() -> std::io::Result<()> { | ||
/// let dir = Dir::new("foo")?; | ||
/// let mut f = dir.open_with("bar.txt", OpenOptions::new().read(true))?; | ||
/// let mut data = vec![]; | ||
/// f.read_to_end(&mut data)?; | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
#[unstable(feature = "dirfd", issue = "120426")] | ||
pub fn create_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<()> { | ||
self.inner.create_dir(path) | ||
} | ||
|
||
/// Attempts to remove a file relative to this directory. | ||
/// | ||
/// # Errors | ||
/// | ||
/// This function will return an error if `path` does not point to an existing file. | ||
/// Other errors may also be returned according to [`OpenOptions::open`]. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// #![feature(dirfd)] | ||
/// use std::fs::Dir; | ||
/// | ||
/// fn main() -> std::io::Result<()> { | ||
/// let dir = Dir::new("foo")?; | ||
/// dir.remove_file("bar.txt")?; | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
#[unstable(feature = "dirfd", issue = "120426")] | ||
pub fn remove_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> { | ||
self.inner.remove_file(path) | ||
} | ||
|
||
/// Attempts to remove a directory relative to this directory. | ||
/// | ||
/// # Errors | ||
/// | ||
/// This function will return an error if `path` does not point to an existing, non-empty directory. | ||
/// Other errors may also be returned according to [`OpenOptions::open`]. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// #![feature(dirfd)] | ||
/// use std::fs::Dir; | ||
/// | ||
/// fn main() -> std::io::Result<()> { | ||
/// let dir = Dir::new("foo")?; | ||
/// dir.remove_dir("baz")?; | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
#[unstable(feature = "dirfd", issue = "120426")] | ||
pub fn remove_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<()> { | ||
self.inner.remove_dir(path) | ||
} | ||
|
||
/// Attempts to rename a file or directory relative to this directory to a new name, replacing | ||
/// the destination file if present. | ||
/// | ||
/// # Errors | ||
/// | ||
/// This function will return an error if `from` does not point to an existing file or directory. | ||
/// Other errors may also be returned according to [`OpenOptions::open`]. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// #![feature(dirfd)] | ||
/// use std::fs::Dir; | ||
/// | ||
/// fn main() -> std::io::Result<()> { | ||
/// let dir = Dir::new("foo")?; | ||
/// dir.rename("bar.txt", &dir, "quux.txt")?; | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
#[unstable(feature = "dirfd", issue = "120426")] | ||
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>( | ||
&self, | ||
from: P, | ||
to_dir: &Self, | ||
to: Q, | ||
) -> io::Result<()> { | ||
self.inner.rename(from, &to_dir.inner, to) | ||
} | ||
} | ||
|
||
#[unstable(feature = "dirfd", issue = "120426")] | ||
impl fmt::Debug for Dir { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.inner.fmt(f) | ||
} | ||
} | ||
|
||
impl OpenOptions { | ||
/// Creates a blank new set of options ready for configuration. | ||
/// | ||
|
Uh oh!
There was an error while loading. Please reload this page.