diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-02-07 10:20:59 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-02-07 10:22:07 +0000 |
commit | eb4c7ce8a34fbc963554da1613bcc081571411ad (patch) | |
tree | 1079602f1873ac6be9be490c78b9112b68f90516 /subplotlib | |
parent | 04fa5886f4aa1d9b6f458ea99a33aac8bdc741bd (diff) | |
download | subplot-eb4c7ce8a34fbc963554da1613bcc081571411ad.tar.gz |
subplotlib: Add directory steps to files library
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'subplotlib')
-rw-r--r-- | subplotlib/src/steplibrary/files.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/subplotlib/src/steplibrary/files.rs b/subplotlib/src/steplibrary/files.rs index f940f08..8ba27cf 100644 --- a/subplotlib/src/steplibrary/files.rs +++ b/subplotlib/src/steplibrary/files.rs @@ -7,6 +7,7 @@ use std::collections::{HashMap, HashSet}; use std::ffi::OsString; use std::fs::{self, Metadata, OpenOptions}; use std::io::{self, Write}; +use std::path::PathBuf; use std::time::{Duration, SystemTime}; use chrono::{TimeZone, Utc}; @@ -41,6 +42,14 @@ pub fn create_from_embedded_with_other_name( filename_on_disk: &str, embedded_file: SubplotDataFile, ) { + let filename_on_disk = PathBuf::from(filename_on_disk); + let parentpath = filename_on_disk.parent().ok_or_else(|| { + format!( + "No parent directory found for {}", + filename_on_disk.display() + ) + })?; + context.create_dir_all(parentpath)?; context .open_write(filename_on_disk)? .write_all(embedded_file.data())?; @@ -245,3 +254,55 @@ pub fn mtime_is_ancient(context: &Datadir, filename: &str) { throw!(format!("{} is younger than 39 years", filename)); } } + +#[step] +pub fn make_directory(context: &Datadir, path: &str) { + context.create_dir_all(path)?; +} + +#[step] +pub fn remove_directory(context: &Datadir, path: &str) { + let full_path = context.canonicalise_filename(path)?; + remove_dir_all::remove_dir_all(full_path)?; +} + +#[step] +pub fn path_exists(context: &Datadir, path: &str) { + let full_path = context.canonicalise_filename(path)?; + fs::metadata(full_path)?; +} + +#[step] +pub fn path_does_not_exist(context: &Datadir, path: &str) { + let full_path = context.canonicalise_filename(path)?; + match fs::metadata(&full_path) { + Ok(_) => throw!(format!("{} exists", full_path.display())), + Err(e) => { + if !matches!(e.kind(), io::ErrorKind::NotFound) { + throw!(e); + } + } + }; +} + +#[step] +pub fn path_is_empty(context: &Datadir, path: &str) { + let full_path = context.canonicalise_filename(path)?; + let mut iter = fs::read_dir(&full_path)?; + match iter.next() { + None => {} + Some(Ok(_)) => throw!(format!("{} is not empty", full_path.display())), + Some(Err(e)) => throw!(e), + } +} + +#[step] +pub fn path_is_not_empty(context: &Datadir, path: &str) { + let full_path = context.canonicalise_filename(path)?; + let mut iter = fs::read_dir(&full_path)?; + match iter.next() { + None => throw!(format!("{} is empty", full_path.display())), + Some(Ok(_)) => {} + Some(Err(e)) => throw!(e), + } +} |