]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: simplify device opening
authorDavidlohr Bueso <dave@gnu.org>
Mon, 23 Apr 2012 11:07:16 +0000 (13:07 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 23 Apr 2012 11:07:16 +0000 (13:07 +0200)
This patch makes fdisk open(2) the device in only one place (get_boot), instead
of having to depend on user input (ie: listing -l).

Signed-off-by: Davidlohr Bueso <dave@gnu.org>
fdisk/fdisk.c

index 81e9236e750e2dc8dd2969637f77f47c86b41947..3c2c1419ad669dfa43c84866b257ab8d368a96b1 100644 (file)
@@ -1096,13 +1096,18 @@ static int get_boot(int try_only) {
        disklabel = ANY_LABEL;
        memset(MBRbuffer, 0, 512);
 
-       if (!try_only) {
+       if (try_only && (fd = open(disk_device, O_RDONLY)) < 0) {
+               fprintf(stderr, _("Cannot open %s\n"), disk_device);
+               fatal(unable_to_open);
+       }
+       else {
                if ((fd = open(disk_device, O_RDWR)) < 0) {
+                       /* ok, can we read-only the device? */
                        if ((fd = open(disk_device, O_RDONLY)) < 0)
                                fatal(unable_to_open);
                        else
                                printf(_("You will not be able to write "
-                                           "the partition table.\n"));
+                                        "the partition table.\n"));
                }
        }
 
@@ -1113,7 +1118,6 @@ static int get_boot(int try_only) {
        }
 
        get_geometry(fd, NULL);
-
        update_units();
 
        if (!check_dos_label())
@@ -2707,26 +2711,15 @@ print_partition_table_from_option(char *device)
        if (setjmp(listingbuf))
                return;
        gpt_warning(device);
-       if ((fd = open(disk_device, O_RDONLY)) >= 0) {
-               gb = get_boot(1);
-               if (gb > 0) { /* I/O error */
-               } else if (gb < 0) { /* no DOS signature */
-                       list_disk_geometry();
-                       if (disklabel != AIX_LABEL && disklabel != MAC_LABEL)
-                               btrydev(device);
-               } else {
-                       list_table(0);
-               }
-               close(fd);
-       } else {
-               /* Ignore other errors, since we try IDE
-                  and SCSI hard disks which may not be
-                  installed on the system. */
-               if (errno == EACCES) {
-                       fprintf(stderr, _("Cannot open %s\n"), device);
-                       return;
-               }
-       }
+       gb = get_boot(1);
+       if (gb < 0) { /* no DOS signature */
+               list_disk_geometry();
+               if (disklabel != AIX_LABEL && disklabel != MAC_LABEL)
+                       btrydev(device);
+       }
+       else if (!gb)
+               list_table(0);
+       close(fd);
 }
 
 /*