Implement Updater.last_update

This commit is contained in:
Danilo Bargen 2016-01-08 08:38:52 +01:00
commit 4db20cb6bb
4 changed files with 37 additions and 2 deletions

11
Cargo.lock generated
View file

@ -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"

View file

@ -15,3 +15,4 @@ curl = "^0.2"
env_logger = { version = "^0.3", optional = true }
rustc-serialize = "^0.3"
docopt = "^0.6"
time = "^0.1"

View file

@ -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;

View file

@ -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<PathBuf, TldrError> {
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<http::Response, TldrError> {
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<u64> {
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
}
}