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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
use std::borrow::Cow;
use std::mem;

use flate2;

use minilzo;

use diskformat::*;

#[ derive (Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd) ]
pub struct BtrfsExtentData <'a> {
	header: & 'a BtrfsLeafItemHeader,
	data_bytes: & 'a [u8],
}

#[ repr (C, packed) ]
#[ derive (Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd) ]
pub struct BtrfsExtentDataData {

	generation: u64,
	logical_data_size: u64,
	compression: u8,
	encryption: u8,
	other_encoding: u16,
	extent_type: u8,

	logical_address: u64,
	extent_size: u64,
	extent_offset: u64,
	logical_bytes: u64,

}

impl <'a> BtrfsExtentData <'a> {

	pub fn from_bytes (
		header: & 'a BtrfsLeafItemHeader,
		data_bytes: & 'a [u8],
	) -> Result <BtrfsExtentData <'a>, String> {

		// create extent data

		let extent_data =
			BtrfsExtentData {
				header: header,
				data_bytes: data_bytes,
			};

		// sanity check

		if extent_data.inline () {

			if data_bytes.len ()
				< mem::size_of::<BtrfsExtentDataData> ()
					- mem::size_of::<u64> () * 4 {

				return Err (
					format! (
						"Must be at least 0x{:x} bytes",
						mem::size_of::<BtrfsExtentDataData> ()
							- mem::size_of::<u64> () * 4));

			}

		} else {

			if data_bytes.len ()
				!= mem::size_of::<BtrfsExtentDataData> () {

				return Err (
					format! (
						"Must be exactly 0x{:x} bytes",
						mem::size_of::<BtrfsExtentDataData> ()));

			}

		}

		// return

		Ok (extent_data)

	}

	pub fn header (& self) -> & BtrfsLeafItemHeader {
		self.header
	}

	pub fn key (& self) -> BtrfsKey {
		self.header.key ()
	}

	pub fn object_id (& self) -> u64 {
		self.header.object_id ()
	}

	pub fn offset (& self) -> u64 {
		self.header.offset ()
	}

	pub fn data (& self) -> & BtrfsExtentDataData {

		unsafe {
			& * (
				self.data_bytes.as_ptr ()
				as * const BtrfsExtentDataData
			)
		}

	}

	pub fn generation (& self) -> u64 {
		self.data ().generation
	}

	pub fn logical_data_size (& self) -> u64 {
		self.data ().logical_data_size
	}

	pub fn compression (& self) -> u8 {
		self.data ().compression
	}

	pub fn encryption (& self) -> u8 {
		self.data ().encryption
	}

	pub fn other_encoding (& self) -> u16 {
		self.data ().other_encoding
	}

	pub fn extent_type (& self) -> u8 {
		self.data ().extent_type
	}

	pub fn logical_address (& self) -> u64 {

		if self.inline () {
			panic! ();
		}

		self.data ().logical_address

	}

	pub fn extent_size (& self) -> u64 {

		if self.inline () {
			panic! ();
		}

		self.data ().extent_size

	}

	pub fn extent_offset (& self) -> u64 {

		if self.inline () {
			panic! ();
		}

		self.data ().extent_offset

	}

	pub fn logical_bytes (& self) -> u64 {

		if self.inline () {
			panic! ();
		}

		self.data ().logical_bytes

	}

	pub fn inline (
		& self,
	) -> bool {
		self.data ().extent_type == BTRFS_EXTENT_DATA_INLINE_TYPE
	}

	pub fn inline_data (
		& self,
	) -> Result <Option <Cow <[u8]>>, String> {

		if self.inline () {

			let raw_data_start =
				mem::size_of::<BtrfsExtentDataData> ()
				- 4 * mem::size_of::<u64> ();

			let raw_data_end =
				self.data_bytes.len ();

			let raw_data =
				& self.data_bytes [raw_data_start .. raw_data_end];

			if self.data ().encryption != 0 {

				return Err (
					format! (
						"Unrecognised encryption type {}",
						self.data ().encryption)
				);

			}

			if self.data ().other_encoding != 0 {

				return Err (
					format! (
						"Unrecognised other encoding type {}",
						self.data ().other_encoding)
				);

			}

			match self.data ().compression {

				BTRFS_EXTENT_DATA_NO_COMPRESSION =>
					Ok (Some (
						Cow::Borrowed (
							raw_data)
					)),

				BTRFS_EXTENT_DATA_LZO_COMPRESSION => try! (
					minilzo::decompress (
						raw_data,
						self.data ().logical_data_size as usize,
					).map (
						|uncompressed_data|

						Ok (Some (
							Cow::Owned (
								uncompressed_data)
						))

					).or_else (
						|error|

						Err (
							format! (
								"LZO decompression failed: {:?}",
								error)
						)

					)
				),

				BTRFS_EXTENT_DATA_ZLIB_COMPRESSION => {

					let mut uncompressed_data =
						Vec::with_capacity (
							self.data ().logical_data_size as usize);

					uncompressed_data.resize (
						self.data ().logical_data_size as usize,
						0u8);

					let mut decompress =
						flate2::Decompress::new (
							false);

					match (
						decompress.decompress (
							raw_data,
							& mut uncompressed_data,
							flate2::Flush::Finish,
						).unwrap_or_else (
							|error|

							panic! (
								"ZLIB decompression failed: {:?}",
								error)

						)
					) {

						flate2::Status::Ok =>
							(),

						_ =>
							panic! (
								"ZLIB decompression failed"),

					}

					if decompress.total_out () as usize
						!= uncompressed_data.len () {

						panic! (
							"ZLIB decompressed size {} does not match {}",
							decompress.total_out (),
							uncompressed_data.len ());

					}

					Ok (Some (
						Cow::Owned (
							uncompressed_data
						)
					))

				},

				_ =>
					panic! (
						format! (
							"Unrecognised inline extent data compression {}",
							self.data ().compression)),

			}

		} else {

			Ok (None)

		}

	}

}

pub const BTRFS_EXTENT_DATA_INLINE_TYPE: u8 = 0;
pub const BTRFS_EXTENT_DATA_REGULAR_TYPE: u8 = 1;
pub const BTRFS_EXTENT_DATA_PREALLOC_TYPE: u8 = 2;

pub const BTRFS_EXTENT_DATA_NO_COMPRESSION: u8 = 0;
pub const BTRFS_EXTENT_DATA_ZLIB_COMPRESSION: u8 = 1;
pub const BTRFS_EXTENT_DATA_LZO_COMPRESSION: u8 = 2;

// ex: noet ts=4 filetype=rust