mirror of
https://github.com/tiffany352/rink-rs
synced 2024-11-10 13:44:15 +00:00
Cache into a temp file instead in case it fails
Should make rink behave better when run offline, and when the source goes down.
This commit is contained in:
parent
1c474736ea
commit
bb3d781df7
1 changed files with 8 additions and 3 deletions
11
src/lib.rs
11
src/lib.rs
|
@ -357,7 +357,7 @@ fn btree_merge<K: ::std::cmp::Ord+Clone, V:Clone, F:Fn(&V, &V) -> Option<V>>(
|
||||||
fn cached(file: &str, url: &str, expiration: Duration) -> Result<File, String> {
|
fn cached(file: &str, url: &str, expiration: Duration) -> Result<File, String> {
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
use std::fs::create_dir_all;
|
use std::fs;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use hyper::Client;
|
use hyper::Client;
|
||||||
use hyper::status::StatusCode;
|
use hyper::status::StatusCode;
|
||||||
|
@ -367,7 +367,10 @@ fn cached(file: &str, url: &str, expiration: Duration) -> Result<File, String> {
|
||||||
}
|
}
|
||||||
let mut path = try!(config_dir());
|
let mut path = try!(config_dir());
|
||||||
path.push("rink/");
|
path.push("rink/");
|
||||||
|
let mut tmppath = path.clone();
|
||||||
path.push(file);
|
path.push(file);
|
||||||
|
let tmpfile = format!("{}.part", file);
|
||||||
|
tmppath.push(tmpfile);
|
||||||
|
|
||||||
File::open(path.clone())
|
File::open(path.clone())
|
||||||
.map_err(ts)
|
.map_err(ts)
|
||||||
|
@ -383,8 +386,8 @@ fn cached(file: &str, url: &str, expiration: Duration) -> Result<File, String> {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.or_else(|_| {
|
.or_else(|_| {
|
||||||
try!(create_dir_all(path.parent().unwrap()).map_err(|x| format!("{}", x)));
|
try!(fs::create_dir_all(path.parent().unwrap()).map_err(|x| format!("{}", x)));
|
||||||
let mut f = try!(File::create(path.clone()).map_err(|x| format!("{}", x)));
|
let mut f = try!(File::create(tmppath.clone()).map_err(|x| format!("{}", x)));
|
||||||
|
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
let mut res = try!(client.get(url).send().map_err(|x| format!("{}", x)));
|
let mut res = try!(client.get(url).send().map_err(|x| format!("{}", x)));
|
||||||
|
@ -403,6 +406,8 @@ fn cached(file: &str, url: &str, expiration: Duration) -> Result<File, String> {
|
||||||
}
|
}
|
||||||
try!(f.sync_all().map_err(|x| format!("{}", x)));
|
try!(f.sync_all().map_err(|x| format!("{}", x)));
|
||||||
drop(f);
|
drop(f);
|
||||||
|
try!(fs::rename(tmppath.clone(), path.clone())
|
||||||
|
.map_err(|x| format!("{}", x)));
|
||||||
File::open(path).map_err(|x| format!("{}", x))
|
File::open(path).map_err(|x| format!("{}", x))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue