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

Add Permissions argument to create_dir{,_all} #22422

Closed
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
16 changes: 12 additions & 4 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub struct OpenOptions(fs_imp::OpenOptions);
/// functionality, such as mode bits, is available through the
/// `os::unix::PermissionsExt` trait.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Permissions(fs_imp::FilePermissions);
pub struct Permissions(fs_imp::Permissions);

impl File {
/// Attempts to open a file in read-only mode.
Expand Down Expand Up @@ -319,8 +319,8 @@ impl Permissions {
}
}

impl FromInner<fs_imp::FilePermissions> for Permissions {
fn from_inner(f: fs_imp::FilePermissions) -> Permissions {
impl FromInner<fs_imp::Permissions> for Permissions {
fn from_inner(f: fs_imp::Permissions) -> Permissions {
Permissions(f)
}
}
Expand Down Expand Up @@ -521,7 +521,15 @@ pub fn read_link<P: AsPath + ?Sized>(path: &P) -> io::Result<PathBuf> {
/// This function will return an error if the user lacks permissions to make a
/// new directory at the provided `path`, or if the directory already exists.
pub fn create_dir<P: AsPath + ?Sized>(path: &P) -> io::Result<()> {
fs_imp::mkdir(path.as_path())
#[cfg(windows)]
fn mkdir(path: &Path) -> io::Result<()> {
fs_imp::mkdir(path)
}
#[cfg(unix)]
fn mkdir(path: &Path) -> io::Result<()> {
fs_imp::mkdir(path, FromInner::from_inner(0o777))
}
mkdir(path.as_path())
}

/// Recursively create a directory and all of its parent components if they
Expand Down
20 changes: 10 additions & 10 deletions src/libstd/sys/unix/fs2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub struct OpenOptions {
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct FilePermissions { mode: mode_t }
pub struct Permissions { mode: mode_t }

impl FileAttr {
pub fn is_dir(&self) -> bool {
Expand All @@ -60,8 +60,8 @@ impl FileAttr {
(self.stat.st_mode as mode_t) & libc::S_IFMT == libc::S_IFREG
}
pub fn size(&self) -> u64 { self.stat.st_size as u64 }
pub fn perm(&self) -> FilePermissions {
FilePermissions { mode: (self.stat.st_mode as mode_t) & 0o777 }
pub fn perm(&self) -> Permissions {
Permissions { mode: (self.stat.st_mode as mode_t) & 0o777 }
}

pub fn accessed(&self) -> u64 {
Expand All @@ -77,7 +77,7 @@ impl FileAttr {
}
}

impl FilePermissions {
impl Permissions {
pub fn readonly(&self) -> bool { self.mode & 0o222 == 0 }
pub fn set_readonly(&mut self, readonly: bool) {
if readonly {
Expand All @@ -88,9 +88,9 @@ impl FilePermissions {
}
}

impl FromInner<i32> for FilePermissions {
fn from_inner(mode: i32) -> FilePermissions {
FilePermissions { mode: mode as mode_t }
impl FromInner<i32> for Permissions {
fn from_inner(mode: i32) -> Permissions {
Permissions { mode: mode as mode_t }
}
}

Expand Down Expand Up @@ -272,9 +272,9 @@ fn cstr(path: &Path) -> CString {
CString::from_slice(path.as_os_str().as_bytes())
}

pub fn mkdir(p: &Path) -> io::Result<()> {
pub fn mkdir(p: &Path, perms: Permissions) -> io::Result<()> {
let p = cstr(p);
try!(cvt(unsafe { libc::mkdir(p.as_ptr(), 0o777) }));
try!(cvt(unsafe { libc::mkdir(p.as_ptr(), perms.mode) }));
Ok(())
}

Expand Down Expand Up @@ -304,7 +304,7 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
Ok(())
}

pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> {
pub fn set_perm(p: &Path, perm: Permissions) -> io::Result<()> {
let p = cstr(p);
try!(cvt_r(|| unsafe { libc::chmod(p.as_ptr(), perm.mode) }));
Ok(())
Expand Down
10 changes: 5 additions & 5 deletions src/libstd/sys/windows/fs2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct OpenOptions {
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct FilePermissions { attrs: libc::DWORD }
pub struct Permissions { attrs: libc::DWORD }

impl Iterator for ReadDir {
type Item = io::Result<DirEntry>;
Expand Down Expand Up @@ -266,8 +266,8 @@ impl FileAttr {
pub fn size(&self) -> u64 {
((self.data.nFileSizeHigh as u64) << 32) | (self.data.nFileSizeLow as u64)
}
pub fn perm(&self) -> FilePermissions {
FilePermissions { attrs: self.data.dwFileAttributes }
pub fn perm(&self) -> Permissions {
Permissions { attrs: self.data.dwFileAttributes }
}

pub fn accessed(&self) -> u64 { self.to_ms(&self.data.ftLastAccessTime) }
Expand All @@ -281,7 +281,7 @@ impl FileAttr {
}
}

impl FilePermissions {
impl Permissions {
pub fn readonly(&self) -> bool {
self.attrs & c::FILE_ATTRIBUTE_READONLY != 0
}
Expand Down Expand Up @@ -390,7 +390,7 @@ pub fn stat(p: &Path) -> io::Result<FileAttr> {
}
}

pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> {
pub fn set_perm(p: &Path, perm: Permissions) -> io::Result<()> {
let p = to_utf16(p);
unsafe {
try!(cvt(c::SetFileAttributesW(p.as_ptr(), perm.attrs)));
Expand Down