1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-18 03:44:05 -05:00

Replaced read_file_sync{_string} with std::fs::read{_to_string}

This commit is contained in:
Aaron Power 2018-08-26 10:51:25 +01:00 committed by Ryan Dahl
parent 84c38f34ee
commit 3a5cf9ca8b
3 changed files with 19 additions and 36 deletions

View file

@ -1,12 +1,11 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
use errors::DenoError;
use errors::DenoResult;
use fs;
use fs as deno_fs;
use net;
use sha1;
use std;
use std::fs::File;
use std::io::Write;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::result::Result;
@ -55,8 +54,8 @@ impl DenoDir {
deps,
reload,
};
fs::mkdir(deno_dir.gen.as_ref())?;
fs::mkdir(deno_dir.deps.as_ref())?;
deno_fs::mkdir(deno_dir.gen.as_ref())?;
deno_fs::mkdir(deno_dir.deps.as_ref())?;
debug!("root {}", deno_dir.root.display());
debug!("gen {}", deno_dir.gen.display());
@ -82,7 +81,7 @@ impl DenoDir {
) -> std::io::Result<String> {
let path = self.cache_path(filename, source_code);
debug!("load_cache {}", path.display());
fs::read_file_sync_string(&path)
fs::read_to_string(&path)
}
pub fn code_cache(
@ -99,9 +98,7 @@ impl DenoDir {
if cache_path.exists() {
Ok(())
} else {
let mut file = File::create(cache_path)?;
file.write_all(output_code.as_bytes())?;
Ok(())
fs::write(cache_path, output_code.as_bytes())
}
}
@ -117,13 +114,13 @@ impl DenoDir {
println!("Downloading {}", module_name);
let source = net::fetch_sync_string(module_name)?;
match p.parent() {
Some(ref parent) => std::fs::create_dir_all(parent),
Some(ref parent) => fs::create_dir_all(parent),
None => Ok(()),
}?;
fs::write_file_sync(&p, source.as_bytes())?;
deno_fs::write_file_sync(&p, source.as_bytes())?;
source
} else {
let source = fs::read_file_sync_string(&p)?;
let source = fs::read_to_string(&p)?;
source
};
Ok(src)
@ -144,7 +141,7 @@ impl DenoDir {
module_name == filename,
"if a module isn't remote, it should have the same filename"
);
let src = fs::read_file_sync_string(Path::new(filename))?;
let src = fs::read_to_string(Path::new(filename))?;
Ok(src)
}
}
@ -250,13 +247,13 @@ impl DenoDir {
match j.scheme() {
"file" => {
let mut p = fs::normalize_path(j.to_file_path().unwrap().as_ref());
let mut p = deno_fs::normalize_path(j.to_file_path().unwrap().as_ref());
module_name = p.clone();
filename = p;
}
_ => {
module_name = module_specifier.to_string();
filename = fs::normalize_path(
filename = deno_fs::normalize_path(
get_cache_filename(self.deps.as_path(), j).as_ref(),
)
}
@ -329,7 +326,7 @@ fn test_code_cache() {
let r = deno_dir.code_cache(filename, source_code, output_code);
r.expect("code_cache error");
assert!(cache_path.exists());
assert_eq!(output_code, fs::read_file_sync_string(&cache_path).unwrap());
assert_eq!(output_code, fs::read_to_string(&cache_path).unwrap());
}
// https://github.com/denoland/deno/blob/golang/deno_dir.go#L25-L30
@ -408,7 +405,7 @@ fn test_src_file_to_url() {
fn test_resolve_module() {
let (_temp_dir, deno_dir) = test_setup();
let d = fs::normalize_path(
let d = deno_fs::normalize_path(
deno_dir
.deps
.join("localhost/testdata/subdir/print_hello.ts")

View file

@ -1,23 +1,8 @@
use std;
use std::fs::File;
use std::io::Read;
use std::io::Write;
use std::path::Path;
pub fn read_file_sync(path: &Path) -> std::io::Result<Vec<u8>> {
File::open(path).and_then(|mut f| {
let mut buffer = Vec::new();
f.read_to_end(&mut buffer)?;
Ok(buffer)
})
}
pub fn read_file_sync_string(path: &Path) -> std::io::Result<String> {
let vec = read_file_sync(path)?;
String::from_utf8(vec)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))
}
pub fn write_file_sync(path: &Path, content: &[u8]) -> std::io::Result<()> {
let mut f = File::create(path)?;
f.write_all(content)

View file

@ -5,7 +5,7 @@ use errors::DenoResult;
use flatbuffers;
use flatbuffers::FlatBufferBuilder;
use from_c;
use fs;
use fs as deno_fs;
use futures;
use futures::sync::oneshot;
use hyper;
@ -13,6 +13,7 @@ use hyper::rt::{Future, Stream};
use hyper::Client;
use msg_generated::deno as msg;
use std;
use std::fs;
use std::path::Path;
use std::time::{Duration, Instant};
use tokio::prelude::future;
@ -128,7 +129,7 @@ fn handle_start(
let cwd_path = std::env::current_dir().unwrap();
let cwd_off =
builder.create_string(fs::normalize_path(cwd_path.as_ref()).as_ref());
builder.create_string(deno_fs::normalize_path(cwd_path.as_ref()).as_ref());
let msg = msg::StartRes::create(
builder,
@ -401,7 +402,7 @@ fn handle_read_file_sync(
filename: &str,
) -> HandlerResult {
debug!("handle_read_file_sync {}", filename);
let vec = fs::read_file_sync(Path::new(filename))?;
let vec = fs::read(Path::new(filename))?;
// Build the response message. memcpy data into msg.
// TODO(ry) zero-copy.
let data_off = builder.create_byte_vector(vec.as_slice());
@ -433,7 +434,7 @@ fn handle_write_file_sync(
let deno = from_c(d);
if deno.flags.allow_write {
// TODO(ry) Use perm.
fs::write_file_sync(Path::new(filename), data)?;
deno_fs::write_file_sync(Path::new(filename), data)?;
Ok(null_buf())
} else {
let err = std::io::Error::new(