Whamcloud - gitweb
Fix FreeBSD portability problem caused by it using character mode disk devices
authorTheodore Ts'o <tytso@mit.edu>
Mon, 28 Aug 2017 22:04:11 +0000 (18:04 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Mon, 28 Aug 2017 22:17:26 +0000 (18:17 -0400)
We were using S_ISBLK() to test if a device could be used as a disk
device.  This doesn't work for FreeBSD.  We need to test for S_ISBLK()
|| S_ISCHR().

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/blkid/blkidP.h
lib/blkid/devname.c
lib/blkid/devno.c
lib/blkid/getsize.c
lib/ext2fs/blkmap64_rb.c
lib/ext2fs/ext2fsP.h
lib/ext2fs/finddev.c
lib/ext2fs/ismounted.c
lib/ext2fs/unix_io.c
misc/e2image.c

index b90bfed..b3fe4a6 100644 (file)
 
 #include <sys/types.h>
 #include <stdio.h>
+#if HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#if HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
 
 #include <blkid/blkid.h>
 
@@ -152,6 +158,15 @@ extern void blkid_debug_dump_dev(blkid_dev dev);
 extern void blkid_debug_dump_tag(blkid_tag tag);
 #endif
 
+static inline int blkidP_is_disk_device(mode_t mode)
+{
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
+       return S_ISBLK(mode) || S_ISCHR(mode);
+#else
+       return S_ISBLK(mode);
+#endif
+}
+
 /* devno.c */
 struct dir_list {
        char    *name;
index 671e781..f3956da 100644 (file)
@@ -231,7 +231,8 @@ static void probe_one(blkid_cache cache, const char *ptname,
                    dev->bid_devno == devno)
                        goto set_pri;
 
-               if (stat(device, &st) == 0 && S_ISBLK(st.st_mode) &&
+               if (stat(device, &st) == 0 &&
+                   blkidP_is_disk_device(st.st_mode) &&
                    st.st_rdev == devno) {
                        devname = blkid_strdup(device);
                        goto get_dev;
index 480030f..34ceb3c 100644 (file)
@@ -119,7 +119,7 @@ void blkid__scan_dir(const char *dirname, dev_t devno, struct dir_list **list,
                if (stat(path, &st) < 0)
                        continue;
 
-               if (S_ISBLK(st.st_mode) && st.st_rdev == devno) {
+               if (blkidP_is_disk_device(st.st_mode) && st.st_rdev == devno) {
                        *devname = blkid_strdup(path);
                        DBG(DEBUG_DEVNO,
                            printf("found 0x%llx at %s (%p)\n", (long long)devno,
index 8e8eb4c..4e2835f 100644 (file)
@@ -149,7 +149,7 @@ blkid_loff_t blkid_get_dev_size(int fd)
                 * character) devices, so we need to check for S_ISCHR, too.
                 */
                if (fstat(fd, &st) >= 0 &&
-                   (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)))
+                   blkidP_is_disk_device(st.st_mode))
                        part = st.st_rdev & 7;
 
                if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
index 7e7e29d..448318c 100644 (file)
@@ -9,6 +9,7 @@
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 #include <string.h>
 #if HAVE_UNISTD_H
index 8de9d33..9d001d5 100644 (file)
@@ -9,10 +9,23 @@
  * %End-Header%
  */
 
+#if HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+
 #include "ext2fs.h"
 
 #define EXT2FS_MAX_NESTED_LINKS  8
 
+static inline int ext2fsP_is_disk_device(mode_t mode)
+{
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
+       return S_ISBLK(mode) || S_ISCHR(mode);
+#else
+       return S_ISBLK(mode);
+#endif
+}
+
 /*
  * Badblocks list
  */
index 62fa0db..cd85ef5 100644 (file)
@@ -104,7 +104,8 @@ static int scan_dir(char *dirname, dev_t device, struct dir_list **list,
                        goto skip_to_next;
                if (S_ISDIR(st.st_mode))
                        add_to_dirlist(path, list);
-               if (S_ISBLK(st.st_mode) && st.st_rdev == device) {
+               if (ext2fsP_is_disk_device(st.st_mode) &&
+                   st.st_rdev == device) {
                        cp = malloc(strlen(path)+1);
                        if (!cp) {
                                closedir(dir);
index 120299c..6cd497d 100644 (file)
@@ -55,6 +55,7 @@
 
 #include "ext2_fs.h"
 #include "ext2fs.h"
+#include "ext2fsP.h"
 
 #ifdef HAVE_SETMNTENT
 /*
@@ -115,7 +116,7 @@ static errcode_t check_mntent_file(const char *mtab_file, const char *file,
                return errno;
        }
        if (stat(file, &st_buf) == 0) {
-               if (S_ISBLK(st_buf.st_mode)) {
+               if (ext2fsP_is_disk_device(st_buf.st_mode)) {
 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
                        file_rdev = st_buf.st_rdev;
 #endif /* __GNU__ */
@@ -130,7 +131,7 @@ static errcode_t check_mntent_file(const char *mtab_file, const char *file,
                if (strcmp(file, mnt->mnt_fsname) == 0)
                        break;
                if (stat(mnt->mnt_fsname, &st_buf) == 0) {
-                       if (S_ISBLK(st_buf.st_mode)) {
+                       if (ext2fsP_is_disk_device(st_buf.st_mode)) {
 #ifndef __GNU__
                                if (file_rdev && (file_rdev == st_buf.st_rdev))
                                        break;
@@ -310,7 +311,7 @@ static int is_swap_device(const char *file)
        file_dev = 0;
 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
        if ((stat(file, &st_buf) == 0) &&
-           S_ISBLK(st_buf.st_mode))
+           ext2fsP_is_disk_device(st_buf.st_mode))
                file_dev = st_buf.st_rdev;
 #endif /* __GNU__ */
 
@@ -337,7 +338,7 @@ valid_first_line:
                }
 #ifndef __GNU__
                if (file_dev && (stat(buf, &st_buf) == 0) &&
-                   S_ISBLK(st_buf.st_mode) &&
+                   ext2fsP_is_disk_device(st_buf.st_mode) &&
                    file_dev == st_buf.st_rdev) {
                        ret++;
                        break;
@@ -404,7 +405,8 @@ errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
        {
                struct stat st_buf;
 
-               if (stat(device, &st_buf) == 0 && S_ISBLK(st_buf.st_mode)) {
+               if (stat(device, &st_buf) == 0 &&
+                   ext2fsP_is_disk_device(st_buf.st_mode)) {
                        int fd = open(device, O_RDONLY | O_EXCL);
 
                        if (fd >= 0)
index 6414195..c2ce9a1 100644 (file)
@@ -76,6 +76,7 @@
 
 #include "ext2_fs.h"
 #include "ext2fs.h"
+#include "ext2fsP.h"
 
 /*
  * For checking structure magic numbers...
@@ -611,7 +612,7 @@ static errcode_t unix_open_channel(const char *name, int fd,
         * zero.
         */
        if (ext2fs_fstat(data->dev, &st) == 0) {
-               if (S_ISBLK(st.st_mode))
+               if (ext2fsP_is_disk_device(st.st_mode))
                        io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
                else
                        io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
@@ -682,7 +683,7 @@ static errcode_t unix_open_channel(const char *name, int fd,
             (ut.release[4] == '1') && (ut.release[5] >= '0') &&
             (ut.release[5] < '8')) &&
            (ext2fs_fstat(data->dev, &st) == 0) &&
-           (S_ISBLK(st.st_mode))) {
+           (ext2fsP_is_disk_device(st.st_mode))) {
                struct rlimit   rlim;
 
                rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
index e0c3188..030f9cb 100644 (file)
@@ -43,6 +43,7 @@ extern int optind;
 
 #include "ext2fs/ext2_fs.h"
 #include "ext2fs/ext2fs.h"
+#include "ext2fs/ext2fsP.h"
 #include "et/com_err.h"
 #include "uuid/uuid.h"
 #include "e2p/e2p.h"
@@ -1620,7 +1621,7 @@ skip_device:
                                _("Can not stat output\n"));
                        exit(1);
                }
-               if (S_ISBLK(st.st_mode))
+               if (ext2fsP_is_disk_device(st.st_mode))
                        output_is_blk = 1;
        }
        if (flags & E2IMAGE_IS_QCOW2_FLAG) {