]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
wipefs: allow storage of backups in specific location
authorThomas Weißschuh <thomas@t-8ch.de>
Sun, 31 Dec 2023 13:10:01 +0000 (14:10 +0100)
committerThomas Weißschuh <thomas@t-8ch.de>
Tue, 2 Jan 2024 10:38:34 +0000 (11:38 +0100)
This also avoids cluttering the home directory during test execution.

Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
bash-completion/wipefs
misc-utils/wipefs.8.adoc
misc-utils/wipefs.c
tests/ts/wipefs/wipefs

index 8e49a8bda1a0b5896b2a454ebbf2e1982298559a..de172725b11ef6614a1fe55f0ed91abe5adf9f2a 100644 (file)
@@ -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##*,}"
index d53b7681d96c024e73b9abcd26b4008575c89d34..fb2ba9c577f4be030d95e210139940f24860f242 100644 (file)
@@ -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-<devname>-<offset>.bak_. For more details see the *EXAMPLE* section.
+*-b*, *--backup*[=_dir_]::
+Create a signature backup to the file _wipefs-<devname>-<offset>.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.
index 132f677b14c59f5bad669bd6adfe5a56da7f409a..b89006f53b94bc47bc4c152bd90841cb74a8e380 100644 (file)
@@ -65,6 +65,7 @@ struct wipe_control {
        char            *devname;
        const char      *type_pattern;          /* -t <pattern> */
        const char      *lockmode;
+       const char      *backup;                /* location of backups */
 
        struct libscols_table *outtab;
        struct wipe_desc *offsets;              /* -o <offset> -o <offset> ... */
@@ -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 <num>  offset to erase, in bytes"), stdout);
-       fputs(_(" -O, --output <list> 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 <list>  limit the set of filesystem, RAIDs or partition tables"), stdout);
+       fputs(_(" -a, --all            wipe all magic strings (BE CAREFUL!)"), stdout);
+       fputs(_(" -b, --backup[=<dir>] create a signature backup in <dir> 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 <num>   offset to erase, in bytes"), stdout);
+       fputs(_(" -O, --output <list>  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 <list>   limit the set of filesystem, RAIDs or partition tables"), stdout);
        fprintf(stdout,
             _("     --lock[=<mode>] 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(_("<num>")));
@@ -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;
index e96f3717f078dea12be5bee609f145f7ed1a861c..0f0f8d8b14e284051e2922eeb8273d69b242d907 100755 (executable)
@@ -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