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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//! Packs
//! -----
//!
//! Each folder configured for backup maintains 2 "packsets", one for trees and commits,
//! and one for all other small files. The packsets are named:
//!
//! ```ascii
//! <folder_uuid>-trees
//! <folder_uuid>-blobs
//! ```
//!
//! Small files are separated into 2 packsets because the trees and commits are cached
//! locally (so that Arq gives reasonable performance for browsing backups); all other
//! small blobs don't need to be cached.
//!
//! A packset is a set of "packs". When Arq is backing up a folder, it combines small
//! files into a single larger packfile; when the packfile reaches 10MB, it is stored at
//! the destination. Also, when Arq finishes backing up a folder it stores its unsaved
//! packfiles no matter their sizes.
//!
//! When storing a pack, Arq stores the packfile as:
//!
//! `/<computer_uuid>/packsets/<folder_uuid>-(blobs|trees)/<sha1>.pack`
//!
//! It also stores an index of the SHA1s contained in the pack as:
//!
//! `/<computer_uuid>/packsets/<folder_uuid>-(blobs|trees)/<sha1>.index`
use ;
use std;
use ;
use crateCompressionType;
use crateResult;
use crate;
use crateArqRead;
use crateconvert_to_hex_string;
///Pack File Format
///----------------
///
///```ascii
///signature 50 41 43 4b ("PACK")
///version (2) 00 00 00 02 (network-byte-order 4 bytes)
///object count 00 00 00 00 (network-byte-order 8 bytes)
///object count 00 00 f0 f2
///object[0] mimetype not null 01 (1 byte) (this is usually zero)
///object[0] mimetype strlen 00 00 00 00 (network-byte-order 8 bytes) (this isn't here if not-null is zero)
/// 00 00 00 08
///object[0] mimetype string xx xx xx xx (n bytes)
/// xx xx xx xx
///object[0] name not null 01 (1 byte) (this is usually zero)
///object[0] name strlen 00 00 00 00 (network-byte-order 8 bytes) (this isn't here if not-null is zero)
/// 00 00 00 08
///object[0] name string xx xx xx xx (n bytes)
/// xx xx xx xx
///object[0] data length 00 00 00 00 (network-byte-order 8 bytes)
/// 00 00 00 06
///object[0] data xx xx xx xx (n bytes)
/// xx xx
///...
///object[f0f2] mimetype not null 01 (1 byte) (this is usually zero)
///object[f0f2] mimetype len 00 00 00 00 (network-byte-order 8 bytes) (this isn't here if not-null is zero)
/// 00 00 00 08
///object[f0f2] mimetype str xx xx xx xx (n bytes)
/// xx xx xx xx
///object[f0f2] name not null 01 (1 byte) (this is usually zero)
///object[f0f2] name strlen 00 00 00 00 (network-byte-order 8 bytes) (this isn't here if not-null is zero)
/// 00 00 00 08
///object[f0f2] name string xx xx xx xx (n bytes)
/// xx xx xx xx
///object[f0f2] data length 00 00 00 00 (network-byte-order 8 bytes)
/// 00 00 00 04
///object[f0f2] data 12 34 12 34
///20-byte SHA1 of all of the xx xx xx xx
///above xx xx xx xx
/// xx xx xx xx
/// xx xx xx xx
/// xx xx xx xx
///```
/// PackObject
/// ----------
///
/// This is an auxiliary structure to access the objects described in the "Pack File
/// Format". Each one of these has the following format:
///
/// ```ascii
/// mimetype not null 01 (1 byte) (this is usually zero)
/// mimetype strlen 00 00 00 00 (network-byte-order 8 bytes) (this isn't here if not-null is zero)
/// 00 00 00 08
/// mimetype string xx xx xx xx (n bytes)
/// xx xx xx xx
/// name not null 01 (1 byte) (this is usually zero)
/// name strlen 00 00 00 00 (network-byte-order 8 bytes) (this isn't here if not-null is zero)
/// 00 00 00 08
/// name string xx xx xx xx (n bytes)
/// xx xx xx xx
/// data length 00 00 00 00 (network-byte-order 8 bytes)
/// 00 00 00 06
/// data xx xx xx xx (n bytes)
/// xx xx
///```
/// Pack Index Format
/// -----------------
///
/// ```ascii
/// magic number ff 74 4f 63
/// version (2) 00 00 00 02 network-byte-order
/// fanout[0] 00 00 00 02 (4-byte count of SHA1s starting with 0x00)
/// ...
/// fanout[255] 00 00 f0 f2 (4-byte count of total objects == count of SHA1s starting with 0xff or smaller)
/// object[0] 00 00 00 00 (8-byte network-byte-order offset)
/// 00 00 00 00
/// 00 00 00 00 (8-byte network-byte-order data length)
/// 00 00 00 00
/// 00 xx xx xx (sha1 starting with 00)
/// xx xx xx xx
/// xx xx xx xx
/// xx xx xx xx
/// xx xx xx xx
/// 00 00 00 00 (4 bytes for alignment)
/// object[1] 00 00 00 00 (8-byte network-byte-order offset)
/// 00 00 00 00
/// 00 00 00 00 (8-byte network-byte-order data length)
/// 00 00 00 00
/// 00 xx xx xx (sha1 starting with 00)
/// xx xx xx xx
/// xx xx xx xx
/// xx xx xx xx
/// xx xx xx xx
/// 00 00 00 00 (4 bytes for alignment)
/// object[2] 00 00 00 00 (8-byte network-byte-order offset)
/// 00 00 00 00
/// 00 00 00 00 (8-byte network-byte-order data length)
/// 00 00 00 00
/// 00 xx xx xx (sha1 starting with 00)
/// xx xx xx xx
/// xx xx xx xx
/// xx xx xx xx
/// xx xx xx xx
/// 00 00 00 00 (4 bytes for alignment)
/// ...
/// object[f0f1] 00 00 00 00 (8-byte network-byte-order offset)
/// 00 00 00 00
/// 00 00 00 00 (8-byte network-byte-order data length)
/// 00 00 00 00
/// ff xx xx xx (sha1 starting with ff)
/// xx xx xx xx
/// xx xx xx xx
/// xx xx xx xx
/// xx xx xx xx
/// 00 00 00 00 (4 bytes for alignment)
/// Glacier archiveId not null 01 (1 byte) /* Glacier only */
/// Glacier archiveId strlen 00 00 00 00 (network-byte-order 8 bytes) /* Glacier only */
/// 00 00 00 08 /* Glacier only */
/// Glacier archiveId string xx xx xx xx (n bytes) /* Glacier only */
/// xx xx xx xx /* Glacier only */
/// Glacier pack size 00 00 00 00 (8-byte network-byte-order data length) /* Glacier only */
/// 00 00 00 00 /* Glacier only */
/// 20-byte SHA1 of all of the xx xx xx xx
/// above xx xx xx xx
/// xx xx xx xx
/// xx xx xx xx
/// xx xx xx xx
/// ```
/// PackIndexObject
/// ----------
///
/// This is an auxiliary structure to access the objects described in the "Pack Index
/// Format". Each one of these has the following format:
///
/// ```ascii
/// offset 00 00 00 00 (8-byte network-byte-order offset)
/// 00 00 00 00
/// data length 00 00 00 00 (8-byte network-byte-order data length)
/// 00 00 00 00
/// sha1 00 xx xx xx (sha1 starting with 00)
/// xx xx xx xx
/// xx xx xx xx
/// xx xx xx xx
/// xx xx xx xx
/// alignment 00 00 00 00 (4 bytes for alignment) - we don't include this one
/// ```