From 69f30c31261190fdf2ddf7001ebc3ec03bae4a5c Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 6 Sep 2019 14:17:28 +0200 Subject: [PATCH] sfdisk: (--move-data) make log optional 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 --- disk-utils/sfdisk.8 | 7 ++--- disk-utils/sfdisk.c | 64 +++++++++++++++++++++++++-------------------- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/disk-utils/sfdisk.8 b/disk-utils/sfdisk.8 index 0f8a1dfc7b..c572145fb3 100644 --- a/disk-utils/sfdisk.8 +++ b/disk-utils/sfdisk.8 @@ -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-.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-.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 diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c index c8efb5280b..e7481aab79 100644 --- a/disk-utils/sfdisk.c +++ b/disk-utils/sfdisk.c @@ -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 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 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); -- 2.47.2