summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Barrett <spalf0@gmail.com>2019-08-19 09:10:34 -0500
committerTom Barrett <spalf0@gmail.com>2019-08-19 09:10:34 -0500
commitf29c854251ee0f47a94fb88fd94b9c24809c53a2 (patch)
treee110b539b0c5c37db37f30ecb8031aa7fbbd4e87 /src
parentc79294e0bdd3f045e2de4215f71db262c6e94848 (diff)
fixed restore bug and wrote test for it, added ideas to readme, created all_paths functionHEADmaster
Diffstat (limited to 'src')
-rw-r--r--src/main.rs36
1 files changed, 20 insertions, 16 deletions
diff --git a/src/main.rs b/src/main.rs
index bb1701a..28c3acf 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -59,7 +59,14 @@ pub fn build_path(filename: &str) -> Result<PathBuf, io::Error> {
.iter()
.collect();
- fs::canonicalize(&relative)
+ if relative.exists() {
+ fs::canonicalize(&relative)
+ } else {
+ fs::File::create(relative.clone())?;
+ let full_path = fs::canonicalize(&relative.clone())?;
+ fs::remove_file(relative)?;
+ Ok(full_path)
+ }
}
pub fn rsycle(rsyclebin: PathBuf, old_path: PathBuf) -> Result<(), io::Error> {
@@ -148,13 +155,7 @@ pub fn most_recent_current_path(rsyclebin: PathBuf, path: PathBuf) -> Result<Pat
}
pub fn list(rsyclebin: PathBuf) -> Result<(), io::Error> {
- let mut paths: Vec<PathBuf> = fs::read_dir(rsyclebin.clone())
- .unwrap()
- .map(Result::unwrap)
- .map(|dir| dir.path())
- .collect();
-
- for current_path in paths.iter_mut() {
+ for current_path in all_paths(rsyclebin.clone())? {
let mut filename: Vec<&str> = current_path
.file_name()
.unwrap()
@@ -186,7 +187,9 @@ fn find_original_path(rsyclebin: PathBuf, current_path: PathBuf) -> Result<PathB
let mut reader = ReaderBuilder::new().has_headers(false).from_reader(file);
- let path_str = current_path.to_str().unwrap();
+ let path_str = current_path
+ .to_str()
+ .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "Cannot convert."))?;
Ok(PathBuf::from(
reader
@@ -199,13 +202,7 @@ fn find_original_path(rsyclebin: PathBuf, current_path: PathBuf) -> Result<PathB
}
pub fn empty(rsyclebin: PathBuf) -> Result<(), io::Error> {
- let paths: Vec<PathBuf> = fs::read_dir(rsyclebin.clone())
- .unwrap()
- .map(Result::unwrap)
- .map(|dir| dir.path())
- .collect();
-
- for path in paths {
+ for path in all_paths(rsyclebin)? {
if fs::remove_file(path.clone()).is_err() {
fs::remove_dir_all(path)?
}
@@ -214,6 +211,13 @@ pub fn empty(rsyclebin: PathBuf) -> Result<(), io::Error> {
Ok(())
}
+fn all_paths(rsyclebin: PathBuf) -> Result<Vec<PathBuf>, io::Error> {
+ Ok(fs::read_dir(rsyclebin.clone())?
+ .map(Result::unwrap)
+ .map(|dir| dir.path())
+ .collect())
+}
+
fn restore_cli(_rsyclebin: PathBuf) {
println!("unimplemented!")
}