1 /*- 2 * Copyright (c) 2003-2010 Tim Kientzle 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "archive_platform.h" 27 28 #ifdef HAVE_SYS_IOCTL_H 29 #include <sys/ioctl.h> 30 #endif 31 #ifdef HAVE_SYS_STAT_H 32 #include <sys/stat.h> 33 #endif 34 #ifdef HAVE_ERRNO_H 35 #include <errno.h> 36 #endif 37 #ifdef HAVE_FCNTL_H 38 #include <fcntl.h> 39 #endif 40 #ifdef HAVE_IO_H 41 #include <io.h> 42 #endif 43 #ifdef HAVE_STDLIB_H 44 #include <stdlib.h> 45 #endif 46 #ifdef HAVE_STRING_H 47 #include <string.h> 48 #endif 49 #ifdef HAVE_UNISTD_H 50 #include <unistd.h> 51 #endif 52 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 53 #include <sys/disk.h> 54 #elif defined(__NetBSD__) || defined(__OpenBSD__) 55 #include <sys/disklabel.h> 56 #include <sys/dkio.h> 57 #elif defined(__DragonFly__) 58 #include <sys/diskslice.h> 59 #endif 60 61 #include "archive.h" 62 #include "archive_platform_stat.h" 63 #include "archive_private.h" 64 #include "archive_string.h" 65 66 #ifndef O_BINARY 67 #define O_BINARY 0 68 #endif 69 #ifndef O_CLOEXEC 70 #define O_CLOEXEC 0 71 #endif 72 73 struct read_file_data { 74 int fd; 75 size_t block_size; 76 void *buffer; 77 mode_t st_mode; /* Mode bits for opened file. */ 78 int64_t size; 79 char use_lseek; 80 enum fnt_e { FNT_STDIN, FNT_MBS, FNT_WCS } filename_type; 81 union { 82 char m[1];/* MBS filename. */ 83 wchar_t w[1];/* WCS filename. */ 84 } filename; /* Must be last! */ 85 }; 86 87 static int file_open(struct archive *, void *); 88 static int file_close(struct archive *, void *); 89 static int file_close2(struct archive *, void *); 90 static int file_switch(struct archive *, void *, void *); 91 static ssize_t file_read(struct archive *, void *, const void **buff); 92 static int64_t file_seek(struct archive *, void *, int64_t request, int); 93 static int64_t file_skip(struct archive *, void *, int64_t request); 94 static int64_t file_skip_lseek(struct archive *, void *, int64_t request); 95 96 int 97 archive_read_open_file(struct archive *a, const char *filename, 98 size_t block_size) 99 { 100 return (archive_read_open_filename(a, filename, block_size)); 101 } 102 103 int 104 archive_read_open_filename(struct archive *a, const char *filename, 105 size_t block_size) 106 { 107 const char *filenames[2]; 108 filenames[0] = filename; 109 filenames[1] = NULL; 110 return archive_read_open_filenames(a, filenames, block_size); 111 } 112 113 int 114 archive_read_open_filenames(struct archive *a, const char **filenames, 115 size_t block_size) 116 { 117 struct read_file_data *mine; 118 const char *filename = NULL; 119 if (filenames) 120 filename = *(filenames++); 121 122 archive_clear_error(a); 123 do 124 { 125 size_t len; 126 if (filename == NULL) 127 filename = ""; 128 len = strlen(filename); 129 mine = calloc(1, sizeof(*mine) + len); 130 if (mine == NULL) 131 goto no_memory; 132 memcpy(mine->filename.m, filename, len + 1); 133 mine->block_size = block_size; 134 mine->fd = -1; 135 mine->buffer = NULL; 136 mine->st_mode = mine->use_lseek = 0; 137 if (filename == NULL || filename[0] == '\0') { 138 mine->filename_type = FNT_STDIN; 139 } else 140 mine->filename_type = FNT_MBS; 141 if (archive_read_append_callback_data(a, mine) != (ARCHIVE_OK)) { 142 free(mine); 143 return (ARCHIVE_FATAL); 144 } 145 if (filenames == NULL) 146 break; 147 filename = *(filenames++); 148 } while (filename != NULL && filename[0] != '\0'); 149 archive_read_set_open_callback(a, file_open); 150 archive_read_set_read_callback(a, file_read); 151 archive_read_set_skip_callback(a, file_skip); 152 archive_read_set_close_callback(a, file_close); 153 archive_read_set_switch_callback(a, file_switch); 154 archive_read_set_seek_callback(a, file_seek); 155 156 return (archive_read_open1(a)); 157 no_memory: 158 archive_set_error(a, ENOMEM, "No memory"); 159 return (ARCHIVE_FATAL); 160 } 161 162 /* 163 * This function is an implementation detail of archive_read_open_filename_w, 164 * which is exposed as a separate API on Windows. 165 */ 166 #if !defined(_WIN32) || defined(__CYGWIN__) 167 static 168 #endif 169 int 170 archive_read_open_filenames_w(struct archive *a, const wchar_t **wfilenames, 171 size_t block_size) 172 { 173 struct read_file_data *mine; 174 const wchar_t *wfilename = NULL; 175 if (wfilenames) 176 wfilename = *(wfilenames++); 177 178 archive_clear_error(a); 179 do 180 { 181 if (wfilename == NULL) 182 wfilename = L""; 183 mine = calloc(1, 184 sizeof(*mine) + wcslen(wfilename) * sizeof(wchar_t)); 185 if (mine == NULL) 186 goto no_memory; 187 mine->block_size = block_size; 188 mine->fd = -1; 189 190 if (wfilename == NULL || wfilename[0] == L'\0') { 191 mine->filename_type = FNT_STDIN; 192 } else { 193 #if defined(_WIN32) && !defined(__CYGWIN__) 194 mine->filename_type = FNT_WCS; 195 wcscpy(mine->filename.w, wfilename); 196 #else 197 /* 198 * POSIX system does not support a wchar_t interface for 199 * open() system call, so we have to translate a wchar_t 200 * filename to multi-byte one and use it. 201 */ 202 struct archive_string fn; 203 204 archive_string_init(&fn); 205 if (archive_string_append_from_wcs(&fn, wfilename, 206 wcslen(wfilename)) != 0) { 207 if (errno == ENOMEM) 208 archive_set_error(a, errno, 209 "Can't allocate memory"); 210 else 211 archive_set_error(a, EINVAL, 212 "Failed to convert a wide-character" 213 " filename to a multi-byte filename"); 214 archive_string_free(&fn); 215 free(mine); 216 return (ARCHIVE_FATAL); 217 } 218 mine->filename_type = FNT_MBS; 219 strcpy(mine->filename.m, fn.s); 220 archive_string_free(&fn); 221 #endif 222 } 223 if (archive_read_append_callback_data(a, mine) != (ARCHIVE_OK)) { 224 free(mine); 225 return (ARCHIVE_FATAL); 226 } 227 if (wfilenames == NULL) 228 break; 229 wfilename = *(wfilenames++); 230 } while (wfilename != NULL && wfilename[0] != '\0'); 231 archive_read_set_open_callback(a, file_open); 232 archive_read_set_read_callback(a, file_read); 233 archive_read_set_skip_callback(a, file_skip); 234 archive_read_set_close_callback(a, file_close); 235 archive_read_set_switch_callback(a, file_switch); 236 archive_read_set_seek_callback(a, file_seek); 237 238 return (archive_read_open1(a)); 239 no_memory: 240 archive_set_error(a, ENOMEM, "No memory"); 241 return (ARCHIVE_FATAL); 242 } 243 244 int 245 archive_read_open_filename_w(struct archive *a, const wchar_t *wfilename, 246 size_t block_size) 247 { 248 const wchar_t *wfilenames[2]; 249 wfilenames[0] = wfilename; 250 wfilenames[1] = NULL; 251 return archive_read_open_filenames_w(a, wfilenames, block_size); 252 } 253 254 static int 255 file_open(struct archive *a, void *client_data) 256 { 257 la_seek_stat_t st; 258 struct read_file_data *mine = (struct read_file_data *)client_data; 259 void *buffer; 260 const char *filename = NULL; 261 #if defined(_WIN32) && !defined(__CYGWIN__) 262 const wchar_t *wfilename = NULL; 263 #endif 264 int fd = -1; 265 int is_disk_like = 0; 266 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 267 off_t mediasize = 0; /* FreeBSD-specific, so off_t okay here. */ 268 #elif defined(__NetBSD__) || defined(__OpenBSD__) 269 struct disklabel dl; 270 #elif defined(__DragonFly__) 271 struct partinfo pi; 272 #endif 273 274 archive_clear_error(a); 275 if (mine->filename_type == FNT_STDIN) { 276 /* We used to delegate stdin support by 277 * directly calling archive_read_open_fd(a,0,block_size) 278 * here, but that doesn't (and shouldn't) handle the 279 * end-of-file flush when reading stdout from a pipe. 280 * Basically, read_open_fd() is intended for folks who 281 * are willing to handle such details themselves. This 282 * API is intended to be a little smarter for folks who 283 * want easy handling of the common case. 284 */ 285 fd = 0; 286 #if defined(__CYGWIN__) || defined(_WIN32) 287 setmode(0, O_BINARY); 288 #endif 289 filename = ""; 290 } else if (mine->filename_type == FNT_MBS) { 291 filename = mine->filename.m; 292 fd = open(filename, O_RDONLY | O_BINARY | O_CLOEXEC); 293 __archive_ensure_cloexec_flag(fd); 294 if (fd < 0) { 295 archive_set_error(a, errno, 296 "Failed to open '%s'", filename); 297 return (ARCHIVE_FATAL); 298 } 299 } else { 300 #if defined(_WIN32) && !defined(__CYGWIN__) 301 wfilename = mine->filename.w; 302 fd = _wopen(wfilename, O_RDONLY | O_BINARY); 303 if (fd < 0 && errno == ENOENT) { 304 wchar_t *fullpath; 305 fullpath = __la_win_permissive_name_w(wfilename); 306 if (fullpath != NULL) { 307 fd = _wopen(fullpath, O_RDONLY | O_BINARY); 308 free(fullpath); 309 } 310 } 311 if (fd < 0) { 312 archive_set_error(a, errno, 313 "Failed to open '%ls'", wfilename); 314 return (ARCHIVE_FATAL); 315 } 316 #else 317 archive_set_error(a, ARCHIVE_ERRNO_MISC, 318 "Unexpedted operation in archive_read_open_filename"); 319 goto fail; 320 #endif 321 } 322 if (la_seek_fstat(fd, &st) != 0) { 323 #if defined(_WIN32) && !defined(__CYGWIN__) 324 if (mine->filename_type == FNT_WCS) 325 archive_set_error(a, errno, "Can't stat '%ls'", 326 wfilename); 327 else 328 #endif 329 archive_set_error(a, errno, "Can't stat '%s'", 330 filename); 331 goto fail; 332 } 333 334 /* 335 * Determine whether the input looks like a disk device or a 336 * tape device. The results are used below to select an I/O 337 * strategy: 338 * = "disk-like" devices support arbitrary lseek() and will 339 * support I/O requests of any size. So we get easy skipping 340 * and can cheat on block sizes to get better performance. 341 * = "tape-like" devices require strict blocking and use 342 * specialized ioctls for seeking. 343 * = "socket-like" devices cannot seek at all but can improve 344 * performance by using nonblocking I/O to read "whatever is 345 * available right now". 346 * 347 * Right now, we only specially recognize disk-like devices, 348 * but it should be straightforward to add probes and strategy 349 * here for tape-like and socket-like devices. 350 */ 351 if (S_ISREG(st.st_mode)) { 352 /* Safety: Tell the extractor not to overwrite the input. */ 353 archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino); 354 /* Regular files act like disks. */ 355 is_disk_like = 1; 356 } 357 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 358 /* FreeBSD: if it supports DIOCGMEDIASIZE ioctl, it's disk-like. */ 359 else if (S_ISCHR(st.st_mode) && 360 ioctl(fd, DIOCGMEDIASIZE, &mediasize) == 0 && 361 mediasize > 0) { 362 is_disk_like = 1; 363 } 364 #elif defined(__NetBSD__) || defined(__OpenBSD__) 365 /* Net/OpenBSD: if it supports DIOCGDINFO ioctl, it's disk-like. */ 366 else if ((S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) && 367 ioctl(fd, DIOCGDINFO, &dl) == 0 && 368 dl.d_partitions[DISKPART(st.st_rdev)].p_size > 0) { 369 is_disk_like = 1; 370 } 371 #elif defined(__DragonFly__) 372 /* DragonFly BSD: if it supports DIOCGPART ioctl, it's disk-like. */ 373 else if (S_ISCHR(st.st_mode) && 374 ioctl(fd, DIOCGPART, &pi) == 0 && 375 pi.media_size > 0) { 376 is_disk_like = 1; 377 } 378 #elif defined(__linux__) 379 /* Linux: All block devices are disk-like. */ 380 else if (S_ISBLK(st.st_mode) && 381 lseek(fd, 0, SEEK_CUR) == 0 && 382 lseek(fd, 0, SEEK_SET) == 0 && 383 lseek(fd, 0, SEEK_END) > 0 && 384 lseek(fd, 0, SEEK_SET) == 0) { 385 is_disk_like = 1; 386 } 387 #endif 388 /* TODO: Add an "is_tape_like" variable and appropriate tests. */ 389 390 /* Disk-like devices prefer power-of-two block sizes. */ 391 /* Use provided block_size as a guide so users have some control. */ 392 if (is_disk_like) { 393 size_t new_block_size = 64 * 1024; 394 while (new_block_size < mine->block_size 395 && new_block_size < 64 * 1024 * 1024) 396 new_block_size *= 2; 397 mine->block_size = new_block_size; 398 } 399 buffer = malloc(mine->block_size); 400 if (buffer == NULL) { 401 archive_set_error(a, ENOMEM, "No memory"); 402 goto fail; 403 } 404 mine->buffer = buffer; 405 mine->fd = fd; 406 /* Remember mode so close can decide whether to flush. */ 407 mine->st_mode = st.st_mode; 408 409 /* Disk-like inputs can use lseek(). */ 410 if (is_disk_like) { 411 mine->use_lseek = 1; 412 mine->size = st.st_size; 413 } 414 415 return (ARCHIVE_OK); 416 fail: 417 /* 418 * Don't close file descriptors not opened or ones pointing referring 419 * to `FNT_STDIN`. 420 */ 421 if (fd != -1 && fd != 0) 422 close(fd); 423 return (ARCHIVE_FATAL); 424 } 425 426 static ssize_t 427 file_read(struct archive *a, void *client_data, const void **buff) 428 { 429 struct read_file_data *mine = (struct read_file_data *)client_data; 430 ssize_t bytes_read; 431 432 /* TODO: If a recent lseek() operation has left us 433 * mis-aligned, read and return a short block to try to get 434 * us back in alignment. */ 435 436 /* TODO: Someday, try mmap() here; if that succeeds, give 437 * the entire file to libarchive as a single block. That 438 * could be a lot faster than block-by-block manual I/O. */ 439 440 /* TODO: We might be able to improve performance on pipes and 441 * sockets by setting non-blocking I/O and just accepting 442 * whatever we get here instead of waiting for a full block 443 * worth of data. */ 444 445 *buff = mine->buffer; 446 for (;;) { 447 bytes_read = read(mine->fd, mine->buffer, mine->block_size); 448 if (bytes_read < 0) { 449 if (errno == EINTR) 450 continue; 451 else if (mine->filename_type == FNT_STDIN) 452 archive_set_error(a, errno, 453 "Error reading stdin"); 454 else if (mine->filename_type == FNT_MBS) 455 archive_set_error(a, errno, 456 "Error reading '%s'", mine->filename.m); 457 else 458 archive_set_error(a, errno, 459 "Error reading '%ls'", mine->filename.w); 460 } 461 return (bytes_read); 462 } 463 } 464 465 /* 466 * Regular files and disk-like block devices can use simple lseek 467 * without needing to round the request to the block size. 468 * 469 * TODO: This can leave future reads mis-aligned. Since we know the 470 * offset here, we should store it and use it in file_read() above 471 * to determine whether we should perform a short read to get back 472 * into alignment. Long series of mis-aligned reads can negatively 473 * impact disk throughput. (Of course, the performance impact should 474 * be carefully tested; extra code complexity is only worthwhile if 475 * it does provide measurable improvement.) 476 * 477 * TODO: Be lazy about the actual seek. There are a few pathological 478 * cases where libarchive makes a bunch of seek requests in a row 479 * without any intervening reads. This isn't a huge performance 480 * problem, since the kernel handles seeks lazily already, but 481 * it would be very slightly faster if we simply remembered the 482 * seek request here and then actually performed the seek at the 483 * top of the read callback above. 484 */ 485 static int64_t 486 file_skip_lseek(struct archive *a, void *client_data, int64_t request) 487 { 488 struct read_file_data *mine = (struct read_file_data *)client_data; 489 #if defined(_WIN32) && !defined(__CYGWIN__) 490 /* We use _lseeki64() on Windows. */ 491 int64_t old_offset, new_offset; 492 #else 493 off_t old_offset, new_offset; 494 #endif 495 la_seek_t skip = (la_seek_t)request; 496 int skip_bits = sizeof(skip) * 8 - 1; 497 498 /* We use off_t here because lseek() is declared that way. */ 499 500 /* Reduce a request that would overflow the 'skip' variable. */ 501 if (sizeof(request) > sizeof(skip)) { 502 const int64_t max_skip = 503 (((int64_t)1 << (skip_bits - 1)) - 1) * 2 + 1; 504 if (request > max_skip) 505 skip = max_skip; 506 } 507 508 if ((old_offset = lseek(mine->fd, 0, SEEK_CUR)) >= 0) { 509 if (old_offset >= mine->size || 510 skip > mine->size - old_offset) { 511 /* Do not seek past end of file. */ 512 errno = ESPIPE; 513 } else if ((new_offset = lseek(mine->fd, skip, SEEK_CUR)) >= 0) 514 return (new_offset - old_offset); 515 } 516 517 /* If lseek() fails, don't bother trying again. */ 518 mine->use_lseek = 0; 519 520 /* Let libarchive recover with read+discard */ 521 if (errno == ESPIPE) 522 return (0); 523 524 /* If the input is corrupted or truncated, fail. */ 525 if (mine->filename_type == FNT_STDIN) 526 archive_set_error(a, errno, "Error seeking in stdin"); 527 else if (mine->filename_type == FNT_MBS) 528 archive_set_error(a, errno, "Error seeking in '%s'", 529 mine->filename.m); 530 else 531 archive_set_error(a, errno, "Error seeking in '%ls'", 532 mine->filename.w); 533 return (-1); 534 } 535 536 537 /* 538 * TODO: Implement another file_skip_XXXX that uses MTIO ioctls to 539 * accelerate operation on tape drives. 540 */ 541 542 static int64_t 543 file_skip(struct archive *a, void *client_data, int64_t request) 544 { 545 struct read_file_data *mine = (struct read_file_data *)client_data; 546 547 /* Delegate skip requests. */ 548 if (mine->use_lseek) 549 return (file_skip_lseek(a, client_data, request)); 550 551 /* If we can't skip, return 0; libarchive will read+discard instead. */ 552 return (0); 553 } 554 555 /* 556 * TODO: Store the offset and use it in the read callback. 557 */ 558 static int64_t 559 file_seek(struct archive *a, void *client_data, int64_t request, int whence) 560 { 561 struct read_file_data *mine = (struct read_file_data *)client_data; 562 la_seek_t seek = (la_seek_t)request; 563 int64_t r; 564 int seek_bits = sizeof(seek) * 8 - 1; 565 566 /* We use off_t here because lseek() is declared that way. */ 567 568 /* Do not perform a seek which cannot be fulfilled. */ 569 if (sizeof(request) > sizeof(seek)) { 570 const int64_t max_seek = 571 (((int64_t)1 << (seek_bits - 1)) - 1) * 2 + 1; 572 const int64_t min_seek = ~max_seek; 573 if (request < min_seek || request > max_seek) { 574 errno = EOVERFLOW; 575 goto err; 576 } 577 } 578 579 r = lseek(mine->fd, seek, whence); 580 if (r >= 0) 581 return r; 582 583 /* If the input is corrupted or truncated, fail. */ 584 err: 585 if (mine->filename_type == FNT_STDIN) 586 archive_set_error(a, errno, "Error seeking in stdin"); 587 else if (mine->filename_type == FNT_MBS) 588 archive_set_error(a, errno, "Error seeking in '%s'", 589 mine->filename.m); 590 else 591 archive_set_error(a, errno, "Error seeking in '%ls'", 592 mine->filename.w); 593 return (ARCHIVE_FATAL); 594 } 595 596 static int 597 file_close2(struct archive *a, void *client_data) 598 { 599 struct read_file_data *mine = (struct read_file_data *)client_data; 600 601 (void)a; /* UNUSED */ 602 603 /* Only flush and close if open succeeded. */ 604 if (mine->fd >= 0) { 605 /* 606 * Sometimes, we should flush the input before closing. 607 * Regular files: faster to just close without flush. 608 * Disk-like devices: Ditto. 609 * Tapes: must not flush (user might need to 610 * read the "next" item on a non-rewind device). 611 * Pipes and sockets: must flush (otherwise, the 612 * program feeding the pipe or socket may complain). 613 * Here, I flush everything except for regular files and 614 * device nodes. 615 */ 616 if (!S_ISREG(mine->st_mode) 617 && !S_ISCHR(mine->st_mode) 618 && !S_ISBLK(mine->st_mode)) { 619 ssize_t bytesRead; 620 do { 621 bytesRead = read(mine->fd, mine->buffer, 622 mine->block_size); 623 } while (bytesRead > 0); 624 } 625 /* If a named file was opened, then it needs to be closed. */ 626 if (mine->filename_type != FNT_STDIN) 627 close(mine->fd); 628 } 629 free(mine->buffer); 630 mine->buffer = NULL; 631 mine->fd = -1; 632 return (ARCHIVE_OK); 633 } 634 635 static int 636 file_close(struct archive *a, void *client_data) 637 { 638 struct read_file_data *mine = (struct read_file_data *)client_data; 639 file_close2(a, client_data); 640 free(mine); 641 return (ARCHIVE_OK); 642 } 643 644 static int 645 file_switch(struct archive *a, void *client_data1, void *client_data2) 646 { 647 file_close2(a, client_data1); 648 return file_open(a, client_data2); 649 } 650