From: Thomas Weißschuh Date: Sun, 31 Dec 2023 13:10:01 +0000 (+0100) Subject: wipefs: allow storage of backups in specific location X-Git-Tag: v2.40-rc1~81^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2295e66361c82eb371d017f993aad4a2c6a3c6ea;p=thirdparty%2Futil-linux.git wipefs: allow storage of backups in specific location This also avoids cluttering the home directory during test execution. Signed-off-by: Thomas Weißschuh --- diff --git a/bash-completion/wipefs b/bash-completion/wipefs index 8e49a8bda1..de172725b1 100644 --- a/bash-completion/wipefs +++ b/bash-completion/wipefs @@ -5,6 +5,10 @@ _wipefs_module() cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" case $prev in + '-b'|'--backup') + COMPREPLY=( $(compgen -o dirnames -- ${cur:-"/"}) ) + return 0 + ;; '-O'|'--output') local prefix realcur OUTPUT_ALL OUTPUT realcur="${cur##*,}" diff --git a/misc-utils/wipefs.8.adoc b/misc-utils/wipefs.8.adoc index d53b7681d9..fb2ba9c577 100644 --- a/misc-utils/wipefs.8.adoc +++ b/misc-utils/wipefs.8.adoc @@ -40,8 +40,8 @@ Note that by default *wipefs* does not erase nested partition tables on non-whol *-a*, *--all*:: Erase all available signatures. The set of erased signatures can be restricted with the *-t* option. -*-b*, *--backup*:: -Create a signature backup to the file _$HOME/wipefs--.bak_. For more details see the *EXAMPLE* section. +*-b*, *--backup*[=_dir_]:: +Create a signature backup to the file _wipefs--.bak_ in _$HOME_ or the directory specified as the optional argument. For more details see the *EXAMPLE* section. *-f*, *--force*:: Force erasure, even if the filesystem is mounted. This is required in order to erase a partition-table signature on a block device. diff --git a/misc-utils/wipefs.c b/misc-utils/wipefs.c index 132f677b14..b89006f53b 100644 --- a/misc-utils/wipefs.c +++ b/misc-utils/wipefs.c @@ -65,6 +65,7 @@ struct wipe_control { char *devname; const char *type_pattern; /* -t */ const char *lockmode; + const char *backup; /* location of backups */ struct libscols_table *outtab; struct wipe_desc *offsets; /* -o -o ... */ @@ -77,7 +78,6 @@ struct wipe_control { unsigned int noact : 1, all : 1, quiet : 1, - backup : 1, force : 1, json : 1, no_headings : 1, @@ -535,12 +535,9 @@ static int do_wipe(struct wipe_control *ctl) } if (ctl->backup) { - const char *home = getenv ("HOME"); char *tmp = xstrdup(ctl->devname); - if (!home) - errx(EXIT_FAILURE, _("failed to create a signature backup, $HOME undefined")); - xasprintf (&backup, "%s/wipefs-%s-", home, basename(tmp)); + xasprintf(&backup, "%s/wipefs-%s-", ctl->backup, basename(tmp)); free(tmp); } @@ -636,21 +633,21 @@ usage(void) fputs(_("Wipe signatures from a device."), stdout); fputs(USAGE_OPTIONS, stdout); - fputs(_(" -a, --all wipe all magic strings (BE CAREFUL!)"), stdout); - fputs(_(" -b, --backup create a signature backup in $HOME"), stdout); - fputs(_(" -f, --force force erasure"), stdout); - fputs(_(" -i, --noheadings don't print headings"), stdout); - fputs(_(" -J, --json use JSON output format"), stdout); - fputs(_(" -n, --no-act do everything except the actual write() call"), stdout); - fputs(_(" -o, --offset offset to erase, in bytes"), stdout); - fputs(_(" -O, --output COLUMNS to display (see below)"), stdout); - fputs(_(" -p, --parsable print out in parsable instead of printable format"), stdout); - fputs(_(" -q, --quiet suppress output messages"), stdout); - fputs(_(" -t, --types limit the set of filesystem, RAIDs or partition tables"), stdout); + fputs(_(" -a, --all wipe all magic strings (BE CAREFUL!)"), stdout); + fputs(_(" -b, --backup[=] create a signature backup in or $HOME"), stdout); + fputs(_(" -f, --force force erasure"), stdout); + fputs(_(" -i, --noheadings don't print headings"), stdout); + fputs(_(" -J, --json use JSON output format"), stdout); + fputs(_(" -n, --no-act do everything except the actual write() call"), stdout); + fputs(_(" -o, --offset offset to erase, in bytes"), stdout); + fputs(_(" -O, --output COLUMNS to display (see below)"), stdout); + fputs(_(" -p, --parsable print out in parsable instead of printable format"), stdout); + fputs(_(" -q, --quiet suppress output messages"), stdout); + fputs(_(" -t, --types limit the set of filesystem, RAIDs or partition tables"), stdout); fprintf(stdout, _(" --lock[=] use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock"); - fprintf(stdout, USAGE_HELP_OPTIONS(21)); + fprintf(stdout, USAGE_HELP_OPTIONS(22)); fputs(USAGE_ARGUMENTS, stdout); fprintf(stdout, USAGE_ARG_SIZE(_(""))); @@ -676,7 +673,7 @@ main(int argc, char **argv) }; static const struct option longopts[] = { { "all", no_argument, NULL, 'a' }, - { "backup", no_argument, NULL, 'b' }, + { "backup", optional_argument, NULL, 'b' }, { "force", no_argument, NULL, 'f' }, { "help", no_argument, NULL, 'h' }, { "lock", optional_argument, NULL, OPT_LOCK }, @@ -703,7 +700,7 @@ main(int argc, char **argv) textdomain(PACKAGE); close_stdout_atexit(); - while ((c = getopt_long(argc, argv, "abfhiJnO:o:pqt:V", longopts, NULL)) != -1) { + while ((c = getopt_long(argc, argv, "ab::fhiJnO:o:pqt:V", longopts, NULL)) != -1) { err_exclusive_options(c, longopts, excl, excl_st); @@ -712,7 +709,14 @@ main(int argc, char **argv) ctl.all = 1; break; case 'b': - ctl.backup = 1; + if (optarg) { + ctl.backup = optarg; + } else { + ctl.backup = getenv("HOME"); + if (!ctl.backup) + errx(EXIT_FAILURE, + _("failed to create a signature backup, $HOME undefined")); + } break; case 'f': ctl.force = 1; diff --git a/tests/ts/wipefs/wipefs b/tests/ts/wipefs/wipefs index e96f3717f0..0f0f8d8b14 100755 --- a/tests/ts/wipefs/wipefs +++ b/tests/ts/wipefs/wipefs @@ -10,17 +10,19 @@ ts_check_test_command "$TS_CMD_WIPEFS" ts_check_test_command "$TS_CMD_MKMINIX" img="$(ts_image_init 50)" +backups="$TS_OUTDIR/backups" "$TS_CMD_MKMINIX" "$img" &>/dev/null # remove old backups -rm -rf $HOME/wipefs-$(basename ${img})-*.bak +rm -rf "$backups" +mkdir -p "$backups" #remove the magic byte, back it up -$TS_CMD_WIPEFS --all --backup ${img} &>/dev/null || ts_die "wipefs failed" +$TS_CMD_WIPEFS --all --backup="${backups}" ${img} &>/dev/null || ts_die "wipefs failed" #there should be just one magic string/backup file in this case -INFILE=$(ls $HOME/wipefs-$(basename ${img})-*.bak) +INFILE=$(ls $backups/wipefs-$(basename ${img})-*.bak) OFFT=$(echo $INFILE | sed 's/^.*-\(.*\).bak$/\1/') dd if=$INFILE of=$img bs=1 conv=notrunc seek=$(($OFFT)) &>/dev/null