2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 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;
|
2018-08-23 18:36:45 -04:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
use rand;
|
2018-08-29 14:06:41 -04:00
|
|
|
use rand::Rng;
|
2018-07-26 17:54:22 -04:00
|
|
|
|
2018-09-14 15:30:43 -04:00
|
|
|
#[cfg(any(unix))]
|
|
|
|
use std::os::unix::fs::DirBuilderExt;
|
2018-09-11 12:00:57 -04:00
|
|
|
#[cfg(any(unix))]
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(unix))]
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
#[cfg(not(any(unix)))]
|
|
|
|
fn set_permissions(_file: &mut File, _perm: u32) -> std::io::Result<()> {
|
|
|
|
// NOOP on windows
|
|
|
|
Ok(())
|
2018-08-22 13:19:32 -04:00
|
|
|
}
|
|
|
|
|
2018-08-23 18:36:45 -04:00
|
|
|
pub fn make_temp_dir(
|
|
|
|
dir: Option<&Path>,
|
|
|
|
prefix: Option<&str>,
|
|
|
|
suffix: Option<&str>,
|
|
|
|
) -> 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("_");
|
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.
|
|
|
|
let r = create_dir(buf.as_path());
|
|
|
|
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
|
|
|
|
2018-09-14 15:30:43 -04:00
|
|
|
#[cfg(any(unix))]
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(any(unix)))]
|
|
|
|
fn set_dir_permission(_builder: &mut DirBuilder, _perm: u32) {
|
|
|
|
// NOOP on windows
|
|
|
|
}
|
|
|
|
|
2018-08-23 03:58:59 -04:00
|
|
|
pub fn normalize_path(path: &Path) -> String {
|
|
|
|
let s = String::from(path.to_str().unwrap());
|
|
|
|
if cfg!(windows) {
|
|
|
|
// TODO This isn't correct. Probbly should iterate over components.
|
|
|
|
s.replace("\\", "/")
|
|
|
|
} else {
|
|
|
|
s
|
|
|
|
}
|
|
|
|
}
|