浏览代码

Create required history dirs, remove history file if emtpy, update roadmap

timvisee 7 年之前
父节点
当前提交
8f60c9e426
共有 2 个文件被更改,包括 34 次插入9 次删除
  1. 8 1
      ROADMAP.md
  2. 26 8
      cli/src/history.rs

+ 8 - 1
ROADMAP.md

@@ -1,5 +1,12 @@
 # Release 0.1
 # Release 0.1
-- Remember all uploaded files, make files listable
+- Define minimum arguments for options such as `--history`, do not allow empty
+- Add/remove files from history in other commands, such as:
+  - Delete, if file is deleted
+  - Download, if file expired
+  - Exists, if file doesn't exist
+  - Info, if file doesn't exist, and update TTL
+  - Parameters, if file doesn't exist
+  - Password, if file doesn't exist
 - Incognito mode, to not remember files `--incognito`
 - Incognito mode, to not remember files `--incognito`
 - Automatically get owner token, from file history when setting password
 - Automatically get owner token, from file history when setting password
 - Allow file/directory archiving on upload
 - Allow file/directory archiving on upload

+ 26 - 8
cli/src/history.rs

@@ -1,5 +1,6 @@
 extern crate toml;
 extern crate toml;
 
 
+use std::fs;
 use std::io::Error as IoError;
 use std::io::Error as IoError;
 use std::path::PathBuf;
 use std::path::PathBuf;
 
 
@@ -38,12 +39,12 @@ impl History {
         // Read the file to a string
         // Read the file to a string
         use std::fs::File;
         use std::fs::File;
         use std::io::Read;
         use std::io::Read;
-        let mut file = File::open(path.clone())?;
+        let mut file = File::open(&path)?;
         let mut data = String::new();
         let mut data = String::new();
         file.read_to_string(&mut data)?;
         file.read_to_string(&mut data)?;
 
 
         // TODO: switch to this instead in stable Rust 1.26
         // TODO: switch to this instead in stable Rust 1.26
-        // let data = fs::read_to_string(path.clone())?;
+        // let data = fs::read_to_string(&path)?;
 
 
         // Parse the data, set the autosave path
         // Parse the data, set the autosave path
         let mut history: Self = toml::from_str(&data)?;
         let mut history: Self = toml::from_str(&data)?;
@@ -66,20 +67,32 @@ impl History {
 
 
     /// Save the history to the internal autosave file.
     /// Save the history to the internal autosave file.
     pub fn save(&mut self) -> Result<(), SaveError> {
     pub fn save(&mut self) -> Result<(), SaveError> {
-        // TODO: create the parent directories if needed
-
-        // Build the data
-        let data = toml::to_string(self)?;
-
         // Get the path
         // Get the path
         let path = self.autosave
         let path = self.autosave
             .as_ref()
             .as_ref()
             .ok_or(SaveError::NoPath)?;
             .ok_or(SaveError::NoPath)?;
 
 
+        // If we have no files, remove the history file if it exists
+        if self.files.is_empty() {
+            if path.is_file() {
+                fs::remove_file(&path)
+                    .map_err(|err| SaveError::Delete(err))?;
+            }
+            return Ok(());
+        }
+
+        // Ensure the file parnet directories are available
+        if let Some(parent) = path.parent() {
+            fs::create_dir_all(parent)?;
+        }
+
+        // Build the data
+        let data = toml::to_string(self)?;
+
         // Write to the file
         // Write to the file
         use std::fs::File;
         use std::fs::File;
         use std::io::Write;
         use std::io::Write;
-        File::create(path)?.write_all(data.as_ref())?;
+        File::create(&path)?.write_all(data.as_ref())?;
 
 
         // TODO: switch to this instead in stable Rust 1.26
         // TODO: switch to this instead in stable Rust 1.26
         // let data = fs::read_to_string(path.clone())?;
         // let data = fs::read_to_string(path.clone())?;
@@ -195,6 +208,11 @@ pub enum SaveError {
     /// Failed to write to the history file.
     /// Failed to write to the history file.
     #[fail(display = "Failed to write to the history file")]
     #[fail(display = "Failed to write to the history file")]
     Write(#[cause] IoError),
     Write(#[cause] IoError),
+
+    /// Failed to delete the history file, which was tried because there
+    /// are no history items to save.
+    #[fail(display = "Failed to delete history file, because history is empty")]
+    Delete(#[cause] IoError),
 }
 }
 
 
 impl From<SerError> for SaveError {
 impl From<SerError> for SaveError {