archive_read_open_filename.c revision 248616
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: head/contrib/libarchive/libarchive/archive_read_open_filename.c 248616 2013-03-22 13:36:03Z 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{ 106248616Smm const char *filenames[2] = { filename, NULL }; 107248616Smm return archive_read_open_filenames(a, filenames, block_size); 108248616Smm} 109232153Smm 110248616Smmint 111248616Smmarchive_read_open_filenames(struct archive *a, const char **filenames, 112248616Smm size_t block_size) 113248616Smm{ 114248616Smm struct read_file_data *mine; 115248616Smm const char *filename = NULL; 116248616Smm if (filenames) 117248616Smm filename = *(filenames++); 118248616Smm 119248616Smm archive_clear_error(a); 120248616Smm do 121248616Smm { 122248616Smm if (filename == NULL) 123248616Smm filename = ""; 124248616Smm mine = (struct read_file_data *)calloc(1, 125248616Smm sizeof(*mine) + strlen(filename)); 126248616Smm if (mine == NULL) 127248616Smm goto no_memory; 128248616Smm strcpy(mine->filename.m, filename); 129248616Smm mine->block_size = block_size; 130248616Smm mine->fd = -1; 131248616Smm mine->buffer = NULL; 132248616Smm mine->st_mode = mine->use_lseek = 0; 133248616Smm if (filename == NULL || filename[0] == '\0') { 134248616Smm mine->filename_type = FNT_STDIN; 135248616Smm } else 136248616Smm mine->filename_type = FNT_MBS; 137248616Smm if (archive_read_append_callback_data(a, mine) != (ARCHIVE_OK)) 138248616Smm return (ARCHIVE_FATAL); 139248616Smm if (filenames == NULL) 140248616Smm break; 141248616Smm filename = *(filenames++); 142248616Smm } while (filename != NULL && filename[0] != '\0'); 143248616Smm archive_read_set_open_callback(a, file_open); 144248616Smm archive_read_set_read_callback(a, file_read); 145248616Smm archive_read_set_skip_callback(a, file_skip); 146248616Smm archive_read_set_close_callback(a, file_close); 147248616Smm archive_read_set_switch_callback(a, file_switch); 148248616Smm archive_read_set_seek_callback(a, file_seek); 149248616Smm 150248616Smm return (archive_read_open1(a)); 151248616Smmno_memory: 152248616Smm archive_set_error(a, ENOMEM, "No memory"); 153248616Smm return (ARCHIVE_FATAL); 154232153Smm} 155232153Smm 156232153Smmint 157232153Smmarchive_read_open_filename_w(struct archive *a, const wchar_t *wfilename, 158232153Smm size_t block_size) 159232153Smm{ 160248616Smm struct read_file_data *mine = (struct read_file_data *)calloc(1, 161248616Smm sizeof(*mine) + wcslen(wfilename) * sizeof(wchar_t)); 162248616Smm if (!mine) 163248616Smm { 164248616Smm archive_set_error(a, ENOMEM, "No memory"); 165248616Smm return (ARCHIVE_FATAL); 166248616Smm } 167248616Smm mine->fd = -1; 168248616Smm mine->block_size = block_size; 169232153Smm 170232153Smm if (wfilename == NULL || wfilename[0] == L'\0') { 171248616Smm mine->filename_type = FNT_STDIN; 172232153Smm } else { 173232153Smm#if defined(_WIN32) && !defined(__CYGWIN__) 174248616Smm mine->filename_type = FNT_WCS; 175248616Smm wcscpy(mine->filename.w, wfilename); 176232153Smm#else 177232153Smm /* 178232153Smm * POSIX system does not support a wchar_t interface for 179232153Smm * open() system call, so we have to translate a whcar_t 180232153Smm * filename to multi-byte one and use it. 181232153Smm */ 182232153Smm struct archive_string fn; 183232153Smm 184232153Smm archive_string_init(&fn); 185232153Smm if (archive_string_append_from_wcs(&fn, wfilename, 186232153Smm wcslen(wfilename)) != 0) { 187238856Smm if (errno == ENOMEM) 188238856Smm archive_set_error(a, errno, 189238856Smm "Can't allocate memory"); 190238856Smm else 191238856Smm archive_set_error(a, EINVAL, 192238856Smm "Failed to convert a wide-character" 193238856Smm " filename to a multi-byte filename"); 194232153Smm archive_string_free(&fn); 195248616Smm free(mine); 196232153Smm return (ARCHIVE_FATAL); 197232153Smm } 198248616Smm mine->filename_type = FNT_MBS; 199248616Smm strcpy(mine->filename.m, fn.s); 200232153Smm archive_string_free(&fn); 201232153Smm#endif 202232153Smm } 203248616Smm if (archive_read_append_callback_data(a, mine) != (ARCHIVE_OK)) 204248616Smm return (ARCHIVE_FATAL); 205248616Smm archive_read_set_open_callback(a, file_open); 206248616Smm archive_read_set_read_callback(a, file_read); 207248616Smm archive_read_set_skip_callback(a, file_skip); 208248616Smm archive_read_set_close_callback(a, file_close); 209248616Smm archive_read_set_switch_callback(a, file_switch); 210248616Smm archive_read_set_seek_callback(a, file_seek); 211248616Smm 212248616Smm return (archive_read_open1(a)); 213232153Smm} 214232153Smm 215232153Smmstatic int 216248616Smmfile_open(struct archive *a, void *client_data) 217232153Smm{ 218228753Smm struct stat st; 219248616Smm struct read_file_data *mine = (struct read_file_data *)client_data; 220232153Smm void *buffer; 221232153Smm const char *filename = NULL; 222232153Smm const wchar_t *wfilename = NULL; 223228753Smm int fd; 224232153Smm int is_disk_like = 0; 225232153Smm#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 226232153Smm off_t mediasize = 0; /* FreeBSD-specific, so off_t okay here. */ 227232153Smm#elif defined(__NetBSD__) || defined(__OpenBSD__) 228232153Smm struct disklabel dl; 229232153Smm#elif defined(__DragonFly__) 230232153Smm struct partinfo pi; 231232153Smm#endif 232228753Smm 233228753Smm archive_clear_error(a); 234248616Smm if (mine->filename_type == FNT_STDIN) { 235232153Smm /* We used to delegate stdin support by 236232153Smm * directly calling archive_read_open_fd(a,0,block_size) 237228753Smm * here, but that doesn't (and shouldn't) handle the 238228753Smm * end-of-file flush when reading stdout from a pipe. 239228753Smm * Basically, read_open_fd() is intended for folks who 240228753Smm * are willing to handle such details themselves. This 241228753Smm * API is intended to be a little smarter for folks who 242228753Smm * want easy handling of the common case. 243228753Smm */ 244228753Smm fd = 0; 245228753Smm#if defined(__CYGWIN__) || defined(_WIN32) 246228753Smm setmode(0, O_BINARY); 247228753Smm#endif 248232153Smm filename = ""; 249248616Smm } else if (mine->filename_type == FNT_MBS) { 250248616Smm filename = mine->filename.m; 251248616Smm fd = open(filename, O_RDONLY | O_BINARY | O_CLOEXEC); 252248616Smm __archive_ensure_cloexec_flag(fd); 253228753Smm if (fd < 0) { 254228753Smm archive_set_error(a, errno, 255228753Smm "Failed to open '%s'", filename); 256228753Smm return (ARCHIVE_FATAL); 257228753Smm } 258232153Smm } else { 259232153Smm#if defined(_WIN32) && !defined(__CYGWIN__) 260248616Smm wfilename = mine->filename.w; 261232153Smm fd = _wopen(wfilename, O_RDONLY | O_BINARY); 262232153Smm if (fd < 0 && errno == ENOENT) { 263232153Smm wchar_t *fullpath; 264232153Smm fullpath = __la_win_permissive_name_w(wfilename); 265232153Smm if (fullpath != NULL) { 266232153Smm fd = _wopen(fullpath, O_RDONLY | O_BINARY); 267232153Smm free(fullpath); 268232153Smm } 269232153Smm } 270232153Smm if (fd < 0) { 271232153Smm archive_set_error(a, errno, 272232153Smm "Failed to open '%S'", wfilename); 273232153Smm return (ARCHIVE_FATAL); 274232153Smm } 275232153Smm#else 276232153Smm archive_set_error(a, ARCHIVE_ERRNO_MISC, 277232153Smm "Unexpedted operation in archive_read_open_filename"); 278232153Smm return (ARCHIVE_FATAL); 279232153Smm#endif 280228753Smm } 281228753Smm if (fstat(fd, &st) != 0) { 282248616Smm if (mine->filename_type == FNT_WCS) 283232153Smm archive_set_error(a, errno, "Can't stat '%S'", 284232153Smm wfilename); 285232153Smm else 286232153Smm archive_set_error(a, errno, "Can't stat '%s'", 287232153Smm filename); 288228753Smm return (ARCHIVE_FATAL); 289228753Smm } 290228753Smm 291232153Smm /* 292232153Smm * Determine whether the input looks like a disk device or a 293232153Smm * tape device. The results are used below to select an I/O 294232153Smm * strategy: 295232153Smm * = "disk-like" devices support arbitrary lseek() and will 296232153Smm * support I/O requests of any size. So we get easy skipping 297232153Smm * and can cheat on block sizes to get better performance. 298232153Smm * = "tape-like" devices require strict blocking and use 299232153Smm * specialized ioctls for seeking. 300232153Smm * = "socket-like" devices cannot seek at all but can improve 301232153Smm * performance by using nonblocking I/O to read "whatever is 302232153Smm * available right now". 303232153Smm * 304232153Smm * Right now, we only specially recognize disk-like devices, 305232153Smm * but it should be straightforward to add probes and strategy 306232153Smm * here for tape-like and socket-like devices. 307232153Smm */ 308232153Smm if (S_ISREG(st.st_mode)) { 309232153Smm /* Safety: Tell the extractor not to overwrite the input. */ 310232153Smm archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino); 311232153Smm /* Regular files act like disks. */ 312232153Smm is_disk_like = 1; 313232153Smm } 314232153Smm#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 315232153Smm /* FreeBSD: if it supports DIOCGMEDIASIZE ioctl, it's disk-like. */ 316232153Smm else if (S_ISCHR(st.st_mode) && 317232153Smm ioctl(fd, DIOCGMEDIASIZE, &mediasize) == 0 && 318232153Smm mediasize > 0) { 319232153Smm is_disk_like = 1; 320232153Smm } 321232153Smm#elif defined(__NetBSD__) || defined(__OpenBSD__) 322232153Smm /* Net/OpenBSD: if it supports DIOCGDINFO ioctl, it's disk-like. */ 323232153Smm else if ((S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) && 324232153Smm ioctl(fd, DIOCGDINFO, &dl) == 0 && 325232153Smm dl.d_partitions[DISKPART(st.st_rdev)].p_size > 0) { 326232153Smm is_disk_like = 1; 327232153Smm } 328232153Smm#elif defined(__DragonFly__) 329232153Smm /* DragonFly BSD: if it supports DIOCGPART ioctl, it's disk-like. */ 330232153Smm else if (S_ISCHR(st.st_mode) && 331232153Smm ioctl(fd, DIOCGPART, &pi) == 0 && 332232153Smm pi.media_size > 0) { 333232153Smm is_disk_like = 1; 334232153Smm } 335232153Smm#elif defined(__linux__) 336232153Smm /* Linux: All block devices are disk-like. */ 337232153Smm else if (S_ISBLK(st.st_mode) && 338232153Smm lseek(fd, 0, SEEK_CUR) == 0 && 339232153Smm lseek(fd, 0, SEEK_SET) == 0 && 340232153Smm lseek(fd, 0, SEEK_END) > 0 && 341232153Smm lseek(fd, 0, SEEK_SET) == 0) { 342232153Smm is_disk_like = 1; 343232153Smm } 344232153Smm#endif 345232153Smm /* TODO: Add an "is_tape_like" variable and appropriate tests. */ 346232153Smm 347232153Smm /* Disk-like devices prefer power-of-two block sizes. */ 348232153Smm /* Use provided block_size as a guide so users have some control. */ 349232153Smm if (is_disk_like) { 350232153Smm size_t new_block_size = 64 * 1024; 351248616Smm while (new_block_size < mine->block_size 352232153Smm && new_block_size < 64 * 1024 * 1024) 353232153Smm new_block_size *= 2; 354248616Smm mine->block_size = new_block_size; 355232153Smm } 356248616Smm buffer = malloc(mine->block_size); 357232153Smm if (mine == NULL || buffer == NULL) { 358228753Smm archive_set_error(a, ENOMEM, "No memory"); 359228753Smm free(mine); 360232153Smm free(buffer); 361228753Smm return (ARCHIVE_FATAL); 362228753Smm } 363232153Smm mine->buffer = buffer; 364228753Smm mine->fd = fd; 365228753Smm /* Remember mode so close can decide whether to flush. */ 366228753Smm mine->st_mode = st.st_mode; 367232153Smm 368232153Smm /* Disk-like inputs can use lseek(). */ 369248616Smm if (is_disk_like) 370232153Smm mine->use_lseek = 1; 371232153Smm 372248616Smm return (ARCHIVE_OK); 373228753Smm} 374228753Smm 375228753Smmstatic ssize_t 376228753Smmfile_read(struct archive *a, void *client_data, const void **buff) 377228753Smm{ 378228753Smm struct read_file_data *mine = (struct read_file_data *)client_data; 379228753Smm ssize_t bytes_read; 380228753Smm 381232153Smm /* TODO: If a recent lseek() operation has left us 382232153Smm * mis-aligned, read and return a short block to try to get 383232153Smm * us back in alignment. */ 384232153Smm 385232153Smm /* TODO: Someday, try mmap() here; if that succeeds, give 386232153Smm * the entire file to libarchive as a single block. That 387232153Smm * could be a lot faster than block-by-block manual I/O. */ 388232153Smm 389232153Smm /* TODO: We might be able to improve performance on pipes and 390232153Smm * sockets by setting non-blocking I/O and just accepting 391232153Smm * whatever we get here instead of waiting for a full block 392232153Smm * worth of data. */ 393232153Smm 394228753Smm *buff = mine->buffer; 395228753Smm for (;;) { 396228753Smm bytes_read = read(mine->fd, mine->buffer, mine->block_size); 397228753Smm if (bytes_read < 0) { 398228753Smm if (errno == EINTR) 399228753Smm continue; 400232153Smm else if (mine->filename_type == FNT_STDIN) 401232153Smm archive_set_error(a, errno, 402232153Smm "Error reading stdin"); 403232153Smm else if (mine->filename_type == FNT_MBS) 404232153Smm archive_set_error(a, errno, 405232153Smm "Error reading '%s'", mine->filename.m); 406228753Smm else 407232153Smm archive_set_error(a, errno, 408232153Smm "Error reading '%S'", mine->filename.w); 409228753Smm } 410228753Smm return (bytes_read); 411228753Smm } 412228753Smm} 413228753Smm 414232153Smm/* 415232153Smm * Regular files and disk-like block devices can use simple lseek 416232153Smm * without needing to round the request to the block size. 417232153Smm * 418232153Smm * TODO: This can leave future reads mis-aligned. Since we know the 419232153Smm * offset here, we should store it and use it in file_read() above 420232153Smm * to determine whether we should perform a short read to get back 421232153Smm * into alignment. Long series of mis-aligned reads can negatively 422232153Smm * impact disk throughput. (Of course, the performance impact should 423232153Smm * be carefully tested; extra code complexity is only worthwhile if 424232153Smm * it does provide measurable improvement.) 425232153Smm * 426232153Smm * TODO: Be lazy about the actual seek. There are a few pathological 427232153Smm * cases where libarchive makes a bunch of seek requests in a row 428232153Smm * without any intervening reads. This isn't a huge performance 429232153Smm * problem, since the kernel handles seeks lazily already, but 430232153Smm * it would be very slightly faster if we simply remembered the 431232153Smm * seek request here and then actually performed the seek at the 432232153Smm * top of the read callback above. 433232153Smm */ 434232153Smmstatic int64_t 435232153Smmfile_skip_lseek(struct archive *a, void *client_data, int64_t request) 436228753Smm{ 437228753Smm struct read_file_data *mine = (struct read_file_data *)client_data; 438232153Smm#if defined(_WIN32) && !defined(__CYGWIN__) 439232153Smm /* We use _lseeki64() on Windows. */ 440232153Smm int64_t old_offset, new_offset; 441232153Smm#else 442228753Smm off_t old_offset, new_offset; 443232153Smm#endif 444228753Smm 445232153Smm /* We use off_t here because lseek() is declared that way. */ 446228753Smm 447232153Smm /* TODO: Deal with case where off_t isn't 64 bits. 448232153Smm * This shouldn't be a problem on Linux or other POSIX 449232153Smm * systems, since the configuration logic for libarchive 450248616Smm * tries to obtain a 64-bit off_t. 451232153Smm */ 452232153Smm if ((old_offset = lseek(mine->fd, 0, SEEK_CUR)) >= 0 && 453232153Smm (new_offset = lseek(mine->fd, request, SEEK_CUR)) >= 0) 454232153Smm return (new_offset - old_offset); 455232153Smm 456232153Smm /* If lseek() fails, don't bother trying again. */ 457232153Smm mine->use_lseek = 0; 458232153Smm 459232153Smm /* Let libarchive recover with read+discard */ 460232153Smm if (errno == ESPIPE) 461228753Smm return (0); 462228753Smm 463232153Smm /* If the input is corrupted or truncated, fail. */ 464232153Smm if (mine->filename_type == FNT_STDIN) 465232153Smm archive_set_error(a, errno, "Error seeking in stdin"); 466232153Smm else if (mine->filename_type == FNT_MBS) 467232153Smm archive_set_error(a, errno, "Error seeking in '%s'", 468232153Smm mine->filename.m); 469232153Smm else 470232153Smm archive_set_error(a, errno, "Error seeking in '%S'", 471232153Smm mine->filename.w); 472232153Smm return (-1); 473232153Smm} 474228753Smm 475232153Smm 476232153Smm/* 477232153Smm * TODO: Implement another file_skip_XXXX that uses MTIO ioctls to 478232153Smm * accelerate operation on tape drives. 479232153Smm */ 480232153Smm 481232153Smmstatic int64_t 482232153Smmfile_skip(struct archive *a, void *client_data, int64_t request) 483232153Smm{ 484232153Smm struct read_file_data *mine = (struct read_file_data *)client_data; 485232153Smm 486232153Smm /* Delegate skip requests. */ 487232153Smm if (mine->use_lseek) 488232153Smm return (file_skip_lseek(a, client_data, request)); 489232153Smm 490232153Smm /* If we can't skip, return 0; libarchive will read+discard instead. */ 491232153Smm return (0); 492228753Smm} 493228753Smm 494232153Smm/* 495232153Smm * TODO: Store the offset and use it in the read callback. 496232153Smm */ 497232153Smmstatic int64_t 498232153Smmfile_seek(struct archive *a, void *client_data, int64_t request, int whence) 499232153Smm{ 500232153Smm struct read_file_data *mine = (struct read_file_data *)client_data; 501238856Smm int64_t r; 502232153Smm 503232153Smm /* We use off_t here because lseek() is declared that way. */ 504232153Smm /* See above for notes about when off_t is less than 64 bits. */ 505232153Smm r = lseek(mine->fd, request, whence); 506232153Smm if (r >= 0) 507232153Smm return r; 508232153Smm 509232153Smm /* If the input is corrupted or truncated, fail. */ 510232153Smm if (mine->filename_type == FNT_STDIN) 511232153Smm archive_set_error(a, errno, "Error seeking in stdin"); 512232153Smm else if (mine->filename_type == FNT_MBS) 513232153Smm archive_set_error(a, errno, "Error seeking in '%s'", 514232153Smm mine->filename.m); 515232153Smm else 516232153Smm archive_set_error(a, errno, "Error seeking in '%S'", 517232153Smm mine->filename.w); 518232153Smm return (ARCHIVE_FATAL); 519232153Smm} 520232153Smm 521228753Smmstatic int 522248616Smmfile_close2(struct archive *a, void *client_data) 523228753Smm{ 524228753Smm struct read_file_data *mine = (struct read_file_data *)client_data; 525228753Smm 526228753Smm (void)a; /* UNUSED */ 527228753Smm 528228753Smm /* Only flush and close if open succeeded. */ 529228753Smm if (mine->fd >= 0) { 530228753Smm /* 531228753Smm * Sometimes, we should flush the input before closing. 532228753Smm * Regular files: faster to just close without flush. 533232153Smm * Disk-like devices: Ditto. 534232153Smm * Tapes: must not flush (user might need to 535228753Smm * read the "next" item on a non-rewind device). 536228753Smm * Pipes and sockets: must flush (otherwise, the 537228753Smm * program feeding the pipe or socket may complain). 538228753Smm * Here, I flush everything except for regular files and 539228753Smm * device nodes. 540228753Smm */ 541228753Smm if (!S_ISREG(mine->st_mode) 542228753Smm && !S_ISCHR(mine->st_mode) 543228753Smm && !S_ISBLK(mine->st_mode)) { 544228753Smm ssize_t bytesRead; 545228753Smm do { 546228753Smm bytesRead = read(mine->fd, mine->buffer, 547228753Smm mine->block_size); 548228753Smm } while (bytesRead > 0); 549228753Smm } 550228753Smm /* If a named file was opened, then it needs to be closed. */ 551232153Smm if (mine->filename_type != FNT_STDIN) 552228753Smm close(mine->fd); 553228753Smm } 554228753Smm free(mine->buffer); 555248616Smm mine->buffer = NULL; 556248616Smm mine->fd = -1; 557248616Smm return (ARCHIVE_OK); 558248616Smm} 559248616Smm 560248616Smmstatic int 561248616Smmfile_close(struct archive *a, void *client_data) 562248616Smm{ 563248616Smm struct read_file_data *mine = (struct read_file_data *)client_data; 564248616Smm file_close2(a, client_data); 565228753Smm free(mine); 566228753Smm return (ARCHIVE_OK); 567228753Smm} 568248616Smm 569248616Smmstatic int 570248616Smmfile_switch(struct archive *a, void *client_data1, void *client_data2) 571248616Smm{ 572248616Smm file_close2(a, client_data1); 573248616Smm return file_open(a, client_data2); 574248616Smm} 575