Whamcloud - gitweb
libext2fs: create the inode bitmap checksum
[tools/e2fsprogs.git] / lib / blkid / getsize.c
index 94e00fa..f670e1b 100644 (file)
 #define _LARGEFILE_SOURCE
 #define _LARGEFILE64_SOURCE
 
+/* include this before sys/queues.h! */
+#include "config.h"
+#include "blkidP.h"
+
 #include <stdio.h>
 #if HAVE_UNISTD_H
 #include <unistd.h>
@@ -28,7 +32,6 @@
 #endif
 #ifdef HAVE_SYS_DISKLABEL_H
 #include <sys/disklabel.h>
-#include <sys/stat.h>
 #endif
 #ifdef HAVE_SYS_DISK_H
 #ifdef HAVE_SYS_QUEUE_H
@@ -39,8 +42,9 @@
 #ifdef __linux__
 #include <sys/utsname.h>
 #endif
-
-#include "blkidP.h"
+#if HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
 
 
 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
@@ -67,13 +71,13 @@ static int valid_offset(int fd, blkid_loff_t offset)
 }
 
 /*
- * Returns the number of blocks in a partition
+ * Returns the number of bytes in a partition
  */
 blkid_loff_t blkid_get_dev_size(int fd)
 {
        int valid_blkgetsize64 = 1;
 #ifdef __linux__
-       struct          utsname ut;
+       struct          utsname ut;
 #endif
        unsigned long long size64;
        unsigned long size;
@@ -92,7 +96,7 @@ blkid_loff_t blkid_get_dev_size(int fd)
 #ifdef DKIOCGETBLOCKCOUNT      /* For Apple Darwin */
        if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
                if ((sizeof(blkid_loff_t) < sizeof(unsigned long long))
-                   && ((size64 / (blocksize / 512)) > 0xFFFFFFFF))
+                   && (size64 << 9 > 0xFFFFFFFF))
                        return 0; /* EFBIG */
                return (blkid_loff_t) size64 << 9;
        }
@@ -112,33 +116,53 @@ blkid_loff_t blkid_get_dev_size(int fd)
                        return 0; /* EFBIG */
                return size64;
        }
-#endif
+#endif /* BLKGETSIZE64 */
 
 #ifdef BLKGETSIZE
        if (ioctl(fd, BLKGETSIZE, &size) >= 0)
                return (blkid_loff_t)size << 9;
 #endif
 
+/* tested on FreeBSD 6.1-RELEASE i386 */
+#ifdef DIOCGMEDIASIZE
+       if (ioctl(fd, DIOCGMEDIASIZE, &size64) >= 0)
+               return (off_t)size64;
+#endif /* DIOCGMEDIASIZE */
+
 #ifdef FDGETPRM
        if (ioctl(fd, FDGETPRM, &this_floppy) >= 0)
                return (blkid_loff_t)this_floppy.size << 9;
 #endif
 #ifdef HAVE_SYS_DISKLABEL_H
-#if 0
        /*
-        * This should work in theory but I haven't tested it.  Anyone
-        * on a BSD system want to test this for me?  In the meantime,
-        * binary search mechanism should work just fine.
+        * 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))
+       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
 #endif /* HAVE_SYS_DISKLABEL_H */
+       {
+#if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
+               struct stat64   st;
+               if (fstat64(fd, &st) == 0)
+#else
+               struct stat     st;
+               if (fstat(fd, &st) == 0)
+#endif
+                       if (S_ISREG(st.st_mode))
+                               return st.st_size;
+       }
 
        /*
         * OK, we couldn't figure it out by using a specialized ioctl,
@@ -148,8 +172,7 @@ blkid_loff_t blkid_get_dev_size(int fd)
        low = 0;
        for (high = 1024; valid_offset(fd, high); high *= 2)
                low = high;
-       while (low < high - 1)
-       {
+       while (low < high - 1) {
                const blkid_loff_t mid = (low + high) / 2;
 
                if (valid_offset(fd, mid))
@@ -163,7 +186,7 @@ blkid_loff_t blkid_get_dev_size(int fd)
 #ifdef TEST_PROGRAM
 int main(int argc, char **argv)
 {
-       blkid_loff_t bytes;
+       long long bytes;
        int     fd;
 
        if (argc < 2) {
@@ -176,7 +199,8 @@ int main(int argc, char **argv)
                perror(argv[0]);
 
        bytes = blkid_get_dev_size(fd);
-       printf("Device %s has %Ld 1k blocks.\n", argv[1], bytes >> 10);
+       printf("Device %s has %Ld 1k blocks.\n", argv[1],
+              (unsigned long long) bytes >> 10);
 
        return 0;
 }