Import libarchive 2.1.9.
[dragonfly.git] / contrib / libarchive-2.1 / 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: src/lib/libarchive/archive_read_open_file.c,v 1.19 2007/01/09 08:05:55 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         FILE    *f;
52         size_t   block_size;
53         void    *buffer;
54 };
55
56 static int      file_close(struct archive *, void *);
57 static int      file_open(struct archive *, void *);
58 static ssize_t  file_read(struct archive *, void *, const void **buff);
59 #if ARCHIVE_API_VERSION < 2
60 static ssize_t  file_skip(struct archive *, void *, size_t request);
61 #else
62 static off_t    file_skip(struct archive *, void *, off_t request);
63 #endif
64
65 int
66 archive_read_open_FILE(struct archive *a, FILE *f)
67 {
68         struct read_FILE_data *mine;
69
70         mine = (struct read_FILE_data *)malloc(sizeof(*mine));
71         if (mine == NULL) {
72                 archive_set_error(a, ENOMEM, "No memory");
73                 return (ARCHIVE_FATAL);
74         }
75         mine->block_size = 128 * 1024;
76         mine->buffer = malloc(mine->block_size);
77         if (mine->buffer == NULL) {
78                 archive_set_error(a, ENOMEM, "No memory");
79                 free(mine);
80                 return (ARCHIVE_FATAL);
81         }
82         mine->f = f;
83         return (archive_read_open2(a, mine, file_open, file_read,
84                     file_skip, file_close));
85 }
86
87 static int
88 file_open(struct archive *a, void *client_data)
89 {
90         struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
91         struct stat st;
92
93         /*
94          * If we can't fstat() the file, it may just be that
95          * it's not a file.  (FILE * objects can wrap many kinds
96          * of I/O streams.)
97          */
98         if (fstat(fileno(mine->f), &st) == 0 && S_ISREG(st.st_mode))
99                 archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
100
101         return (ARCHIVE_OK);
102 }
103
104 static ssize_t
105 file_read(struct archive *a, void *client_data, const void **buff)
106 {
107         struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
108         ssize_t bytes_read;
109
110         *buff = mine->buffer;
111         bytes_read = fread(mine->buffer, 1, mine->block_size, mine->f);
112         if (bytes_read < 0) {
113                 archive_set_error(a, errno, "Error reading file");
114         }
115         return (bytes_read);
116 }
117
118 #if ARCHIVE_API_VERSION < 2
119 static ssize_t
120 file_skip(struct archive *a, void *client_data, size_t request)
121 #else
122 static off_t
123 file_skip(struct archive *a, void *client_data, off_t request)
124 #endif
125 {
126         struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
127
128         /*
129          * Note: the 'fd' and 'filename' versions round the request
130          * down to a multiple of the block size to ensure proper
131          * operation on block-oriented media such as tapes.  But stdio
132          * doesn't work with such media (it doesn't ensure blocking),
133          * so we don't need to bother.
134          */
135 #if HAVE_FSEEKO
136         if (fseeko(mine->f, request, SEEK_CUR) != 0)
137 #else
138         if (fseek(mine->f, request, SEEK_CUR) != 0)
139 #endif
140         {
141                 archive_set_error(a, errno, "Error skipping forward");
142                 return (ARCHIVE_FATAL);
143         }
144         return (request);
145 }
146
147 static int
148 file_close(struct archive *a, void *client_data)
149 {
150         struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
151
152         (void)a; /* UNUSED */
153         if (mine->buffer != NULL)
154                 free(mine->buffer);
155         free(mine);
156         return (ARCHIVE_OK);
157 }