-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathmod.rs
112 lines (88 loc) · 2.67 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
pub mod options;
pub mod metadata;
mod chroot_builder;
mod crates;
mod queue;
pub use self::chroot_builder::ChrootBuilderResult;
use std::fs;
use std::io::prelude::*;
use std::io::BufReader;
use std::path::PathBuf;
use std::collections::BTreeSet;
use DocBuilderOptions;
use error::Result;
/// chroot based documentation builder
pub struct DocBuilder {
options: DocBuilderOptions,
cache: BTreeSet<String>,
db_cache: BTreeSet<String>,
}
impl DocBuilder {
pub fn new(options: DocBuilderOptions) -> DocBuilder {
DocBuilder {
options: options,
cache: BTreeSet::new(),
db_cache: BTreeSet::new(),
}
}
/// Loads build cache
pub fn load_cache(&mut self) -> Result<()> {
debug!("Loading cache");
let path = PathBuf::from(&self.options.prefix).join("cache");
let reader = fs::File::open(path).map(|f| BufReader::new(f));
if reader.is_err() {
return Ok(());
}
for line in reader.unwrap().lines() {
self.cache.insert(try!(line));
}
try!(self.load_database_cache());
Ok(())
}
fn load_database_cache(&mut self) -> Result<()> {
debug!("Loading database cache");
use db::connect_db;
let conn = try!(connect_db());
for row in &conn.query("SELECT name, version FROM crates, releases \
WHERE crates.id = releases.crate_id",
&[])
.unwrap() {
let name: String = row.get(0);
let version: String = row.get(1);
self.db_cache.insert(format!("{}-{}", name, version));
}
Ok(())
}
/// Saves build cache
pub fn save_cache(&self) -> Result<()> {
debug!("Saving cache");
let path = PathBuf::from(&self.options.prefix).join("cache");
let mut file = try!(fs::OpenOptions::new()
.write(true)
.create(true)
.open(path));
for krate in &self.cache {
try!(writeln!(file, "{}", krate));
}
Ok(())
}
fn lock_path(&self) -> PathBuf {
self.options.prefix.join("cratesfyi.lock")
}
/// Creates a lock file. Daemon will check this lock file and stop operating if its exists.
pub fn lock(&self) -> Result<()> {
let path = self.lock_path();
if !path.exists() {
try!(fs::OpenOptions::new().write(true).create(true).open(path));
}
Ok(())
}
/// Removes lock file.
pub fn unlock(&self) -> Result<()> {
let path = self.lock_path();
if path.exists() {
try!(fs::remove_file(path));
}
Ok(())
}
}