1228753Smm/*- 2232153Smm * Copyright (c) 2003-2010 Tim Kientzle 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10228753Smm * 2. Redistributions in binary form must reproduce the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer in the 12228753Smm * documentation and/or other materials provided with the distribution. 13228753Smm * 14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24228753Smm */ 25228753Smm 26228753Smm#include "archive_platform.h" 27228763Smm__FBSDID("$FreeBSD: stable/11/contrib/libarchive/libarchive/archive_read_open_filename.c 358926 2020-03-13 01:05:55Z mm $"); 28228753Smm 29232153Smm#ifdef HAVE_SYS_IOCTL_H 30232153Smm#include <sys/ioctl.h> 31232153Smm#endif 32228753Smm#ifdef HAVE_SYS_STAT_H 33228753Smm#include <sys/stat.h> 34228753Smm#endif 35228753Smm#ifdef HAVE_ERRNO_H 36228753Smm#include <errno.h> 37228753Smm#endif 38228753Smm#ifdef HAVE_FCNTL_H 39228753Smm#include <fcntl.h> 40228753Smm#endif 41228753Smm#ifdef HAVE_IO_H 42228753Smm#include <io.h> 43228753Smm#endif 44228753Smm#ifdef HAVE_STDLIB_H 45228753Smm#include <stdlib.h> 46228753Smm#endif 47228753Smm#ifdef HAVE_STRING_H 48228753Smm#include <string.h> 49228753Smm#endif 50228753Smm#ifdef HAVE_UNISTD_H 51228753Smm#include <unistd.h> 52228753Smm#endif 53232153Smm#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 54232153Smm#include <sys/disk.h> 55232153Smm#elif defined(__NetBSD__) || defined(__OpenBSD__) 56232153Smm#include <sys/disklabel.h> 57232153Smm#include <sys/dkio.h> 58232153Smm#elif defined(__DragonFly__) 59232153Smm#include <sys/diskslice.h> 60232153Smm#endif 61228753Smm 62228753Smm#include "archive.h" 63248616Smm#include "archive_private.h" 64232153Smm#include "archive_string.h" 65228753Smm 66228753Smm#ifndef O_BINARY 67228753Smm#define O_BINARY 0 68228753Smm#endif 69248616Smm#ifndef O_CLOEXEC 70248616Smm#define O_CLOEXEC 0 71248616Smm#endif 72228753Smm 73228753Smmstruct read_file_data { 74228753Smm int fd; 75228753Smm size_t block_size; 76228753Smm void *buffer; 77228753Smm mode_t st_mode; /* Mode bits for opened file. */ 78232153Smm char use_lseek; 79232153Smm enum fnt_e { FNT_STDIN, FNT_MBS, FNT_WCS } filename_type; 80232153Smm union { 81232153Smm char m[1];/* MBS filename. */ 82232153Smm wchar_t w[1];/* WCS filename. */ 83232153Smm } filename; /* Must be last! */ 84228753Smm}; 85228753Smm 86248616Smmstatic int file_open(struct archive *, void *); 87228753Smmstatic int file_close(struct archive *, void *); 88248616Smmstatic int file_close2(struct archive *, void *); 89248616Smmstatic int file_switch(struct archive *, void *, void *); 90228753Smmstatic ssize_t file_read(struct archive *, void *, const void **buff); 91232153Smmstatic int64_t file_seek(struct archive *, void *, int64_t request, int); 92232153Smmstatic int64_t file_skip(struct archive *, void *, int64_t request); 93232153Smmstatic int64_t file_skip_lseek(struct archive *, void *, int64_t request); 94228753Smm 95228753Smmint 96228753Smmarchive_read_open_file(struct archive *a, const char *filename, 97228753Smm size_t block_size) 98228753Smm{ 99228753Smm return (archive_read_open_filename(a, filename, block_size)); 100228753Smm} 101228753Smm 102228753Smmint 103228753Smmarchive_read_open_filename(struct archive *a, const char *filename, 104228753Smm size_t block_size) 105228753Smm{ 106299529Smm const char *filenames[2]; 107299529Smm filenames[0] = filename; 108299529Smm filenames[1] = NULL; 109248616Smm return archive_read_open_filenames(a, filenames, block_size); 110248616Smm} 111232153Smm 112248616Smmint 113248616Smmarchive_read_open_filenames(struct archive *a, const char **filenames, 114248616Smm size_t block_size) 115248616Smm{ 116248616Smm struct read_file_data *mine; 117248616Smm const char *filename = NULL; 118248616Smm if (filenames) 119248616Smm filename = *(filenames++); 120248616Smm 121248616Smm archive_clear_error(a); 122248616Smm do 123248616Smm { 124248616Smm if (filename == NULL) 125248616Smm filename = ""; 126248616Smm mine = (struct read_file_data *)calloc(1, 127248616Smm sizeof(*mine) + strlen(filename)); 128248616Smm if (mine == NULL) 129248616Smm goto no_memory; 130248616Smm strcpy(mine->filename.m, filename); 131248616Smm mine->block_size = block_size; 132248616Smm mine->fd = -1; 133248616Smm mine->buffer = NULL; 134248616Smm mine->st_mode = mine->use_lseek = 0; 135248616Smm if (filename == NULL || filename[0] == '\0') { 136248616Smm mine->filename_type = FNT_STDIN; 137248616Smm } else 138248616Smm mine->filename_type = FNT_MBS; 139248616Smm if (archive_read_append_callback_data(a, mine) != (ARCHIVE_OK)) 140248616Smm return (ARCHIVE_FATAL); 141248616Smm if (filenames == NULL) 142248616Smm break; 143248616Smm filename = *(filenames++); 144248616Smm } while (filename != NULL && filename[0] != '\0'); 145248616Smm archive_read_set_open_callback(a, file_open); 146248616Smm archive_read_set_read_callback(a, file_read); 147248616Smm archive_read_set_skip_callback(a, file_skip); 148248616Smm archive_read_set_close_callback(a, file_close); 149248616Smm archive_read_set_switch_callback(a, file_switch); 150248616Smm archive_read_set_seek_callback(a, file_seek); 151248616Smm 152248616Smm return (archive_read_open1(a)); 153248616Smmno_memory: 154248616Smm archive_set_error(a, ENOMEM, "No memory"); 155248616Smm return (ARCHIVE_FATAL); 156232153Smm} 157232153Smm 158232153Smmint 159232153Smmarchive_read_open_filename_w(struct archive *a, const wchar_t *wfilename, 160232153Smm size_t block_size) 161232153Smm{ 162248616Smm struct read_file_data *mine = (struct read_file_data *)calloc(1, 163248616Smm sizeof(*mine) + wcslen(wfilename) * sizeof(wchar_t)); 164248616Smm if (!mine) 165248616Smm { 166248616Smm archive_set_error(a, ENOMEM, "No memory"); 167248616Smm return (ARCHIVE_FATAL); 168248616Smm } 169248616Smm mine->fd = -1; 170248616Smm mine->block_size = block_size; 171232153Smm 172232153Smm if (wfilename == NULL || wfilename[0] == L'\0') { 173248616Smm mine->filename_type = FNT_STDIN; 174232153Smm } else { 175232153Smm#if defined(_WIN32) && !defined(__CYGWIN__) 176248616Smm mine->filename_type = FNT_WCS; 177248616Smm wcscpy(mine->filename.w, wfilename); 178232153Smm#else 179232153Smm /* 180232153Smm * POSIX system does not support a wchar_t interface for 181299529Smm * open() system call, so we have to translate a wchar_t 182232153Smm * filename to multi-byte one and use it. 183232153Smm */ 184232153Smm struct archive_string fn; 185232153Smm 186232153Smm archive_string_init(&fn); 187232153Smm if (archive_string_append_from_wcs(&fn, wfilename, 188232153Smm wcslen(wfilename)) != 0) { 189238856Smm if (errno == ENOMEM) 190238856Smm archive_set_error(a, errno, 191238856Smm "Can't allocate memory"); 192238856Smm else 193238856Smm archive_set_error(a, EINVAL, 194238856Smm "Failed to convert a wide-character" 195238856Smm " filename to a multi-byte filename"); 196232153Smm archive_string_free(&fn); 197248616Smm free(mine); 198232153Smm return (ARCHIVE_FATAL); 199232153Smm } 200248616Smm mine->filename_type = FNT_MBS; 201248616Smm strcpy(mine->filename.m, fn.s); 202232153Smm archive_string_free(&fn); 203232153Smm#endif 204232153Smm } 205248616Smm if (archive_read_append_callback_data(a, mine) != (ARCHIVE_OK)) 206248616Smm return (ARCHIVE_FATAL); 207248616Smm archive_read_set_open_callback(a, file_open); 208248616Smm archive_read_set_read_callback(a, file_read); 209248616Smm archive_read_set_skip_callback(a, file_skip); 210248616Smm archive_read_set_close_callback(a, file_close); 211248616Smm archive_read_set_switch_callback(a, file_switch); 212248616Smm archive_read_set_seek_callback(a, file_seek); 213248616Smm 214248616Smm return (archive_read_open1(a)); 215232153Smm} 216232153Smm 217232153Smmstatic int 218248616Smmfile_open(struct archive *a, void *client_data) 219232153Smm{ 220228753Smm struct stat st; 221248616Smm struct read_file_data *mine = (struct read_file_data *)client_data; 222232153Smm void *buffer; 223232153Smm const char *filename = NULL; 224358926Smm#if defined(_WIN32) && !defined(__CYGWIN__) 225232153Smm const wchar_t *wfilename = NULL; 226358926Smm#endif 227313570Smm int fd = -1; 228232153Smm int is_disk_like = 0; 229232153Smm#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 230232153Smm off_t mediasize = 0; /* FreeBSD-specific, so off_t okay here. */ 231232153Smm#elif defined(__NetBSD__) || defined(__OpenBSD__) 232232153Smm struct disklabel dl; 233232153Smm#elif defined(__DragonFly__) 234232153Smm struct partinfo pi; 235232153Smm#endif 236228753Smm 237228753Smm archive_clear_error(a); 238248616Smm if (mine->filename_type == FNT_STDIN) { 239232153Smm /* We used to delegate stdin support by 240232153Smm * directly calling archive_read_open_fd(a,0,block_size) 241228753Smm * here, but that doesn't (and shouldn't) handle the 242228753Smm * end-of-file flush when reading stdout from a pipe. 243228753Smm * Basically, read_open_fd() is intended for folks who 244228753Smm * are willing to handle such details themselves. This 245228753Smm * API is intended to be a little smarter for folks who 246228753Smm * want easy handling of the common case. 247228753Smm */ 248228753Smm fd = 0; 249228753Smm#if defined(__CYGWIN__) || defined(_WIN32) 250228753Smm setmode(0, O_BINARY); 251228753Smm#endif 252232153Smm filename = ""; 253248616Smm } else if (mine->filename_type == FNT_MBS) { 254248616Smm filename = mine->filename.m; 255248616Smm fd = open(filename, O_RDONLY | O_BINARY | O_CLOEXEC); 256248616Smm __archive_ensure_cloexec_flag(fd); 257228753Smm if (fd < 0) { 258228753Smm archive_set_error(a, errno, 259228753Smm "Failed to open '%s'", filename); 260228753Smm return (ARCHIVE_FATAL); 261228753Smm } 262232153Smm } else { 263232153Smm#if defined(_WIN32) && !defined(__CYGWIN__) 264248616Smm wfilename = mine->filename.w; 265232153Smm fd = _wopen(wfilename, O_RDONLY | O_BINARY); 266232153Smm if (fd < 0 && errno == ENOENT) { 267232153Smm wchar_t *fullpath; 268232153Smm fullpath = __la_win_permissive_name_w(wfilename); 269232153Smm if (fullpath != NULL) { 270232153Smm fd = _wopen(fullpath, O_RDONLY | O_BINARY); 271232153Smm free(fullpath); 272232153Smm } 273232153Smm } 274232153Smm if (fd < 0) { 275232153Smm archive_set_error(a, errno, 276232153Smm "Failed to open '%S'", wfilename); 277232153Smm return (ARCHIVE_FATAL); 278232153Smm } 279232153Smm#else 280232153Smm archive_set_error(a, ARCHIVE_ERRNO_MISC, 281232153Smm "Unexpedted operation in archive_read_open_filename"); 282313570Smm goto fail; 283232153Smm#endif 284228753Smm } 285228753Smm if (fstat(fd, &st) != 0) { 286358926Smm#if defined(_WIN32) && !defined(__CYGWIN__) 287248616Smm if (mine->filename_type == FNT_WCS) 288232153Smm archive_set_error(a, errno, "Can't stat '%S'", 289232153Smm wfilename); 290232153Smm else 291358926Smm#endif 292232153Smm archive_set_error(a, errno, "Can't stat '%s'", 293232153Smm filename); 294313570Smm goto fail; 295228753Smm } 296228753Smm 297232153Smm /* 298232153Smm * Determine whether the input looks like a disk device or a 299232153Smm * tape device. The results are used below to select an I/O 300232153Smm * strategy: 301232153Smm * = "disk-like" devices support arbitrary lseek() and will 302232153Smm * support I/O requests of any size. So we get easy skipping 303232153Smm * and can cheat on block sizes to get better performance. 304232153Smm * = "tape-like" devices require strict blocking and use 305232153Smm * specialized ioctls for seeking. 306232153Smm * = "socket-like" devices cannot seek at all but can improve 307232153Smm * performance by using nonblocking I/O to read "whatever is 308232153Smm * available right now". 309232153Smm * 310232153Smm * Right now, we only specially recognize disk-like devices, 311232153Smm * but it should be straightforward to add probes and strategy 312232153Smm * here for tape-like and socket-like devices. 313232153Smm */ 314232153Smm if (S_ISREG(st.st_mode)) { 315232153Smm /* Safety: Tell the extractor not to overwrite the input. */ 316232153Smm archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino); 317232153Smm /* Regular files act like disks. */ 318232153Smm is_disk_like = 1; 319232153Smm } 320232153Smm#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 321232153Smm /* FreeBSD: if it supports DIOCGMEDIASIZE ioctl, it's disk-like. */ 322232153Smm else if (S_ISCHR(st.st_mode) && 323232153Smm ioctl(fd, DIOCGMEDIASIZE, &mediasize) == 0 && 324232153Smm mediasize > 0) { 325232153Smm is_disk_like = 1; 326232153Smm } 327232153Smm#elif defined(__NetBSD__) || defined(__OpenBSD__) 328232153Smm /* Net/OpenBSD: if it supports DIOCGDINFO ioctl, it's disk-like. */ 329232153Smm else if ((S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) && 330232153Smm ioctl(fd, DIOCGDINFO, &dl) == 0 && 331232153Smm dl.d_partitions[DISKPART(st.st_rdev)].p_size > 0) { 332232153Smm is_disk_like = 1; 333232153Smm } 334232153Smm#elif defined(__DragonFly__) 335232153Smm /* DragonFly BSD: if it supports DIOCGPART ioctl, it's disk-like. */ 336232153Smm else if (S_ISCHR(st.st_mode) && 337232153Smm ioctl(fd, DIOCGPART, &pi) == 0 && 338232153Smm pi.media_size > 0) { 339232153Smm is_disk_like = 1; 340232153Smm } 341232153Smm#elif defined(__linux__) 342232153Smm /* Linux: All block devices are disk-like. */ 343232153Smm else if (S_ISBLK(st.st_mode) && 344232153Smm lseek(fd, 0, SEEK_CUR) == 0 && 345232153Smm lseek(fd, 0, SEEK_SET) == 0 && 346232153Smm lseek(fd, 0, SEEK_END) > 0 && 347232153Smm lseek(fd, 0, SEEK_SET) == 0) { 348232153Smm is_disk_like = 1; 349232153Smm } 350232153Smm#endif 351232153Smm /* TODO: Add an "is_tape_like" variable and appropriate tests. */ 352232153Smm 353232153Smm /* Disk-like devices prefer power-of-two block sizes. */ 354232153Smm /* Use provided block_size as a guide so users have some control. */ 355232153Smm if (is_disk_like) { 356232153Smm size_t new_block_size = 64 * 1024; 357248616Smm while (new_block_size < mine->block_size 358232153Smm && new_block_size < 64 * 1024 * 1024) 359232153Smm new_block_size *= 2; 360248616Smm mine->block_size = new_block_size; 361232153Smm } 362248616Smm buffer = malloc(mine->block_size); 363313570Smm if (buffer == NULL) { 364228753Smm archive_set_error(a, ENOMEM, "No memory"); 365313570Smm goto fail; 366228753Smm } 367232153Smm mine->buffer = buffer; 368228753Smm mine->fd = fd; 369228753Smm /* Remember mode so close can decide whether to flush. */ 370228753Smm mine->st_mode = st.st_mode; 371232153Smm 372232153Smm /* Disk-like inputs can use lseek(). */ 373248616Smm if (is_disk_like) 374232153Smm mine->use_lseek = 1; 375232153Smm 376248616Smm return (ARCHIVE_OK); 377313570Smmfail: 378313570Smm /* 379313570Smm * Don't close file descriptors not opened or ones pointing referring 380313570Smm * to `FNT_STDIN`. 381313570Smm */ 382313570Smm if (fd != -1 && fd != 0) 383313570Smm close(fd); 384313570Smm return (ARCHIVE_FATAL); 385228753Smm} 386228753Smm 387228753Smmstatic ssize_t 388228753Smmfile_read(struct archive *a, void *client_data, const void **buff) 389228753Smm{ 390228753Smm struct read_file_data *mine = (struct read_file_data *)client_data; 391228753Smm ssize_t bytes_read; 392228753Smm 393232153Smm /* TODO: If a recent lseek() operation has left us 394232153Smm * mis-aligned, read and return a short block to try to get 395232153Smm * us back in alignment. */ 396232153Smm 397232153Smm /* TODO: Someday, try mmap() here; if that succeeds, give 398232153Smm * the entire file to libarchive as a single block. That 399232153Smm * could be a lot faster than block-by-block manual I/O. */ 400232153Smm 401232153Smm /* TODO: We might be able to improve performance on pipes and 402232153Smm * sockets by setting non-blocking I/O and just accepting 403232153Smm * whatever we get here instead of waiting for a full block 404232153Smm * worth of data. */ 405232153Smm 406228753Smm *buff = mine->buffer; 407228753Smm for (;;) { 408228753Smm bytes_read = read(mine->fd, mine->buffer, mine->block_size); 409228753Smm if (bytes_read < 0) { 410228753Smm if (errno == EINTR) 411228753Smm continue; 412232153Smm else if (mine->filename_type == FNT_STDIN) 413232153Smm archive_set_error(a, errno, 414232153Smm "Error reading stdin"); 415232153Smm else if (mine->filename_type == FNT_MBS) 416232153Smm archive_set_error(a, errno, 417232153Smm "Error reading '%s'", mine->filename.m); 418228753Smm else 419232153Smm archive_set_error(a, errno, 420232153Smm "Error reading '%S'", mine->filename.w); 421228753Smm } 422228753Smm return (bytes_read); 423228753Smm } 424228753Smm} 425228753Smm 426232153Smm/* 427232153Smm * Regular files and disk-like block devices can use simple lseek 428232153Smm * without needing to round the request to the block size. 429232153Smm * 430232153Smm * TODO: This can leave future reads mis-aligned. Since we know the 431232153Smm * offset here, we should store it and use it in file_read() above 432232153Smm * to determine whether we should perform a short read to get back 433232153Smm * into alignment. Long series of mis-aligned reads can negatively 434232153Smm * impact disk throughput. (Of course, the performance impact should 435232153Smm * be carefully tested; extra code complexity is only worthwhile if 436232153Smm * it does provide measurable improvement.) 437232153Smm * 438232153Smm * TODO: Be lazy about the actual seek. There are a few pathological 439232153Smm * cases where libarchive makes a bunch of seek requests in a row 440232153Smm * without any intervening reads. This isn't a huge performance 441232153Smm * problem, since the kernel handles seeks lazily already, but 442232153Smm * it would be very slightly faster if we simply remembered the 443232153Smm * seek request here and then actually performed the seek at the 444232153Smm * top of the read callback above. 445232153Smm */ 446232153Smmstatic int64_t 447232153Smmfile_skip_lseek(struct archive *a, void *client_data, int64_t request) 448228753Smm{ 449228753Smm struct read_file_data *mine = (struct read_file_data *)client_data; 450232153Smm#if defined(_WIN32) && !defined(__CYGWIN__) 451232153Smm /* We use _lseeki64() on Windows. */ 452232153Smm int64_t old_offset, new_offset; 453232153Smm#else 454228753Smm off_t old_offset, new_offset; 455232153Smm#endif 456228753Smm 457232153Smm /* We use off_t here because lseek() is declared that way. */ 458228753Smm 459232153Smm /* TODO: Deal with case where off_t isn't 64 bits. 460232153Smm * This shouldn't be a problem on Linux or other POSIX 461232153Smm * systems, since the configuration logic for libarchive 462248616Smm * tries to obtain a 64-bit off_t. 463232153Smm */ 464232153Smm if ((old_offset = lseek(mine->fd, 0, SEEK_CUR)) >= 0 && 465232153Smm (new_offset = lseek(mine->fd, request, SEEK_CUR)) >= 0) 466232153Smm return (new_offset - old_offset); 467232153Smm 468232153Smm /* If lseek() fails, don't bother trying again. */ 469232153Smm mine->use_lseek = 0; 470232153Smm 471232153Smm /* Let libarchive recover with read+discard */ 472232153Smm if (errno == ESPIPE) 473228753Smm return (0); 474228753Smm 475232153Smm /* If the input is corrupted or truncated, fail. */ 476232153Smm if (mine->filename_type == FNT_STDIN) 477232153Smm archive_set_error(a, errno, "Error seeking in stdin"); 478232153Smm else if (mine->filename_type == FNT_MBS) 479232153Smm archive_set_error(a, errno, "Error seeking in '%s'", 480232153Smm mine->filename.m); 481232153Smm else 482232153Smm archive_set_error(a, errno, "Error seeking in '%S'", 483232153Smm mine->filename.w); 484232153Smm return (-1); 485232153Smm} 486228753Smm 487232153Smm 488232153Smm/* 489232153Smm * TODO: Implement another file_skip_XXXX that uses MTIO ioctls to 490232153Smm * accelerate operation on tape drives. 491232153Smm */ 492232153Smm 493232153Smmstatic int64_t 494232153Smmfile_skip(struct archive *a, void *client_data, int64_t request) 495232153Smm{ 496232153Smm struct read_file_data *mine = (struct read_file_data *)client_data; 497232153Smm 498232153Smm /* Delegate skip requests. */ 499232153Smm if (mine->use_lseek) 500232153Smm return (file_skip_lseek(a, client_data, request)); 501232153Smm 502232153Smm /* If we can't skip, return 0; libarchive will read+discard instead. */ 503232153Smm return (0); 504228753Smm} 505228753Smm 506232153Smm/* 507232153Smm * TODO: Store the offset and use it in the read callback. 508232153Smm */ 509232153Smmstatic int64_t 510232153Smmfile_seek(struct archive *a, void *client_data, int64_t request, int whence) 511232153Smm{ 512232153Smm struct read_file_data *mine = (struct read_file_data *)client_data; 513238856Smm int64_t r; 514232153Smm 515232153Smm /* We use off_t here because lseek() is declared that way. */ 516232153Smm /* See above for notes about when off_t is less than 64 bits. */ 517232153Smm r = lseek(mine->fd, request, whence); 518232153Smm if (r >= 0) 519232153Smm return r; 520232153Smm 521232153Smm /* If the input is corrupted or truncated, fail. */ 522232153Smm if (mine->filename_type == FNT_STDIN) 523232153Smm archive_set_error(a, errno, "Error seeking in stdin"); 524232153Smm else if (mine->filename_type == FNT_MBS) 525232153Smm archive_set_error(a, errno, "Error seeking in '%s'", 526232153Smm mine->filename.m); 527232153Smm else 528232153Smm archive_set_error(a, errno, "Error seeking in '%S'", 529232153Smm mine->filename.w); 530232153Smm return (ARCHIVE_FATAL); 531232153Smm} 532232153Smm 533228753Smmstatic int 534248616Smmfile_close2(struct archive *a, void *client_data) 535228753Smm{ 536228753Smm struct read_file_data *mine = (struct read_file_data *)client_data; 537228753Smm 538228753Smm (void)a; /* UNUSED */ 539228753Smm 540228753Smm /* Only flush and close if open succeeded. */ 541228753Smm if (mine->fd >= 0) { 542228753Smm /* 543228753Smm * Sometimes, we should flush the input before closing. 544228753Smm * Regular files: faster to just close without flush. 545232153Smm * Disk-like devices: Ditto. 546232153Smm * Tapes: must not flush (user might need to 547228753Smm * read the "next" item on a non-rewind device). 548228753Smm * Pipes and sockets: must flush (otherwise, the 549228753Smm * program feeding the pipe or socket may complain). 550228753Smm * Here, I flush everything except for regular files and 551228753Smm * device nodes. 552228753Smm */ 553228753Smm if (!S_ISREG(mine->st_mode) 554228753Smm && !S_ISCHR(mine->st_mode) 555228753Smm && !S_ISBLK(mine->st_mode)) { 556228753Smm ssize_t bytesRead; 557228753Smm do { 558228753Smm bytesRead = read(mine->fd, mine->buffer, 559228753Smm mine->block_size); 560228753Smm } while (bytesRead > 0); 561228753Smm } 562228753Smm /* If a named file was opened, then it needs to be closed. */ 563232153Smm if (mine->filename_type != FNT_STDIN) 564228753Smm close(mine->fd); 565228753Smm } 566228753Smm free(mine->buffer); 567248616Smm mine->buffer = NULL; 568248616Smm mine->fd = -1; 569248616Smm return (ARCHIVE_OK); 570248616Smm} 571248616Smm 572248616Smmstatic int 573248616Smmfile_close(struct archive *a, void *client_data) 574248616Smm{ 575248616Smm struct read_file_data *mine = (struct read_file_data *)client_data; 576248616Smm file_close2(a, client_data); 577228753Smm free(mine); 578228753Smm return (ARCHIVE_OK); 579228753Smm} 580248616Smm 581248616Smmstatic int 582248616Smmfile_switch(struct archive *a, void *client_data1, void *client_data2) 583248616Smm{ 584248616Smm file_close2(a, client_data1); 585248616Smm return file_open(a, client_data2); 586248616Smm} 587