2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2018-07-26 17:54:22 -04:00
|
|
|
use std;
|
2018-09-14 15:30:43 -04:00
|
|
|
use std::fs::{create_dir, DirBuilder, File, OpenOptions};
|
2018-08-23 18:36:45 -04:00
|
|
|
use std::io::ErrorKind;
|
2018-08-22 13:19:32 -04:00
|
|
|
use std::io::Write;
|
2020-02-26 16:11:52 -05:00
|
|
|
use std::path::{Component, Path, PathBuf};
|
2018-08-23 18:36:45 -04:00
|
|
|
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ErrBox;
|
2018-08-23 18:36:45 -04:00
|
|
|
use rand;
|
2018-08-29 14:06:41 -04:00
|
|
|
use rand::Rng;
|
2020-02-17 13:11:45 -05:00
|
|
|
use walkdir::WalkDir;
|
2018-07-26 17:54:22 -04:00
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::fs::{DirBuilderExt, PermissionsExt};
|
|
|
|
|
2019-05-07 21:58:58 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
use nix::unistd::{chown as unix_chown, Gid, Uid};
|
2018-09-11 12:00:57 -04:00
|
|
|
|
2018-12-11 08:36:34 -05:00
|
|
|
pub fn write_file<T: AsRef<[u8]>>(
|
2018-09-11 12:00:57 -04:00
|
|
|
filename: &Path,
|
2018-12-11 08:36:34 -05:00
|
|
|
data: T,
|
2018-09-11 12:00:57 -04:00
|
|
|
perm: u32,
|
|
|
|
) -> std::io::Result<()> {
|
2019-02-02 14:26:18 -05:00
|
|
|
write_file_2(filename, data, true, perm, true, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_file_2<T: AsRef<[u8]>>(
|
|
|
|
filename: &Path,
|
|
|
|
data: T,
|
|
|
|
update_perm: bool,
|
|
|
|
perm: u32,
|
|
|
|
is_create: bool,
|
|
|
|
is_append: bool,
|
|
|
|
) -> std::io::Result<()> {
|
2018-09-11 12:00:57 -04:00
|
|
|
let mut file = OpenOptions::new()
|
|
|
|
.read(false)
|
|
|
|
.write(true)
|
|
|
|
.append(is_append)
|
|
|
|
.truncate(!is_append)
|
2019-02-02 14:26:18 -05:00
|
|
|
.create(is_create)
|
2018-09-11 12:00:57 -04:00
|
|
|
.open(filename)?;
|
|
|
|
|
2019-02-02 14:26:18 -05:00
|
|
|
if update_perm {
|
|
|
|
set_permissions(&mut file, perm)?;
|
|
|
|
}
|
|
|
|
|
2018-12-11 08:36:34 -05:00
|
|
|
file.write_all(data.as_ref())
|
2018-09-11 12:00:57 -04:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
#[cfg(unix)]
|
2018-09-11 12:00:57 -04:00
|
|
|
fn set_permissions(file: &mut File, perm: u32) -> std::io::Result<()> {
|
|
|
|
debug!("set file perm to {}", perm);
|
|
|
|
file.set_permissions(PermissionsExt::from_mode(perm & 0o777))
|
|
|
|
}
|
2020-03-02 10:19:42 -05:00
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
2018-09-11 12:00:57 -04:00
|
|
|
fn set_permissions(_file: &mut File, _perm: u32) -> std::io::Result<()> {
|
|
|
|
// NOOP on windows
|
|
|
|
Ok(())
|
2018-08-22 13:19:32 -04:00
|
|
|
}
|
|
|
|
|
2020-02-18 14:45:59 -05:00
|
|
|
pub fn make_temp(
|
2018-08-23 18:36:45 -04:00
|
|
|
dir: Option<&Path>,
|
|
|
|
prefix: Option<&str>,
|
|
|
|
suffix: Option<&str>,
|
2020-02-18 14:45:59 -05:00
|
|
|
is_dir: bool,
|
2018-08-23 18:36:45 -04:00
|
|
|
) -> 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(),
|
2019-07-31 17:11:37 -04:00
|
|
|
}
|
|
|
|
.join("_");
|
2018-08-29 14:06:41 -04:00
|
|
|
let mut rng = rand::thread_rng();
|
2018-08-23 18:36:45 -04:00
|
|
|
loop {
|
2018-08-29 14:06:41 -04:00
|
|
|
let unique = rng.gen::<u32>();
|
2018-08-23 18:36:45 -04:00
|
|
|
buf.set_file_name(format!("{}{:08x}{}", prefix_, unique, suffix_));
|
|
|
|
// TODO: on posix, set mode flags to 0o700.
|
2020-02-18 14:45:59 -05:00
|
|
|
let r = if is_dir {
|
|
|
|
create_dir(buf.as_path())
|
|
|
|
} else {
|
|
|
|
OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.create_new(true)
|
|
|
|
.open(buf.as_path())
|
|
|
|
.map(|_| ())
|
|
|
|
};
|
2018-08-23 18:36:45 -04:00
|
|
|
match r {
|
|
|
|
Err(ref e) if e.kind() == ErrorKind::AlreadyExists => continue,
|
|
|
|
Ok(_) => return Ok(buf),
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 23:39:06 -05:00
|
|
|
pub fn mkdir(path: &Path, perm: u32, recursive: bool) -> std::io::Result<()> {
|
2018-07-26 17:54:22 -04:00
|
|
|
debug!("mkdir -p {}", path.display());
|
2018-09-14 15:30:43 -04:00
|
|
|
let mut builder = DirBuilder::new();
|
2019-01-17 23:39:06 -05:00
|
|
|
builder.recursive(recursive);
|
2018-09-14 15:30:43 -04:00
|
|
|
set_dir_permission(&mut builder, perm);
|
2019-01-17 23:39:06 -05:00
|
|
|
builder.create(path)
|
2018-07-26 17:54:22 -04:00
|
|
|
}
|
2018-08-23 03:58:59 -04:00
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
#[cfg(unix)]
|
2018-09-14 15:30:43 -04:00
|
|
|
fn set_dir_permission(builder: &mut DirBuilder, perm: u32) {
|
|
|
|
debug!("set dir perm to {}", perm);
|
2018-09-17 19:53:55 -04:00
|
|
|
builder.mode(perm & 0o777);
|
2018-09-14 15:30:43 -04:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
#[cfg(not(unix))]
|
2018-09-14 15:30:43 -04:00
|
|
|
fn set_dir_permission(_builder: &mut DirBuilder, _perm: u32) {
|
|
|
|
// NOOP on windows
|
|
|
|
}
|
|
|
|
|
2019-05-07 21:58:58 -04:00
|
|
|
#[cfg(unix)]
|
2019-07-10 18:53:48 -04:00
|
|
|
pub fn chown(path: &str, uid: u32, gid: u32) -> Result<(), ErrBox> {
|
2019-05-07 21:58:58 -04:00
|
|
|
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))
|
2019-07-10 18:53:48 -04:00
|
|
|
.map_err(ErrBox::from)
|
2019-05-07 21:58:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
2019-07-10 18:53:48 -04:00
|
|
|
pub fn chown(_path: &str, _uid: u32, _gid: u32) -> Result<(), ErrBox> {
|
2020-03-02 10:19:42 -05:00
|
|
|
// FAIL on Windows
|
2019-05-07 21:58:58 -04:00
|
|
|
// TODO: implement chown for Windows
|
2020-02-23 14:51:29 -05:00
|
|
|
let e = std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
2020-03-02 10:19:42 -05:00
|
|
|
"Not implemented".to_string(),
|
2020-02-23 14:51:29 -05:00
|
|
|
);
|
|
|
|
Err(ErrBox::from(e))
|
2019-05-07 21:58:58 -04:00
|
|
|
}
|
2019-07-17 18:15:30 -04:00
|
|
|
|
2020-02-26 16:11:52 -05:00
|
|
|
/// Normalize all itermediate components of the path (ie. remove "./" and "../" components).
|
|
|
|
/// Similar to `fs::canonicalize()` but doesn't resolve symlinks.
|
|
|
|
///
|
|
|
|
/// Taken from Cargo
|
|
|
|
/// https://github.com/rust-lang/cargo/blob/af307a38c20a753ec60f0ad18be5abed3db3c9ac/src/cargo/util/paths.rs#L60-L85
|
|
|
|
pub fn normalize_path(path: &Path) -> PathBuf {
|
|
|
|
let mut components = path.components().peekable();
|
|
|
|
let mut ret =
|
|
|
|
if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
|
|
|
|
components.next();
|
|
|
|
PathBuf::from(c.as_os_str())
|
|
|
|
} else {
|
|
|
|
PathBuf::new()
|
|
|
|
};
|
|
|
|
|
|
|
|
for component in components {
|
|
|
|
match component {
|
|
|
|
Component::Prefix(..) => unreachable!(),
|
|
|
|
Component::RootDir => {
|
|
|
|
ret.push(component.as_os_str());
|
|
|
|
}
|
|
|
|
Component::CurDir => {}
|
|
|
|
Component::ParentDir => {
|
|
|
|
ret.pop();
|
|
|
|
}
|
|
|
|
Component::Normal(c) => {
|
|
|
|
ret.push(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
2020-01-20 09:45:44 -05:00
|
|
|
pub fn resolve_from_cwd(path: &Path) -> Result<PathBuf, ErrBox> {
|
|
|
|
let resolved_path = if path.is_absolute() {
|
|
|
|
path.to_owned()
|
2019-07-17 18:15:30 -04:00
|
|
|
} else {
|
|
|
|
let cwd = std::env::current_dir().unwrap();
|
|
|
|
cwd.join(path)
|
|
|
|
};
|
|
|
|
|
2020-02-26 16:11:52 -05:00
|
|
|
Ok(normalize_path(&resolved_path))
|
2019-07-17 18:15:30 -04:00
|
|
|
}
|
2019-09-07 14:13:09 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_child() {
|
|
|
|
let cwd = std::env::current_dir().unwrap();
|
2020-01-20 09:45:44 -05:00
|
|
|
assert_eq!(resolve_from_cwd(Path::new("a")).unwrap(), cwd.join("a"));
|
2019-09-07 14:13:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_dot() {
|
|
|
|
let cwd = std::env::current_dir().unwrap();
|
2020-01-20 09:45:44 -05:00
|
|
|
assert_eq!(resolve_from_cwd(Path::new(".")).unwrap(), cwd);
|
2019-09-07 14:13:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_parent() {
|
|
|
|
let cwd = std::env::current_dir().unwrap();
|
2020-01-20 09:45:44 -05:00
|
|
|
assert_eq!(resolve_from_cwd(Path::new("a/..")).unwrap(), cwd);
|
2019-09-07 14:13:09 -04:00
|
|
|
}
|
|
|
|
|
2020-02-26 16:11:52 -05:00
|
|
|
#[test]
|
|
|
|
fn test_normalize_path() {
|
|
|
|
assert_eq!(normalize_path(Path::new("a/../b")), PathBuf::from("b"));
|
|
|
|
assert_eq!(normalize_path(Path::new("a/./b/")), PathBuf::from("a/b/"));
|
|
|
|
assert_eq!(
|
|
|
|
normalize_path(Path::new("a/./b/../c")),
|
|
|
|
PathBuf::from("a/c")
|
|
|
|
);
|
|
|
|
|
|
|
|
if cfg!(windows) {
|
|
|
|
assert_eq!(
|
|
|
|
normalize_path(Path::new("C:\\a\\.\\b\\..\\c")),
|
|
|
|
PathBuf::from("C:\\a\\c")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:16:30 -04:00
|
|
|
// TODO: Get a good expected value here for Windows.
|
|
|
|
#[cfg(not(windows))]
|
2019-09-07 14:13:09 -04:00
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_absolute() {
|
2019-09-10 23:16:30 -04:00
|
|
|
let expected = Path::new("/a");
|
2020-01-20 09:45:44 -05:00
|
|
|
assert_eq!(resolve_from_cwd(expected).unwrap(), expected);
|
2019-09-07 14:13:09 -04:00
|
|
|
}
|
|
|
|
}
|
2020-02-17 13:11:45 -05:00
|
|
|
|
|
|
|
pub fn files_in_subtree<F>(root: PathBuf, filter: F) -> Vec<PathBuf>
|
|
|
|
where
|
|
|
|
F: Fn(&Path) -> bool,
|
|
|
|
{
|
|
|
|
assert!(root.is_dir());
|
|
|
|
|
|
|
|
WalkDir::new(root)
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|e| e.ok())
|
|
|
|
.map(|e| e.path().to_owned())
|
|
|
|
.filter(|p| if p.is_dir() { false } else { filter(&p) })
|
|
|
|
.collect()
|
|
|
|
}
|