2 * Copyright (c) 2003-2010 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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$");
29 #ifdef HAVE_SYS_IOCTL_H
30 #include <sys/ioctl.h>
32 #ifdef HAVE_SYS_STAT_H
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
55 #elif defined(__NetBSD__) || defined(__OpenBSD__)
56 #include <sys/disklabel.h>
58 #elif defined(__DragonFly__)
59 #include <sys/diskslice.h>
63 #include "archive_private.h"
64 #include "archive_string.h"
73 struct read_file_data {
77 mode_t st_mode; /* Mode bits for opened file. */
79 enum fnt_e { FNT_STDIN, FNT_MBS, FNT_WCS } filename_type;
81 char m[1];/* MBS filename. */
82 wchar_t w[1];/* WCS filename. */
83 } filename; /* Must be last! */
86 static int file_open(struct archive *, void *);
87 static int file_close(struct archive *, void *);
88 static int file_close2(struct archive *, void *);
89 static int file_switch(struct archive *, void *, void *);
90 static ssize_t file_read(struct archive *, void *, const void **buff);
91 static int64_t file_seek(struct archive *, void *, int64_t request, int);
92 static int64_t file_skip(struct archive *, void *, int64_t request);
93 static int64_t file_skip_lseek(struct archive *, void *, int64_t request);
96 archive_read_open_file(struct archive *a, const char *filename,
99 return (archive_read_open_filename(a, filename, block_size));
103 archive_read_open_filename(struct archive *a, const char *filename,
106 const char *filenames[2];
107 filenames[0] = filename;
109 return archive_read_open_filenames(a, filenames, block_size);
113 archive_read_open_filenames(struct archive *a, const char **filenames,
116 struct read_file_data *mine;
117 const char *filename = NULL;
119 filename = *(filenames++);
121 archive_clear_error(a);
124 if (filename == NULL)
126 mine = (struct read_file_data *)calloc(1,
127 sizeof(*mine) + strlen(filename));
130 strcpy(mine->filename.m, filename);
131 mine->block_size = block_size;
134 mine->st_mode = mine->use_lseek = 0;
135 if (filename == NULL || filename[0] == '\0') {
136 mine->filename_type = FNT_STDIN;
138 mine->filename_type = FNT_MBS;
139 if (archive_read_append_callback_data(a, mine) != (ARCHIVE_OK))
140 return (ARCHIVE_FATAL);
141 if (filenames == NULL)
143 filename = *(filenames++);
144 } while (filename != NULL && filename[0] != '\0');
145 archive_read_set_open_callback(a, file_open);
146 archive_read_set_read_callback(a, file_read);
147 archive_read_set_skip_callback(a, file_skip);
148 archive_read_set_close_callback(a, file_close);
149 archive_read_set_switch_callback(a, file_switch);
150 archive_read_set_seek_callback(a, file_seek);
152 return (archive_read_open1(a));
154 archive_set_error(a, ENOMEM, "No memory");
155 return (ARCHIVE_FATAL);
159 archive_read_open_filename_w(struct archive *a, const wchar_t *wfilename,
162 struct read_file_data *mine = (struct read_file_data *)calloc(1,
163 sizeof(*mine) + wcslen(wfilename) * sizeof(wchar_t));
166 archive_set_error(a, ENOMEM, "No memory");
167 return (ARCHIVE_FATAL);
170 mine->block_size = block_size;
172 if (wfilename == NULL || wfilename[0] == L'\0') {
173 mine->filename_type = FNT_STDIN;
175 #if defined(_WIN32) && !defined(__CYGWIN__)
176 mine->filename_type = FNT_WCS;
177 wcscpy(mine->filename.w, wfilename);
180 * POSIX system does not support a wchar_t interface for
181 * open() system call, so we have to translate a wchar_t
182 * filename to multi-byte one and use it.
184 struct archive_string fn;
186 archive_string_init(&fn);
187 if (archive_string_append_from_wcs(&fn, wfilename,
188 wcslen(wfilename)) != 0) {
190 archive_set_error(a, errno,
191 "Can't allocate memory");
193 archive_set_error(a, EINVAL,
194 "Failed to convert a wide-character"
195 " filename to a multi-byte filename");
196 archive_string_free(&fn);
198 return (ARCHIVE_FATAL);
200 mine->filename_type = FNT_MBS;
201 strcpy(mine->filename.m, fn.s);
202 archive_string_free(&fn);
205 if (archive_read_append_callback_data(a, mine) != (ARCHIVE_OK))
206 return (ARCHIVE_FATAL);
207 archive_read_set_open_callback(a, file_open);
208 archive_read_set_read_callback(a, file_read);
209 archive_read_set_skip_callback(a, file_skip);
210 archive_read_set_close_callback(a, file_close);
211 archive_read_set_switch_callback(a, file_switch);
212 archive_read_set_seek_callback(a, file_seek);
214 return (archive_read_open1(a));
218 file_open(struct archive *a, void *client_data)
221 struct read_file_data *mine = (struct read_file_data *)client_data;
223 const char *filename = NULL;
224 #if defined(_WIN32) && !defined(__CYGWIN__)
225 const wchar_t *wfilename = NULL;
228 int is_disk_like = 0;
229 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
230 off_t mediasize = 0; /* FreeBSD-specific, so off_t okay here. */
231 #elif defined(__NetBSD__) || defined(__OpenBSD__)
233 #elif defined(__DragonFly__)
237 archive_clear_error(a);
238 if (mine->filename_type == FNT_STDIN) {
239 /* We used to delegate stdin support by
240 * directly calling archive_read_open_fd(a,0,block_size)
241 * here, but that doesn't (and shouldn't) handle the
242 * end-of-file flush when reading stdout from a pipe.
243 * Basically, read_open_fd() is intended for folks who
244 * are willing to handle such details themselves. This
245 * API is intended to be a little smarter for folks who
246 * want easy handling of the common case.
249 #if defined(__CYGWIN__) || defined(_WIN32)
250 setmode(0, O_BINARY);
253 } else if (mine->filename_type == FNT_MBS) {
254 filename = mine->filename.m;
255 fd = open(filename, O_RDONLY | O_BINARY | O_CLOEXEC);
256 __archive_ensure_cloexec_flag(fd);
258 archive_set_error(a, errno,
259 "Failed to open '%s'", filename);
260 return (ARCHIVE_FATAL);
263 #if defined(_WIN32) && !defined(__CYGWIN__)
264 wfilename = mine->filename.w;
265 fd = _wopen(wfilename, O_RDONLY | O_BINARY);
266 if (fd < 0 && errno == ENOENT) {
268 fullpath = __la_win_permissive_name_w(wfilename);
269 if (fullpath != NULL) {
270 fd = _wopen(fullpath, O_RDONLY | O_BINARY);
275 archive_set_error(a, errno,
276 "Failed to open '%S'", wfilename);
277 return (ARCHIVE_FATAL);
280 archive_set_error(a, ARCHIVE_ERRNO_MISC,
281 "Unexpedted operation in archive_read_open_filename");
285 if (fstat(fd, &st) != 0) {
286 #if defined(_WIN32) && !defined(__CYGWIN__)
287 if (mine->filename_type == FNT_WCS)
288 archive_set_error(a, errno, "Can't stat '%S'",
292 archive_set_error(a, errno, "Can't stat '%s'",
298 * Determine whether the input looks like a disk device or a
299 * tape device. The results are used below to select an I/O
301 * = "disk-like" devices support arbitrary lseek() and will
302 * support I/O requests of any size. So we get easy skipping
303 * and can cheat on block sizes to get better performance.
304 * = "tape-like" devices require strict blocking and use
305 * specialized ioctls for seeking.
306 * = "socket-like" devices cannot seek at all but can improve
307 * performance by using nonblocking I/O to read "whatever is
308 * available right now".
310 * Right now, we only specially recognize disk-like devices,
311 * but it should be straightforward to add probes and strategy
312 * here for tape-like and socket-like devices.
314 if (S_ISREG(st.st_mode)) {
315 /* Safety: Tell the extractor not to overwrite the input. */
316 archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
317 /* Regular files act like disks. */
320 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
321 /* FreeBSD: if it supports DIOCGMEDIASIZE ioctl, it's disk-like. */
322 else if (S_ISCHR(st.st_mode) &&
323 ioctl(fd, DIOCGMEDIASIZE, &mediasize) == 0 &&
327 #elif defined(__NetBSD__) || defined(__OpenBSD__)
328 /* Net/OpenBSD: if it supports DIOCGDINFO ioctl, it's disk-like. */
329 else if ((S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) &&
330 ioctl(fd, DIOCGDINFO, &dl) == 0 &&
331 dl.d_partitions[DISKPART(st.st_rdev)].p_size > 0) {
334 #elif defined(__DragonFly__)
335 /* DragonFly BSD: if it supports DIOCGPART ioctl, it's disk-like. */
336 else if (S_ISCHR(st.st_mode) &&
337 ioctl(fd, DIOCGPART, &pi) == 0 &&
341 #elif defined(__linux__)
342 /* Linux: All block devices are disk-like. */
343 else if (S_ISBLK(st.st_mode) &&
344 lseek(fd, 0, SEEK_CUR) == 0 &&
345 lseek(fd, 0, SEEK_SET) == 0 &&
346 lseek(fd, 0, SEEK_END) > 0 &&
347 lseek(fd, 0, SEEK_SET) == 0) {
351 /* TODO: Add an "is_tape_like" variable and appropriate tests. */
353 /* Disk-like devices prefer power-of-two block sizes. */
354 /* Use provided block_size as a guide so users have some control. */
356 size_t new_block_size = 64 * 1024;
357 while (new_block_size < mine->block_size
358 && new_block_size < 64 * 1024 * 1024)
360 mine->block_size = new_block_size;
362 buffer = malloc(mine->block_size);
363 if (buffer == NULL) {
364 archive_set_error(a, ENOMEM, "No memory");
367 mine->buffer = buffer;
369 /* Remember mode so close can decide whether to flush. */
370 mine->st_mode = st.st_mode;
372 /* Disk-like inputs can use lseek(). */
379 * Don't close file descriptors not opened or ones pointing referring
382 if (fd != -1 && fd != 0)
384 return (ARCHIVE_FATAL);
388 file_read(struct archive *a, void *client_data, const void **buff)
390 struct read_file_data *mine = (struct read_file_data *)client_data;
393 /* TODO: If a recent lseek() operation has left us
394 * mis-aligned, read and return a short block to try to get
395 * us back in alignment. */
397 /* TODO: Someday, try mmap() here; if that succeeds, give
398 * the entire file to libarchive as a single block. That
399 * could be a lot faster than block-by-block manual I/O. */
401 /* TODO: We might be able to improve performance on pipes and
402 * sockets by setting non-blocking I/O and just accepting
403 * whatever we get here instead of waiting for a full block
406 *buff = mine->buffer;
408 bytes_read = read(mine->fd, mine->buffer, mine->block_size);
409 if (bytes_read < 0) {
412 else if (mine->filename_type == FNT_STDIN)
413 archive_set_error(a, errno,
414 "Error reading stdin");
415 else if (mine->filename_type == FNT_MBS)
416 archive_set_error(a, errno,
417 "Error reading '%s'", mine->filename.m);
419 archive_set_error(a, errno,
420 "Error reading '%S'", mine->filename.w);
427 * Regular files and disk-like block devices can use simple lseek
428 * without needing to round the request to the block size.
430 * TODO: This can leave future reads mis-aligned. Since we know the
431 * offset here, we should store it and use it in file_read() above
432 * to determine whether we should perform a short read to get back
433 * into alignment. Long series of mis-aligned reads can negatively
434 * impact disk throughput. (Of course, the performance impact should
435 * be carefully tested; extra code complexity is only worthwhile if
436 * it does provide measurable improvement.)
438 * TODO: Be lazy about the actual seek. There are a few pathological
439 * cases where libarchive makes a bunch of seek requests in a row
440 * without any intervening reads. This isn't a huge performance
441 * problem, since the kernel handles seeks lazily already, but
442 * it would be very slightly faster if we simply remembered the
443 * seek request here and then actually performed the seek at the
444 * top of the read callback above.
447 file_skip_lseek(struct archive *a, void *client_data, int64_t request)
449 struct read_file_data *mine = (struct read_file_data *)client_data;
450 #if defined(_WIN32) && !defined(__CYGWIN__)
451 /* We use _lseeki64() on Windows. */
452 int64_t old_offset, new_offset;
454 off_t old_offset, new_offset;
457 /* We use off_t here because lseek() is declared that way. */
459 /* TODO: Deal with case where off_t isn't 64 bits.
460 * This shouldn't be a problem on Linux or other POSIX
461 * systems, since the configuration logic for libarchive
462 * tries to obtain a 64-bit off_t.
464 if ((old_offset = lseek(mine->fd, 0, SEEK_CUR)) >= 0 &&
465 (new_offset = lseek(mine->fd, request, SEEK_CUR)) >= 0)
466 return (new_offset - old_offset);
468 /* If lseek() fails, don't bother trying again. */
471 /* Let libarchive recover with read+discard */
475 /* If the input is corrupted or truncated, fail. */
476 if (mine->filename_type == FNT_STDIN)
477 archive_set_error(a, errno, "Error seeking in stdin");
478 else if (mine->filename_type == FNT_MBS)
479 archive_set_error(a, errno, "Error seeking in '%s'",
482 archive_set_error(a, errno, "Error seeking in '%S'",
489 * TODO: Implement another file_skip_XXXX that uses MTIO ioctls to
490 * accelerate operation on tape drives.
494 file_skip(struct archive *a, void *client_data, int64_t request)
496 struct read_file_data *mine = (struct read_file_data *)client_data;
498 /* Delegate skip requests. */
500 return (file_skip_lseek(a, client_data, request));
502 /* If we can't skip, return 0; libarchive will read+discard instead. */
507 * TODO: Store the offset and use it in the read callback.
510 file_seek(struct archive *a, void *client_data, int64_t request, int whence)
512 struct read_file_data *mine = (struct read_file_data *)client_data;
515 /* We use off_t here because lseek() is declared that way. */
516 /* See above for notes about when off_t is less than 64 bits. */
517 r = lseek(mine->fd, request, whence);
521 /* If the input is corrupted or truncated, fail. */
522 if (mine->filename_type == FNT_STDIN)
523 archive_set_error(a, errno, "Error seeking in stdin");
524 else if (mine->filename_type == FNT_MBS)
525 archive_set_error(a, errno, "Error seeking in '%s'",
528 archive_set_error(a, errno, "Error seeking in '%S'",
530 return (ARCHIVE_FATAL);
534 file_close2(struct archive *a, void *client_data)
536 struct read_file_data *mine = (struct read_file_data *)client_data;
538 (void)a; /* UNUSED */
540 /* Only flush and close if open succeeded. */
543 * Sometimes, we should flush the input before closing.
544 * Regular files: faster to just close without flush.
545 * Disk-like devices: Ditto.
546 * Tapes: must not flush (user might need to
547 * read the "next" item on a non-rewind device).
548 * Pipes and sockets: must flush (otherwise, the
549 * program feeding the pipe or socket may complain).
550 * Here, I flush everything except for regular files and
553 if (!S_ISREG(mine->st_mode)
554 && !S_ISCHR(mine->st_mode)
555 && !S_ISBLK(mine->st_mode)) {
558 bytesRead = read(mine->fd, mine->buffer,
560 } while (bytesRead > 0);
562 /* If a named file was opened, then it needs to be closed. */
563 if (mine->filename_type != FNT_STDIN)
573 file_close(struct archive *a, void *client_data)
575 struct read_file_data *mine = (struct read_file_data *)client_data;
576 file_close2(a, client_data);
582 file_switch(struct archive *a, void *client_data1, void *client_data2)
584 file_close2(a, client_data1);
585 return file_open(a, client_data2);