Initial original import from: fuse-2.4.2-2.fc4
[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 "../client/giochannel-blind.h" /* for captive_giochannel_blind_get_size() */
24 #include "../client/giochannel-subrange.h"      /* for captive_giochannel_subrange_get_size() */
25 #include "../sandbox/client-CaptiveIOChannel.h" /* for captive_io_channel_get_size() */
26 #include <glib/gmessages.h>
27 #include <glib/gtypes.h>
28 #include <fcntl.h>
29 #include <sys/ioctl.h>
30 #include <unistd.h>
31 /* Do not: #include <linux/fs.h>        * for 'BLKGETSIZE64' *
32  * as including any Linux kernel include files is too much incompatible.
33  */
34 #include <sys/mount.h>  /* for 'BLKGETSIZE' */
35 #include "iounixchannel.h"
36
37
38 static guint64 size_subrange(GIOChannel *iochannel)
39 {
40 guint64 r;
41
42         g_return_val_if_fail(iochannel!=NULL,0);
43
44         if (!captive_giochannel_subrange_get_size(iochannel,&r))
45                 return 0;
46
47         return r;
48 }
49
50
51 static guint64 size_blind(GIOChannel *iochannel)
52 {
53 guint64 r;
54
55         g_return_val_if_fail(iochannel!=NULL,0);
56
57         if (!captive_giochannel_blind_get_size(iochannel,&r))
58                 return 0;
59
60         return r;
61 }
62
63
64 static guint64 size_sandbox(GIOChannel *iochannel)
65 {
66 guint64 r;
67
68         g_return_val_if_fail(iochannel!=NULL,0);
69
70         if (!captive_io_channel_get_size(iochannel,&r))
71                 return 0;
72
73         return r;
74 }
75
76
77 guint64 captive_giochannel_size_ioctl(GIOChannel *iochannel)
78 {
79 int fd;
80 guint64 r;
81 long r_long;
82
83         g_return_val_if_fail(iochannel!=NULL,0);
84
85         if (-1==(fd=captive_iounixchannel_get_fd(iochannel)))
86                 return 0;
87
88 #ifdef BLKGETSIZE64
89         if (!ioctl(fd,BLKGETSIZE64,&r))
90                 return r;
91 #endif
92         if (ioctl(fd,BLKGETSIZE,&r_long))
93                 return 0;
94         if (r_long<0)
95                 return 0;
96         r=((guint64)512)*r_long;
97
98         return r;
99 }
100
101
102 static guint64 size_seek(GIOChannel *iochannel)
103 {
104 int fd;
105 off_t offset_orig,offset;
106
107         g_return_val_if_fail(iochannel!=NULL,0);
108
109         /* We may need '_FILE_OFFSET_BITS=64'.
110          * Setting '__USE_FILE_OFFSET64' did not help.
111          * Done by 'AC_SYS_LARGEFILE' of configure.ac.
112          */
113         g_return_val_if_fail(sizeof(offset)==sizeof(guint64),0);
114
115         if (-1==(fd=captive_iounixchannel_get_fd(iochannel)))
116                 return 0;
117
118         if (-1==(offset_orig=lseek(fd,0,SEEK_CUR)))
119                 return 0;
120         g_return_val_if_fail(offset_orig>=0,0);
121         offset=lseek(fd,0,SEEK_END);
122         if (offset_orig!=lseek(fd,offset_orig,SEEK_SET))
123                 g_assert_not_reached();
124
125         if (-1==offset)
126                 return 0;
127         g_return_val_if_fail(offset>=0,0);
128
129         return offset;
130 }
131
132
133
134 static guint64 size_read(GIOChannel *iochannel)
135 {
136 gint64 low,high,mid;
137 GIOStatus erriostatus;
138 gchar bufchar;
139 gsize bufchargot;
140
141         g_return_val_if_fail(iochannel!=NULL,0);
142         /* Default "UTF-8" encoding is not much usable for us */
143         g_return_val_if_fail(g_io_channel_get_encoding(iochannel)==NULL,0);
144
145         /* low  ==high: low (high)
146          * low+1==high: mid==low; NORMAL: no change: high
147          * low+1==high: mid==low; EOF   : high=mid => 'low==high' case
148          */
149         for (low=0,
150                         high=
151                                         G_MAXINT64;
152                         low+1<high;) {
153                 mid=low+(high-low)/2;   /* beware of 'gint64' overflow! */
154
155                 erriostatus=g_io_channel_seek_position(iochannel,mid,G_SEEK_SET,
156                                 NULL);  /* error */
157                 /* During seek in block device such as on URL file:///dev/hda1#captive-fastfat.sys-ro:/
158                  * we will do llseek(2) on "/dev/hda1" device from captive_giochannel_size().
159                  * Although we are allowed to seek behind EOF on regular files
160                  * at least linux-kernel-2.4.19-ac4/fs/block_dev.c/block_llseek() will give
161                  * EINVAL on seek behind EOF therefore it must be accepted without complaints by us.
162                  */
163                 if (erriostatus!=G_IO_STATUS_NORMAL) {
164                         erriostatus=G_IO_STATUS_EOF;
165                         bufchargot=0;
166                         }
167                 else {
168                         g_assert(sizeof(bufchar)==1);
169                         erriostatus=g_io_channel_read_chars(iochannel,
170                                         &bufchar, /* buf */
171                                         sizeof(bufchar),        /* count */
172                                         &bufchargot,    /* bytes_read */
173                                         NULL);  /* error */
174                         /* During read on the end boundary of Linux kernel block device we will
175                          * get GNOME_VFS_ERROR_IO at least from linux-kernel-2.4.19-ac4
176                          * which will get mapped to G_IO_STATUS_ERROR by captive_gnomevfs_giognomevfs_io_read().
177                          */
178                         g_assert(0
179                                         || erriostatus==G_IO_STATUS_NORMAL
180                                         || erriostatus==G_IO_STATUS_EOF
181                                         || erriostatus==G_IO_STATUS_ERROR);
182                         g_assert(0
183                                         || (erriostatus==G_IO_STATUS_NORMAL && bufchargot==1)
184                                         || (erriostatus==G_IO_STATUS_EOF    && bufchargot==0)
185                                         || (erriostatus==G_IO_STATUS_ERROR  && bufchargot==0));
186                         }
187
188                 if (erriostatus==G_IO_STATUS_NORMAL)
189                         low=mid;
190                 else
191                         high=mid;
192                 }
193         
194         g_assert(high>=0);
195         return high;
196 }
197
198
199 guint64 captive_giochannel_size(GIOChannel *iochannel)
200 {
201 guint64 r;
202
203         if ((r=size_subrange(iochannel)))
204                 return r;
205         if ((r=size_blind(iochannel)))
206                 return r;
207         if ((r=size_sandbox(iochannel)))
208                 return r;
209         if ((r=captive_giochannel_size_ioctl(iochannel)))
210                 return r;
211         if ((r=size_seek(iochannel)))
212                 return r;
213         if ((r=size_read(iochannel)))
214                 return r;
215
216         return r;
217 }