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;
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
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;
+}
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
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);
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);