2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include "bsdtar_platform.h"
27 __FBSDID("$FreeBSD: src/usr.bin/tar/util.c,v 1.17 2007/04/18 04:36:11 kientzle Exp $");
29 #ifdef HAVE_SYS_STAT_H
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h> /* Linux doesn't define mode_t, etc. in sys/stat.h. */
52 static void bsdtar_vwarnc(struct bsdtar
*, int code
,
53 const char *fmt
, va_list ap
);
56 * Print a string, taking care with any non-printable characters.
60 safe_fprintf(FILE *f
, const char *fmt
, ...)
72 /* Use a stack-allocated buffer if we can, for speed and safety. */
74 buff_length
= sizeof(buff_stack
);
78 length
= vsnprintf(buff
, buff_length
, fmt
, ap
);
80 /* If the result is too large, allocate a buffer on the heap. */
81 if (length
>= buff_length
) {
82 buff_length
= length
+1;
83 buff_heap
= malloc(buff_length
);
84 /* Failsafe: use the truncated string if malloc fails. */
85 if (buff_heap
!= NULL
) {
88 length
= vsnprintf(buff
, buff_length
, fmt
, ap
);
93 /* Write data, expanding unprintable characters. */
97 unsigned char c
= *p
++;
99 if (isprint(c
) && c
!= '\\')
102 copy_buff
[i
++] = '\\';
104 case '\a': copy_buff
[i
++] = 'a'; break;
105 case '\b': copy_buff
[i
++] = 'b'; break;
106 case '\f': copy_buff
[i
++] = 'f'; break;
107 case '\n': copy_buff
[i
++] = 'n'; break;
109 /* On some platforms, \n and \r are the same. */
110 case '\r': copy_buff
[i
++] = 'r'; break;
112 case '\t': copy_buff
[i
++] = 't'; break;
113 case '\v': copy_buff
[i
++] = 'v'; break;
114 case '\\': copy_buff
[i
++] = '\\'; break;
116 sprintf(copy_buff
+ i
, "%03o", c
);
121 /* If our temp buffer is full, dump it and keep going. */
122 if (i
> (sizeof(copy_buff
) - 8)) {
123 copy_buff
[i
++] = '\0';
124 fprintf(f
, "%s", copy_buff
);
128 copy_buff
[i
++] = '\0';
129 fprintf(f
, "%s", copy_buff
);
131 /* If we allocated a heap-based buffer, free it now. */
132 if (buff_heap
!= NULL
)
137 bsdtar_vwarnc(struct bsdtar
*bsdtar
, int code
, const char *fmt
, va_list ap
)
139 fprintf(stderr
, "%s: ", bsdtar
->progname
);
140 vfprintf(stderr
, fmt
, ap
);
142 fprintf(stderr
, ": %s", strerror(code
));
143 fprintf(stderr
, "\n");
147 bsdtar_warnc(struct bsdtar
*bsdtar
, int code
, const char *fmt
, ...)
152 bsdtar_vwarnc(bsdtar
, code
, fmt
, ap
);
157 bsdtar_errc(struct bsdtar
*bsdtar
, int eval
, int code
, const char *fmt
, ...)
162 bsdtar_vwarnc(bsdtar
, code
, fmt
, ap
);
168 yes(const char *fmt
, ...)
176 vfprintf(stderr
, fmt
, ap
);
178 fprintf(stderr
, " (y/N)? ");
181 l
= read(2, buff
, sizeof(buff
));
186 for (p
= buff
; *p
!= '\0'; p
++) {
187 if (isspace(0xff & (int)*p
))
203 * Read lines from file and do something with each one. If option_null
204 * is set, lines are terminated with zero bytes; otherwise, they're
205 * terminated with newlines.
207 * This uses a self-sizing buffer to handle arbitrarily-long lines.
208 * If the "process" function returns non-zero for any line, this
209 * function will return non-zero after attempting to process all
213 process_lines(struct bsdtar
*bsdtar
, const char *pathname
,
214 int (*process
)(struct bsdtar
*, const char *))
217 char *buff
, *buff_end
, *line_start
, *line_end
, *p
;
218 size_t buff_length
, bytes_read
, bytes_wanted
;
222 separator
= bsdtar
->option_null
? '\0' : '\n';
225 if (strcmp(pathname
, "-") == 0)
228 f
= fopen(pathname
, "r");
230 bsdtar_errc(bsdtar
, 1, errno
, "Couldn't open %s", pathname
);
232 buff
= malloc(buff_length
);
234 bsdtar_errc(bsdtar
, 1, ENOMEM
, "Can't read %s", pathname
);
235 line_start
= line_end
= buff_end
= buff
;
237 /* Get some more data into the buffer. */
238 bytes_wanted
= buff
+ buff_length
- buff_end
;
239 bytes_read
= fread(buff_end
, 1, bytes_wanted
, f
);
240 buff_end
+= bytes_read
;
241 /* Process all complete lines in the buffer. */
242 while (line_end
< buff_end
) {
243 if (*line_end
== separator
) {
245 if ((*process
)(bsdtar
, line_start
) != 0)
247 line_start
= line_end
+ 1;
248 line_end
= line_start
;
255 bsdtar_errc(bsdtar
, 1, errno
,
256 "Can't read %s", pathname
);
257 if (line_start
> buff
) {
258 /* Move a leftover fractional line to the beginning. */
259 memmove(buff
, line_start
, buff_end
- line_start
);
260 buff_end
-= line_start
- buff
;
261 line_end
-= line_start
- buff
;
264 /* Line is too big; enlarge the buffer. */
265 p
= realloc(buff
, buff_length
*= 2);
267 bsdtar_errc(bsdtar
, 1, ENOMEM
,
268 "Line too long in %s", pathname
);
269 buff_end
= p
+ (buff_end
- buff
);
270 line_end
= p
+ (line_end
- buff
);
271 line_start
= buff
= p
;
274 /* At end-of-file, handle the final line. */
275 if (line_end
> line_start
) {
277 if ((*process
)(bsdtar
, line_start
) != 0)
287 * The logic here for -C <dir> attempts to avoid
288 * chdir() as long as possible. For example:
289 * "-C /foo -C /bar file" needs chdir("/bar") but not chdir("/foo")
290 * "-C /foo -C bar file" needs chdir("/foo/bar")
291 * "-C /foo -C bar /file1" does not need chdir()
292 * "-C /foo -C bar /file1 file2" needs chdir("/foo/bar") before file2
294 * The only correct way to handle this is to record a "pending" chdir
295 * request and combine multiple requests intelligently until we
296 * need to process a non-absolute file. set_chdir() adds the new dir
297 * to the pending list; do_chdir() actually executes any pending chdir.
299 * This way, programs that build tar command lines don't have to worry
300 * about -C with non-existent directories; such requests will only
301 * fail if the directory must be accessed.
304 set_chdir(struct bsdtar
*bsdtar
, const char *newdir
)
306 if (newdir
[0] == '/') {
307 /* The -C /foo -C /bar case; dump first one. */
308 free(bsdtar
->pending_chdir
);
309 bsdtar
->pending_chdir
= NULL
;
311 if (bsdtar
->pending_chdir
== NULL
)
312 /* Easy case: no previously-saved dir. */
313 bsdtar
->pending_chdir
= strdup(newdir
);
315 /* The -C /foo -C bar case; concatenate */
316 char *old_pending
= bsdtar
->pending_chdir
;
317 size_t old_len
= strlen(old_pending
);
318 bsdtar
->pending_chdir
= malloc(old_len
+ strlen(newdir
) + 2);
319 if (old_pending
[old_len
- 1] == '/')
320 old_pending
[old_len
- 1] = '\0';
321 if (bsdtar
->pending_chdir
!= NULL
)
322 sprintf(bsdtar
->pending_chdir
, "%s/%s",
323 old_pending
, newdir
);
326 if (bsdtar
->pending_chdir
== NULL
)
327 bsdtar_errc(bsdtar
, 1, errno
, "No memory");
331 do_chdir(struct bsdtar
*bsdtar
)
333 if (bsdtar
->pending_chdir
== NULL
)
336 if (chdir(bsdtar
->pending_chdir
) != 0) {
337 bsdtar_errc(bsdtar
, 1, 0, "could not chdir to '%s'\n",
338 bsdtar
->pending_chdir
);
340 free(bsdtar
->pending_chdir
);
341 bsdtar
->pending_chdir
= NULL
;
345 * Handle --strip-components and any future path-rewriting options.
346 * Returns non-zero if the pathname should not be extracted.
348 * TODO: Support pax-style regex path rewrites.
351 edit_pathname(struct bsdtar
*bsdtar
, struct archive_entry
*entry
)
353 const char *name
= archive_entry_pathname(entry
);
355 /* Strip leading dir names as per --strip-components option. */
356 if (bsdtar
->strip_components
> 0) {
357 int r
= bsdtar
->strip_components
;
358 const char *p
= name
;
367 /* Path is too short, skip it. */
373 /* Strip redundant leading '/' characters. */
374 while (name
[0] == '/' && name
[1] == '/')
377 /* Strip leading '/' unless user has asked us not to. */
378 if (name
[0] == '/' && !bsdtar
->option_absolute_paths
) {
379 /* Generate a warning the first time this happens. */
380 if (!bsdtar
->warned_lead_slash
) {
381 bsdtar_warnc(bsdtar
, 0,
382 "Removing leading '/' from member names");
383 bsdtar
->warned_lead_slash
= 1;
386 /* Special case: Stripping leading '/' from "/" yields ".". */
391 /* Safely replace name in archive_entry. */
392 if (name
!= archive_entry_pathname(entry
)) {
393 char *q
= strdup(name
);
394 archive_entry_copy_pathname(entry
, q
);
401 * Like strcmp(), but try to be a little more aware of the fact that
402 * we're comparing two paths. Right now, it just handles leading
403 * "./" and trailing '/' specially, so that "a/b/" == "./a/b"
405 * TODO: Make this better, so that "./a//b/./c/" == "a/b/c"
406 * TODO: After this works, push it down into libarchive.
407 * TODO: Publish the path normalization routines in libarchive so
408 * that bsdtar can normalize paths and use fast strcmp() instead
413 pathcmp(const char *a
, const char *b
)
415 /* Skip leading './' */
416 if (a
[0] == '.' && a
[1] == '/' && a
[2] != '\0')
418 if (b
[0] == '.' && b
[1] == '/' && b
[2] != '\0')
420 /* Find the first difference, or return (0) if none. */
428 * If one ends in '/' and the other one doesn't,
431 if (a
[0] == '/' && a
[1] == '\0' && b
[0] == '\0')
433 if (a
[0] == '\0' && b
[0] == '/' && b
[1] == '\0')
435 /* They're really different, return the correct sign. */
436 return (*(const unsigned char *)a
- *(const unsigned char *)b
);