Import libarchive 2.1.9.
[dragonfly.git] / contrib / libarchive-2.1 / libarchive / archive_read_open_fd.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
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.
13  *
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.
24  */
25
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_open_fd.c,v 1.11 2007/01/09 08:05:55 kientzle Exp $");
28
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
44
45 #include "archive.h"
46
47 struct read_fd_data {
48         int      fd;
49         size_t   block_size;
50         void    *buffer;
51 };
52
53 static int      file_close(struct archive *, void *);
54 static int      file_open(struct archive *, void *);
55 static ssize_t  file_read(struct archive *, void *, const void **buff);
56 #if ARCHIVE_API_VERSION < 2
57 static ssize_t  file_skip(struct archive *, void *, size_t request);
58 #else
59 static off_t    file_skip(struct archive *, void *, off_t request);
60 #endif
61
62 int
63 archive_read_open_fd(struct archive *a, int fd, size_t block_size)
64 {
65         struct read_fd_data *mine;
66
67         mine = (struct read_fd_data *)malloc(sizeof(*mine));
68         if (mine == NULL) {
69                 archive_set_error(a, ENOMEM, "No memory");
70                 return (ARCHIVE_FATAL);
71         }
72         mine->block_size = block_size;
73         mine->buffer = malloc(mine->block_size);
74         if (mine->buffer == NULL) {
75                 archive_set_error(a, ENOMEM, "No memory");
76                 free(mine);
77                 return (ARCHIVE_FATAL);
78         }
79         mine->fd = fd;
80         return (archive_read_open2(a, mine, file_open, file_read, file_skip, file_close));
81 }
82
83 static int
84 file_open(struct archive *a, void *client_data)
85 {
86         struct read_fd_data *mine = (struct read_fd_data *)client_data;
87         struct stat st;
88
89         if (fstat(mine->fd, &st) != 0) {
90                 archive_set_error(a, errno, "Can't stat fd %d", mine->fd);
91                 return (ARCHIVE_FATAL);
92         }
93
94         if (S_ISREG(st.st_mode))
95                 archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
96         return (ARCHIVE_OK);
97 }
98
99 static ssize_t
100 file_read(struct archive *a, void *client_data, const void **buff)
101 {
102         struct read_fd_data *mine = (struct read_fd_data *)client_data;
103         ssize_t bytes_read;
104
105         *buff = mine->buffer;
106         bytes_read = read(mine->fd, mine->buffer, mine->block_size);
107         if (bytes_read < 0) {
108                 archive_set_error(a, errno, "Error reading fd %d", mine->fd);
109         }
110         return (bytes_read);
111 }
112
113 #if ARCHIVE_API_VERSION < 2
114 static ssize_t
115 file_skip(struct archive *a, void *client_data, size_t request)
116 #else
117 static off_t
118 file_skip(struct archive *a, void *client_data, off_t request)
119 #endif
120 {
121         struct read_fd_data *mine = (struct read_fd_data *)client_data;
122         off_t old_offset, new_offset;
123
124         /* Reduce request to the next smallest multiple of block_size */
125         request = (request / mine->block_size) * mine->block_size;
126         /*
127          * Hurray for lazy evaluation: if the first lseek fails, the second
128          * one will not be executed.
129          */
130         if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
131             ((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
132         {
133                 if (errno == ESPIPE)
134                 {
135                         /*
136                          * Failure to lseek() can be caused by the file
137                          * descriptor pointing to a pipe, socket or FIFO.
138                          * Return 0 here, so the compression layer will use
139                          * read()s instead to advance the file descriptor.
140                          * It's slower of course, but works as well.
141                          */
142                         return (0);
143                 }
144                 /*
145                  * There's been an error other than ESPIPE. This is most
146                  * likely caused by a programmer error (too large request)
147                  * or a corrupted archive file.
148                  */
149                 archive_set_error(a, errno, "Error seeking");
150                 return (-1);
151         }
152         return (new_offset - old_offset);
153 }
154
155 static int
156 file_close(struct archive *a, void *client_data)
157 {
158         struct read_fd_data *mine = (struct read_fd_data *)client_data;
159
160         (void)a; /* UNUSED */
161         if (mine->buffer != NULL)
162                 free(mine->buffer);
163         free(mine);
164         return (ARCHIVE_OK);
165 }