]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libarchive/archive_read_open_file.c
These files no longer use internal APIs, so no longer need to include
[FreeBSD/FreeBSD.git] / lib / libarchive / archive_read_open_file.c
1 /*-
2  * Copyright (c) 2003-2004 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  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
29
30 #ifdef HAVE_SYS_STAT_H
31 #include <sys/stat.h>
32 #endif
33 #ifdef HAVE_ERRNO_H
34 #include <errno.h>
35 #endif
36 #ifdef HAVE_FCNTL_H
37 #include <fcntl.h>
38 #endif
39 #ifdef HAVE_STDLIB_H
40 #include <stdlib.h>
41 #endif
42 #ifdef HAVE_STRING_H
43 #include <string.h>
44 #endif
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48
49 #include "archive.h"
50
51 struct read_file_data {
52         int      fd;
53         size_t   block_size;
54         void    *buffer;
55         mode_t   st_mode;  /* Mode bits for opened file. */
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 static ssize_t  file_skip(struct archive *, void *, size_t request);
63
64 int
65 archive_read_open_file(struct archive *a, const char *filename,
66     size_t block_size)
67 {
68         struct read_file_data *mine;
69
70         if (filename == NULL || filename[0] == '\0') {
71                 mine = (struct read_file_data *)malloc(sizeof(*mine));
72                 if (mine == NULL) {
73                         archive_set_error(a, ENOMEM, "No memory");
74                         return (ARCHIVE_FATAL);
75                 }
76                 mine->filename[0] = '\0';
77         } else {
78                 mine = (struct read_file_data *)malloc(sizeof(*mine) + strlen(filename));
79                 if (mine == NULL) {
80                         archive_set_error(a, ENOMEM, "No memory");
81                         return (ARCHIVE_FATAL);
82                 }
83                 strcpy(mine->filename, filename);
84         }
85         mine->block_size = block_size;
86         mine->buffer = NULL;
87         mine->fd = -1;
88         return (archive_read_open2(a, mine, file_open, file_read, file_skip, file_close));
89 }
90
91 static int
92 file_open(struct archive *a, void *client_data)
93 {
94         struct read_file_data *mine = (struct read_file_data *)client_data;
95         struct stat st;
96
97         mine->buffer = malloc(mine->block_size);
98         if (mine->buffer == NULL) {
99                 archive_set_error(a, ENOMEM, "No memory");
100                 return (ARCHIVE_FATAL);
101         }
102         if (mine->filename[0] != '\0')
103                 mine->fd = open(mine->filename, O_RDONLY);
104         else
105                 mine->fd = 0; /* Fake "open" for stdin. */
106         if (mine->fd < 0) {
107                 archive_set_error(a, errno, "Failed to open '%s'",
108                     mine->filename);
109                 return (ARCHIVE_FATAL);
110         }
111         if (fstat(mine->fd, &st) == 0) {
112                 /* If we're reading a file from disk, ensure that we don't
113                    overwrite it with an extracted file. */
114                 if (S_ISREG(st.st_mode))
115                         archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
116                 /* Remember mode so close can decide whether to flush. */
117                 mine->st_mode = st.st_mode;
118         } else {
119                 if (mine->filename[0] == '\0')
120                         archive_set_error(a, errno, "Can't stat stdin");
121                 else
122                         archive_set_error(a, errno, "Can't stat '%s'",
123                             mine->filename);
124                 return (ARCHIVE_FATAL);
125         }
126         return (0);
127 }
128
129 static ssize_t
130 file_read(struct archive *a, void *client_data, const void **buff)
131 {
132         struct read_file_data *mine = (struct read_file_data *)client_data;
133         ssize_t bytes_read;
134
135         *buff = mine->buffer;
136         bytes_read = read(mine->fd, mine->buffer, mine->block_size);
137         if (bytes_read < 0) {
138                 if (mine->filename[0] == '\0')
139                         archive_set_error(a, errno, "Error reading stdin");
140                 else
141                         archive_set_error(a, errno, "Error reading '%s'",
142                             mine->filename);
143         }
144         return (bytes_read);
145 }
146
147 static ssize_t
148 file_skip(struct archive *a, void *client_data, size_t request)
149 {
150         struct read_file_data *mine = (struct read_file_data *)client_data;
151         off_t old_offset, new_offset;
152
153         /* Reduce request to the next smallest multiple of block_size */
154         request = (request / mine->block_size) * mine->block_size;
155         /*
156          * Hurray for lazy evaluation: if the first lseek fails, the second
157          * one will not be executed.
158          */
159         if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
160             ((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
161         {
162                 if (errno == ESPIPE)
163                 {
164                         /*
165                          * Failure to lseek() can be caused by the file
166                          * descriptor pointing to a pipe, socket or FIFO.
167                          * Return 0 here, so the compression layer will use
168                          * read()s instead to advance the file descriptor.
169                          * It's slower of course, but works as well.
170                          */
171                         return (0);
172                 }
173                 /*
174                  * There's been an error other than ESPIPE. This is most
175                  * likely caused by a programmer error (too large request)
176                  * or a corrupted archive file.
177                  */
178                 if (mine->filename[0] == '\0')
179                         /*
180                          * Should never get here, since lseek() on stdin ought
181                          * to return an ESPIPE error.
182                          */
183                         archive_set_error(a, errno, "Error seeking in stdin");
184                 else
185                         archive_set_error(a, errno, "Error seeking in '%s'",
186                             mine->filename);
187                 return (-1);
188         }
189         return (new_offset - old_offset);
190 }
191
192 static int
193 file_close(struct archive *a, void *client_data)
194 {
195         struct read_file_data *mine = (struct read_file_data *)client_data;
196
197         (void)a; /* UNUSED */
198
199         /*
200          * Sometimes, we should flush the input before closing.
201          *   Regular files: faster to just close without flush.
202          *   Devices: must not flush (user might need to
203          *      read the "next" item on a non-rewind device).
204          *   Pipes and sockets:  must flush (otherwise, the
205          *      program feeding the pipe or socket may complain).
206          * Here, I flush everything except for regular files and
207          * device nodes.
208          */
209         if (!S_ISREG(mine->st_mode)
210             && !S_ISCHR(mine->st_mode)
211             && !S_ISBLK(mine->st_mode)) {
212                 ssize_t bytesRead;
213                 do {
214                         bytesRead = read(mine->fd, mine->buffer,
215                             mine->block_size);
216                 } while (bytesRead > 0);
217         }
218         /* If a named file was opened, then it needs to be closed. */
219         if (mine->filename[0] != '\0')
220                 close(mine->fd);
221         if (mine->buffer != NULL)
222                 free(mine->buffer);
223         free(mine);
224         return (ARCHIVE_OK);
225 }