select.2: timeout is restrict too.
[dragonfly.git] / contrib / libarchive / libarchive / archive_read_open_file.c
blobbfe933bf32e9da2ec118dc8f8814577672e5027e
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
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.
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.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: head/lib/libarchive/archive_read_open_file.c 201093 2009-12-28 02:28:44Z kientzle $");
29 #ifdef HAVE_SYS_STAT_H
30 #include <sys/stat.h>
31 #endif
32 #ifdef HAVE_ERRNO_H
33 #include <errno.h>
34 #endif
35 #ifdef HAVE_FCNTL_H
36 #include <fcntl.h>
37 #endif
38 #ifdef HAVE_IO_H
39 #include <io.h>
40 #endif
41 #ifdef HAVE_STDLIB_H
42 #include <stdlib.h>
43 #endif
44 #ifdef HAVE_STRING_H
45 #include <string.h>
46 #endif
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
51 #include "archive.h"
53 struct read_FILE_data {
54 FILE *f;
55 size_t block_size;
56 void *buffer;
57 char can_skip;
60 static int file_close(struct archive *, void *);
61 static ssize_t file_read(struct archive *, void *, const void **buff);
62 static int64_t file_skip(struct archive *, void *, int64_t request);
64 int
65 archive_read_open_FILE(struct archive *a, FILE *f)
67 struct stat st;
68 struct read_FILE_data *mine;
69 size_t block_size = 128 * 1024;
70 void *b;
72 archive_clear_error(a);
73 mine = (struct read_FILE_data *)malloc(sizeof(*mine));
74 b = malloc(block_size);
75 if (mine == NULL || b == NULL) {
76 archive_set_error(a, ENOMEM, "No memory");
77 free(mine);
78 free(b);
79 return (ARCHIVE_FATAL);
81 mine->block_size = block_size;
82 mine->buffer = b;
83 mine->f = f;
85 * If we can't fstat() the file, it may just be that it's not
86 * a file. (On some platforms, FILE * objects can wrap I/O
87 * streams that don't support fileno()). As a result, fileno()
88 * should be used cautiously.)
90 if (fstat(fileno(mine->f), &st) == 0 && S_ISREG(st.st_mode)) {
91 archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
92 /* Enable the seek optimization only for regular files. */
93 mine->can_skip = 1;
94 } else
95 mine->can_skip = 0;
97 #if defined(__CYGWIN__) || defined(_WIN32)
98 setmode(fileno(mine->f), O_BINARY);
99 #endif
101 archive_read_set_read_callback(a, file_read);
102 archive_read_set_skip_callback(a, file_skip);
103 archive_read_set_close_callback(a, file_close);
104 archive_read_set_callback_data(a, mine);
105 return (archive_read_open1(a));
108 static ssize_t
109 file_read(struct archive *a, void *client_data, const void **buff)
111 struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
112 size_t bytes_read;
114 *buff = mine->buffer;
115 bytes_read = fread(mine->buffer, 1, mine->block_size, mine->f);
116 if (bytes_read < mine->block_size && ferror(mine->f)) {
117 archive_set_error(a, errno, "Error reading file");
119 return (bytes_read);
122 static int64_t
123 file_skip(struct archive *a, void *client_data, int64_t request)
125 struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
126 #if HAVE_FSEEKO
127 off_t skip = (off_t)request;
128 #elif HAVE__FSEEKI64
129 int64_t skip = request;
130 #else
131 long skip = (long)request;
132 #endif
133 int skip_bits = sizeof(skip) * 8 - 1;
135 (void)a; /* UNUSED */
138 * If we can't skip, return 0 as the amount we did step and
139 * the caller will work around by reading and discarding.
141 if (!mine->can_skip)
142 return (0);
143 if (request == 0)
144 return (0);
146 /* If request is too big for a long or an off_t, reduce it. */
147 if (sizeof(request) > sizeof(skip)) {
148 int64_t max_skip =
149 (((int64_t)1 << (skip_bits - 1)) - 1) * 2 + 1;
150 if (request > max_skip)
151 skip = max_skip;
154 #ifdef __ANDROID__
155 /* fileno() isn't safe on all platforms ... see above. */
156 if (lseek(fileno(mine->f), skip, SEEK_CUR) < 0)
157 #elif HAVE_FSEEKO
158 if (fseeko(mine->f, skip, SEEK_CUR) != 0)
159 #elif HAVE__FSEEKI64
160 if (_fseeki64(mine->f, skip, SEEK_CUR) != 0)
161 #else
162 if (fseek(mine->f, skip, SEEK_CUR) != 0)
163 #endif
165 mine->can_skip = 0;
166 return (0);
168 return (request);
171 static int
172 file_close(struct archive *a, void *client_data)
174 struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
176 (void)a; /* UNUSED */
177 if (mine->buffer != NULL)
178 free(mine->buffer);
179 free(mine);
180 return (ARCHIVE_OK);