]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libarchive/archive_read_open_file.c
This commit was generated by cvs2svn to compensate for changes in r162017,
[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 #include <sys/stat.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36
37 #include "archive.h"
38 #include "archive_private.h"
39
40 struct read_file_data {
41         int      fd;
42         size_t   block_size;
43         void    *buffer;
44         mode_t   st_mode;  /* Mode bits for opened file. */
45         char     filename[1]; /* Must be last! */
46 };
47
48 static int      file_close(struct archive *, void *);
49 static int      file_open(struct archive *, void *);
50 static ssize_t  file_read(struct archive *, void *, const void **buff);
51 static ssize_t  file_skip(struct archive *, void *, size_t request);
52
53 int
54 archive_read_open_file(struct archive *a, const char *filename,
55     size_t block_size)
56 {
57         struct read_file_data *mine;
58
59         if (filename == NULL || filename[0] == '\0') {
60                 mine = malloc(sizeof(*mine));
61                 if (mine == NULL) {
62                         archive_set_error(a, ENOMEM, "No memory");
63                         return (ARCHIVE_FATAL);
64                 }
65                 mine->filename[0] = '\0';
66         } else {
67                 mine = malloc(sizeof(*mine) + strlen(filename));
68                 if (mine == NULL) {
69                         archive_set_error(a, ENOMEM, "No memory");
70                         return (ARCHIVE_FATAL);
71                 }
72                 strcpy(mine->filename, filename);
73         }
74         mine->block_size = block_size;
75         mine->buffer = NULL;
76         mine->fd = -1;
77         return (archive_read_open2(a, mine, file_open, file_read, file_skip, file_close));
78 }
79
80 static int
81 file_open(struct archive *a, void *client_data)
82 {
83         struct read_file_data *mine = client_data;
84         struct stat st;
85
86         mine->buffer = malloc(mine->block_size);
87         if (mine->buffer == NULL) {
88                 archive_set_error(a, ENOMEM, "No memory");
89                 return (ARCHIVE_FATAL);
90         }
91         if (mine->filename[0] != '\0')
92                 mine->fd = open(mine->filename, O_RDONLY);
93         else
94                 mine->fd = 0; /* Fake "open" for stdin. */
95         if (mine->fd < 0) {
96                 archive_set_error(a, errno, "Failed to open '%s'",
97                     mine->filename);
98                 return (ARCHIVE_FATAL);
99         }
100         if (fstat(mine->fd, &st) == 0) {
101                 /* Set dev/ino of archive file so extract won't overwrite. */
102                 a->skip_file_dev = st.st_dev;
103                 a->skip_file_ino = st.st_ino;
104                 /* Remember mode so close can decide whether to flush. */
105                 mine->st_mode = st.st_mode;
106         } else {
107                 if (mine->filename[0] == '\0')
108                         archive_set_error(a, errno, "Can't stat stdin");
109                 else
110                         archive_set_error(a, errno, "Can't stat '%s'",
111                             mine->filename);
112                 return (ARCHIVE_FATAL);
113         }
114         return (0);
115 }
116
117 static ssize_t
118 file_read(struct archive *a, void *client_data, const void **buff)
119 {
120         struct read_file_data *mine = client_data;
121         ssize_t bytes_read;
122
123         *buff = mine->buffer;
124         bytes_read = read(mine->fd, mine->buffer, mine->block_size);
125         if (bytes_read < 0) {
126                 if (mine->filename[0] == '\0')
127                         archive_set_error(a, errno, "Error reading stdin");
128                 else
129                         archive_set_error(a, errno, "Error reading '%s'",
130                             mine->filename);
131         }
132         return (bytes_read);
133 }
134
135 static ssize_t
136 file_skip(struct archive *a, void *client_data, size_t request)
137 {
138         struct read_file_data *mine = client_data;
139         off_t old_offset, new_offset;
140         
141         /* Reduce request to the next smallest multiple of block_size */
142         request = (request / mine->block_size) * mine->block_size;
143         /*
144          * Hurray for lazy evaluation: if the first lseek fails, the second
145          * one will not be executed.
146          */
147         if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
148             ((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
149         {
150                 if (errno == ESPIPE)
151                 {
152                         /*
153                          * Failure to lseek() can be caused by the file
154                          * descriptor pointing to a pipe, socket or FIFO.
155                          * Return 0 here, so the compression layer will use
156                          * read()s instead to advance the file descriptor.
157                          * It's slower of course, but works as well.
158                          */
159                         return (0);
160                 }
161                 /*
162                  * There's been an error other than ESPIPE. This is most
163                  * likely caused by a programmer error (too large request)
164                  * or a corrupted archive file.
165                  */
166                 if (mine->filename[0] == '\0')
167                         /*
168                          * Should never get here, since lseek() on stdin ought
169                          * to return an ESPIPE error.
170                          */
171                         archive_set_error(a, errno, "Error seeking in stdin");
172                 else
173                         archive_set_error(a, errno, "Error seeking in '%s'",
174                             mine->filename);
175                 return (-1);
176         }
177         return (new_offset - old_offset);
178 }
179
180 static int
181 file_close(struct archive *a, void *client_data)
182 {
183         struct read_file_data *mine = client_data;
184
185         (void)a; /* UNUSED */
186
187         /*
188          * Sometimes, we should flush the input before closing.
189          *   Regular files: faster to just close without flush.
190          *   Devices: must not flush (user might need to
191          *      read the "next" item on a non-rewind device).
192          *   Pipes and sockets:  must flush (otherwise, the
193          *      program feeding the pipe or socket may complain).
194          * Here, I flush everything except for regular files and
195          * device nodes.
196          */
197         if (!S_ISREG(mine->st_mode)
198             && !S_ISCHR(mine->st_mode)
199             && !S_ISBLK(mine->st_mode)) {
200                 ssize_t bytesRead;
201                 do {
202                         bytesRead = read(mine->fd, mine->buffer,
203                             mine->block_size);
204                 } while (bytesRead > 0);
205         }
206         /* If a named file was opened, then it needs to be closed. */
207         if (mine->filename[0] != '\0')
208                 close(mine->fd);
209         if (mine->buffer != NULL)
210                 free(mine->buffer);
211         free(mine);
212         return (ARCHIVE_OK);
213 }