From: Karel Zak Date: Tue, 15 Jul 2014 10:28:16 +0000 (+0200) Subject: libfdisk: make disk sync() optional X-Git-Tag: v2.25~68 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bc787727d4a114d5f8926fb9c22fb11c40d1d8c0;p=thirdparty%2Futil-linux.git libfdisk: make disk sync() optional ... this allows to avoid unnecessary sync() from cfdisk. Reported-by: Benno Schulenberg Signed-off-by: Karel Zak --- diff --git a/disk-utils/cfdisk.c b/disk-utils/cfdisk.c index fb089f580b..8a627e52e4 100644 --- a/disk-utils/cfdisk.c +++ b/disk-utils/cfdisk.c @@ -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; diff --git a/disk-utils/fdisk-menu.c b/disk-utils/fdisk-menu.c index c688fdfef6..f8ae95de24 100644 --- a/disk-utils/fdisk-menu.c +++ b/disk-utils/fdisk-menu.c @@ -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); diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c index 2e224b363e..61a6ce30a9 100644 --- a/libfdisk/src/context.c +++ b/libfdisk/src/context.c @@ -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; } diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index 4143d9bc34..a717f4ea3e 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -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);