]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: make disk sync() optional
authorKarel Zak <kzak@redhat.com>
Tue, 15 Jul 2014 10:28:16 +0000 (12:28 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 15 Jul 2014 10:32:03 +0000 (12:32 +0200)
 ... this allows to avoid unnecessary sync() from cfdisk.

Reported-by: Benno Schulenberg <bensberg@justemail.net>
Signed-off-by: Karel Zak <kzak@redhat.com>
disk-utils/cfdisk.c
disk-utils/fdisk-menu.c
libfdisk/src/context.c
libfdisk/src/libfdisk.h

index fb089f580b5c9a8a8a084b1e3ae4f1293ab2528a..8a627e52e40479cd61896c3da65d4d9fa09d18ee 100644 (file)
@@ -148,6 +148,8 @@ struct cfdisk {
        size_t  lines_idx;      /* current line <0..N>, exclude header */
        size_t  page_sz;
 
+       unsigned int nwrites;   /* fdisk_write_disklabel() counter */
+
        unsigned int    wrong_order :1,         /* PT not in right order */
                        zero_start :1;          /* ignore existing partition table */
 };
@@ -1805,6 +1807,7 @@ static int main_menu_action(struct cfdisk *cf, int key)
                        fdisk_reread_partition_table(cf->cxt);
                        info = _("The partition table has been altered.");
                }
+               cf->nwrites++;
                break;
        }
        default:
@@ -2019,7 +2022,7 @@ int main(int argc, char *argv[])
        free(cf->linesbuf);
        fdisk_unref_table(cf->table);
 
-       rc = fdisk_context_deassign_device(cf->cxt);
+       rc = fdisk_context_deassign_device(cf->cxt, cf->nwrites == 0);
        fdisk_free_context(cf->cxt);
        DBG(FRONTEND, ul_debug("bye! [rc=%d]", rc));
        return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
index c688fdfef6a6fd6646e76d4b61aadcd28102530f..f8ae95de24a7337309189db29787efc59e14ec63 100644 (file)
@@ -459,7 +459,7 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
                fdisk_info(cxt, _("The partition table has been altered."));
                rc = fdisk_reread_partition_table(cxt);
                if (!rc)
-                       rc = fdisk_context_deassign_device(cxt);
+                       rc = fdisk_context_deassign_device(cxt, 0);
                /* fallthrough */
        case 'q':
                fdisk_free_context(cxt);
index 2e224b363e998c041da8d038b57c6916ff456a51..61a6ce30a9e73c4732264109d950dbca5916203f 100644 (file)
@@ -294,19 +294,24 @@ fail:
        return -errno;
 }
 
-int fdisk_context_deassign_device(struct fdisk_context *cxt)
+int fdisk_context_deassign_device(struct fdisk_context *cxt, int nosync)
 {
        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;
-       }
+       if (cxt->readonly || nosync)
+               close(cxt->dev_fd);
 
-       fdisk_info(cxt, _("Syncing disks."));
-       sync();
+       else {
+               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;
 }
index 4143d9bc34843328a82204aa51fea28e6314a698..a717f4ea3e4cbbf2fbf608b1f3f3124743c66e96 100644 (file)
@@ -80,7 +80,7 @@ extern int fdisk_context_set_ask(struct fdisk_context *cxt,
 extern int fdisk_context_is_readonly(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 int fdisk_context_deassign_device(struct fdisk_context *cxt, int nosync);
 
 extern struct fdisk_label *fdisk_context_get_label(struct fdisk_context *cxt,
                                const char *name);