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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use std::path::{Path, PathBuf};
use super::{ExplainAction, ObjectStore, RebuildReason};
impl ObjectStore {
/// Check if a product needs rebuilding
/// Returns true if inputs changed or outputs are missing
pub fn needs_rebuild(&self, cache_key: &str, input_checksum: &str, output_paths: &[PathBuf]) -> bool {
// Check if we have a cache entry
let entry = match self.get_entry(cache_key) {
Some(e) => e,
None => return true,
};
// Check if input checksum matches
if entry.input_checksum != input_checksum {
return true;
}
// For checkers (empty outputs), cache entry with matching checksum = up-to-date
if output_paths.is_empty() {
return false;
}
// Check if all outputs exist at their original paths
for output_path in output_paths {
if !output_path.exists() {
// Output missing - check if we can restore from cache
let rel_path = Self::path_string(output_path);
let cached_output = entry.outputs.iter()
.find(|o| o.path == rel_path);
match cached_output {
Some(out) if self.has_object(&out.checksum) => {
// Can restore from cache, but still "needs rebuild" to trigger restore
return true;
}
_ => return true,
}
}
}
false
}
/// Check if outputs can be restored from cache (read-only, does not restore)
/// Returns true if all missing outputs are available in cache
pub fn can_restore(&self, cache_key: &str, input_checksum: &str, output_paths: &[PathBuf]) -> bool {
// For checkers (empty outputs), cache entry with matching checksum = restorable
if output_paths.is_empty() {
return self.get_entry(cache_key)
.map(|e| e.input_checksum == input_checksum)
.unwrap_or(false);
}
let entry = match self.get_entry(cache_key) {
Some(e) if e.input_checksum == input_checksum => e,
_ => return false,
};
for output_path in output_paths {
if output_path.exists() {
continue;
}
let rel_path = Self::path_string(output_path);
let cached_output = entry.outputs.iter()
.find(|o| o.path == rel_path);
match cached_output {
Some(out) if self.has_object(&out.checksum) => {}
_ => return false,
}
}
true
}
/// Explain what action will be taken for a product and why.
/// Mirrors the logic in needs_rebuild/can_restore but returns structured reasons.
pub fn explain_action(&self, cache_key: &str, input_checksum: &str, output_paths: &[PathBuf], force: bool) -> ExplainAction {
if force {
return ExplainAction::Rebuild(RebuildReason::Force);
}
let entry = match self.get_entry(cache_key) {
Some(e) => e,
None => return ExplainAction::Rebuild(RebuildReason::NoCacheEntry),
};
if entry.input_checksum != input_checksum {
// Inputs changed — check if restorable (shouldn't be, since checksum differs)
return ExplainAction::Rebuild(RebuildReason::InputsChanged);
}
// For checkers (empty outputs), matching checksum means up-to-date
if output_paths.is_empty() {
return ExplainAction::Skip;
}
// Check outputs — must verify ALL missing outputs are restorable before
// reporting Restore, otherwise the actual restore would fail and fall through
// to a rebuild.
let mut first_missing: Option<String> = None;
for output_path in output_paths {
if !output_path.exists() {
let rel_path = Self::path_string(output_path);
let cached_output = entry.outputs.iter().find(|o| o.path == rel_path);
match cached_output {
Some(out) if self.has_object(&out.checksum) => {
if first_missing.is_none() {
first_missing = Some(rel_path);
}
}
_ => {
return ExplainAction::Rebuild(RebuildReason::OutputMissing(rel_path));
}
}
}
}
match first_missing {
Some(path) => ExplainAction::Restore(RebuildReason::OutputMissing(path)),
None => ExplainAction::Skip,
}
}
/// Check if a product with an output directory needs rebuilding.
/// Returns true if no cache entry, input checksum differs, or output dir doesn't exist.
pub fn needs_rebuild_output_dir(&self, cache_key: &str, input_checksum: &str, dir: &Path) -> bool {
let entry = match self.get_entry(cache_key) {
Some(e) => e,
None => return true,
};
if entry.input_checksum != input_checksum {
return true;
}
// If the output directory doesn't exist, we need a rebuild (or restore)
!dir.exists()
}
/// Check if an output directory can be restored from cache.
/// Returns true if cache entry exists with matching checksum and all objects are available.
pub fn can_restore_output_dir(&self, cache_key: &str, input_checksum: &str) -> bool {
let entry = match self.get_entry(cache_key) {
Some(e) if e.input_checksum == input_checksum => e,
_ => return false,
};
entry.outputs.iter().all(|o| self.has_object(&o.checksum))
}
/// Explain what action will be taken for a product with an output directory.
pub fn explain_action_output_dir(&self, cache_key: &str, input_checksum: &str, dir: &Path, force: bool) -> ExplainAction {
if force {
return ExplainAction::Rebuild(RebuildReason::Force);
}
let entry = match self.get_entry(cache_key) {
Some(e) => e,
None => return ExplainAction::Rebuild(RebuildReason::NoCacheEntry),
};
if entry.input_checksum != input_checksum {
return ExplainAction::Rebuild(RebuildReason::InputsChanged);
}
if dir.exists() {
return ExplainAction::Skip;
}
// Directory missing — check if all objects are available for restore
let all_available = entry.outputs.iter().all(|o| self.has_object(&o.checksum));
let dir_display = dir.display().to_string();
if all_available {
ExplainAction::Restore(RebuildReason::OutputMissing(dir_display))
} else {
ExplainAction::Rebuild(RebuildReason::OutputMissing(dir_display))
}
}
}