2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-08-12 16:34:17 -04:00
|
|
|
pub use deno_core::normalize_path;
|
2020-03-20 09:46:26 -04:00
|
|
|
use std::env::current_dir;
|
|
|
|
use std::fs::OpenOptions;
|
2018-08-22 13:19:32 -04:00
|
|
|
use std::io::Write;
|
2020-08-12 16:34:17 -04:00
|
|
|
use std::path::{Path, PathBuf};
|
2020-02-17 13:11:45 -05:00
|
|
|
use walkdir::WalkDir;
|
2018-07-26 17:54:22 -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,
|
2020-03-07 22:29:12 -05:00
|
|
|
mode: u32,
|
2018-09-11 12:00:57 -04:00
|
|
|
) -> std::io::Result<()> {
|
2020-03-07 22:29:12 -05:00
|
|
|
write_file_2(filename, data, true, mode, true, false)
|
2019-02-02 14:26:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_file_2<T: AsRef<[u8]>>(
|
|
|
|
filename: &Path,
|
|
|
|
data: T,
|
2020-03-07 22:29:12 -05:00
|
|
|
update_mode: bool,
|
|
|
|
mode: u32,
|
2019-02-02 14:26:18 -05:00
|
|
|
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)?;
|
|
|
|
|
2020-03-07 22:29:12 -05:00
|
|
|
if update_mode {
|
2020-03-20 09:46:26 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
let mode = mode & 0o777;
|
|
|
|
let permissions = PermissionsExt::from_mode(mode);
|
|
|
|
file.set_permissions(permissions)?;
|
2018-08-23 18:36:45 -04:00
|
|
|
}
|
2020-03-20 09:46:26 -04:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
let _ = mode;
|
2018-08-23 18:36:45 -04:00
|
|
|
}
|
2018-09-14 15:30:43 -04:00
|
|
|
|
2020-03-20 09:46:26 -04:00
|
|
|
file.write_all(data.as_ref())
|
2019-05-07 21:58:58 -04:00
|
|
|
}
|
2019-07-17 18:15:30 -04:00
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
pub fn resolve_from_cwd(path: &Path) -> Result<PathBuf, AnyError> {
|
2020-01-20 09:45:44 -05:00
|
|
|
let resolved_path = if path.is_absolute() {
|
|
|
|
path.to_owned()
|
2019-07-17 18:15:30 -04:00
|
|
|
} else {
|
2020-03-20 09:46:26 -04:00
|
|
|
let cwd = current_dir().unwrap();
|
2019-07-17 18:15:30 -04:00
|
|
|
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() {
|
2020-03-20 09:46:26 -04:00
|
|
|
let cwd = 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() {
|
2020-03-20 09:46:26 -04:00
|
|
|
let cwd = 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() {
|
2020-03-20 09:46:26 -04:00
|
|
|
let cwd = 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())
|
2020-10-08 04:57:45 -04:00
|
|
|
.filter(|p| !p.is_dir() && filter(&p))
|
2020-02-17 13:11:45 -05:00
|
|
|
.collect()
|
|
|
|
}
|