Skip to content

Commit

Permalink
std: Implement fs::DirBuilder
Browse files Browse the repository at this point in the history
This is the last remaining portion of #24796
  • Loading branch information
alexcrichton committed Apr 28, 2015
1 parent 9348700 commit ba4e4fc
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 17 deletions.
62 changes: 57 additions & 5 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ pub struct Permissions(fs_imp::FilePermissions);
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct FileType(fs_imp::FileType);

/// A builder used to create directories in various manners.
///
/// This builder also supports platform-specific options.
#[unstable(feature = "dir_builder", reason = "recently added API")]
pub struct DirBuilder {
inner: fs_imp::DirBuilder,
recursive: bool,
}

impl File {
/// Attempts to open a file in read-only mode.
///
Expand Down Expand Up @@ -997,7 +1006,7 @@ pub fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
fs_imp::mkdir(path.as_ref())
DirBuilder::new().create(path.as_ref())
}

/// Recursively create a directory and all of its parent components if they
Expand All @@ -1022,10 +1031,7 @@ pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
let path = path.as_ref();
if path == Path::new("") || path.is_dir() { return Ok(()) }
if let Some(p) = path.parent() { try!(create_dir_all(p)) }
create_dir(path)
DirBuilder::new().recursive(true).create(path.as_ref())
}

/// Removes an existing, empty directory.
Expand Down Expand Up @@ -1286,6 +1292,52 @@ pub fn set_permissions<P: AsRef<Path>>(path: P, perm: Permissions) -> io::Result
fs_imp::set_perm(path.as_ref(), perm.0)
}

impl DirBuilder {
/// Creates a new set of options with default mode/security settings for all
/// platforms and also non-recursive.
pub fn new() -> DirBuilder {
DirBuilder {
inner: fs_imp::DirBuilder::new(),
recursive: false,
}
}

/// Indicate that directories create should be created recursively, creating
/// all parent directories if they do not exist with the same security and
/// permissions settings.
///
/// This option defaults to `false`
pub fn recursive(&mut self, recursive: bool) -> &mut Self {
self.recursive = recursive;
self
}

/// Create the specified directory with the options configured in this
/// builder.
pub fn create<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
let path = path.as_ref();
if self.recursive {
self.create_dir_all(path)
} else {
self.inner.mkdir(path)
}
}

fn create_dir_all(&self, path: &Path) -> io::Result<()> {
if path == Path::new("") || path.is_dir() { return Ok(()) }
if let Some(p) = path.parent() {
try!(self.create_dir_all(p))
}
self.inner.mkdir(path)
}
}

impl AsInnerMut<fs_imp::DirBuilder> for DirBuilder {
fn as_inner_mut(&mut self) -> &mut fs_imp::DirBuilder {
&mut self.inner
}
}

#[cfg(test)]
mod tests {
#![allow(deprecated)] //rand
Expand Down
16 changes: 16 additions & 0 deletions src/libstd/sys/unix/ext/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,19 @@ pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()>
{
sys::fs2::symlink(src.as_ref(), dst.as_ref())
}

#[unstable(feature = "dir_builder", reason = "recently added API")]
/// An extension trait for `fs::DirBuilder` for unix-specific options.
pub trait DirBuilderExt {
/// Sets the mode to create new directories with. This option defaults to
/// 0o777.
fn mode(&mut self, mode: raw::mode_t) -> &mut Self;
}

impl DirBuilderExt for fs::DirBuilder {
fn mode(&mut self, mode: raw::mode_t) -> &mut fs::DirBuilder {
self.as_inner_mut().set_mode(mode);
self
}
}

24 changes: 18 additions & 6 deletions src/libstd/sys/unix/fs2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub struct FilePermissions { mode: mode_t }
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct FileType { mode: mode_t }

pub struct DirBuilder { mode: mode_t }

impl FileAttr {
pub fn size(&self) -> u64 { self.stat.st_size as u64 }
pub fn perm(&self) -> FilePermissions {
Expand Down Expand Up @@ -342,6 +344,22 @@ impl File {
pub fn fd(&self) -> &FileDesc { &self.0 }
}

impl DirBuilder {
pub fn new() -> DirBuilder {
DirBuilder { mode: 0o777 }
}

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

pub fn set_mode(&mut self, mode: mode_t) {
self.mode = mode;
}
}

fn cstr(path: &Path) -> io::Result<CString> {
path.as_os_str().to_cstring().ok_or(
io::Error::new(io::ErrorKind::InvalidInput, "path contained a null"))
Expand Down Expand Up @@ -401,12 +419,6 @@ impl fmt::Debug for File {
}
}

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

pub fn readdir(p: &Path) -> io::Result<ReadDir> {
let root = Arc::new(p.to_path_buf());
let p = try!(cstr(p));
Expand Down
20 changes: 14 additions & 6 deletions src/libstd/sys/windows/fs2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ pub struct OpenOptions {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct FilePermissions { attrs: libc::DWORD }

pub struct DirBuilder;

impl Iterator for ReadDir {
type Item = io::Result<DirEntry>;
fn next(&mut self) -> Option<io::Result<DirEntry>> {
Expand Down Expand Up @@ -425,12 +427,18 @@ impl FileType {
pub fn is_symlink(&self) -> bool { *self == FileType::Symlink }
}

pub fn mkdir(p: &Path) -> io::Result<()> {
let p = to_utf16(p);
try!(cvt(unsafe {
libc::CreateDirectoryW(p.as_ptr(), ptr::null_mut())
}));
Ok(())
impl DirBuilder {
pub fn new() -> DirBuilder {
DirBuilder { mode: 0o777 }
}

pub fn mkdir(&self, p: &Path) -> io::Result<()> {
let p = to_utf16(p);
try!(cvt(unsafe {
libc::CreateDirectoryW(p.as_ptr(), ptr::null_mut())
}));
Ok(())
}
}

pub fn readdir(p: &Path) -> io::Result<ReadDir> {
Expand Down

0 comments on commit ba4e4fc

Please sign in to comment.