Whamcloud - gitweb
ext2fs, blkid: localize environment-specific variables
authorAndreas Dilger <adilger@dilger.ca>
Thu, 22 Nov 2012 23:17:19 +0000 (16:17 -0700)
committerTheodore Ts'o <tytso@mit.edu>
Mon, 26 Nov 2012 14:34:20 +0000 (09:34 -0500)
Restructure the ext2fs_get_device_size() and blkid_get_dev_size()
code to localize the variables used for different device probing
methods.  This at least reduces the #ifdef mess to only one part
of the code for each method, and avoids "unused variable" compiler
warnings added when variables are declared without being #ifdef'd.

Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/blkid/getsize.c
lib/ext2fs/getsize.c

index f670e1b..f9be962 100644 (file)
@@ -75,52 +75,46 @@ static int valid_offset(int fd, blkid_loff_t offset)
  */
 blkid_loff_t blkid_get_dev_size(int fd)
 {
-       int valid_blkgetsize64 = 1;
-#ifdef __linux__
-       struct          utsname ut;
-#endif
        unsigned long long size64;
-       unsigned long size;
        blkid_loff_t high, low;
-#ifdef FDGETPRM
-       struct floppy_struct this_floppy;
-#endif
-#ifdef HAVE_SYS_DISKLABEL_H
-       int part = -1;
-       struct disklabel lab;
-       struct partition *pp;
-       char ch;
-       struct stat st;
-#endif /* HAVE_SYS_DISKLABEL_H */
 
 #ifdef DKIOCGETBLOCKCOUNT      /* For Apple Darwin */
        if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
-               if ((sizeof(blkid_loff_t) < sizeof(unsigned long long))
-                   && (size64 << 9 > 0xFFFFFFFF))
+               if (sizeof(blkid_loff_t) < sizeof(unsigned long long) &&
+                   (size64 << 9) > 0xFFFFFFFF)
                        return 0; /* EFBIG */
-               return (blkid_loff_t) size64 << 9;
+               return (blkid_loff_t)size64 << 9;
        }
 #endif
 
 #ifdef BLKGETSIZE64
+       {
+               int valid_blkgetsize64 = 1;
 #ifdef __linux__
-       if ((uname(&ut) == 0) &&
-           ((ut.release[0] == '2') && (ut.release[1] == '.') &&
-            (ut.release[2] < '6') && (ut.release[3] == '.')))
-               valid_blkgetsize64 = 0;
-#endif
-       if (valid_blkgetsize64 &&
-           ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
-               if ((sizeof(blkid_loff_t) < sizeof(unsigned long long))
-                   && ((size64) > 0xFFFFFFFF))
-                       return 0; /* EFBIG */
-               return size64;
+               struct          utsname ut;
+
+               if ((uname(&ut) == 0) &&
+                   ((ut.release[0] == '2') && (ut.release[1] == '.') &&
+                    (ut.release[2] < '6') && (ut.release[3] == '.')))
+                       valid_blkgetsize64 = 0;
+#endif
+               if (valid_blkgetsize64 &&
+                   ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
+                       if (sizeof(blkid_loff_t) < sizeof(unsigned long long) &&
+                           (size64 > 0xFFFFFFFF))
+                               return 0; /* EFBIG */
+                       return size64;
+               }
        }
 #endif /* BLKGETSIZE64 */
 
 #ifdef BLKGETSIZE
-       if (ioctl(fd, BLKGETSIZE, &size) >= 0)
-               return (blkid_loff_t)size << 9;
+       {
+               unsigned long size;
+
+               if (ioctl(fd, BLKGETSIZE, &size) >= 0)
+                       return (blkid_loff_t)size << 9;
+       }
 #endif
 
 /* tested on FreeBSD 6.1-RELEASE i386 */
@@ -130,26 +124,39 @@ blkid_loff_t blkid_get_dev_size(int fd)
 #endif /* DIOCGMEDIASIZE */
 
 #ifdef FDGETPRM
-       if (ioctl(fd, FDGETPRM, &this_floppy) >= 0)
-               return (blkid_loff_t)this_floppy.size << 9;
+       {
+               struct floppy_struct this_floppy;
+
+               if (ioctl(fd, FDGETPRM, &this_floppy) >= 0)
+                       return (blkid_loff_t)this_floppy.size << 9;
+       }
 #endif
 #ifdef HAVE_SYS_DISKLABEL_H
-       /*
-        * This code works for FreeBSD 4.11 i386, except for the full device
-        * (such as /dev/ad0). It doesn't work properly for newer FreeBSD
-        * though. FreeBSD >= 5.0 should be covered by the DIOCGMEDIASIZE
-        * above however.
-        *
-        * Note that FreeBSD >= 4.0 has disk devices as unbuffered (raw,
-        * 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)))
-               part = st.st_rdev & 7;
-
-       if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
-               pp = &lab.d_partitions[part];
-               if (pp->p_size)
-                       return pp->p_size << 9;
+       {
+               int part = -1;
+               struct disklabel lab;
+               struct partition *pp;
+               char ch;
+               struct stat st;
+
+               /*
+                * This code works for FreeBSD 4.11 i386, except for the full
+                * device (such as /dev/ad0). It doesn't work properly for
+                * newer FreeBSD though. FreeBSD >= 5.0 should be covered by
+                * the DIOCGMEDIASIZE above however.
+                *
+                * Note that FreeBSD >= 4.0 has disk devices as unbuffered (raw,
+                * 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)))
+                       part = st.st_rdev & 7;
+
+               if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
+                       pp = &lab.d_partitions[part];
+                       if (pp->p_size)
+                               return pp->p_size << 9;
+               }
        }
 #endif /* HAVE_SYS_DISKLABEL_H */
        {
index 0a7053e..a9a4812 100644 (file)
@@ -140,25 +140,11 @@ static int valid_offset (int fd, ext2_loff_t offset)
  * Returns the number of blocks in a partition
  */
 errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
-                                blk64_t *retblocks)
+                                 blk64_t *retblocks)
 {
        int     fd, rc = 0;
-       int valid_blkgetsize64 = 1;
-#ifdef __linux__
-       struct          utsname ut;
-#endif
        unsigned long long size64;
-       unsigned long   size;
        ext2_loff_t high, low;
-#ifdef FDGETPRM
-       struct floppy_struct this_floppy;
-#endif
-#ifdef HAVE_SYS_DISKLABEL_H
-       int part;
-       struct disklabel lab;
-       struct partition *pp;
-       char ch;
-#endif /* HAVE_SYS_DISKLABEL_H */
 
        fd = ext2fs_open_file(file, O_RDONLY, 0);
        if (fd < 0)
@@ -172,63 +158,83 @@ errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
 #endif
 
 #ifdef BLKGETSIZE64
+       {
+               int valid_blkgetsize64 = 1;
 #ifdef __linux__
-       if ((uname(&ut) == 0) &&
-           ((ut.release[0] == '2') && (ut.release[1] == '.') &&
-            (ut.release[2] < '6') && (ut.release[3] == '.')))
-               valid_blkgetsize64 = 0;
+               struct utsname ut;
+
+               if ((uname(&ut) == 0) &&
+                   ((ut.release[0] == '2') && (ut.release[1] == '.') &&
+                    (ut.release[2] < '6') && (ut.release[3] == '.')))
+                       valid_blkgetsize64 = 0;
 #endif
-       if (valid_blkgetsize64 &&
-           ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
-               *retblocks = size64 / blocksize;
-               goto out;
+               if (valid_blkgetsize64 &&
+                   ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
+                       *retblocks = size64 / blocksize;
+                       goto out;
+               }
        }
 #endif /* BLKGETSIZE64 */
 
 #ifdef BLKGETSIZE
-       if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
-               *retblocks = size / (blocksize / 512);
-               goto out;
+       {
+               unsigned long   size;
+
+               if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
+                       *retblocks = size / (blocksize / 512);
+                       goto out;
+               }
        }
 #endif
 
 #ifdef FDGETPRM
-       if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
-               *retblocks = this_floppy.size / (blocksize / 512);
-               goto out;
+       {
+               struct floppy_struct this_floppy;
+
+               if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
+                       *retblocks = this_floppy.size / (blocksize / 512);
+                       goto out;
+               }
        }
 #endif
 
 #ifdef HAVE_SYS_DISKLABEL_H
-#if defined(DIOCGMEDIASIZE)
        {
-           off_t ms;
-           u_int bs;
-           if (ioctl(fd, DIOCGMEDIASIZE, &ms) >= 0) {
-               *retblocks = ms / blocksize;
-               goto out;
-           }
-       }
+               int part;
+               struct disklabel lab;
+               struct partition *pp;
+               char ch;
+
+#if defined(DIOCGMEDIASIZE)
+               {
+                       off_t ms;
+                       u_int bs;
+                       if (ioctl(fd, DIOCGMEDIASIZE, &ms) >= 0) {
+                               *retblocks = ms / blocksize;
+                               goto out;
+                       }
+               }
 #elif defined(DIOCGDINFO)
-       /* old disklabel interface */
-       part = strlen(file) - 1;
-       if (part >= 0) {
-               ch = file[part];
-               if (isdigit(ch))
-                       part = 0;
-               else if (ch >= 'a' && ch <= 'h')
-                       part = ch - 'a';
-               else
-                       part = -1;
-       }
-       if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
-               pp = &lab.d_partitions[part];
-               if (pp->p_size) {
-                       *retblocks = pp->p_size / (blocksize / 512);
-                       goto out;
+               /* old disklabel interface */
+               part = strlen(file) - 1;
+               if (part >= 0) {
+                       ch = file[part];
+                       if (isdigit(ch))
+                               part = 0;
+                       else if (ch >= 'a' && ch <= 'h')
+                               part = ch - 'a';
+                       else
+                               part = -1;
+               }
+               if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
+                       pp = &lab.d_partitions[part];
+                       if (pp->p_size) {
+                               *retblocks = pp->p_size / (blocksize / 512);
+                               goto out;
+                       }
                }
-       }
 #endif /* defined(DIOCG*) */
+       }
 #endif /* HAVE_SYS_DISKLABEL_H */
 
        {
@@ -247,10 +253,9 @@ errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
         * find the size of the partition.
         */
        low = 0;
-       for (high = 1024; valid_offset (fd, high); high *= 2)
+       for (high = 1024; valid_offset(fd, high); high *= 2)
                low = high;
-       while (low < high - 1)
-       {
+       while (low < high - 1) {
                const ext2_loff_t mid = (low + high) / 2;
 
                if (valid_offset (fd, mid))
@@ -258,7 +263,7 @@ errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
                else
                        high = mid;
        }
-       valid_offset (fd, 0);
+       valid_offset(fd, 0);
        size64 = low + 1;
        *retblocks = size64 / blocksize;
 out: