| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 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 |
|
| 49 |
|
| 50 |
|
| 51 |
typedef struct file_source { |
| 52 |
SOURCE c; |
| 53 |
int fd; |
| 54 |
} FILE_SOURCE; |
| 55 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 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) |
| 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 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 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 |
|
| 111 |
|
| 112 |
|
| 113 |
#ifdef BLKGETSIZE64 |
| 114 |
#define u64 __u64 |
| 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 |
|
| 144 |
|
| 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 |
|
| 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 |
|
| 162 |
|
| 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 |
|
| 183 |
|
| 184 |
|
| 185 |
if (!fs->c.size_known) { |
| 186 |
u8 lower, upper, current; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
#if DEBUG_SIZE |
| 191 |
printf("Size: Doing a binary search\n"); |
| 192 |
#endif |
| 193 |
|
| 194 |
|
| 195 |
lower = 0; |
| 196 |
upper = 1024 * 1024; |
| 197 |
while (check_position(fd, upper)) { |
| 198 |
lower = upper; |
| 199 |
upper <<= 2; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|