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
use std::fs::File as StdFile;
use std::slice::Iter;
use convert::TryInto;
use headers::{ChunkHeader, ChunkType, FileHeader};
use headers::CHUNK_HEADER_SIZE;
use result::Result;
pub type ChunkIter<'a> = Iter<'a, Chunk>;
#[derive(Debug)]
pub struct File {
block_size: u32,
backing_file: Option<StdFile>,
chunks: Vec<Chunk>,
}
impl File {
pub fn new(block_size: u32) -> Self {
Self {
block_size: block_size,
backing_file: None,
chunks: Vec::new(),
}
}
pub fn with_backing_file(file: StdFile, block_size: u32) -> Self {
Self {
block_size: block_size,
backing_file: Some(file),
chunks: Vec::new(),
}
}
pub fn header(&self) -> FileHeader {
let total_chunks = self.chunks
.len()
.try_into()
.expect("number of chunks doesn't fit into u32");
FileHeader {
block_size: self.block_size,
total_blocks: self.total_blocks(),
total_chunks: total_chunks,
image_checksum: self.image_checksum(),
}
}
pub fn chunk_header(&self, chunk: &Chunk) -> ChunkHeader {
ChunkHeader {
chunk_type: chunk.chunk_type(),
chunk_size: chunk.raw_size() / self.block_size,
total_size: chunk.size(),
}
}
pub fn add_raw(&mut self, buf: &[u8]) -> Result<()> {
if buf.len() % self.block_size as usize != 0 {
return Err("bytes size must be multiple of block_size".into());
}
let new_buf = buf;
if let Some(&mut Chunk::Raw { ref mut buf }) = self.chunks.iter_mut().last() {
buf.extend(new_buf.iter().cloned());
return Ok(());
}
let buf = new_buf.to_vec();
self.chunks.push(Chunk::Raw { buf });
Ok(())
}
pub fn add_raw_file_backed(&mut self, offset: u64, size: u32) -> Result<()> {
let backing_file = match self.backing_file {
Some(ref f) => f,
None => return Err("Sparse File not created with backing file".into()),
};
if size % self.block_size != 0 {
return Err("size must be multiple of block_size".into());
}
let (new_offset, new_size) = (offset, size);
if let Some(&mut Chunk::RawFileBacked {
offset,
ref mut size,
..
}) = self.chunks.iter_mut().last()
{
if new_offset == offset + u64::from(*size) {
*size += new_size;
return Ok(());
}
}
self.chunks.push(Chunk::RawFileBacked {
file: backing_file.try_clone()?,
offset: offset,
size: size,
});
Ok(())
}
pub fn add_fill(&mut self, fill: [u8; 4], size: u32) -> Result<()> {
if size % self.block_size != 0 {
return Err("size must be multiple of block_size".into());
}
let (new_fill, new_size) = (fill, size);
if let Some(&mut Chunk::Fill { fill, ref mut size }) = self.chunks.iter_mut().last() {
if fill == new_fill {
*size += new_size;
return Ok(());
}
}
self.chunks.push(Chunk::Fill { fill, size });
Ok(())
}
pub fn add_dont_care(&mut self, size: u32) -> Result<()> {
if size % self.block_size != 0 {
return Err("size must be multiple of block_size".into());
}
let new_size = size;
if let Some(&mut Chunk::DontCare { ref mut size }) = self.chunks.iter_mut().last() {
*size += new_size;
return Ok(());
}
self.chunks.push(Chunk::DontCare { size });
Ok(())
}
pub fn add_crc32(&mut self, crc: u32) -> Result<()> {
self.chunks.push(Chunk::Crc32 { crc });
Ok(())
}
pub fn chunk_iter(&self) -> ChunkIter {
self.chunks.iter()
}
fn total_blocks(&self) -> u32 {
self.chunks
.iter()
.fold(0, |sum, chunk| sum + chunk.raw_size() / self.block_size)
}
fn image_checksum(&self) -> u32 {
0
}
}
#[derive(Debug)]
pub enum Chunk {
Raw { buf: Vec<u8> },
RawFileBacked {
file: StdFile,
offset: u64,
size: u32,
},
Fill { fill: [u8; 4], size: u32 },
DontCare { size: u32 },
Crc32 { crc: u32 },
}
impl Chunk {
pub fn size(&self) -> u32 {
let body_size = match *self {
Chunk::Raw { ref buf } => buf.len()
.try_into()
.expect("chunk size doesn't fit into u32"),
Chunk::RawFileBacked { size, .. } => size,
Chunk::Fill { .. } | Chunk::Crc32 { .. } => 4,
Chunk::DontCare { .. } => 0,
};
u32::from(CHUNK_HEADER_SIZE) + body_size
}
pub fn raw_size(&self) -> u32 {
match *self {
Chunk::Raw { ref buf } => buf.len()
.try_into()
.expect("raw chunk size doesn't fit into u32"),
Chunk::RawFileBacked { size, .. } |
Chunk::Fill { size, .. } |
Chunk::DontCare { size } => size,
Chunk::Crc32 { .. } => 0,
}
}
pub fn chunk_type(&self) -> ChunkType {
match *self {
Chunk::Raw { .. } | Chunk::RawFileBacked { .. } => ChunkType::Raw,
Chunk::Fill { .. } => ChunkType::Fill,
Chunk::DontCare { .. } => ChunkType::DontCare,
Chunk::Crc32 { .. } => ChunkType::Crc32,
}
}
}