Make sure UFS disallows mknod()'s with type VDIR.
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_read_open_fd.c
blob2ebd46d6cdf8b8fa48d881bec4040b5383ebcb60
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: src/lib/libarchive/archive_read_open_fd.c,v 1.13 2007/06/26 03:06:48 kientzle Exp $");
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_STDLIB_H
36 #include <stdlib.h>
37 #endif
38 #ifdef HAVE_STRING_H
39 #include <string.h>
40 #endif
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
45 #include "archive.h"
47 struct read_fd_data {
48 int fd;
49 size_t block_size;
50 char can_skip;
51 void *buffer;
54 static int file_close(struct archive *, void *);
55 static int file_open(struct archive *, void *);
56 static ssize_t file_read(struct archive *, void *, const void **buff);
57 #if ARCHIVE_API_VERSION < 2
58 static ssize_t file_skip(struct archive *, void *, size_t request);
59 #else
60 static off_t file_skip(struct archive *, void *, off_t request);
61 #endif
63 int
64 archive_read_open_fd(struct archive *a, int fd, size_t block_size)
66 struct read_fd_data *mine;
68 mine = (struct read_fd_data *)malloc(sizeof(*mine));
69 if (mine == NULL) {
70 archive_set_error(a, ENOMEM, "No memory");
71 return (ARCHIVE_FATAL);
73 mine->block_size = block_size;
74 mine->buffer = malloc(mine->block_size);
75 if (mine->buffer == NULL) {
76 archive_set_error(a, ENOMEM, "No memory");
77 free(mine);
78 return (ARCHIVE_FATAL);
80 mine->fd = fd;
81 /* lseek() hardly ever works, so disable it by default. See below. */
82 mine->can_skip = 0;
83 return (archive_read_open2(a, mine, file_open, file_read, file_skip, file_close));
86 static int
87 file_open(struct archive *a, void *client_data)
89 struct read_fd_data *mine = (struct read_fd_data *)client_data;
90 struct stat st;
92 if (fstat(mine->fd, &st) != 0) {
93 archive_set_error(a, errno, "Can't stat fd %d", mine->fd);
94 return (ARCHIVE_FATAL);
97 if (S_ISREG(st.st_mode)) {
98 archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
100 * Enabling skip here is a performance optimization for
101 * anything that supports lseek(). On FreeBSD, only
102 * regular files and raw disk devices support lseek() and
103 * there's no portable way to determine if a device is
104 * a raw disk device, so we only enable this optimization
105 * for regular files.
107 mine->can_skip = 1;
109 return (ARCHIVE_OK);
112 static ssize_t
113 file_read(struct archive *a, void *client_data, const void **buff)
115 struct read_fd_data *mine = (struct read_fd_data *)client_data;
116 ssize_t bytes_read;
118 *buff = mine->buffer;
119 bytes_read = read(mine->fd, mine->buffer, mine->block_size);
120 if (bytes_read < 0) {
121 archive_set_error(a, errno, "Error reading fd %d", mine->fd);
123 return (bytes_read);
126 #if ARCHIVE_API_VERSION < 2
127 static ssize_t
128 file_skip(struct archive *a, void *client_data, size_t request)
129 #else
130 static off_t
131 file_skip(struct archive *a, void *client_data, off_t request)
132 #endif
134 struct read_fd_data *mine = (struct read_fd_data *)client_data;
135 off_t old_offset, new_offset;
137 if (!mine->can_skip)
138 return (0);
140 /* Reduce request to the next smallest multiple of block_size */
141 request = (request / mine->block_size) * mine->block_size;
142 if (request == 0)
143 return (0);
146 * Hurray for lazy evaluation: if the first lseek fails, the second
147 * one will not be executed.
149 if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
150 ((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
152 /* If seek failed once, it will probably fail again. */
153 mine->can_skip = 0;
155 if (errno == ESPIPE)
158 * Failure to lseek() can be caused by the file
159 * descriptor pointing to a pipe, socket or FIFO.
160 * Return 0 here, so the compression layer will use
161 * read()s instead to advance the file descriptor.
162 * It's slower of course, but works as well.
164 return (0);
167 * There's been an error other than ESPIPE. This is most
168 * likely caused by a programmer error (too large request)
169 * or a corrupted archive file.
171 archive_set_error(a, errno, "Error seeking");
172 return (-1);
174 return (new_offset - old_offset);
177 static int
178 file_close(struct archive *a, void *client_data)
180 struct read_fd_data *mine = (struct read_fd_data *)client_data;
182 (void)a; /* UNUSED */
183 if (mine->buffer != NULL)
184 free(mine->buffer);
185 free(mine);
186 return (ARCHIVE_OK);