Kaynağa Gözat

Add application version to history file for compatibility checking

timvisee 7 yıl önce
ebeveyn
işleme
1557438e28
4 değiştirilmiş dosya ile 48 ekleme ve 1 silme
  1. 7 0
      Cargo.lock
  2. 1 0
      cli/Cargo.toml
  3. 32 1
      cli/src/history.rs
  4. 8 0
      cli/src/util.rs

+ 7 - 0
Cargo.lock

@@ -363,6 +363,7 @@ dependencies = [
  "serde_derive 1.0.43 (registry+https://github.com/rust-lang/crates.io-index)",
  "time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
  "toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "version-compare 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -1458,6 +1459,11 @@ name = "vec_map"
 version = "0.8.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
+[[package]]
+name = "version-compare"
+version = "0.0.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "version_check"
 version = "0.1.3"
@@ -1698,6 +1704,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 "checksum uuid 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bcc7e3b898aa6f6c08e5295b6c89258d1331e9ac578cc992fb818759951bdc22"
 "checksum vcpkg 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7ed0f6789c8a85ca41bbc1c9d175422116a9869bd1cf31bb08e1493ecce60380"
 "checksum vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "887b5b631c2ad01628bbbaa7dd4c869f80d3186688f8d0b6f58774fbe324988c"
+"checksum version-compare 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "78068add8bf1e4d37d13fa5867182fe4c03f8e525c831053733f83aaba942d37"
 "checksum version_check 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6b772017e347561807c1aa192438c5fd74242a670a6cffacc40f2defd1dc069d"
 "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
 "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"

+ 1 - 0
cli/Cargo.toml

@@ -32,3 +32,4 @@ serde = "1.0"
 serde_derive = "1.0"
 time = "0.1"
 toml = "0.4"
+version-compare = "0.0.6"

+ 32 - 1
cli/src/history.rs

@@ -1,4 +1,5 @@
 extern crate toml;
+extern crate version_compare;
 
 use std::fs;
 use std::io::Error as IoError;
@@ -8,11 +9,25 @@ use failure::Fail;
 use ffsend_api::file::remote_file::RemoteFile;
 use self::toml::de::Error as DeError;
 use self::toml::ser::Error as SerError;
+use self::version_compare::{
+    CompOp,
+    VersionCompare,
+};
 
-use util::print_error;
+use util::{print_error, print_warning};
+
+/// The minimum supported history file version.
+const VERSION_MIN: &'static str = "0.0.1";
+
+/// The maximum supported history file version.
+const VERSION_MAX: &'static str = crate_version!();
 
 #[derive(Serialize, Deserialize)]
 pub struct History {
+    /// The application version the history file was built with.
+    /// Used for compatability checking.
+    version: Option<String>,
+
     /// The file history.
     files: Vec<RemoteFile>,
 
@@ -50,6 +65,21 @@ impl History {
         let mut history: Self = toml::from_str(&data)?;
         history.autosave = Some(path);
 
+        // Make sure the file version is supported
+        if history.version.is_none() { 
+            print_warning("History file has no version, ignoring");
+            history.version = Some(crate_version!().into());
+        } else {
+            // Get the version number from the file
+            let version = history.version.as_ref().unwrap();
+
+            if let Ok(true) = VersionCompare::compare_to(version, VERSION_MIN, &CompOp::Lt) {
+                print_warning("History file version is too old, ignoring");
+            } else if let Ok(true) = VersionCompare::compare_to(version, VERSION_MAX, &CompOp::Gt) {
+                print_warning("History file has an unknown version, ignoring");
+            }
+        }
+
         // Garbage collect
         history.gc();
 
@@ -214,6 +244,7 @@ impl Drop for History {
 impl Default for History {
     fn default() -> Self {
         Self {
+            version: Some(crate_version!().into()),
             files: Vec::new(),
             changed: false,
             autosave: None,

+ 8 - 0
cli/src/util.rs

@@ -58,6 +58,14 @@ pub fn print_error_msg<S>(err: S)
     print_error(err_msg(err).compat());
 }
 
+/// Print a warning.
+pub fn print_warning<S>(err: S)
+    where
+        S: AsRef<str> + Display + Debug + Sync + Send + 'static
+{
+    eprintln!("{} {}", "warning:".yellow().bold(), err);
+}
+
 /// Quit the application regularly.
 pub fn quit() -> ! {
     exit(0);