From 4db20cb6bbcb8a61957aeb1413fd131fa862ff91 Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Fri, 8 Jan 2016 08:38:52 +0100 Subject: [PATCH] Implement Updater.last_update --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + src/main.rs | 1 + src/updater.rs | 26 ++++++++++++++++++++++++-- 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dba7014..47ad332 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,6 +10,7 @@ dependencies = [ "log 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", "tar 0.3.2 (git+https://github.com/dbrgn/tar-rs?branch=pax_header)", + "time 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -253,6 +254,16 @@ dependencies = [ "rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "time" +version = "0.1.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "url" version = "0.2.38" diff --git a/Cargo.toml b/Cargo.toml index 11c12c0..141d1c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,4 @@ curl = "^0.2" env_logger = { version = "^0.3", optional = true } rustc-serialize = "^0.3" docopt = "^0.6" +time = "^0.1" diff --git a/src/main.rs b/src/main.rs index c88a9df..c8a5109 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ extern crate flate2; extern crate tar; extern crate curl; extern crate rustc_serialize; +extern crate time; use std::io::BufReader; use std::fs::File; diff --git a/src/updater.rs b/src/updater.rs index 90253ca..174d30d 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -1,10 +1,14 @@ use std::io::Read; use std::fs; use std::env; +use std::path::PathBuf; + +#[cfg(unix)] use std::os::unix::fs::MetadataExt; use flate2::read::GzDecoder; use tar::Archive; use curl::http; +use time; use error::TldrError::{self, UpdateError}; @@ -22,6 +26,12 @@ impl Updater { } } + /// Return the path to the cache directory. + fn get_cache_dir(&self) -> Result { + let home_dir = try!(env::home_dir().ok_or(UpdateError("Could not determine home directory".into()))); + Ok(home_dir.join(".cache").join("tldr-rs")) + } + /// Download the archive fn download(&self) -> Result { let resp = try!( @@ -48,8 +58,7 @@ impl Updater { let mut archive = try!(self.decompress(response.get_body())); // Determine paths - let home_dir = try!(env::home_dir().ok_or(UpdateError("Could not determine home directory".into()))); - let cache_dir = home_dir.join(".cache").join("tldr-rs"); + let cache_dir = try!(self.get_cache_dir()); // Extract archive try!(archive.unpack(&cache_dir).map_err(|e| { @@ -65,4 +74,17 @@ impl Updater { Ok(()) } + /// Return the number of seconds since the cache directory was last modified. + #[cfg(unix)] + pub fn last_update(&self) -> Option { + if let Ok(cache_dir) = self.get_cache_dir() { + if let Ok(metadata) = fs::metadata(cache_dir) { + let mtime = metadata.mtime(); + let now = time::now_utc().to_timespec(); + println!("Changed {} seconds ago", now.sec - mtime); + }; + }; + None + } + }