Use older 2TB-limited 'BLKGETSIZE' if 'BLKGETSIZE64' is not available.
[captive.git] / src / libcaptive / storage / size.c
index f1a80c2..5348bb9 100644 (file)
  */
 
 
-#define _FILE_OFFSET_BITS 64   /* for 64-bit 'off_t' */
-
 #include "config.h"
 
 #include "captive/storage.h"   /* self */
 #include "../client/giochannel-blind.h"        /* for captive_giochannel_blind_get_size() */
+#include "../sandbox/client-CaptiveIOChannel.h"        /* for captive_io_channel_get_size() */
 #include <glib/gmessages.h>
 #include <glib/gtypes.h>
 #include <fcntl.h>
@@ -46,6 +45,19 @@ guint64 r;
 }
 
 
+static guint64 size_sandbox(GIOChannel *iochannel)
+{
+guint64 r;
+
+       g_return_val_if_fail(iochannel!=NULL,0);
+
+       if (!captive_io_channel_get_size(iochannel,&r))
+               return 0;
+
+       return r;
+}
+
+
 static GIOChannel *iochannel_null;
 
 static int iounixchannel_get_fd(GIOChannel *iochannel)
@@ -80,16 +92,27 @@ int fd;
 
 static guint64 size_ioctl(GIOChannel *iochannel)
 {
-int fd,err;
+int fd;
 guint64 r;
+#ifndef BLKGETSIZE64
+long r_long;
+#endif
 
        g_return_val_if_fail(iochannel!=NULL,0);
 
        if (-1==(fd=iounixchannel_get_fd(iochannel)))
                return 0;
 
-       if ((err=ioctl(fd,BLKGETSIZE64,&r)))
+#ifdef BLKGETSIZE64
+       if (ioctl(fd,BLKGETSIZE64,&r))
+               return 0;
+#else
+       if (ioctl(fd,BLKGETSIZE,&r_long))
                return 0;
+       if (r_long<0)
+               return 0;
+       r=((guint64)512)*r_long;
+#endif
 
        return r;
 }
@@ -104,6 +127,7 @@ off_t offset_orig,offset;
 
        /* We may need '_FILE_OFFSET_BITS=64'.
         * Setting '__USE_FILE_OFFSET64' did not help.
+        * Done by 'AC_SYS_LARGEFILE' of configure.in.
         */
        g_return_val_if_fail(sizeof(offset)==sizeof(guint64),0);
 
@@ -197,6 +221,8 @@ guint64 r;
 
        if ((r=size_blind(iochannel)))
                return r;
+       if ((r=size_sandbox(iochannel)))
+               return r;
        if ((r=size_ioctl(iochannel)))
                return r;
        if ((r=size_seek(iochannel)))