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
use std::collections::HashMap;
use libimagstore::store::Store;
use libimagstore::storeid::StoreId;
use failure::ResultExt;
use failure::Fallible as Result;
use failure::Error;
use failure::err_msg;
use crate::linkable::*;
pub trait StoreLinkConsistentExt {
fn check_link_consistency(&self) -> Result<()>;
}
impl StoreLinkConsistentExt for Store {
fn check_link_consistency(&self) -> Result<()> {
#[derive(Debug, Default)]
struct Linking {
outgoing: Vec<StoreId>,
incoming: Vec<StoreId>,
}
let aggregate_link_network = |store: &Store| -> Result<HashMap<StoreId, Linking>> {
store
.entries()?
.into_get_iter()
.fold(Ok(HashMap::new()), |map, element| {
map.and_then(|mut map| {
debug!("Checking element = {:?}", element);
let entry = element?.ok_or_else(|| err_msg("TODO: Not yet handled"))?;
debug!("Checking entry = {:?}", entry.get_location());
let internal_links = entry
.links()?
.into_getter(store);
let mut linking = Linking::default();
for internal_link in internal_links {
debug!("internal link = {:?}", internal_link);
linking.outgoing.push(internal_link?.get_location().clone());
linking.incoming.push(entry.get_location().clone());
}
map.insert(entry.get_location().clone(), linking);
Ok(map)
})
})
};
let all_collected_storeids_exist = |network: &HashMap<StoreId, Linking>| -> Result<()> {
for (id, _) in network.iter() {
if is_match!(self.get(id.clone()), Ok(Some(_))) {
debug!("Exists in store: {:?}", id);
if !self.exists(id.clone())? {
warn!("Does exist in store but not on FS: {:?}", id);
return Err(err_msg("Link target does not exist"))
}
} else {
warn!("Does not exist in store: {:?}", id);
return Err(err_msg("Link target does not exist"))
}
}
Ok(())
};
let mk_one_directional_link_err = |src: StoreId, target: StoreId| -> Error {
format_err!("Dead link: {} -> {}",
src.local_display_string(),
target.local_display_string())
};
let incoming_links_exists_as_outgoing_links =
|src: &StoreId, linking: &Linking, network: &HashMap<StoreId, Linking>| -> Result<()> {
for link in linking.incoming.iter() {
let incoming_consistent = network.get(link)
.map(|l| l.outgoing.contains(src))
.unwrap_or(false);
if !incoming_consistent {
return Err(mk_one_directional_link_err(src.clone(), link.clone()))
}
}
Ok(())
};
let outgoing_links_exist_as_incoming_links =
|src: &StoreId, linking: &Linking, network: &HashMap<StoreId, Linking>| -> Result<()> {
for link in linking.outgoing.iter() {
let outgoing_consistent = network.get(link)
.map(|l| l.incoming.contains(src))
.unwrap_or(false);
if !outgoing_consistent {
return Err(mk_one_directional_link_err(link.clone(), src.clone()))
}
}
Ok(())
};
let nw = aggregate_link_network(&self)?;
for (k, v) in nw.iter() {
debug!("{}\n in: {:?}\n out: {:?}", k, v.incoming, v.outgoing);
}
all_collected_storeids_exist(&nw).context("Link handling error")?;
for (id, linking) in nw.iter() {
incoming_links_exists_as_outgoing_links(id, linking, &nw)?;
outgoing_links_exist_as_incoming_links(id, linking, &nw)?;
}
Ok(())
}
}