2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-06 02:34:02 +02:00
|
|
|
|
2020-09-14 18:48:57 +02:00
|
|
|
use deno_core::error::AnyError;
|
2020-08-13 02:04:17 +05:30
|
|
|
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-13 02:04:17 +05:30
|
|
|
use std::path::{Path, PathBuf};
|
2020-02-17 19:11:45 +01:00
|
|
|
use walkdir::WalkDir;
|
2018-07-26 17:54:22 -04:00
|
|
|
|
2018-12-11 21:36:34 +08:00
|
|
|
pub fn write_file<T: AsRef<[u8]>>(
|
2018-09-11 09:00:57 -07:00
|
|
|
filename: &Path,
|
2018-12-11 21:36:34 +08:00
|
|
|
data: T,
|
2020-03-07 22:29:12 -05:00
|
|
|
mode: u32,
|
2018-09-11 09:00:57 -07:00
|
|
|
) -> std::io::Result<()> {
|
2020-03-07 22:29:12 -05:00
|
|
|
write_file_2(filename, data, true, mode, true, false)
|
2019-02-02 11:26:18 -08: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 11:26:18 -08:00
|
|
|
is_create: bool,
|
|
|
|
is_append: bool,
|
|
|
|
) -> std::io::Result<()> {
|
2018-09-11 09:00:57 -07:00
|
|
|
let mut file = OpenOptions::new()
|
|
|
|
.read(false)
|
|
|
|
.write(true)
|
|
|
|
.append(is_append)
|
|
|
|
.truncate(!is_append)
|
2019-02-02 11:26:18 -08:00
|
|
|
.create(is_create)
|
2018-09-11 09:00:57 -07: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-24 00:36:45 +02:00
|
|
|
}
|
2020-03-20 09:46:26 -04:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
let _ = mode;
|
2018-08-24 00:36:45 +02:00
|
|
|
}
|
2018-09-14 12:30:43 -07:00
|
|
|
|
2020-03-20 09:46:26 -04:00
|
|
|
file.write_all(data.as_ref())
|
2019-05-07 18:58:58 -07:00
|
|
|
}
|
2019-07-18 00:15:30 +02:00
|
|
|
|
2020-09-14 18:48:57 +02:00
|
|
|
pub fn resolve_from_cwd(path: &Path) -> Result<PathBuf, AnyError> {
|
2020-01-20 14:45:44 +00:00
|
|
|
let resolved_path = if path.is_absolute() {
|
|
|
|
path.to_owned()
|
2019-07-18 00:15:30 +02:00
|
|
|
} else {
|
2020-03-20 09:46:26 -04:00
|
|
|
let cwd = current_dir().unwrap();
|
2019-07-18 00:15:30 +02:00
|
|
|
cwd.join(path)
|
|
|
|
};
|
|
|
|
|
2020-02-26 22:11:52 +01:00
|
|
|
Ok(normalize_path(&resolved_path))
|
2019-07-18 00:15:30 +02:00
|
|
|
}
|
2019-09-07 19:13:09 +01: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 14:45:44 +00:00
|
|
|
assert_eq!(resolve_from_cwd(Path::new("a")).unwrap(), cwd.join("a"));
|
2019-09-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_dot() {
|
2020-03-20 09:46:26 -04:00
|
|
|
let cwd = current_dir().unwrap();
|
2020-01-20 14:45:44 +00:00
|
|
|
assert_eq!(resolve_from_cwd(Path::new(".")).unwrap(), cwd);
|
2019-09-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_parent() {
|
2020-03-20 09:46:26 -04:00
|
|
|
let cwd = current_dir().unwrap();
|
2020-01-20 14:45:44 +00:00
|
|
|
assert_eq!(resolve_from_cwd(Path::new("a/..")).unwrap(), cwd);
|
2019-09-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
2020-02-26 22:11:52 +01: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-11 04:16:30 +01:00
|
|
|
// TODO: Get a good expected value here for Windows.
|
|
|
|
#[cfg(not(windows))]
|
2019-09-07 19:13:09 +01:00
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_absolute() {
|
2019-09-11 04:16:30 +01:00
|
|
|
let expected = Path::new("/a");
|
2020-01-20 14:45:44 +00:00
|
|
|
assert_eq!(resolve_from_cwd(expected).unwrap(), expected);
|
2019-09-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-17 19:11:45 +01: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 10:57:45 +02:00
|
|
|
.filter(|p| !p.is_dir() && filter(&p))
|
2020-02-17 19:11:45 +01:00
|
|
|
.collect()
|
|
|
|
}
|