]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
sfdisk: (--move-data) make log optional
authorKarel Zak <kzak@redhat.com>
Fri, 6 Sep 2019 12:17:28 +0000 (14:17 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 6 Sep 2019 13:07:30 +0000 (15:07 +0200)
The log may be pretty huge and very probably not used by many users.
Let's make it optional.

The patch also clean up move-data output messages.

Addresses: https://github.com/karelzak/util-linux/issues/848
Signed-off-by: Karel Zak <kzak@redhat.com>
disk-utils/sfdisk.8
disk-utils/sfdisk.c

index 0f8a1dfc7b5ba218bb51c788711107c32f4777d7..c572145fb3876adb6d79ecf69cd9fa5fcab51822 100644 (file)
@@ -210,9 +210,10 @@ of a partition to another place on the disk.  The size of the partition has
 to remain the same, the new and old location may overlap.  This option requires
 option \fB\-N\fR in order to be processed on one specific partition only.
 
-The \fIpath\fR overrides the default log file name
-(the default is ~/sfdisk-<devname>.move).  The log file contains information
-about all read/write operations on the partition data.
+The optional \fIpath\fR specifies log file name. The log file contains information
+about all read/write operations on the partition data. The word "@default" as
+a \fIpath\fR forces sfdisk to use ~/sfdisk-<devname>.move for the log.  The log is
+optional since v2.35.
 
 Note that this operation is risky and not atomic. \fBDon't forget to backup your data!\fR
 
index c8efb5280bf9bb19470aa890722b3c58a06c2c8c..e7481aab79dab7fcb4280192cdfe68527719ce4b 100644 (file)
@@ -323,7 +323,7 @@ static char *mk_backup_filename_tpl(const char *filename, const char *devname, c
 
        name = basename(buf);
 
-       if (!filename) {
+       if (!filename || strcmp(filename, "@default") == 0) {
                const char *home = getenv ("HOME");
                if (!home)
                        errx(EXIT_FAILURE, _("failed to create a backup file, $HOME undefined"));
@@ -436,16 +436,20 @@ static int move_partition_data(struct sfdisk *sf, size_t partno, struct fdisk_pa
                posix_fadvise(fd, from * ss, nsectors * ss, POSIX_FADV_SEQUENTIAL);
 #endif
        devname = fdisk_partname(fdisk_get_devname(sf->cxt), partno+1);
-       typescript = mk_backup_filename_tpl(sf->move_typescript, devname, ".move");
+       if (sf->move_typescript)
+               typescript = mk_backup_filename_tpl(sf->move_typescript, devname, ".move");
 
        if (!sf->quiet) {
                fdisk_info(sf->cxt,"");
                color_scheme_enable("header", UL_COLOR_BOLD);
                fdisk_info(sf->cxt, sf->noact ? _("Data move: (--no-act)") : _("Data move:"));
                color_disable();
-               fdisk_info(sf->cxt, _(" typescript file: %s"), typescript);
-               printf(_(" old start: %ju, new start: %ju (move %ju sectors)\n"),
-                       (uintmax_t) from, (uintmax_t) to, (uintmax_t) nsectors);
+               if (typescript)
+                       fdisk_info(sf->cxt, _(" typescript file: %s"), typescript);
+               printf(_("  start sector: (from/to) %ju / %ju\n"), (uintmax_t) from, (uintmax_t) to);
+               printf(_("  sectors: %ju\n"), (uintmax_t) nsectors);
+               printf(_("  step size: %zu bytes\n"), step_bytes);
+               putchar('\n');
                fflush(stdout);
        }
 
@@ -458,28 +462,30 @@ static int move_partition_data(struct sfdisk *sf, size_t partno, struct fdisk_pa
                }
        }
 
-       f = fopen(typescript, "w");
-       if (!f) {
-               fdisk_warn(sf->cxt, _("cannot open %s"), typescript);
-               goto fail;
-       }
+       if (typescript) {
+               f = fopen(typescript, "w");
+               if (!f) {
+                       fdisk_warn(sf->cxt, _("cannot open %s"), typescript);
+                       goto fail;
+               }
 
-       /* don't translate */
-       fprintf(f, "# sfdisk: " PACKAGE_STRING "\n");
-       fprintf(f, "# Disk: %s\n", devname);
-       fprintf(f, "# Partition: %zu\n", partno + 1);
-       fprintf(f, "# Operation: move data\n");
-       fprintf(f, "# Original start offset (sectors/bytes): %ju/%ju\n",
-               (uintmax_t)from, (uintmax_t)from * ss);
-       fprintf(f, "# New start offset (sectors/bytes): %ju/%ju\n",
-               (uintmax_t)to, (uintmax_t)to * ss);
-       fprintf(f, "# Area size (sectors/bytes): %ju/%ju\n",
-               (uintmax_t)nsectors, (uintmax_t)nsectors * ss);
-       fprintf(f, "# Sector size: %zu\n", ss);
-       fprintf(f, "# Step size (in bytes): %zu\n", step_bytes);
-       fprintf(f, "# Steps: %ju\n", (uintmax_t)(nsectors / step));
-       fprintf(f, "#\n");
-       fprintf(f, "# <step>: <from> <to> (step offsets in bytes)\n");
+               /* don't translate */
+               fprintf(f, "# sfdisk: " PACKAGE_STRING "\n");
+               fprintf(f, "# Disk: %s\n", devname);
+               fprintf(f, "# Partition: %zu\n", partno + 1);
+               fprintf(f, "# Operation: move data\n");
+               fprintf(f, "# Original start offset (sectors/bytes): %ju/%ju\n",
+                       (uintmax_t)from, (uintmax_t)from * ss);
+               fprintf(f, "# New start offset (sectors/bytes): %ju/%ju\n",
+                       (uintmax_t)to, (uintmax_t)to * ss);
+               fprintf(f, "# Area size (sectors/bytes): %ju/%ju\n",
+                       (uintmax_t)nsectors, (uintmax_t)nsectors * ss);
+               fprintf(f, "# Sector size: %zu\n", ss);
+               fprintf(f, "# Step size (in bytes): %zu\n", step_bytes);
+               fprintf(f, "# Steps: %ju\n", (uintmax_t)(nsectors / step));
+               fprintf(f, "#\n");
+               fprintf(f, "# <step>: <from> <to> (step offsets in bytes)\n");
+       }
 
        src = (backward ? from + nsectors : from) * ss;
        dst = (backward ? to + nsectors : to) * ss;
@@ -513,7 +519,8 @@ static int move_partition_data(struct sfdisk *sf, size_t partno, struct fdisk_pa
                }
 
                /* write log */
-               fprintf(f, "%05zu: %12ju %12ju\n", cc, src, dst);
+               if (f)
+                       fprintf(f, "%05zu: %12ju %12ju\n", cc, src, dst);
 
 #if defined(POSIX_FADV_DONTNEED) && defined(HAVE_POSIX_FADVISE)
                if (!sf->noact)
@@ -523,7 +530,8 @@ static int move_partition_data(struct sfdisk *sf, size_t partno, struct fdisk_pa
                        src += step_bytes, dst += step_bytes;
        }
 
-       fclose(f);
+       if (f)
+               fclose(f);
        free(buf);
        free(devname);
        free(typescript);