]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libarchive/archive_read_open_file.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libarchive / archive_read_open_file.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$");
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         FILE    *f;
52         size_t   block_size;
53         void    *buffer;
54         char     can_skip;
55 };
56
57 static int      file_close(struct archive *, void *);
58 static int      file_open(struct archive *, void *);
59 static ssize_t  file_read(struct archive *, void *, const void **buff);
60 #if ARCHIVE_API_VERSION < 2
61 static ssize_t  file_skip(struct archive *, void *, size_t request);
62 #else
63 static off_t    file_skip(struct archive *, void *, off_t request);
64 #endif
65
66 int
67 archive_read_open_FILE(struct archive *a, FILE *f)
68 {
69         struct read_FILE_data *mine;
70
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->block_size = 128 * 1024;
77         mine->buffer = malloc(mine->block_size);
78         if (mine->buffer == NULL) {
79                 archive_set_error(a, ENOMEM, "No memory");
80                 free(mine);
81                 return (ARCHIVE_FATAL);
82         }
83         mine->f = f;
84         /* Suppress skip by default. See below. */
85         mine->can_skip = 0;
86         return (archive_read_open2(a, mine, file_open, file_read,
87                     file_skip, file_close));
88 }
89
90 static int
91 file_open(struct archive *a, void *client_data)
92 {
93         struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
94         struct stat st;
95
96         /*
97          * If we can't fstat() the file, it may just be that
98          * it's not a file.  (FILE * objects can wrap many kinds
99          * of I/O streams.)
100          */
101         if (fstat(fileno(mine->f), &st) == 0 && S_ISREG(st.st_mode)) {
102                 archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
103                 /* Enable the seek optimization for regular files. */
104                 mine->can_skip = 1;
105         }
106
107         return (ARCHIVE_OK);
108 }
109
110 static ssize_t
111 file_read(struct archive *a, void *client_data, const void **buff)
112 {
113         struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
114         ssize_t bytes_read;
115
116         *buff = mine->buffer;
117         bytes_read = fread(mine->buffer, 1, mine->block_size, mine->f);
118         if (bytes_read < 0) {
119                 archive_set_error(a, errno, "Error reading file");
120         }
121         return (bytes_read);
122 }
123
124 #if ARCHIVE_API_VERSION < 2
125 static ssize_t
126 file_skip(struct archive *a, void *client_data, size_t request)
127 #else
128 static off_t
129 file_skip(struct archive *a, void *client_data, off_t request)
130 #endif
131 {
132         struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
133
134         (void)a; /* UNUSED */
135
136         /*
137          * If we can't skip, return 0 as the amount we did step and
138          * the caller will work around by reading and discarding.
139          */
140         if (!mine->can_skip)
141                 return (0);
142         if (request == 0)
143                 return (0);
144
145 #if HAVE_FSEEKO
146         if (fseeko(mine->f, request, SEEK_CUR) != 0)
147 #else
148         if (fseek(mine->f, request, SEEK_CUR) != 0)
149 #endif
150         {
151                 mine->can_skip = 0;
152                 return (0);
153         }
154         return (request);
155 }
156
157 static int
158 file_close(struct archive *a, void *client_data)
159 {
160         struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
161
162         (void)a; /* UNUSED */
163         if (mine->buffer != NULL)
164                 free(mine->buffer);
165         free(mine);
166         return (ARCHIVE_OK);
167 }