Permit Gnome-VFS mount of Linux block device (where seeks are failing behind EOF)
[captive.git] / src / libcaptive / storage / size.c
1 /* $Id$
2  * Detect media size of given GIOChannel for libcaptive
3  * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include "captive/storage.h"    /* self */
23 #include <glib/gmessages.h>
24 #include <glib/gtypes.h>
25
26
27 guint64 captive_giochannel_size(GIOChannel *iochannel)
28 {
29 gint fd;
30 gint64 low,high,mid;
31 GIOStatus erriostatus;
32 gchar bufchar;
33 gsize bufchargot;
34
35         g_return_val_if_fail(iochannel!=NULL,0);
36         /* Default "UTF-8" encoding is not much usable for us */
37         g_return_val_if_fail(g_io_channel_get_encoding(iochannel)==NULL,0);
38
39         fd=g_io_channel_unix_get_fd(iochannel);
40         g_return_val_if_fail(fd!=-1,0);
41
42         /* FIXME: TODO: ioctl() detection */
43
44         /* low  ==high: low (high)
45          * low+1==high: mid==low; NORMAL: no change: high
46          * low+1==high: mid==low; EOF   : high=mid => 'low==high' case
47          */
48         for (low=0,
49                         high=
50                                         G_MAXINT;       /* FIXME: 'G_MAXINT64' fails on g_io_channel_seek_position() */
51                         low+1<high;) {
52                 mid=low+(high-low)/2;   /* beware of 'gint64' overflow! */
53
54                 erriostatus=g_io_channel_seek_position(iochannel,mid,G_SEEK_SET,
55                                 NULL);  /* error */
56                 /* During seek in block device such as on URL file:///dev/hda1#captive-fastfat.sys-ro:/
57                  * we will do llseek(2) on "/dev/hda1" device from captive_giochannel_size().
58                  * Although we are allowed to seek behind EOF on regular files
59                  * at least linux-kernel-2.4.19-ac4/fs/block_dev.c/block_llseek() will give
60                  * EINVAL on seek behind EOF therefore it must be accepted without complaints by us.
61                  */
62                 if (erriostatus!=G_IO_STATUS_NORMAL) {
63                         erriostatus=G_IO_STATUS_EOF;
64                         bufchargot=0;
65                         }
66                 else {
67                         g_assert(sizeof(bufchar)==1);
68                         erriostatus=g_io_channel_read_chars(iochannel,
69                                         &bufchar, /* buf */
70                                         sizeof(bufchar),        /* count */
71                                         &bufchargot,    /* bytes_read */
72                                         NULL);  /* error */
73                         g_assert(erriostatus==G_IO_STATUS_NORMAL || erriostatus==G_IO_STATUS_EOF);
74                         g_assert(0
75                                         || (erriostatus==G_IO_STATUS_NORMAL && bufchargot==1)
76                                         || (erriostatus==G_IO_STATUS_EOF    && bufchargot==0));
77                         }
78
79                 if (erriostatus==G_IO_STATUS_NORMAL)
80                         low=mid;
81                 else
82                         high=mid;
83                 }
84         
85         g_assert(high>=0);
86         return high;
87 }