Skip to content

Commit

Permalink
fix: use resolve_path instead of canonicalize
Browse files Browse the repository at this point in the history
Allow on a path that will be existing in the future seems acceptible
  • Loading branch information
kevinkassimo committed May 9, 2019
1 parent 15fc00b commit 99bcd41
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
8 changes: 8 additions & 0 deletions cli/deno_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,14 @@ pub fn resolve_file_url(
Ok(j)
}

pub fn resolve_path(path: &str) -> Result<(PathBuf, String), DenoError> {
let url = resolve_file_url(path.to_string(), ".".to_string())
.map_err(DenoError::from)?;
let path = url.to_file_path().unwrap();
let path_string = path.to_str().unwrap().to_string();
Ok((path, path_string))
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
25 changes: 14 additions & 11 deletions cli/flags.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
use crate::deno_dir;

// Creates vector of strings, Vec<String>
macro_rules! svec {
Expand Down Expand Up @@ -283,24 +284,23 @@ This command has implicit access to all permissions (equivalent to deno run --al
)
}

/// Convert paths supplied into canonical (abs) paths.
/// Convert paths supplied into full path.
/// If a path is invalid, we print out a warning
/// and ignore this path in the output.
fn canonicalize_paths(paths: Vec<String>) -> Vec<String> {
fn resolve_paths(paths: Vec<String>) -> Vec<String> {
let mut out: Vec<String> = vec![];
for pathstr in paths.iter() {
let result = std::fs::canonicalize(pathstr);
let result = deno_dir::resolve_path(pathstr);
if result.is_err() {
eprintln!("Unrecognized path to whitelist: {}", pathstr);
continue;
}

if let Ok(full_path_string) = result.unwrap().into_os_string().into_string()
{
out.push(full_path_string);
} else {
eprintln!("Unrecognized path to whitelist: {}", pathstr);
let mut full_path = result.unwrap().1;
// Remove trailing slash.
if full_path.len() > 1 && full_path.ends_with('/') {
full_path.pop();
}
out.push(full_path);
}
out
}
Expand Down Expand Up @@ -343,7 +343,8 @@ pub fn parse_flags(matches: ArgMatches) -> DenoFlags {
let read_wl = run_matches.values_of("allow-read").unwrap();
let raw_read_whitelist: Vec<String> =
read_wl.map(std::string::ToString::to_string).collect();
flags.read_whitelist = canonicalize_paths(raw_read_whitelist);
flags.read_whitelist = resolve_paths(raw_read_whitelist);
debug!("read whitelist: {:#?}", &flags.read_whitelist);
} else {
flags.allow_read = true;
}
Expand All @@ -353,7 +354,8 @@ pub fn parse_flags(matches: ArgMatches) -> DenoFlags {
let write_wl = run_matches.values_of("allow-write").unwrap();
let raw_write_whitelist =
write_wl.map(std::string::ToString::to_string).collect();
flags.write_whitelist = canonicalize_paths(raw_write_whitelist);
flags.write_whitelist = resolve_paths(raw_write_whitelist);
debug!("write whitelist: {:#?}", &flags.write_whitelist);
} else {
flags.allow_write = true;
}
Expand All @@ -363,6 +365,7 @@ pub fn parse_flags(matches: ArgMatches) -> DenoFlags {
let net_wl = run_matches.values_of("allow-net").unwrap();
flags.net_whitelist =
net_wl.map(std::string::ToString::to_string).collect();
debug!("net whitelist: {:#?}", &flags.net_whitelist);
} else {
flags.allow_net = true;
}
Expand Down
10 changes: 1 addition & 9 deletions cli/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use atty;
use crate::ansi;
use crate::compiler::get_compiler_config;
use crate::deno_dir;
use crate::deno_dir::resolve_path;
use crate::dispatch_minimal::dispatch_minimal;
use crate::dispatch_minimal::parse_min_record;
use crate::errors;
Expand Down Expand Up @@ -241,14 +241,6 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option<OpCreator> {
}
}

fn resolve_path(path: &str) -> Result<(PathBuf, String), DenoError> {
let url = deno_dir::resolve_file_url(path.to_string(), ".".to_string())
.map_err(DenoError::from)?;
let path = url.to_file_path().unwrap();
let path_string = path.to_str().unwrap().to_string();
Ok((path, path_string))
}

// Returns a milliseconds and nanoseconds subsec
// since the start time of the deno runtime.
// If the High precision flag is not set, the
Expand Down

0 comments on commit 99bcd41

Please sign in to comment.