Skip to content

Commit 69180d9

Browse files
committed
Using user cache instead of local dir
1 parent fa82f9c commit 69180d9

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ authors = ["Martin Billinger <flkazemakase@gmail.com>"]
77
simple_logger = "0.5"
88

99
[dependencies]
10+
app_dirs = "1.2.1"
1011
arff = { path = "../arff" }
1112
fs2 = "*"
1213
futures = "0.1"

src/error.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::io::Error as IoError;
22
use std::result::Result as StdResult;
33
use std::string::FromUtf8Error;
44

5+
use app_dirs::AppDirsError;
56
use arff::Error as ArffError;
67
use hyper::Error as HyperError;
78
use hyper::error::UriError;
@@ -19,6 +20,7 @@ pub enum Error {
1920
HyperTlsError(TlsError),
2021
JsonError(JsonError),
2122
ArffError(ArffError),
23+
AppDirsError(AppDirsError),
2224
}
2325

2426
impl From<IoError> for Error {
@@ -62,3 +64,12 @@ impl From<ArffError> for Error {
6264
Error::ArffError(e)
6365
}
6466
}
67+
68+
impl From<AppDirsError> for Error {
69+
fn from(e: AppDirsError) -> Self {
70+
match e {
71+
AppDirsError::Io(e) => Error::IoError(e),
72+
_ => Error::AppDirsError(e)
73+
}
74+
}
75+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
extern crate app_dirs;
12
extern crate arff;
23
extern crate fs2;
34
extern crate futures;

src/openml_api/web_access.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fs::{File, OpenOptions};
22
use std::io::{self, Read, Write};
3-
use std::path::Path;
43

4+
use app_dirs::{app_root, AppDataType, AppInfo};
55
use futures::{Future, Stream};
66
use hyper::Client;
77
use hyper_tls::HttpsConnector;
@@ -11,14 +11,19 @@ use error::Result;
1111

1212
use super::file_lock::{ExclusiveLock, SharedLock};
1313

14+
const APP_INFO: AppInfo = AppInfo{name: "openml-rust", author: "openml-rust"};
15+
1416
pub fn get_cached(url: &str) -> Result<String> {
1517
// todo: is there a potential race condition with a process locking the file for reading while
1618
// the writer has created but not yet locked the file?
17-
let filename = "cache/".to_owned() + &url_to_file(url);
18-
let path = Path::new(&filename);
19+
20+
let mut path = app_root(AppDataType::UserCache, &APP_INFO)?;
21+
path.push(url_to_file(url));
22+
23+
println!("{:?}", path);
1924

2025
loop {
21-
match File::open(path) {
26+
match File::open(&path) {
2227
Ok(f) => {
2328
info!("Loading cached {}", url);
2429
let mut file = SharedLock::new(f)?;
@@ -29,7 +34,7 @@ pub fn get_cached(url: &str) -> Result<String> {
2934
Err(_) => {}
3035
}
3136

32-
match OpenOptions::new().create_new(true).write(true).open(path) {
37+
match OpenOptions::new().create_new(true).write(true).open(&path) {
3338
Err(e) => {
3439
// todo: is this the correct io error raised if another thread has locked the file currently?
3540
if let io::ErrorKind::PermissionDenied = e.kind() {

0 commit comments

Comments
 (0)