Debian Codegraph @

file.c

raw symbols SCIP
1 /*
2 * file.c
3 * Data source for files and block devices.
4 *
5 * Copyright (c) 2003 Christoph Pfisterer
6 *
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use, copy,
11 * modify, merge, publish, distribute, sublicense, and/or sell copies
12 * of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #include "global.h"
29
30 #define USE_BINARY_SEARCH 0
31 #define DEBUG_SIZE 0
32
33 #ifdef USE_IOCTL_LINUX
34 #include <sys/ioctl.h>
35 #include <linux/fs.h>
36 #endif
37
38 #ifdef USE_IOCTL_FREEBSD
39 #include <sys/disklabel.h>
40 #endif
41
42 #ifdef USE_IOCTL_DARWIN
43 #include <sys/ioctl.h>
44 #include <sys/disk.h>
45 #endif
46
47 /*
48 * types
49 */
50
51 typedef struct file_source {
52 SOURCE c;
53 int fd;
54 } FILE_SOURCE;
55
56 /*
57 * helper functions
58 */
59
60 static int analyze_file(SOURCE *s, int level);
61 static u8 read_file(SOURCE *s, u8 pos, u8 len, void *buf);
62 static void close_file(SOURCE *s);
63
64 #if USE_BINARY_SEARCH
65 static int check_position(int fd, u8 pos);
66 #endif
67
68 /*
69 * initialize the file source
70 */
71
72 SOURCE *init_file_source(int fd, int filekind)
73 {
74 FILE_SOURCE *fs;
75 off_t result;
76
77 fs = (FILE_SOURCE *)malloc(sizeof(FILE_SOURCE));
78 if (fs == NULL)
79 bailout("Out of memory");
80 memset(fs, 0, sizeof(FILE_SOURCE));
81
82 if (filekind != 0) /* special treatment hook for devices */
83 fs->c.analyze = analyze_file;
84 fs->c.read_bytes = read_file;
85 fs->c.close = close_file;
86 fs->fd = fd;
87
88 /*
89 * Determine the size using various methods. The first method that
90 * works is used.
91 */
92
93 /*
94 * lseek() to the end:
95 * Works on files. On some systems (Linux), this also works on devices.
96 */
97 if (!fs->c.size_known) {
98 result = lseek(fd, 0, SEEK_END);
99 #if DEBUG_SIZE
100 printf("Size: lseek returned %lld\n", result);
101 #endif
102 if (result > 0) {
103 fs->c.size_known = 1;
104 fs->c.size = result;
105 }
106 }
107
108 #ifdef USE_IOCTL_LINUX
109 /*
110 * ioctl, Linux style:
111 * Works on certain devices.
112 */
113 #ifdef BLKGETSIZE64
114 #define u64 __u64 /* workaround for broken header file */
115 if (!fs->c.size_known && filekind != 0) {
116 u8 devsize;
117 if (ioctl(fd, BLKGETSIZE64, (void *)&devsize) >= 0) {
118 fs->c.size_known = 1;
119 fs->c.size = devsize;
120 #if DEBUG_SIZE
121 printf("Size: Linux 64-bit ioctl reports %llu\n", fs->c.size);
122 #endif
123 }
124 }
125 #undef u64
126 #endif
127
128 if (!fs->c.size_known && filekind != 0) {
129 u4 blockcount;
130 if (ioctl(fd, BLKGETSIZE, (void *)&blockcount) >= 0) {
131 fs->c.size_known = 1;
132 fs->c.size = (u8)blockcount * 512;
133 #if DEBUG_SIZE
134 printf("Size: Linux 32-bit ioctl reports %llu (%lu blocks)\n",
135 fs->c.size, blockcount);
136 #endif
137 }
138 }
139 #endif
140
141 #ifdef USE_IOCTL_FREEBSD
142 /*
143 * ioctl, FreeBSD style:
144 * Works on partitioned hard disks or somthing like that.
145 */
146 if (!fs->c.size_known && filekind != 0) {
147 struct disklabel dl;
148 if (ioctl(fd, DIOCGDINFO, &dl) >= 0) {
149 fs->c.size_known = 1;
150 fs->c.size = (u8) dl.d_ncylinders * dl.d_secpercyl * dl.d_secsize;
151 /* TODO: check this, it's the whole disk size... */
152 #if DEBUG_SIZE
153 printf("Size: FreeBSD ioctl reports %llu\n", fs->c.size);
154 #endif
155 }
156 }
157 #endif
158
159 #ifdef USE_IOCTL_DARWIN
160 /*
161 * ioctl, Darwin style:
162 * Works on certain devices.
163 */
164 if (!fs->c.size_known && filekind != 0) {
165 u4 blocksize;
166 u8 blockcount;
167 if (ioctl(fd, DKIOCGETBLOCKSIZE, (void *)&blocksize) >= 0) {
168 if (ioctl(fd, DKIOCGETBLOCKCOUNT, (void *)&blockcount) >= 0) {
169 fs->c.size_known = 1;
170 fs->c.size = blockcount * blocksize;
171 #if DEBUG_SIZE
172 printf("Size: Darwin ioctl reports %llu (%llu blocks of %lu bytes)\n",
173 fs->c.size, blockcount, blocksize);
174 #endif
175 }
176 }
177 }
178 #endif
179
180 #if USE_BINARY_SEARCH
181 /*
182 * binary search:
183 * Works on anything that can seek, but is quite expensive.
184 */
185 if (!fs->c.size_known) {
186 u8 lower, upper, current;
187
188 /* TODO: check that the target can seek at all */
189
190 #if DEBUG_SIZE
191 printf("Size: Doing a binary search\n");
192 #endif
193
194 /* first, find an upper bound starting from a reasonable guess */
195 lower = 0;
196 upper = 1024 * 1024; /* start with 1MB */
197 while (check_position(fd, upper)) {
198 lower = upper;
199 upper <<= 2;
200 }
201
202 /* second, nail down the size between the lower and upper bounds */
203 while (upper > lower + 1) {
204 current = (lower + upper) >> 1;
205 if (check_position(fd, current))
206 lower = current;
207 else
208 upper = current;
209 }
210 fs->c.size_known = 1;
211 fs->c.size = lower + 1;
212
213 #if DEBUG_SIZE
214 printf("Size: Binary search reports %llu\n", fs->c.size);
215 #endif
216 }
217 #endif
218
219 return (SOURCE *)fs;
220 }
221
222 /*
223 * special handling hook: devices may have out-of-band structure
224 */
225
226 static int analyze_file(SOURCE *s, int level)
227 {
228 if (analyze_cdaccess(((FILE_SOURCE *)s)->fd, s, level))
229 return 1;
230
231 return 0;
232 }
233
234 /*
235 * raw read
236 */
237
238 static u8 read_file(SOURCE *s, u8 pos, u8 len, void *buf)
239 {
240 off_t result_seek;
241 ssize_t result_read;
242 char *p;
243 u8 got;
244 int fd = ((FILE_SOURCE *)s)->fd;
245
246 /* seek to the requested position */
247 result_seek = lseek(fd, pos, SEEK_SET);
248 if (result_seek != pos) {
249 errore("Seek to %llu failed", pos);
250 return 0;
251 }
252
253 /* read from there */
254 p = (char *)buf;
255 got = 0;
256 while (len > 0) {
257 result_read = read(fd, p, len);
258 if (result_read < 0) {
259 if (errno == EINTR || errno == EAGAIN)
260 continue;
261 errore("Data read failed at position %llu", pos + got);
262 break;
263 } else if (result_read == 0) {
264 /* simple EOF, no message */
265 break;
266 } else {
267 len -= result_read;
268 got += result_read;
269 p += result_read;
270 }
271 }
272
273 return got;
274 }
275
276 /*
277 * dispose of everything
278 */
279
280 static void close_file(SOURCE *s)
281 {
282 int fd = ((FILE_SOURCE *)s)->fd;
283
284 if (fd >= 0)
285 close(fd);
286 }
287
288 /*
289 * check if the given position is inside the file's size
290 */
291
292 #if USE_BINARY_SEARCH
293 static int check_position(int fd, u8 pos)
294 {
295 char buf[2];
296
297 #if DEBUG_SIZE
298 printf(" Probing %llu\n", pos);
299 #endif
300
301 if (lseek(fd, pos, SEEK_SET) != pos)
302 return 0;
303 if (read(fd, buf, 1) != 1)
304 return 0;
305 return 1;
306 }
307 #endif
308
309 /* EOF */