Skip to content

Commit dfcc22d

Browse files
authored
Merge pull request #601 from Tarsnap/list-archives-opt-hashes
Add --hashes option for --list-archives command
2 parents 57a7b2c + 46bda99 commit dfcc22d

File tree

12 files changed

+73
-17
lines changed

12 files changed

+73
-17
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
name with a null character (like `find -print0`). If one or more -v
1212
arguments are specified, multiple null characters are used to separate
1313
fields; see the man page for details.
14+
- tarsnap now accepts --hashes, which causes --list-archives to print hashes
15+
of archive names. If one or more -v arguments are specified, it will print
16+
other metadata (as per --list-archives). This option is intended for the
17+
GUI and is not needed for command-line usage.
1418

1519

1620
Tarsnap Releases

misc/bash_completion.d/tarsnap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ _tarsnap ()
5353
--check-links --checkpoint-bytes --chroot --configfile \
5454
--creationtime --csv-file --disk-pause --dry-run \
5555
--dump-config --exclude --fast-read --force-resources \
56-
--fsck --fsck-prune --humanize-numbers --include \
57-
--initialize-cachedir --insane-filesystems --iso-dates \
58-
--keep-going --keep-newer-files --keyfile \
56+
--fsck --fsck-prune --hashes --humanize-numbers \
57+
--include --initialize-cachedir --insane-filesystems \
58+
--iso-dates --keep-going --keep-newer-files --keyfile \
5959
--list-archives --lowmem --maxbw --maxbw-rate \
6060
--maxbw-rate-down --maxbw-rate-up --newer \
6161
--newer-mtime --newer-than --newer-mtime-than \

misc/describe-options.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
--force-resources force the decryption of a passphrase-encrypted keyfile
2121
--fsck perform some integrity checks and reconstruct cache MODE
2222
--fsck-prune run as --fsck, but prune any corrupt archives MODE
23+
--hashes make --list-archives print hashes
2324
--humanize-numbers use SI prefixes for --print-stats
2425
--include process only certain files or directories
2526
--initialize-cachedir create and initialize the cachedir MODE

misc/zsh_completion/_tarsnap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ _shtab_tarsnap__initialize_cachedir_options=(
231231

232232
_shtab_tarsnap__list_archives_options=(
233233
"(- : *)"{-h,--help}"[show this help message and exit]"
234+
"--hashes[make --list-archives print hashes]"
234235
"-v[produce verbose output]"
235236
"--archive-names[read a list of archive names from a file]:filename:{_files}"
236237
"--configfile[add file to the list of configuration files to be read]:filename:{_files}"

tar/bsdtar.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,9 @@ main(int argc, char **argv)
492492
/* Hack: -h by itself is the "help" command. */
493493
possible_help_request = 1;
494494
break;
495+
case OPTION_HASHES: /* tarsnap */
496+
bsdtar->option_hashes = 1;
497+
break;
495498
case OPTION_HELP: /* GNU tar, others */
496499
long_help(bsdtar);
497500
exit(0);
@@ -1229,7 +1232,7 @@ main(int argc, char **argv)
12291232
tarsnap_mode_recover(bsdtar, 0);
12301233
break;
12311234
case OPTION_LIST_ARCHIVES:
1232-
tarsnap_mode_list_archives(bsdtar);
1235+
tarsnap_mode_list_archives(bsdtar, bsdtar->option_hashes);
12331236
break;
12341237
case OPTION_NUKE:
12351238
tarsnap_mode_nuke(bsdtar);

tar/bsdtar.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ struct bsdtar {
7777
char option_dont_traverse_mounts; /* --one-file-system */
7878
char option_dryrun; /* --dry-run */
7979
char option_fast_read; /* --fast-read */
80+
int option_hashes;
8081
char option_honor_nodump; /* --nodump */
8182
char option_interactive; /* -w */
8283
char option_keep_going; /* --keep-going */
@@ -210,6 +211,7 @@ enum {
210211
OPTION_FSCK_DELETE, /* Operation mode, not a real option */
211212
OPTION_FSCK_PRUNE,
212213
OPTION_FSCK_WRITE, /* Operation mode, not a real option */
214+
OPTION_HASHES,
213215
OPTION_HELP,
214216
OPTION_INCLUDE,
215217
OPTION_INITIALIZE_CACHEDIR,
@@ -309,7 +311,7 @@ void tarsnap_mode_t(struct bsdtar *bsdtar);
309311
void tarsnap_mode_x(struct bsdtar *bsdtar);
310312
void tarsnap_mode_fsck(struct bsdtar *bsdtar, int prune, int whichkey);
311313
void tarsnap_mode_initialize_cachedir(struct bsdtar *bsdtar);
312-
void tarsnap_mode_list_archives(struct bsdtar *bsdtar);
314+
void tarsnap_mode_list_archives(struct bsdtar *bsdtar, int print_hashes);
313315
void tarsnap_mode_nuke(struct bsdtar *bsdtar);
314316
void tarsnap_mode_recover(struct bsdtar *bsdtar, int whichkey);
315317
int unmatched_inclusions(struct bsdtar *bsdtar);

tar/cmdline.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ static struct option {
9595
{ "force-resources", 0, OPTION_FORCE_RESOURCES },
9696
{ "fsck", 0, OPTION_FSCK },
9797
{ "fsck-prune", 0, OPTION_FSCK_PRUNE },
98+
{ "hashes", 0, OPTION_HASHES },
9899
{ "help", 0, OPTION_HELP },
99100
{ "humanize-numbers", 0, OPTION_HUMANIZE_NUMBERS },
100101
{ "include", 1, OPTION_INCLUDE },

tar/glue/tape.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ tarsnap_mode_print_stats(struct bsdtar *bsdtar)
170170
* Print the names of all the archives.
171171
*/
172172
void
173-
tarsnap_mode_list_archives(struct bsdtar *bsdtar)
173+
tarsnap_mode_list_archives(struct bsdtar *bsdtar, int print_hashes)
174174
{
175175
TAPE_S * d;
176176

@@ -179,7 +179,8 @@ tarsnap_mode_list_archives(struct bsdtar *bsdtar)
179179
goto err1;
180180

181181
/* Ask for the list of archives to be printed. */
182-
if (statstape_printlist(d, bsdtar->verbose, bsdtar->option_null))
182+
if (statstape_printlist(d, bsdtar->verbose, bsdtar->option_null,
183+
print_hashes))
183184
goto err2;
184185

185186
/* We're done. Close the archive set. */

tar/multitape/multitape.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,16 @@ int statstape_printglobal(TAPE_S *, const char *);
172172
int statstape_printall(TAPE_S *, const char *);
173173

174174
/**
175-
* statstape_printlist(d, verbose, print_nulls):
175+
* statstape_printlist(d, verbose, print_nulls, print_hashes):
176176
* Print the names of each of the archives in a set. If ${verbose} > 0, print
177177
* the creation times; if ${verbose} > 1, print the argument vector of the
178178
* program invocation which created the archive. If ${print_nulls} > 0, print
179179
* null character(s) between archives names and fields instead of newlines,
180-
* tabs, and spaces.
180+
* tabs, and spaces. If ${print_hashes} > 0 and ${verbose} is 0, print hashes
181+
* instead of archive names. If ${print_hashes} > 0 and ${verbose} > 0, print
182+
* hashes in addition to the normal behaviour.
181183
*/
182-
int statstape_printlist(TAPE_S *, int, int);
184+
int statstape_printlist(TAPE_S *, int, int, int);
183185

184186
/**
185187
* statstape_print(d, tapename, csv_filename):

tar/multitape/multitape_stats.c

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "chunks.h"
1212
#include "ctassert.h"
13+
#include "hexify.h"
1314
#include "multitape_internal.h"
1415
#include "storage.h"
1516
#include "sysendian.h"
@@ -243,22 +244,42 @@ print_sep(char sep, int nulls, int num)
243244
}
244245

245246
/**
246-
* statstape_printlist_item(d, tapehash, verbose, print_nulls):
247+
* statstape_printlist_item(d, tapehash, verbose, print_nulls, print_hash):
247248
* Print the name of the archive with ${tapehash}. If ${verbose} > 0, print
248249
* the creation times; if ${verbose} > 1, print the argument vector of the
249250
* program invocation which created the archive. If ${print_nulls} > 0, print
250251
* null character(s) between archives names and fields instead of newlines,
251-
* tabs, and spaces.
252+
* tabs, and spaces. If ${print_hash} > 0 and ${verbose} is 0, print the hash
253+
* instead of the archive name. If ${print_hash} > 0 and ${verbose} > 0,
254+
* print hash in addition to the normal behaviour.
252255
*/
253256
static int
254257
statstape_printlist_item(TAPE_S * d, const uint8_t tapehash[32], int verbose,
255-
int print_nulls)
258+
int print_nulls, int print_hash)
256259
{
257260
struct tapemetadata tmd;
261+
char hexstr[65];
258262
struct tm * ltime;
259263
char datebuf[DATEBUFLEN];
260264
int arg;
261265

266+
/* Print archive hash. */
267+
if (print_hash) {
268+
hexify(tapehash, hexstr, 32);
269+
fprintf(stdout, "%s", hexstr);
270+
271+
if (verbose == 0) {
272+
/* We're finished; print archive separator and quit. */
273+
if (print_sep('\n', print_nulls, 1))
274+
goto err1;
275+
goto done;
276+
} else {
277+
/* We have more fields; print field separator. */
278+
if (print_sep('\t', print_nulls, 2))
279+
goto err1;
280+
}
281+
}
282+
262283
/* Read the tape metadata. */
263284
if (multitape_metadata_get_byhash(d->SR, NULL, &tmd, tapehash, 0))
264285
goto err0;
@@ -317,6 +338,7 @@ statstape_printlist_item(TAPE_S * d, const uint8_t tapehash[32], int verbose,
317338
/* Free parsed metadata. */
318339
multitape_metadata_free(&tmd);
319340

341+
done:
320342
/* Success! */
321343
return (0);
322344

@@ -328,15 +350,17 @@ statstape_printlist_item(TAPE_S * d, const uint8_t tapehash[32], int verbose,
328350
}
329351

330352
/**
331-
* statstape_printlist(d, verbose, print_nulls):
353+
* statstape_printlist(d, verbose, print_nulls, print_hashes):
332354
* Print the names of each of the archives in a set. If ${verbose} > 0, print
333355
* the creation times; if ${verbose} > 1, print the argument vector of the
334356
* program invocation which created the archive. If ${print_nulls} > 0, print
335357
* null character(s) between archives names and fields instead of newlines,
336-
* tabs, and spaces.
358+
* tabs, and spaces. If ${print_hashes} > 0 and ${verbose} is 0, print hashes
359+
* instead of archive names. If ${print_hashes} > 0 and ${verbose} > 0, print
360+
* hashes in addition to the normal behaviour.
337361
*/
338362
int
339-
statstape_printlist(TAPE_S * d, int verbose, int print_nulls)
363+
statstape_printlist(TAPE_S * d, int verbose, int print_nulls, int print_hashes)
340364
{
341365
uint8_t * flist;
342366
size_t nfiles;
@@ -349,7 +373,7 @@ statstape_printlist(TAPE_S * d, int verbose, int print_nulls)
349373
/* Iterate through the files. */
350374
for (file = 0; file < nfiles; file++) {
351375
if (statstape_printlist_item(d, &flist[file * 32], verbose,
352-
print_nulls))
376+
print_nulls, print_hashes))
353377
goto err1;
354378
}
355379

0 commit comments

Comments
 (0)