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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use std::io::{Read, Seek};
use std::ops::Range;
use std::slice::Iter;
use thiserror::Error;
cfg_if::cfg_if! {
if #[cfg(any(target_os = "linux",
target_os = "android",
target_os = "freebsd",
target_os = "macos",
))]{
mod unix;
} else if #[cfg(windows)] {
mod windows;
} else {
mod default;
}
}
#[cfg(test)]
mod test_utils;
#[derive(Error, Debug)]
pub enum ScanError {
#[error("IO Error occurred")]
IO(#[from] std::io::Error),
#[error("An unknown error occurred interacting with the C API")]
Raw(i32),
#[error("The operation you are trying to perform is not supported on this platform")]
UnsupportedPlatform,
#[error("The filesystem does not support operating on sparse files")]
UnsupportedFileSystem,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum SegmentType {
Hole,
Data,
}
impl SegmentType {
pub fn opposite(&self) -> Self {
match self {
SegmentType::Hole => SegmentType::Data,
SegmentType::Data => SegmentType::Hole,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Segment {
pub segment_type: SegmentType,
pub range: Range<u64>,
}
pub struct SegmentIter<'a> {
segment_type: SegmentType,
iter: Iter<'a, Segment>,
}
impl<'a> Iterator for SegmentIter<'a> {
type Item = &'a Range<u64>;
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
for segment in self.iter.by_ref() {
if segment.segment_type == self.segment_type {
return Some(&segment.range);
}
}
None
}
}
pub trait Segments {
fn data(&self) -> SegmentIter;
fn holes(&self) -> SegmentIter;
}
impl Segments for Vec<Segment> {
fn data(&self) -> SegmentIter {
SegmentIter {
segment_type: SegmentType::Data,
iter: self.iter(),
}
}
fn holes(&self) -> SegmentIter {
SegmentIter {
segment_type: SegmentType::Hole,
iter: self.iter(),
}
}
}
#[allow(clippy::len_without_is_empty)]
impl Segment {
pub fn contains(&self, offset: &u64) -> bool {
self.range.contains(offset)
}
pub fn is_hole(&self) -> bool {
self.segment_type == SegmentType::Hole
}
pub fn is_data(&self) -> bool {
self.segment_type == SegmentType::Data
}
pub fn start(&self) -> u64 {
self.range.start
}
pub fn len(&self) -> u64 {
self.range.start - self.range.end
}
}
pub trait SparseFile: Read + Seek {
fn scan_chunks(&mut self) -> Result<Vec<Segment>, ScanError>;
fn drill_hole(&self, start: u64, end: u64) -> Result<(), ScanError>;
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::*;
use quickcheck_macros::quickcheck;
use std::fs::File;
fn test_chunks_match(file: &mut File, input_segments: &[Segment]) -> bool {
let output_segments = file.scan_chunks().expect("Unable to scan chunks");
if *input_segments != output_segments {
println!("Expected: \n {:?} \n", input_segments);
println!("Got: \n {:?} \n", output_segments);
}
*input_segments == output_segments
}
fn test_round_trips(desc: SparseDescription) -> bool {
let mut file = desc.to_file();
let input_segments = desc.segments();
test_chunks_match(file.as_file_mut(), &input_segments)
}
#[quickcheck]
fn round_trips(desc: SparseDescription) -> bool {
test_round_trips(desc)
}
#[quickcheck]
fn drill_hole(desc: SparseDescription, drop: u8) -> bool {
let mut file = desc.to_file();
let mut input_segments = desc.segments();
if input_segments.is_empty() {
return true;
}
#[cfg(target_os = "macos")]
for hole in input_segments.holes() {
file.as_file_mut()
.drill_hole(hole.start, hole.end)
.expect("pre drill holes");
}
test_chunks_match(file.as_file_mut(), &input_segments);
let drop_idx = drop as usize % input_segments.len();
let drop = &mut input_segments[drop_idx];
file.as_file_mut()
.drill_hole(drop.range.start, drop.range.end)
.expect("drilled hole");
drop.segment_type = SegmentType::Hole;
combine_segments(&mut input_segments);
test_chunks_match(file.as_file_mut(), &input_segments)
}
#[quickcheck]
fn one_big_segment(segment_type: SegmentType) -> bool {
let desc = SparseDescription::one_segment(segment_type, 3545868);
test_round_trips(desc)
}
fn combine_segments(segments: &mut Vec<Segment>) {
let mut prev = 0;
for i in 1..segments.len() {
if segments[prev].segment_type == segments[i].segment_type {
segments[prev].range.end = segments[i].range.end;
} else {
prev += 1;
segments[prev] = segments[i].clone();
}
}
segments.truncate(prev + 1)
}
}