]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libarchive/archive_util.c
This commit was generated by cvs2svn to compensate for changes in r177580,
[FreeBSD/FreeBSD.git] / lib / libarchive / archive_util.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_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35 #ifdef HAVE_STRING_H
36 #include <string.h>
37 #endif
38
39 #include "archive.h"
40 #include "archive_private.h"
41 #include "archive_string.h"
42
43 #if ARCHIVE_VERSION_NUMBER < 3000000
44 /* These disappear in libarchive 3.0 */
45 /* Deprecated. */
46 int
47 archive_api_feature(void)
48 {
49         return (ARCHIVE_API_FEATURE);
50 }
51
52 /* Deprecated. */
53 int
54 archive_api_version(void)
55 {
56         return (ARCHIVE_API_VERSION);
57 }
58
59 /* Deprecated synonym for archive_version_number() */
60 int
61 archive_version_stamp(void)
62 {
63         return (archive_version_number());
64 }
65
66 /* Deprecated synonym for archive_version_string() */
67 const char *
68 archive_version(void)
69 {
70         return (archive_version_string());
71 }
72 #endif
73
74 int
75 archive_version_number(void)
76 {
77         return (ARCHIVE_VERSION_NUMBER);
78 }
79
80 /*
81  * Format a version string of the form "libarchive x.y.z", where x, y,
82  * z are the correct parts of the version ID from
83  * archive_version_number().
84  *
85  * I used to do all of this at build time in shell scripts but that
86  * proved to be a portability headache.
87  */
88
89 const char *
90 archive_version_string(void)
91 {
92         static char buff[128];
93         struct archive_string as;
94         int n;
95
96         if (buff[0] == '\0') {
97                 n = archive_version_number();
98                 memset(&as, 0, sizeof(as));
99                 archive_string_sprintf(&as, "libarchive %d.%d.%d",
100                     n / 1000000, (n / 1000) % 1000, n % 1000);
101                 strncpy(buff, as.s, sizeof(buff));
102                 buff[sizeof(buff) - 1] = '\0';
103                 archive_string_free(&as);
104         }
105         return (buff);
106 }
107
108 int
109 archive_errno(struct archive *a)
110 {
111         return (a->archive_error_number);
112 }
113
114 const char *
115 archive_error_string(struct archive *a)
116 {
117
118         if (a->error != NULL  &&  *a->error != '\0')
119                 return (a->error);
120         else
121                 return ("(Empty error message)");
122 }
123
124
125 int
126 archive_format(struct archive *a)
127 {
128         return (a->archive_format);
129 }
130
131 const char *
132 archive_format_name(struct archive *a)
133 {
134         return (a->archive_format_name);
135 }
136
137
138 int
139 archive_compression(struct archive *a)
140 {
141         return (a->compression_code);
142 }
143
144 const char *
145 archive_compression_name(struct archive *a)
146 {
147         return (a->compression_name);
148 }
149
150
151 /*
152  * Return a count of the number of compressed bytes processed.
153  */
154 int64_t
155 archive_position_compressed(struct archive *a)
156 {
157         return (a->raw_position);
158 }
159
160 /*
161  * Return a count of the number of uncompressed bytes processed.
162  */
163 int64_t
164 archive_position_uncompressed(struct archive *a)
165 {
166         return (a->file_position);
167 }
168
169 void
170 archive_clear_error(struct archive *a)
171 {
172         archive_string_empty(&a->error_string);
173         a->error = NULL;
174 }
175
176 void
177 archive_set_error(struct archive *a, int error_number, const char *fmt, ...)
178 {
179         va_list ap;
180 #ifdef HAVE_STRERROR_R
181         char errbuff[512];
182 #endif
183         char *errp;
184
185         a->archive_error_number = error_number;
186         if (fmt == NULL) {
187                 a->error = NULL;
188                 return;
189         }
190
191         va_start(ap, fmt);
192         archive_string_vsprintf(&(a->error_string), fmt, ap);
193         if (error_number > 0) {
194                 archive_strcat(&(a->error_string), ": ");
195 #ifdef HAVE_STRERROR_R
196 #ifdef STRERROR_R_CHAR_P
197                 errp = strerror_r(error_number, errbuff, sizeof(errbuff));
198 #else
199                 strerror_r(error_number, errbuff, sizeof(errbuff));
200                 errp = errbuff;
201 #endif
202 #else
203                 /* Note: this is not threadsafe! */
204                 errp = strerror(error_number);
205 #endif
206                 archive_strcat(&(a->error_string), errp);
207         }
208         a->error = a->error_string.s;
209         va_end(ap);
210 }
211
212 void
213 archive_copy_error(struct archive *dest, struct archive *src)
214 {
215         dest->archive_error_number = src->archive_error_number;
216
217         archive_string_copy(&dest->error_string, &src->error_string);
218         dest->error = dest->error_string.s;
219 }
220
221 void
222 __archive_errx(int retvalue, const char *msg)
223 {
224         static const char *msg1 = "Fatal Internal Error in libarchive: ";
225         write(2, msg1, strlen(msg1));
226         write(2, msg, strlen(msg));
227         write(2, "\n", 1);
228         exit(retvalue);
229 }