Skip to content

Commit 57e9ffd

Browse files
committed
add unix implementation for create_dir
1 parent c3a9db0 commit 57e9ffd

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

library/std/src/fs.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,33 @@ impl Dir {
16081608
self.inner.open_with(path, &opts.0).map(|f| File { inner: f })
16091609
}
16101610

1611+
/// Attempts to create a directory relative to this directory.
1612+
///
1613+
/// # Errors
1614+
///
1615+
/// This function will return an error in these (and other) situations:
1616+
/// * The path exists
1617+
/// * The process doesn't have permission to create the directory
1618+
///
1619+
/// # Examples
1620+
///
1621+
/// ```no_run
1622+
/// #![feature(dirfd)]
1623+
/// use std::{fs::{Dir, OpenOptions}, io::Read};
1624+
///
1625+
/// fn main() -> std::io::Result<()> {
1626+
/// let dir = Dir::new("foo")?;
1627+
/// let mut f = dir.open_with("bar.txt", OpenOptions::new().read(true))?;
1628+
/// let mut data = vec![];
1629+
/// f.read_to_end(&mut data)?;
1630+
/// Ok(())
1631+
/// }
1632+
/// ```
1633+
#[unstable(feature = "dirfd", issue = "120426")]
1634+
pub fn create_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
1635+
self.inner.create_dir(path)
1636+
}
1637+
16111638
/// Attempts to remove a file relative to this directory.
16121639
///
16131640
/// # Errors

library/std/src/sys/fs/unix.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ use libc::{c_int, mode_t};
5454
#[cfg(target_os = "android")]
5555
use libc::{
5656
dirent as dirent64, fstat as fstat64, fstatat as fstatat64, ftruncate64, lseek64,
57-
lstat as lstat64, off64_t, open as open64, openat as openat64, renameat, stat as stat64,
58-
unlinkat,
57+
lstat as lstat64, mkdirat, off64_t, open as open64, openat as openat64, renameat,
58+
stat as stat64, unlinkat,
5959
};
6060
#[cfg(not(any(
6161
all(target_os = "linux", not(target_env = "musl")),
@@ -65,7 +65,7 @@ use libc::{
6565
)))]
6666
use libc::{
6767
dirent as dirent64, fstat as fstat64, ftruncate as ftruncate64, lseek as lseek64,
68-
lstat as lstat64, off_t as off64_t, open as open64, openat as openat64, renameat,
68+
lstat as lstat64, mkdirat, off_t as off64_t, open as open64, openat as openat64, renameat,
6969
stat as stat64, unlinkat,
7070
};
7171
#[cfg(any(
@@ -74,8 +74,8 @@ use libc::{
7474
target_os = "hurd"
7575
))]
7676
use libc::{
77-
dirent64, fstat64, ftruncate64, lseek64, lstat64, off64_t, open64, openat64, renameat, stat64,
78-
unlinkat,
77+
dirent64, fstat64, ftruncate64, lseek64, lstat64, mkdirat, off64_t, open64, openat64, renameat,
78+
stat64, unlinkat,
7979
};
8080

8181
use crate::ffi::{CStr, OsStr, OsString};
@@ -294,6 +294,10 @@ impl Dir {
294294
run_path_with_cstr(path.as_ref(), &|path| self.open_c(path, opts))
295295
}
296296

297+
pub fn create_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
298+
run_path_with_cstr(path.as_ref(), &|path| self.create_dir_c(path))
299+
}
300+
297301
pub fn remove_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
298302
run_path_with_cstr(path.as_ref(), &|path| self.remove_c(path, false))
299303
}
@@ -334,6 +338,10 @@ impl Dir {
334338
Ok(Self(unsafe { OwnedFd::from_raw_fd(fd) }))
335339
}
336340

341+
pub fn create_dir_c(&self, path: &CStr) -> io::Result<()> {
342+
cvt(unsafe { mkdirat(self.0.as_raw_fd(), path.as_ptr(), 0o777) }).map(|_| ())
343+
}
344+
337345
pub fn remove_c(&self, path: &CStr, remove_dir: bool) -> io::Result<()> {
338346
cvt(unsafe {
339347
unlinkat(

library/std/src/sys/fs/windows.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -932,9 +932,9 @@ impl Dir {
932932
}
933933

934934
pub fn create_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
935-
run_path_with_wcstr(path, &|path| {
936-
self.create_dir_native(path, &OpenOptions::new()).map(|_| ())
937-
})
935+
let mut opts = OpenOptions::new();
936+
opts.write(true);
937+
run_path_with_wcstr(path, &|path| self.create_dir_native(path, &opts).map(|_| ()))
938938
}
939939

940940
pub fn remove_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {

0 commit comments

Comments
 (0)