From a57639e1f83149111e9855f3a042630117fe9736 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 12 Jul 2013 11:57:44 +0200 Subject: libfdisk: move sync() and reread PT code from fdisk to libfdisk Signed-off-by: Karel Zak --- fdisks/fdisk-menu.c | 10 ++++++---- fdisks/fdisk.c | 35 ----------------------------------- libfdisk/src/alignment.c | 32 ++++++++++++++++++++++++++++++++ libfdisk/src/context.c | 17 +++++++++++++++++ libfdisk/src/libfdisk.h | 3 ++- 5 files changed, 57 insertions(+), 40 deletions(-) diff --git a/fdisks/fdisk-menu.c b/fdisks/fdisk-menu.c index b28b34cb0..3a5a57434 100644 --- a/fdisks/fdisk-menu.c +++ b/fdisks/fdisk-menu.c @@ -413,12 +413,14 @@ static int generic_menu_cb(struct fdisk_context **cxt0, if (cxt->parent) break; /* nested PT, don't leave */ fdisk_info(cxt, _("The partition table has been altered.")); - reread_partition_table(cxt, 1); - break; + rc = fdisk_reread_partition_table(cxt); + if (!rc) + rc = fdisk_context_deassign_device(cxt); + /* fallthrough */ case 'q': fdisk_free_context(cxt); - printf("\n"); - exit(EXIT_SUCCESS); + fputc('\n', stdout); + exit(rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE); case 'm': rc = print_fdisk_menu(cxt); break; diff --git a/fdisks/fdisk.c b/fdisks/fdisk.c index 44d397caf..45df92906 100644 --- a/fdisks/fdisk.c +++ b/fdisks/fdisk.c @@ -232,41 +232,6 @@ void list_disk_geometry(struct fdisk_context *cxt) printf("\n"); } -void -reread_partition_table(struct fdisk_context *cxt, int leave) { - int i; - struct stat statbuf; - - i = fstat(cxt->dev_fd, &statbuf); - if (i == 0 && S_ISBLK(statbuf.st_mode)) { - sync(); -#ifdef BLKRRPART - printf(_("Calling ioctl() to re-read partition table.\n")); - i = ioctl(cxt->dev_fd, BLKRRPART); -#else - errno = ENOSYS; - i = 1; -#endif - } - - if (i) { - printf(_("\nWARNING: Re-reading the partition table failed with error %d: %m.\n" - "The kernel still uses the old table. The new table will be used at\n" - "the next reboot or after you run partprobe(8) or kpartx(8)\n"), - errno); - } - - if (leave) { - if (fsync(cxt->dev_fd) || close(cxt->dev_fd)) { - fprintf(stderr, _("\nError closing file\n")); - exit(1); - } - - printf(_("Syncing disks.\n")); - sync(); - exit(!!i); - } -} #define MAX_PER_LINE 16 static void diff --git a/libfdisk/src/alignment.c b/libfdisk/src/alignment.c index 9e7d33b67..6df785a94 100644 --- a/libfdisk/src/alignment.c +++ b/libfdisk/src/alignment.c @@ -484,3 +484,35 @@ sector_t fdisk_scround(struct fdisk_context *cxt, sector_t num) sector_t un = fdisk_context_get_units_per_sector(cxt); return (num + un - 1) / un; } + +int fdisk_reread_partition_table(struct fdisk_context *cxt) +{ + int i; + struct stat statbuf; + + assert(cxt); + assert(cxt->dev_fd >= 0); + + i = fstat(cxt->dev_fd, &statbuf); + if (i == 0 && S_ISBLK(statbuf.st_mode)) { + sync(); +#ifdef BLKRRPART + fdisk_info(cxt, _("Calling ioctl() to re-read partition table.")); + i = ioctl(cxt->dev_fd, BLKRRPART); +#else + errno = ENOSYS; + i = 1; +#endif + } + + if (i) { + fdisk_warn(cxt, _("Re-reading the partition table failed.")); + fdisk_info(cxt, _( + "The kernel still uses the old table. The " + "new table will be used at the next reboot " + "or after you run partprobe(8) or kpartx(8).")); + return -errno; + } + + return 0; +} diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c index dc79783c4..c5d2329cf 100644 --- a/libfdisk/src/context.c +++ b/libfdisk/src/context.c @@ -200,6 +200,23 @@ fail: return -errno; } +int fdisk_context_deassign_device(struct fdisk_context *cxt) +{ + assert(cxt); + assert(cxt->dev_fd >= 0); + + if (fsync(cxt->dev_fd) || close(cxt->dev_fd)) { + fdisk_warn(cxt, _("%s: close device failed"), cxt->dev_path); + return -errno; + } + + fdisk_info(cxt, _("Syncing disks.")); + sync(); + + cxt->dev_fd = -1; + return 0; +} + /** * fdisk_free_context: * @cxt: fdisk context diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index e9d5cc12c..44f2a9146 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -79,6 +79,7 @@ extern int fdisk_context_set_ask(struct fdisk_context *cxt, extern int fdisk_context_assign_device(struct fdisk_context *cxt, const char *fname, int readonly); +extern int fdisk_context_deassign_device(struct fdisk_context *cxt); extern struct fdisk_label *fdisk_context_get_label(struct fdisk_context *cxt, const char *name); @@ -152,7 +153,7 @@ extern int fdisk_save_user_geometry(struct fdisk_context *cxt, extern int fdisk_save_user_sector_size(struct fdisk_context *cxt, unsigned int phy, unsigned int log); - +extern int fdisk_reread_partition_table(struct fdisk_context *cxt); /* dos.c */ extern int fdisk_dos_enable_compatible(struct fdisk_label *lb, int enable); -- cgit v1.2.3-55-g7522