]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: use CDROM_GET_CAPABILITY ioctl
authorDavidlohr Bueso <dave@gnu.org>
Tue, 20 Dec 2011 13:42:10 +0000 (14:42 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 2 Jan 2012 12:43:06 +0000 (13:43 +0100)
And replace the current archaic logic of is_ide_cdrom_or_tape().

Signed-off-by: Davidlohr Bueso <dave@gnu.org>
fdisk/fdisk.c
fdisk/sfdisk.c
include/blkdev.h
lib/blkdev.c

index c41da7a6e68fe5532e690a3acedafcd9c7787c07..d967d27330bad3201a2e7a3d0e027c826ea96b37 100644 (file)
@@ -2746,37 +2746,16 @@ expert_command_prompt(void)
        }
 }
 
-static int
-is_ide_cdrom_or_tape(char *device) {
-       FILE *procf;
-       char buf[100];
-       struct stat statbuf;
-       int is_ide = 0;
-
-       /* No device was given explicitly, and we are trying some
-          likely things.  But opening /dev/hdc may produce errors like
-           "hdc: tray open or drive not ready"
-          if it happens to be a CD-ROM drive. It even happens that
-          the process hangs on the attempt to read a music CD.
-          So try to be careful. This only works since 2.1.73. */
+static int is_ide_cdrom_or_tape(char *device)
+{
+       int fd, ret;
 
-       if (strncmp("/dev/hd", device, 7))
+       if (fd = open(device, O_RDONLY) < 0)
                return 0;
+       ret = blkdev_is_cdrom(fd);
 
-       snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
-       procf = fopen(buf, "r");
-       if (procf != NULL && fgets(buf, sizeof(buf), procf))
-               is_ide = (!strncmp(buf, "cdrom", 5) ||
-                         !strncmp(buf, "tape", 4));
-       else
-               /* Now when this proc file does not exist, skip the
-                  device when it is read-only. */
-               if (stat(device, &statbuf) == 0)
-                       is_ide = ((statbuf.st_mode & 0222) == 0);
-
-       if (procf)
-               fclose(procf);
-       return is_ide;
+       close(fd);
+       return ret;
 }
 
 static void
index db1a6d7b12c34553245d9361349aae78795e5cf2..bcedad73da97935bd29c1499f93556849203759c 100644 (file)
@@ -57,6 +57,7 @@
 #include "gpt.h"
 #include "pathnames.h"
 #include "canonicalize.h"
+#include "blkdev.h"
 
 /*
  * Table of contents:
@@ -2532,36 +2533,16 @@ static const struct option long_opts[] = {
     { NULL, 0, NULL, 0 }
 };
 
-static int
-is_ide_cdrom_or_tape(char *device) {
-    FILE *procf;
-    char buf[100];
-    struct stat statbuf;
-    int is_ide = 0;
-
-    /* No device was given explicitly, and we are trying some
-       likely things.  But opening /dev/hdc may produce errors like
-       "hdc: tray open or drive not ready"
-       if it happens to be a CD-ROM drive. It even happens that
-       the process hangs on the attempt to read a music CD.
-       So try to be careful. This only works since 2.1.73. */
+static int is_ide_cdrom_or_tape(char *device)
+{
+       int fd, ret;
 
-    if (strncmp("/dev/hd", device, 7))
-       return 0;
+       if (fd = open(device, O_RDONLY) < 0)
+               return 0;
+       ret = blkdev_is_cdrom(fd);
 
-    snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device + 5);
-    procf = fopen(buf, "r");
-    if (procf != NULL && fgets(buf, sizeof(buf), procf))
-       is_ide = (!strncmp(buf, "cdrom", 5) || !strncmp(buf, "tape", 4));
-    else
-       /* Now when this proc file does not exist, skip the
-          device when it is read-only. */
-    if (stat(device, &statbuf) == 0)
-       is_ide = ((statbuf.st_mode & 0222) == 0);
-
-    if (procf)
-       fclose(procf);
-    return is_ide;
+       close(fd);
+       return ret;
 }
 
 static char *
index 1e7409dfd28e58e70b76d9ace6531efc19566269..1a9119d43d9cda1abbbc8aae7b819eca21dc0ef9 100644 (file)
 # ifdef __linux__
 #  define HDIO_GETGEO 0x0301
 # endif
+
+/* uniform CD-ROM information */
+#ifndef CDROM_GET_CAPABILITY
+# define CDROM_GET_CAPABILITY 0x5331
+#endif
+
 struct hd_geometry {
        unsigned char heads;
        unsigned char sectors;
@@ -98,4 +104,7 @@ int blkdev_is_misaligned(int fd);
 /* get physical block device size */
 int blkdev_get_physector_size(int fd, int *sector_size);
 
+/* is the device cdrom capable? */
+int blkdev_is_cdrom(int fd);
+
 #endif /* BLKDEV_H */
index 3f652bb6029a6fbd96d0ecc8beb8777abed7b6ae..9138b0d854b82de0c61d7ee4bfe9845b1eabb904 100644 (file)
@@ -243,6 +243,19 @@ int blkdev_is_misaligned(int fd)
 #endif
 }
 
+int blkdev_is_cdrom(int fd)
+{
+#ifdef CDROM_GET_CAPABILITY
+       int ret;
+
+       if ((ret = ioctl(fd, CDROM_GET_CAPABILITY, NULL)) < 0)
+               return 0;
+       else
+               return ret;
+#else
+       return 0;
+#endif
+}
 
 #ifdef TEST_PROGRAM
 #include <stdio.h>