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

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
}
}