]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: move sync() and reread PT code from fdisk to libfdisk
authorKarel Zak <kzak@redhat.com>
Fri, 12 Jul 2013 09:57:44 +0000 (11:57 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 16 Sep 2013 14:47:08 +0000 (16:47 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
fdisks/fdisk-menu.c
fdisks/fdisk.c
libfdisk/src/alignment.c
libfdisk/src/context.c
libfdisk/src/libfdisk.h

index b28b34cb0be66d50299b1345924f1ada11f8c030..3a5a57434752124935b21e4883e85242c976ab57 100644 (file)
@@ -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;
index 44d397caf70ce89e2a76e08cae113441f4dd5eff..45df92906de55568da92e485fd0e885b594c9f13 100644 (file)
@@ -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
index 9e7d33b67b29061d9c4acbce7bfc032a694fbafe..6df785a9474fc6e5213e3b024f8b0fcf24e7cdd5 100644 (file)
@@ -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;
+}
index dc79783c428244e075a4ec5e64c2a2a64292d099..c5d2329cf637663a2873b34c561fbeed80a7abe7 100644 (file)
@@ -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
index e9d5cc12cab561ac8e9a51e7441623ce6911356f..44f2a914658716f8893845dd926883e56d9bd595 100644 (file)
@@ -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);