Synchronous libarchive to 2.2.4 from FreeBSD, including fixes related to
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_read_open_filename.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_filename.c,v 1.20 2007/06/26 03:06:48 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_FCNTL_H
36 #include <fcntl.h>
37 #endif
38 #ifdef HAVE_STDLIB_H
39 #include <stdlib.h>
40 #endif
41 #ifdef HAVE_STRING_H
42 #include <string.h>
43 #endif
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47
48 #include "archive.h"
49
50 struct read_file_data {
51         int      fd;
52         size_t   block_size;
53         void    *buffer;
54         mode_t   st_mode;  /* Mode bits for opened file. */
55         char     can_skip; /* This file supports skipping. */
56         char     filename[1]; /* Must be last! */
57 };
58
59 static int      file_close(struct archive *, void *);
60 static int      file_open(struct archive *, void *);
61 static ssize_t  file_read(struct archive *, void *, const void **buff);
62 #if ARCHIVE_API_VERSION < 2
63 static ssize_t  file_skip(struct archive *, void *, size_t request);
64 #else
65 static off_t    file_skip(struct archive *, void *, off_t request);
66 #endif
67
68 int
69 archive_read_open_file(struct archive *a, const char *filename,
70     size_t block_size)
71 {
72         return (archive_read_open_filename(a, filename, block_size));
73 }
74
75 int
76 archive_read_open_filename(struct archive *a, const char *filename,
77     size_t block_size)
78 {
79         struct read_file_data *mine;
80
81         if (filename == NULL || filename[0] == '\0') {
82                 mine = (struct read_file_data *)malloc(sizeof(*mine));
83                 if (mine == NULL) {
84                         archive_set_error(a, ENOMEM, "No memory");
85                         return (ARCHIVE_FATAL);
86                 }
87                 mine->filename[0] = '\0';
88         } else {
89                 mine = (struct read_file_data *)malloc(sizeof(*mine) + strlen(filename));
90                 if (mine == NULL) {
91                         archive_set_error(a, ENOMEM, "No memory");
92                         return (ARCHIVE_FATAL);
93                 }
94                 strcpy(mine->filename, filename);
95         }
96         mine->block_size = block_size;
97         mine->buffer = NULL;
98         mine->fd = -1;
99         /* lseek() almost never works; disable it by default.  See below. */
100         mine->can_skip = 0;
101         return (archive_read_open2(a, mine, file_open, file_read, file_skip, file_close));
102 }
103
104 static int
105 file_open(struct archive *a, void *client_data)
106 {
107         struct read_file_data *mine = (struct read_file_data *)client_data;
108         struct stat st;
109
110         mine->buffer = malloc(mine->block_size);
111         if (mine->buffer == NULL) {
112                 archive_set_error(a, ENOMEM, "No memory");
113                 return (ARCHIVE_FATAL);
114         }
115         if (mine->filename[0] != '\0')
116                 mine->fd = open(mine->filename, O_RDONLY);
117         else
118                 mine->fd = 0; /* Fake "open" for stdin. */
119         if (mine->fd < 0) {
120                 archive_set_error(a, errno, "Failed to open '%s'",
121                     mine->filename);
122                 return (ARCHIVE_FATAL);
123         }
124         if (fstat(mine->fd, &st) == 0) {
125                 /* If we're reading a file from disk, ensure that we don't
126                    overwrite it with an extracted file. */
127                 if (S_ISREG(st.st_mode)) {
128                         archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
129                         /*
130                          * Enabling skip here is a performance
131                          * optimization for anything that supports
132                          * lseek().  On FreeBSD, only regular files
133                          * and raw disk devices support lseek() and
134                          * there's no portable way to determine if a
135                          * device is a raw disk device, so we only
136                          * enable this optimization for regular files.
137                          */
138                         mine->can_skip = 1;
139                 }
140                 /* Remember mode so close can decide whether to flush. */
141                 mine->st_mode = st.st_mode;
142         } else {
143                 if (mine->filename[0] == '\0')
144                         archive_set_error(a, errno, "Can't stat stdin");
145                 else
146                         archive_set_error(a, errno, "Can't stat '%s'",
147                             mine->filename);
148                 return (ARCHIVE_FATAL);
149         }
150         return (0);
151 }
152
153 static ssize_t
154 file_read(struct archive *a, void *client_data, const void **buff)
155 {
156         struct read_file_data *mine = (struct read_file_data *)client_data;
157         ssize_t bytes_read;
158
159         *buff = mine->buffer;
160         bytes_read = read(mine->fd, mine->buffer, mine->block_size);
161         if (bytes_read < 0) {
162                 if (mine->filename[0] == '\0')
163                         archive_set_error(a, errno, "Error reading stdin");
164                 else
165                         archive_set_error(a, errno, "Error reading '%s'",
166                             mine->filename);
167         }
168         return (bytes_read);
169 }
170
171 #if ARCHIVE_API_VERSION < 2
172 static ssize_t
173 file_skip(struct archive *a, void *client_data, size_t request)
174 #else
175 static off_t
176 file_skip(struct archive *a, void *client_data, off_t request)
177 #endif
178 {
179         struct read_file_data *mine = (struct read_file_data *)client_data;
180         off_t old_offset, new_offset;
181
182         if (!mine->can_skip) /* We can't skip, so ... */
183                 return (0); /* ... skip zero bytes. */
184
185         /* Reduce request to the next smallest multiple of block_size */
186         request = (request / mine->block_size) * mine->block_size;
187         if (request == 0)
188                 return (0);
189
190         /*
191          * Hurray for lazy evaluation: if the first lseek fails, the second
192          * one will not be executed.
193          */
194         if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
195             ((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
196         {
197                 /* If skip failed once, it will probably fail again. */
198                 mine->can_skip = 0;
199
200                 if (errno == ESPIPE)
201                 {
202                         /*
203                          * Failure to lseek() can be caused by the file
204                          * descriptor pointing to a pipe, socket or FIFO.
205                          * Return 0 here, so the compression layer will use
206                          * read()s instead to advance the file descriptor.
207                          * It's slower of course, but works as well.
208                          */
209                         return (0);
210                 }
211                 /*
212                  * There's been an error other than ESPIPE. This is most
213                  * likely caused by a programmer error (too large request)
214                  * or a corrupted archive file.
215                  */
216                 if (mine->filename[0] == '\0')
217                         /*
218                          * Should never get here, since lseek() on stdin ought
219                          * to return an ESPIPE error.
220                          */
221                         archive_set_error(a, errno, "Error seeking in stdin");
222                 else
223                         archive_set_error(a, errno, "Error seeking in '%s'",
224                             mine->filename);
225                 return (-1);
226         }
227         return (new_offset - old_offset);
228 }
229
230 static int
231 file_close(struct archive *a, void *client_data)
232 {
233         struct read_file_data *mine = (struct read_file_data *)client_data;
234
235         (void)a; /* UNUSED */
236
237         /*
238          * Sometimes, we should flush the input before closing.
239          *   Regular files: faster to just close without flush.
240          *   Devices: must not flush (user might need to
241          *      read the "next" item on a non-rewind device).
242          *   Pipes and sockets:  must flush (otherwise, the
243          *      program feeding the pipe or socket may complain).
244          * Here, I flush everything except for regular files and
245          * device nodes.
246          */
247         if (!S_ISREG(mine->st_mode)
248             && !S_ISCHR(mine->st_mode)
249             && !S_ISBLK(mine->st_mode)) {
250                 ssize_t bytesRead;
251                 do {
252                         bytesRead = read(mine->fd, mine->buffer,
253                             mine->block_size);
254                 } while (bytesRead > 0);
255         }
256         /* If a named file was opened, then it needs to be closed. */
257         if (mine->filename[0] != '\0')
258                 close(mine->fd);
259         if (mine->buffer != NULL)
260                 free(mine->buffer);
261         free(mine);
262         return (ARCHIVE_OK);
263 }