-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathmod.rs
135 lines (107 loc) · 3.4 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
mod crates;
mod limits;
mod metadata;
pub(crate) mod options;
mod queue;
mod rustwide_builder;
pub use self::limits::Limits;
pub(self) use self::metadata::Metadata;
pub(crate) use self::rustwide_builder::BuildResult;
pub use self::rustwide_builder::RustwideBuilder;
use crate::error::Result;
use crate::DocBuilderOptions;
use log::debug;
use std::collections::BTreeSet;
use std::fs;
use std::io::prelude::*;
use std::io::BufReader;
use std::path::PathBuf;
/// 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,
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(BufReader::new);
if let Ok(reader) = reader {
for line in reader.lines() {
self.cache.insert(line?);
}
}
self.load_database_cache()?;
Ok(())
}
fn load_database_cache(&mut self) -> Result<()> {
debug!("Loading database cache");
use crate::db::connect_db;
let conn = connect_db()?;
for row in &conn.query(
"SELECT name, version FROM crates, releases \
WHERE crates.id = releases.crate_id",
&[],
)? {
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 = fs::OpenOptions::new().write(true).create(true).open(path)?;
for krate in &self.cache {
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() {
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() {
fs::remove_file(path)?;
}
Ok(())
}
/// Checks for the lock file and returns whether it currently exists.
pub fn is_locked(&self) -> bool {
self.lock_path().exists()
}
/// Returns a reference of options
pub fn options(&self) -> &DocBuilderOptions {
&self.options
}
fn add_to_cache(&mut self, name: &str, version: &str) {
self.cache.insert(format!("{}-{}", name, version));
}
fn should_build(&self, name: &str, version: &str) -> bool {
let name = format!("{}-{}", name, version);
let local = self.options.skip_if_log_exists && self.cache.contains(&name);
let db = self.options.skip_if_exists && self.db_cache.contains(&name);
!(local || db)
}
}