1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00

[fs] Enable mode for mkdir on unix (#746)

This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2018-09-14 12:30:43 -07:00 committed by Ryan Dahl
parent 66c09de967
commit 662e57b20a
5 changed files with 36 additions and 15 deletions

View file

@ -9,6 +9,15 @@ testPerm({ write: true }, function mkdirSyncSuccess() {
assert(pathInfo.isDirectory()); assert(pathInfo.isDirectory());
}); });
testPerm({ write: true }, function mkdirSyncMode() {
const path = deno.makeTempDirSync() + "/dir/subdir";
deno.mkdirSync(path, 0o755); // no perm for x
const pathInfo = deno.statSync(path);
if (pathInfo.mode !== null) { // Skip windows
assertEqual(pathInfo.mode & 0o777, 0o755);
}
});
testPerm({ write: false }, function mkdirSyncPerm() { testPerm({ write: false }, function mkdirSyncPerm() {
let err; let err;
try { try {

View file

@ -51,7 +51,8 @@ export class FileInfo {
this.modified = modified ? modified : null; this.modified = modified ? modified : null;
this.accessed = accessed ? accessed : null; this.accessed = accessed ? accessed : null;
this.created = created ? created : null; this.created = created ? created : null;
this.mode = mode >= 0 ? mode : null; // null if invalid mode (Windows) // null if invalid mode (Windows)
this.mode = mode >= 0 ? mode & 0o7777 : null;
} }
/** /**

View file

@ -55,8 +55,8 @@ impl DenoDir {
deps, deps,
reload, reload,
}; };
deno_fs::mkdir(deno_dir.gen.as_ref())?; deno_fs::mkdir(deno_dir.gen.as_ref(), 0o755)?;
deno_fs::mkdir(deno_dir.deps.as_ref())?; deno_fs::mkdir(deno_dir.deps.as_ref(), 0o755)?;
debug!("root {}", deno_dir.root.display()); debug!("root {}", deno_dir.root.display());
debug!("gen {}", deno_dir.gen.display()); debug!("gen {}", deno_dir.gen.display());

View file

@ -1,5 +1,5 @@
use std; use std;
use std::fs::{create_dir, File, OpenOptions}; use std::fs::{create_dir, DirBuilder, File, OpenOptions};
use std::io::ErrorKind; use std::io::ErrorKind;
use std::io::Write; use std::io::Write;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -7,6 +7,8 @@ use std::path::{Path, PathBuf};
use rand; use rand;
use rand::Rng; use rand::Rng;
#[cfg(any(unix))]
use std::os::unix::fs::DirBuilderExt;
#[cfg(any(unix))] #[cfg(any(unix))]
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
@ -64,18 +66,28 @@ pub fn make_temp_dir(
} }
} }
pub fn mkdir(path: &Path) -> std::io::Result<()> { pub fn mkdir(path: &Path, perm: u32) -> std::io::Result<()> {
debug!("mkdir -p {}", path.display()); debug!("mkdir -p {}", path.display());
assert!(path.has_root(), "non-has_root not yet implemented"); let mut builder = DirBuilder::new();
std::fs::create_dir_all(path).or_else(|err| { builder.recursive(true);
if err.kind() == std::io::ErrorKind::AlreadyExists { set_dir_permission(&mut builder, perm);
Ok(()) builder.create(path).or_else(|err| match err.kind() {
} else { std::io::ErrorKind::AlreadyExists => Ok(()),
Err(err) _ => Err(err),
}
}) })
} }
#[cfg(any(unix))]
fn set_dir_permission(builder: &mut DirBuilder, perm: u32) {
debug!("set dir perm to {}", perm);
builder.mode(perm);
}
#[cfg(not(any(unix)))]
fn set_dir_permission(_builder: &mut DirBuilder, _perm: u32) {
// NOOP on windows
}
pub fn normalize_path(path: &Path) -> String { pub fn normalize_path(path: &Path) -> String {
let s = String::from(path.to_str().unwrap()); let s = String::from(path.to_str().unwrap());
if cfg!(windows) { if cfg!(windows) {

View file

@ -451,7 +451,7 @@ fn handle_make_temp_dir(d: *const DenoC, base: &msg::Base) -> Box<Op> {
fn handle_mkdir(d: *const DenoC, base: &msg::Base) -> Box<Op> { fn handle_mkdir(d: *const DenoC, base: &msg::Base) -> Box<Op> {
let msg = base.msg_as_mkdir().unwrap(); let msg = base.msg_as_mkdir().unwrap();
// TODO let mode = msg.mode(); let mode = msg.mode();
let path = msg.path().unwrap(); let path = msg.path().unwrap();
let deno = from_c(d); let deno = from_c(d);
if !deno.flags.allow_write { if !deno.flags.allow_write {
@ -460,8 +460,7 @@ fn handle_mkdir(d: *const DenoC, base: &msg::Base) -> Box<Op> {
// TODO Use tokio_threadpool. // TODO Use tokio_threadpool.
Box::new(futures::future::result(|| -> OpResult { Box::new(futures::future::result(|| -> OpResult {
debug!("handle_mkdir {}", path); debug!("handle_mkdir {}", path);
// TODO(ry) Use mode. deno_fs::mkdir(Path::new(path), mode)?;
deno_fs::mkdir(Path::new(path))?;
Ok(None) Ok(None)
}())) }()))
} }