]> 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 r155420,
[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
52 int
53 archive_read_open_file(struct archive *a, const char *filename,
54     size_t block_size)
55 {
56         struct read_file_data *mine;
57
58         if (filename == NULL || filename[0] == '\0') {
59                 mine = malloc(sizeof(*mine));
60                 if (mine == NULL) {
61                         archive_set_error(a, ENOMEM, "No memory");
62                         return (ARCHIVE_FATAL);
63                 }
64                 mine->filename[0] = '\0';
65         } else {
66                 mine = malloc(sizeof(*mine) + strlen(filename));
67                 if (mine == NULL) {
68                         archive_set_error(a, ENOMEM, "No memory");
69                         return (ARCHIVE_FATAL);
70                 }
71                 strcpy(mine->filename, filename);
72         }
73         mine->block_size = block_size;
74         mine->buffer = NULL;
75         mine->fd = -1;
76         return (archive_read_open(a, mine, file_open, file_read, file_close));
77 }
78
79 static int
80 file_open(struct archive *a, void *client_data)
81 {
82         struct read_file_data *mine = client_data;
83         struct stat st;
84
85         mine->buffer = malloc(mine->block_size);
86         if (mine->buffer == NULL) {
87                 archive_set_error(a, ENOMEM, "No memory");
88                 return (ARCHIVE_FATAL);
89         }
90         if (mine->filename[0] != '\0')
91                 mine->fd = open(mine->filename, O_RDONLY);
92         else
93                 mine->fd = 0; /* Fake "open" for stdin. */
94         if (mine->fd < 0) {
95                 archive_set_error(a, errno, "Failed to open '%s'",
96                     mine->filename);
97                 return (ARCHIVE_FATAL);
98         }
99         if (fstat(mine->fd, &st) == 0) {
100                 /* Set dev/ino of archive file so extract won't overwrite. */
101                 a->skip_file_dev = st.st_dev;
102                 a->skip_file_ino = st.st_ino;
103                 /* Remember mode so close can decide whether to flush. */
104                 mine->st_mode = st.st_mode;
105         } else {
106                 if (mine->filename[0] == '\0')
107                         archive_set_error(a, errno, "Can't stat stdin");
108                 else
109                         archive_set_error(a, errno, "Can't stat '%s'",
110                             mine->filename);
111                 return (ARCHIVE_FATAL);
112         }
113         return (0);
114 }
115
116 static ssize_t
117 file_read(struct archive *a, void *client_data, const void **buff)
118 {
119         struct read_file_data *mine = client_data;
120         ssize_t bytes_read;
121
122         (void)a; /* UNUSED */
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 int
136 file_close(struct archive *a, void *client_data)
137 {
138         struct read_file_data *mine = client_data;
139
140         (void)a; /* UNUSED */
141
142         /*
143          * Sometimes, we should flush the input before closing.
144          *   Regular files: faster to just close without flush.
145          *   Devices: must not flush (user might need to
146          *      read the "next" item on a non-rewind device).
147          *   Pipes and sockets:  must flush (otherwise, the
148          *      program feeding the pipe or socket may complain).
149          * Here, I flush everything except for regular files and
150          * device nodes.
151          */
152         if (!S_ISREG(mine->st_mode)
153             && !S_ISCHR(mine->st_mode)
154             && !S_ISBLK(mine->st_mode)) {
155                 ssize_t bytesRead;
156                 do {
157                         bytesRead = read(mine->fd, mine->buffer,
158                             mine->block_size);
159                 } while (bytesRead > 0);
160         }
161         /* If a named file was opened, then it needs to be closed. */
162         if (mine->filename[0] != '\0')
163                 close(mine->fd);
164         if (mine->buffer != NULL)
165                 free(mine->buffer);
166         free(mine);
167         return (ARCHIVE_OK);
168 }