]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
sfdisk: cleanup sync() usage, fix --activate
authorKarel Zak <kzak@redhat.com>
Wed, 17 Sep 2014 10:56:39 +0000 (12:56 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 7 Oct 2014 12:55:32 +0000 (14:55 +0200)
 * use sync() only if we change disk layout
 * don't use sync() when we change details (like partition type)

Signed-off-by: Karel Zak <kzak@redhat.com>
disk-utils/sfdisk.c

index 18a646bb0bf40840303146af06ddd029eadcafed..348c9ce93d0adeb549b7e3b66106c16a857a8010 100644 (file)
@@ -360,24 +360,25 @@ static int command_show_size(struct sfdisk *sf __attribute__((__unused__)),
  */
 static int command_activate(struct sfdisk *sf, int argc, char **argv)
 {
-       int rc, nparts, i;
+       int rc, nparts, i, listonly;
        struct fdisk_partition *pa = NULL;
        const char *devname = NULL;
 
-       if (argc)
+       if (argc < 1)
                errx(EXIT_FAILURE, _("no disk device specified"));
        devname = argv[0];
 
-       if (argc > 2)
-               errx(EXIT_FAILURE, _("uneexpected arguments"));
+       /*  --activate <device> */
+       listonly = argc == 1;
 
-       rc = fdisk_assign_device(sf->cxt, devname, 0);
+       rc = fdisk_assign_device(sf->cxt, devname, listonly);
        if (rc)
                err(EXIT_FAILURE, _("cannot open %s"), devname);
 
        if (!fdisk_is_label(sf->cxt, DOS))
                errx(EXIT_FAILURE, _("toggle boot flags is supported for MBR only"));
 
+
        nparts = fdisk_get_npartitions(sf->cxt);
        for (i = 0; i < nparts; i++) {
                char *data = NULL;
@@ -388,7 +389,7 @@ static int command_activate(struct sfdisk *sf, int argc, char **argv)
                        continue;
 
                /* sfdisk --activate  list bootable partitions */
-               if (argc == 1) {
+               if (listonly) {
                        if (!fdisk_partition_is_bootable(pa))
                                continue;
                        if (fdisk_partition_to_string(pa, sf->cxt,
@@ -414,7 +415,9 @@ static int command_activate(struct sfdisk *sf, int argc, char **argv)
        }
 
        fdisk_unref_partition(pa);
-       rc = fdisk_write_disklabel(sf->cxt);
+
+       if (!listonly)
+               rc = fdisk_write_disklabel(sf->cxt);
        if (!rc)
                rc = fdisk_deassign_device(sf->cxt, 1);
        return rc;
@@ -449,6 +452,7 @@ static int command_dump(struct sfdisk *sf, int argc, char **argv)
        fdisk_script_write_file(dp, stdout);
 
        fdisk_unref_script(dp);
+       fdisk_deassign_device(sf->cxt, 1);
        return 0;
 }
 
@@ -528,7 +532,7 @@ static int command_parttype(struct sfdisk *sf, int argc, char **argv)
        if (!rc)
                rc = fdisk_write_disklabel(sf->cxt);
        if (!rc)
-               rc = fdisk_deassign_device(sf->cxt, 0);
+               rc = fdisk_deassign_device(sf->cxt, 1);
        return rc;
 }