diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2020-12-30 11:16:57 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2020-12-30 11:16:57 +0000 |
commit | 21f1db03313a211665f64b80a3c4127d018a591c (patch) | |
tree | 6a30d9e9e85f70e435853f9533f79e0586692600 /subplotlib | |
parent | 6af541ff22ac8a750ec7ae0ec9243b38cc72f98b (diff) | |
download | subplot-21f1db03313a211665f64b80a3c4127d018a591c.tar.gz |
subplotlib: Move canonicalise_filename into Datadir impl
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'subplotlib')
-rw-r--r-- | subplotlib/src/steplibrary/datadir.rs | 21 | ||||
-rw-r--r-- | subplotlib/src/steplibrary/files.rs | 49 |
2 files changed, 35 insertions, 35 deletions
diff --git a/subplotlib/src/steplibrary/datadir.rs b/subplotlib/src/steplibrary/datadir.rs index d727bcc..69f79cf 100644 --- a/subplotlib/src/steplibrary/datadir.rs +++ b/subplotlib/src/steplibrary/datadir.rs @@ -5,7 +5,7 @@ //! If you want to create files, run commands, etc. from your scenarios, you //! will likely be using them from within the datadir. -use std::path::Path; +use std::path::{Component, Path, PathBuf}; pub use crate::prelude::*; @@ -64,6 +64,25 @@ impl Datadir { pub fn base_path(&self) -> &Path { self.inner().base.path() } + + /// Canonicalise a subpath into this dir + #[throws(StepError)] + pub fn canonicalise_filename<S: AsRef<Path>>(&self, subpath: S) -> PathBuf { + let mut ret = self.base_path().to_path_buf(); + for component in subpath.as_ref().components() { + match component { + Component::CurDir => {} + Component::ParentDir => { + throw!("embedded filenames may not contain .."); + } + Component::RootDir | Component::Prefix(_) => { + throw!("embedded filenames must be relative"); + } + c => ret.push(c), + } + } + ret + } } /// A simple check for enough disk space in the data dir diff --git a/subplotlib/src/steplibrary/files.rs b/subplotlib/src/steplibrary/files.rs index 2a451a5..180fbee 100644 --- a/subplotlib/src/steplibrary/files.rs +++ b/subplotlib/src/steplibrary/files.rs @@ -7,7 +7,6 @@ use std::collections::{HashMap, HashSet}; use std::ffi::OsString; use std::fs::{self, Metadata, OpenOptions}; use std::io::{self, Write}; -use std::path::{Component, Path, PathBuf}; use std::time::{Duration, SystemTime}; use chrono::{TimeZone, Utc}; @@ -29,24 +28,6 @@ impl ContextElement for Files { } } -#[throws(StepError)] -fn canonicalise_filename<S: AsRef<Path>>(base: &Path, subpath: S) -> PathBuf { - let mut ret = base.to_path_buf(); - for component in subpath.as_ref().components() { - match component { - Component::CurDir => {} - Component::ParentDir => { - throw!("embedded filenames may not contain .."); - } - Component::RootDir | Component::Prefix(_) => { - throw!("embedded filenames must be relative"); - } - c => ret.push(c), - } - } - ret -} - #[step] #[context(Datadir)] pub fn create_from_embedded(context: &ScenarioContext, embedded_file: SubplotDataFile) { @@ -60,7 +41,7 @@ pub fn create_from_embedded_with_other_name( filename_on_disk: &str, embedded_file: SubplotDataFile, ) { - let full_path = canonicalise_filename(context.base_path(), filename_on_disk)?; + let full_path = context.canonicalise_filename(filename_on_disk)?; let mut f = OpenOptions::new() .create(true) .write(true) @@ -74,7 +55,7 @@ pub fn touch_with_timestamp(context: &Datadir, filename: &str, mtime: &str) { let ts = Utc.datetime_from_str(mtime, "%Y-%m-%d %H:%M:%S")?; let (secs, nanos) = (ts.timestamp(), ts.timestamp_subsec_nanos()); let mtime = FileTime::from_unix_time(secs, nanos); - let full_path = canonicalise_filename(context.base_path(), filename)?; + let full_path = context.canonicalise_filename(filename)?; // If the file doesn't exist, create it drop( OpenOptions::new() @@ -88,7 +69,7 @@ pub fn touch_with_timestamp(context: &Datadir, filename: &str, mtime: &str) { #[step] pub fn create_from_text(context: &Datadir, text: &str, filename: &str) { - let full_path = canonicalise_filename(context.base_path(), filename)?; + let full_path = context.canonicalise_filename(filename)?; let mut f = OpenOptions::new() .create(true) .write(true) @@ -102,7 +83,7 @@ pub fn create_from_text(context: &Datadir, text: &str, filename: &str) { #[context(Files)] pub fn remember_metadata(context: &ScenarioContext, filename: &str) { let full_path = context.with( - |context: &Datadir| canonicalise_filename(context.base_path(), filename), + |context: &Datadir| context.canonicalise_filename(filename), false, )?; let metadata = fs::metadata(&full_path)?; @@ -117,14 +98,14 @@ pub fn remember_metadata(context: &ScenarioContext, filename: &str) { #[step] pub fn touch(context: &Datadir, filename: &str) { - let full_path = canonicalise_filename(context.base_path(), filename)?; + let full_path = context.canonicalise_filename(filename)?; let now = FileTime::now(); filetime::set_file_mtime(full_path, now)?; } #[step] pub fn file_exists(context: &Datadir, filename: &str) { - let full_path = canonicalise_filename(context.base_path(), filename)?; + let full_path = context.canonicalise_filename(filename)?; match fs::metadata(full_path) { Ok(_) => (), Err(e) => { @@ -139,7 +120,7 @@ pub fn file_exists(context: &Datadir, filename: &str) { #[step] pub fn file_does_not_exist(context: &Datadir, filename: &str) { - let full_path = canonicalise_filename(context.base_path(), filename)?; + let full_path = context.canonicalise_filename(filename)?; match fs::metadata(full_path) { Ok(_) => { throw!(format!("file '{}' was unexpectedly found", filename)) @@ -167,7 +148,7 @@ pub fn only_these_exist(context: &Datadir, filenames: &str) { #[step] pub fn file_contains(context: &Datadir, filename: &str, data: &str) { - let full_path = canonicalise_filename(context.base_path(), filename)?; + let full_path = context.canonicalise_filename(filename)?; let body = fs::read_to_string(full_path)?; if !body.contains(data) { throw!("expected file content not found"); @@ -176,7 +157,7 @@ pub fn file_contains(context: &Datadir, filename: &str, data: &str) { #[step] pub fn file_matches_regex(context: &Datadir, filename: &str, regex: &str) { - let full_path = canonicalise_filename(context.base_path(), filename)?; + let full_path = context.canonicalise_filename(filename)?; let regex = Regex::new(regex)?; let body = fs::read_to_string(full_path)?; if !regex.is_match(&body) { @@ -186,8 +167,8 @@ pub fn file_matches_regex(context: &Datadir, filename: &str, regex: &str) { #[step] pub fn file_match(context: &Datadir, filename1: &str, filename2: &str) { - let full_path1 = canonicalise_filename(context.base_path(), filename1)?; - let full_path2 = canonicalise_filename(context.base_path(), filename2)?; + let full_path1 = context.canonicalise_filename(filename1)?; + let full_path2 = context.canonicalise_filename(filename2)?; let body1 = fs::read(full_path1)?; let body2 = fs::read(full_path2)?; if body1 != body2 { @@ -200,7 +181,7 @@ pub fn file_match(context: &Datadir, filename1: &str, filename2: &str) { #[context(Files)] pub fn has_remembered_metadata(context: &ScenarioContext, filename: &str) { let full_path = context.with( - |context: &Datadir| canonicalise_filename(context.base_path(), filename), + |context: &Datadir| context.canonicalise_filename(filename), false, )?; let metadata = fs::metadata(&full_path)?; @@ -225,7 +206,7 @@ pub fn has_remembered_metadata(context: &ScenarioContext, filename: &str) { #[context(Files)] pub fn has_different_metadata(context: &ScenarioContext, filename: &str) { let full_path = context.with( - |context: &Datadir| canonicalise_filename(context.base_path(), filename), + |context: &Datadir| context.canonicalise_filename(filename), false, )?; let metadata = fs::metadata(&full_path)?; @@ -247,7 +228,7 @@ pub fn has_different_metadata(context: &ScenarioContext, filename: &str) { #[step] pub fn mtime_is_recent(context: &Datadir, filename: &str) { - let full_path = canonicalise_filename(context.base_path(), filename)?; + let full_path = context.canonicalise_filename(filename)?; let metadata = fs::metadata(full_path)?; let mtime = metadata.modified()?; let diff = SystemTime::now().duration_since(mtime)?; @@ -258,7 +239,7 @@ pub fn mtime_is_recent(context: &Datadir, filename: &str) { #[step] pub fn mtime_is_ancient(context: &Datadir, filename: &str) { - let full_path = canonicalise_filename(context.base_path(), filename)?; + let full_path = context.canonicalise_filename(filename)?; let metadata = fs::metadata(full_path)?; let mtime = metadata.modified()?; let diff = SystemTime::now().duration_since(mtime)?; |