summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com>2018-09-11 09:00:57 -0700
committerRyan Dahl <ry@tinyclouds.org>2018-09-11 12:00:57 -0400
commit05f87a0cf23a370c0009db8343b3770b518799c8 (patch)
treeb2279e3257382ea1c1ec8c259e9fecdac9e78820 /src
parentc2663e1d82521e9b68a7e2e96197030a4ee00c30 (diff)
Move writeFileSync to write_file.ts, add writeFile and tests (#728)
Diffstat (limited to 'src')
-rw-r--r--src/deno_dir.rs2
-rw-r--r--src/fs.rs35
-rw-r--r--src/handlers.rs13
-rw-r--r--src/msg.fbs16
4 files changed, 46 insertions, 20 deletions
diff --git a/src/deno_dir.rs b/src/deno_dir.rs
index a56062a34..a8379b900 100644
--- a/src/deno_dir.rs
+++ b/src/deno_dir.rs
@@ -118,7 +118,7 @@ impl DenoDir {
Some(ref parent) => fs::create_dir_all(parent),
None => Ok(()),
}?;
- deno_fs::write_file_sync(&p, source.as_bytes())?;
+ deno_fs::write_file(&p, source.as_bytes(), 0o666)?;
source
} else {
let source = fs::read_to_string(&p)?;
diff --git a/src/fs.rs b/src/fs.rs
index 9290d94e5..e04675f66 100644
--- a/src/fs.rs
+++ b/src/fs.rs
@@ -1,5 +1,5 @@
use std;
-use std::fs::{create_dir, File};
+use std::fs::{create_dir, File, OpenOptions};
use std::io::ErrorKind;
use std::io::Write;
use std::path::{Path, PathBuf};
@@ -7,9 +7,36 @@ use std::path::{Path, PathBuf};
use rand;
use rand::Rng;
-pub fn write_file_sync(path: &Path, content: &[u8]) -> std::io::Result<()> {
- let mut f = File::create(path)?;
- f.write_all(content)
+#[cfg(any(unix))]
+use std::os::unix::fs::PermissionsExt;
+
+pub fn write_file(
+ filename: &Path,
+ data: &[u8],
+ perm: u32,
+) -> std::io::Result<()> {
+ let is_append = perm & (1 << 31) != 0;
+ let mut file = OpenOptions::new()
+ .read(false)
+ .write(true)
+ .append(is_append)
+ .truncate(!is_append)
+ .create(true)
+ .open(filename)?;
+
+ set_permissions(&mut file, perm)?;
+ file.write_all(data)
+}
+
+#[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(())
}
pub fn make_temp_dir(
diff --git a/src/handlers.rs b/src/handlers.rs
index 7d8fafc10..1c821b73c 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -54,7 +54,7 @@ pub extern "C" fn msg_from_js(d: *const DenoC, buf: deno_buf) {
msg::Any::RenameSync => handle_rename_sync,
msg::Any::SetEnv => handle_set_env,
msg::Any::StatSync => handle_stat_sync,
- msg::Any::WriteFileSync => handle_write_file_sync,
+ msg::Any::WriteFile => handle_write_file,
msg::Any::Exit => handle_exit,
_ => panic!(format!(
"Unhandled message {}",
@@ -517,20 +517,19 @@ fn handle_stat_sync(_d: *const DenoC, base: &msg::Base) -> Box<Op> {
}()))
}
-fn handle_write_file_sync(d: *const DenoC, base: &msg::Base) -> Box<Op> {
- let msg = base.msg_as_write_file_sync().unwrap();
+fn handle_write_file(d: *const DenoC, base: &msg::Base) -> Box<Op> {
+ let msg = base.msg_as_write_file().unwrap();
let filename = String::from(msg.filename().unwrap());
let data = msg.data().unwrap();
- // TODO let perm = msg.perm();
+ let perm = msg.perm();
let deno = from_c(d);
- debug!("handle_write_file_sync {}", filename);
+ debug!("handle_write_file {}", filename);
Box::new(futures::future::result(|| -> OpResult {
if !deno.flags.allow_write {
Err(permission_denied())
} else {
- // TODO(ry) Use perm.
- deno_fs::write_file_sync(Path::new(&filename), data)?;
+ deno_fs::write_file(Path::new(&filename), data, perm)?;
Ok(None)
}
}()))
diff --git a/src/msg.fbs b/src/msg.fbs
index 33278a669..fcc863625 100644
--- a/src/msg.fbs
+++ b/src/msg.fbs
@@ -17,11 +17,11 @@ union Any {
Mkdir,
ReadFile,
ReadFileRes,
+ WriteFile,
RenameSync,
StatSync,
StatSyncRes,
SetEnv,
- WriteFileSync,
}
enum ErrorKind: byte {
@@ -181,6 +181,13 @@ table ReadFileRes {
data: [ubyte];
}
+table WriteFile {
+ filename: string;
+ data: [ubyte];
+ perm: uint;
+ // perm specified by https://godoc.org/os#FileMode
+}
+
table RenameSync {
oldpath: string;
newpath: string;
@@ -200,11 +207,4 @@ table StatSyncRes {
created:ulong;
}
-table WriteFileSync {
- filename: string;
- data: [ubyte];
- perm: uint;
- // perm specified by https://godoc.org/os#FileMode
-}
-
root_type Base;