Documentation
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use std::collections::HashSet;
use std::fs;
use std::path::PathBuf;

use clap;

use output::Output;

use convert::utils::*;
use misc::*;
use zbackup::data::*;
use zbackup::disk_format::*;
use zbackup::repository_core::*;

pub fn gc_bundles (
	output: & Output,
	arguments: & GcBundlesArguments,
) -> Result <bool, String> {

	// open repository

	let repository_core =
		string_result_with_prefix (
			|| format! (
				"Error opening repository {}: ",
				arguments.repository_path.to_string_lossy ()),
			RepositoryCore::open (
				& output,
				& arguments.repository_path,
				arguments.password_file_path.clone ()),
		) ?;

	// begin transaction

	let atomic_file_writer =
		AtomicFileWriter::new (
			output,
			& arguments.repository_path,
			None,
		) ?;

	// get list of bundle files

	let old_bundles =
		scan_bundle_files (
			output,
			& arguments.repository_path,
		) ?;

	output.message_format (
		format_args! (
			"Found {} bundle files",
			old_bundles.len ()));

	// get list of index files

	let index_ids_and_sizes = (
		scan_index_files_with_sizes (
			& arguments.repository_path)
	) ?;

	output.message_format (
		format_args! (
			"Found {} index files",
			index_ids_and_sizes.len ()));

	// read indexes

	let mut all_index_entries: HashSet <(BundleId, ChunkId)> =
		HashSet::new ();

	get_all_index_entries (
		output,
		& repository_core,
		& index_ids_and_sizes,
		& mut all_index_entries,
	) ?;

	// read bundle headers

	let mut bundles_to_compact: Vec <BundleId> =
		Vec::new ();

	let mut bundles_to_delete: Vec <BundleId> =
		Vec::new ();

	let mut other_chunks_seen: HashSet <ChunkId> =
		HashSet::new ();

	read_bundles_metadata (
		output,
		& repository_core,
		& old_bundles,
		& all_index_entries,
		& mut bundles_to_compact,
		& mut bundles_to_delete,
		& mut other_chunks_seen,
	) ?;

	// delete bundles

	delete_bundles (
		output,
		& repository_core,
		& bundles_to_delete,
	) ?;

	// compact bundles

	compact_bundles (
		output,
		& repository_core,
		atomic_file_writer,
		& all_index_entries,
		& bundles_to_compact,
		& other_chunks_seen,
	) ?;

	// return

	Ok (true)

}

fn get_all_index_entries (
	output: & Output,
	repository_core: & RepositoryCore,
	index_ids_and_sizes: & Vec <(IndexId, u64)>,
	all_index_entries: & mut HashSet <(BundleId, ChunkId)>,
) -> Result <(), String> {

	let output_job =
		output_job_start! (
			output,
			"Reading indexes");

	let total_index_size: u64 =
		index_ids_and_sizes.iter ().map (
			|& (_, index_size)|
			index_size
		).sum ();

	let mut read_index_size: u64 = 0;

	for & (
		index_id,
		index_size,
	) in index_ids_and_sizes.iter () {

		output_job.progress (
			read_index_size,
			total_index_size);

		let index_path =
			repository_core.index_path (
				index_id);

		let index_entries =
			index_read_path (
				& index_path,
				repository_core.encryption_key (),
			) ?;

		for & RawIndexEntry {
			ref index_bundle_header,
			ref bundle_info,
		} in index_entries.iter () {

			for bundle_chunk
			in bundle_info.chunks () {

				all_index_entries.insert (
					(
						index_bundle_header.bundle_id (),
						bundle_chunk.chunk_id (),
					),
				);

			}

		}

		read_index_size +=
			index_size as u64;

	}

	output_job.complete ();

	Ok (())

}

fn read_bundles_metadata (
	output: & Output,
	repository_core: & RepositoryCore,
	old_bundles: & Vec <BundleId>,
	all_index_entries: & HashSet <(BundleId, ChunkId)>,
	bundles_to_compact: & mut Vec <BundleId>,
	bundles_to_delete: & mut Vec <BundleId>,
	other_chunks_seen: & mut HashSet <ChunkId>,
) -> Result <(), String> {

	let output_job =
		output_job_start! (
			output,
			"Reading bundle metadata");

	let mut old_bundles_count: u64 = 0;
	let old_bundles_total = old_bundles.len () as u64;

	let mut seen_chunk_ids: HashSet <ChunkId> =
		HashSet::new ();

	for & old_bundle_id in old_bundles {

		output_job.progress (
			old_bundles_count,
			old_bundles_total);

		let old_bundle_path =
			repository_core.bundle_path (
				old_bundle_id);

		let old_bundle_info =
			bundle_info_read_path (
				old_bundle_path,
				repository_core.encryption_key (),
			) ?;

		let mut num_to_keep: u64 = 0;
		let mut num_to_reap: u64 = 0;

		for old_bundle_chunk
		in old_bundle_info.chunks () {

			if (
				all_index_entries.contains (
					& (
						old_bundle_id,
						old_bundle_chunk.chunk_id (),
					)
				)
			&&
				! seen_chunk_ids.contains (
					& old_bundle_chunk.chunk_id ())
			) {

				num_to_keep += 1;

				seen_chunk_ids.insert (
					old_bundle_chunk.chunk_id ());

			} else {

				num_to_reap += 1;

			}

		}

		if num_to_keep == 0 {

			bundles_to_delete.push (
				old_bundle_id);

		} else if num_to_reap > 0 {

			bundles_to_compact.push (
				old_bundle_id);

		} else {

			for old_bundle_chunk
			in old_bundle_info.chunks () {

				other_chunks_seen.insert (
					old_bundle_chunk.chunk_id (),
				);

			}

		}

		old_bundles_count += 1;

	}

	output_job_replace! (
		output_job,
		"Found {} bundles to compact and {} to delete",
		bundles_to_compact.len (),
		bundles_to_delete.len ());

	Ok (())

}

fn delete_bundles (
	output: & Output,
	repository_core: & RepositoryCore,
	bundles_to_delete: & Vec <BundleId>,
) -> Result <(), String> {

	if bundles_to_delete.is_empty () {
		return Ok (());
	}

	let output_job =
		output_job_start! (
			output,
			"Deleting bundles");

	let bundles_to_delete_total = bundles_to_delete.len () as u64;
	let mut bundles_to_delete_count: u64 = 0;

	for & bundle_to_delete in bundles_to_delete {

		output_job.progress (
			bundles_to_delete_count,
			bundles_to_delete_total);

		io_result (
			fs::remove_file (
				repository_core.bundle_path (
					bundle_to_delete)),
		) ?;

		bundles_to_delete_count += 1;

	}

	output_job.complete ();

	Ok (())

}

fn compact_bundles (
	output: & Output,
	repository_core: & RepositoryCore,
	atomic_file_writer: AtomicFileWriter,
	all_index_entries: & HashSet <(BundleId, ChunkId)>,
	bundles_to_compact: & Vec <BundleId>,
	other_chunks_seen: & HashSet <ChunkId>,
) -> Result <(), String> {

	let bundles_to_compact_total = bundles_to_compact.len () as u64;
	let mut bundles_to_compact_count: u64 = 0;

	let mut seen_chunk_ids: HashSet <ChunkId> =
		other_chunks_seen.iter ().map (|&c| c).collect ();

	for & bundle_to_compact in bundles_to_compact {

		let bundle_path =
			repository_core.bundle_path (
				bundle_to_compact);

		let output_job =
			output_job_start! (
				output,
				"Reading bundle {} of {}",
				bundles_to_compact_count + 1,
				bundles_to_compact_total);

		let uncompacted_bundle =
			bundle_read_path (
				& bundle_path,
				repository_core.encryption_key ()
			) ?;

		output_job.remove ();

		let output_job =
			output_job_start! (
				output,
				"Compacting bundle {} of {}",
				bundles_to_compact_count + 1,
				bundles_to_compact_total);

		let mut compacted_bundle_file =
			atomic_file_writer.create (
				bundle_path,
			) ?;

		let mut compacted_bundle: Vec <(ChunkId, Vec <u8>)> =
			Vec::new ();

		for (chunk_id, chunk_data)
		in uncompacted_bundle.into_iter () {

			if (
				all_index_entries.contains (
					& (
						bundle_to_compact,
						chunk_id,
					)
				)
			&&
				! seen_chunk_ids.contains (
					& chunk_id)
			) {

				compacted_bundle.push (
					(chunk_id, chunk_data));

				seen_chunk_ids.insert (
					chunk_id);

			}

		}

		let total_chunks =
			compacted_bundle.len () as u64;

		bundle_write_direct (
			& mut compacted_bundle_file,
			repository_core.encryption_key (),
			& compacted_bundle,
			|chunks_written| {

				output_job.progress (
					chunks_written,
					total_chunks)

			}
		) ?;

		atomic_file_writer.commit () ?;

		output_job.complete ();

		bundles_to_compact_count += 1;

	}

	Ok (())

}

command! (

	name = gc_bundles,
	export = gc_bundles_command,

	arguments = GcBundlesArguments {
		repository_path: PathBuf,
		password_file_path: Option <PathBuf>,
	},

	clap_subcommand = {

		clap::SubCommand::with_name ("gc-bundles")
			.about ("Removes chunks from bundles which are not in any index")

			.arg (
				clap::Arg::with_name ("repository")

				.long ("repository")
				.value_name ("REPOSITORY")
				.required (true)
				.help ("Path to the repository")

			)

			.arg (
				clap::Arg::with_name ("password-file")

				.long ("password-file")
				.value_name ("PASSWORD-FILE")
				.required (false)
				.help ("Path to the password file")

			)

	},

	clap_arguments_parse = |clap_matches| {

		GcBundlesArguments {

			repository_path:
				args::path_required (
					& clap_matches,
					"repository"),

			password_file_path:
				args::path_optional (
					& clap_matches,
					"password-file"),

		}

	},

	action = |output, arguments| {
		gc_bundles (output, arguments)
	},

);

// ex: noet ts=4 filetype=rust