diff options
| author | dubiousjim <dubiousjim@gmail.com> | 2020-03-20 09:46:26 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-20 09:46:26 -0400 |
| commit | 69303e21495a14a2c6709f96af364682a485ac21 (patch) | |
| tree | b2662cefde16afce58d6dc73e1c243fc9cefa18b /cli/fs.rs | |
| parent | 798904b0f2ed0c7284b67bba2f125f406b5850de (diff) | |
refactor: move code from fs.rs into ops/fs.rs (#4428)
This a complex boring PR that shifts around code (primarily) in cli/fs.rs and
cli/ops/fs.rs. The gain of this refactoring is to ease the way for #4188 and
#4017, and also to avoid some future development pain.
Mostly there is no change in functionality. Except:
* squashed bugs where op_utime and op_chown weren't using `resolve_from_cwd`
* eliminated the use of the external `remove_dir_all` crate.
* op_chmod now only queries metadata to verify file/dir exists on Windows (it
will already fail on Unix if it doesn't)
* op_chown now verifies the file/dir's existence on Windows like chmod does.
Diffstat (limited to 'cli/fs.rs')
| -rw-r--r-- | cli/fs.rs | 119 |
1 files changed, 16 insertions, 103 deletions
@@ -1,21 +1,12 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -use std; -use std::fs::{DirBuilder, File, OpenOptions}; -use std::io::ErrorKind; +use std::env::current_dir; +use std::fs::OpenOptions; use std::io::Write; use std::path::{Component, Path, PathBuf}; use deno_core::ErrBox; -use rand; -use rand::Rng; use walkdir::WalkDir; -#[cfg(unix)] -use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt, PermissionsExt}; - -#[cfg(unix)] -use nix::unistd::{chown as unix_chown, Gid, Uid}; - pub fn write_file<T: AsRef<[u8]>>( filename: &Path, data: T, @@ -41,97 +32,19 @@ pub fn write_file_2<T: AsRef<[u8]>>( .open(filename)?; if update_mode { - set_permissions(&mut file, mode)?; - } - - file.write_all(data.as_ref()) -} - -#[cfg(unix)] -fn set_permissions(file: &mut File, mode: u32) -> std::io::Result<()> { - debug!("set file mode to {}", mode); - file.set_permissions(PermissionsExt::from_mode(mode & 0o777)) -} - -#[cfg(not(unix))] -fn set_permissions(_file: &mut File, _mode: u32) -> std::io::Result<()> { - // NOOP on windows - Ok(()) -} - -pub fn make_temp( - dir: Option<&Path>, - prefix: Option<&str>, - suffix: Option<&str>, - is_dir: bool, -) -> std::io::Result<PathBuf> { - let prefix_ = prefix.unwrap_or(""); - let suffix_ = suffix.unwrap_or(""); - let mut buf: PathBuf = match dir { - Some(ref p) => p.to_path_buf(), - None => std::env::temp_dir(), - } - .join("_"); - let mut rng = rand::thread_rng(); - loop { - let unique = rng.gen::<u32>(); - buf.set_file_name(format!("{}{:08x}{}", prefix_, unique, suffix_)); - let r = if is_dir { - let mut builder = DirBuilder::new(); - set_dir_permission(&mut builder, 0o700); - builder.create(buf.as_path()) - } else { - let mut open_options = OpenOptions::new(); - open_options.write(true).create_new(true); - #[cfg(unix)] - open_options.mode(0o600); - open_options.open(buf.as_path())?; - Ok(()) - }; - match r { - Err(ref e) if e.kind() == ErrorKind::AlreadyExists => continue, - Ok(_) => return Ok(buf), - Err(e) => return Err(e), + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mode = mode & 0o777; + debug!("set file mode to {:o}", mode); + let permissions = PermissionsExt::from_mode(mode); + file.set_permissions(permissions)?; } + #[cfg(not(unix))] + let _ = mode; } -} - -pub fn mkdir(path: &Path, mode: u32, recursive: bool) -> std::io::Result<()> { - let mut builder = DirBuilder::new(); - builder.recursive(recursive); - set_dir_permission(&mut builder, mode); - builder.create(path) -} - -#[cfg(unix)] -fn set_dir_permission(builder: &mut DirBuilder, mode: u32) { - let mode = mode & 0o777; - debug!("set dir mode to {:o}", mode); - builder.mode(mode); -} - -#[cfg(not(unix))] -fn set_dir_permission(_builder: &mut DirBuilder, _mode: u32) { - // NOOP on windows -} -#[cfg(unix)] -pub fn chown(path: &str, uid: u32, gid: u32) -> Result<(), ErrBox> { - let nix_uid = Uid::from_raw(uid); - let nix_gid = Gid::from_raw(gid); - unix_chown(path, Option::Some(nix_uid), Option::Some(nix_gid)) - .map_err(ErrBox::from) -} - -#[cfg(not(unix))] -pub fn chown(_path: &str, _uid: u32, _gid: u32) -> Result<(), ErrBox> { - // FAIL on Windows - // TODO: implement chown for Windows - let e = std::io::Error::new( - std::io::ErrorKind::Other, - "Not implemented".to_string(), - ); - Err(ErrBox::from(e)) + file.write_all(data.as_ref()) } /// Normalize all itermediate components of the path (ie. remove "./" and "../" components). @@ -171,7 +84,7 @@ pub fn resolve_from_cwd(path: &Path) -> Result<PathBuf, ErrBox> { let resolved_path = if path.is_absolute() { path.to_owned() } else { - let cwd = std::env::current_dir().unwrap(); + let cwd = current_dir().unwrap(); cwd.join(path) }; @@ -184,19 +97,19 @@ mod tests { #[test] fn resolve_from_cwd_child() { - let cwd = std::env::current_dir().unwrap(); + let cwd = current_dir().unwrap(); assert_eq!(resolve_from_cwd(Path::new("a")).unwrap(), cwd.join("a")); } #[test] fn resolve_from_cwd_dot() { - let cwd = std::env::current_dir().unwrap(); + let cwd = current_dir().unwrap(); assert_eq!(resolve_from_cwd(Path::new(".")).unwrap(), cwd); } #[test] fn resolve_from_cwd_parent() { - let cwd = std::env::current_dir().unwrap(); + let cwd = current_dir().unwrap(); assert_eq!(resolve_from_cwd(Path::new("a/..")).unwrap(), cwd); } |
