diff --git a/src/deno_dir.rs b/src/deno_dir.rs index 09f37933bc..1a8c929acc 100644 --- a/src/deno_dir.rs +++ b/src/deno_dir.rs @@ -70,7 +70,7 @@ impl DenoDir { ) -> std::io::Result { let path = self.cache_path(filename, source_code); debug!("load_cache {}", path.display()); - fs::read_file_sync(&path) + fs::read_file_sync_string(&path) } pub fn code_cache( @@ -251,7 +251,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(&cache_path).unwrap()); + assert_eq!(output_code, fs::read_file_sync_string(&cache_path).unwrap()); } // https://github.com/denoland/deno/blob/golang/deno_dir.go#L25-L30 @@ -413,6 +413,6 @@ fn get_source_code( module_name == filename, "if a module isn't remote, it should have the same filename" ); - fs::read_file_sync(Path::new(filename)) + fs::read_file_sync_string(Path::new(filename)) } } diff --git a/src/fs.rs b/src/fs.rs index 4ec9611aea..0aaf2a93d2 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -3,15 +3,20 @@ use std::fs::File; use std::io::Read; use std::path::Path; -#[allow(dead_code)] -pub fn read_file_sync(path: &Path) -> std::io::Result { +pub fn read_file_sync(path: &Path) -> std::io::Result> { File::open(path).and_then(|mut f| { - let mut contents = String::new(); - f.read_to_string(&mut contents)?; - Ok(contents) + let mut buffer = Vec::new(); + f.read_to_end(&mut buffer)?; + Ok(buffer) }) } +pub fn read_file_sync_string(path: &Path) -> std::io::Result { + let vec = read_file_sync(path)?; + String::from_utf8(vec) + .map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err)) +} + pub fn mkdir(path: &Path) -> std::io::Result<()> { debug!("mkdir -p {}", path.display()); assert!(path.has_root(), "non-has_root not yet implemented");