2 * Copyright (c) 2003-2004 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 * 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.
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.
27 #include "bsdtar_platform.h"
28 __FBSDID("$FreeBSD: src/usr.bin/tar/util.c,v 1.13 2005/04/17 19:46:50 kientzle Exp $");
31 #include <sys/types.h> /* Linux doesn't define mode_t, etc. in sys/stat.h. */
32 #include <archive_entry.h>
42 static void bsdtar_vwarnc(struct bsdtar
*, int code
,
43 const char *fmt
, va_list ap
);
46 * Print a string, taking care with any non-printable characters.
50 safe_fprintf(FILE *f
, const char *fmt
, ...)
62 /* Use a stack-allocated buffer if we can, for speed and safety. */
64 buff_length
= sizeof(buff_stack
);
68 length
= vsnprintf(buff
, buff_length
, fmt
, ap
);
70 /* If the result is too large, allocate a buffer on the heap. */
71 if (length
>= buff_length
) {
72 buff_length
= length
+1;
73 buff_heap
= malloc(buff_length
);
74 /* Failsafe: use the truncated string if malloc fails. */
75 if (buff_heap
!= NULL
) {
78 length
= vsnprintf(buff
, buff_length
, fmt
, ap
);
83 /* Write data, expanding unprintable characters. */
87 unsigned char c
= *p
++;
89 if (isprint(c
) && c
!= '\\')
92 copy_buff
[i
++] = '\\';
94 case '\a': copy_buff
[i
++] = 'a'; break;
95 case '\b': copy_buff
[i
++] = 'b'; break;
96 case '\f': copy_buff
[i
++] = 'f'; break;
97 case '\n': copy_buff
[i
++] = 'n'; break;
99 /* On some platforms, \n and \r are the same. */
100 case '\r': copy_buff
[i
++] = 'r'; break;
102 case '\t': copy_buff
[i
++] = 't'; break;
103 case '\v': copy_buff
[i
++] = 'v'; break;
104 case '\\': copy_buff
[i
++] = '\\'; break;
106 sprintf(copy_buff
+ i
, "%03o", c
);
111 /* If our temp buffer is full, dump it and keep going. */
112 if (i
> (sizeof(copy_buff
) - 8)) {
113 copy_buff
[i
++] = '\0';
114 fprintf(f
, "%s", copy_buff
);
118 copy_buff
[i
++] = '\0';
119 fprintf(f
, "%s", copy_buff
);
121 /* If we allocated a heap-based buffer, free it now. */
122 if (buff_heap
!= NULL
)
127 bsdtar_vwarnc(struct bsdtar
*bsdtar
, int code
, const char *fmt
, va_list ap
)
129 fprintf(stderr
, "%s: ", bsdtar
->progname
);
130 vfprintf(stderr
, fmt
, ap
);
132 fprintf(stderr
, ": %s", strerror(code
));
133 fprintf(stderr
, "\n");
137 bsdtar_warnc(struct bsdtar
*bsdtar
, int code
, const char *fmt
, ...)
142 bsdtar_vwarnc(bsdtar
, code
, fmt
, ap
);
147 bsdtar_errc(struct bsdtar
*bsdtar
, int eval
, int code
, const char *fmt
, ...)
152 bsdtar_vwarnc(bsdtar
, code
, fmt
, ap
);
158 yes(const char *fmt
, ...)
166 vfprintf(stderr
, fmt
, ap
);
168 fprintf(stderr
, " (y/N)? ");
171 l
= read(2, buff
, sizeof(buff
));
176 for (p
= buff
; *p
!= '\0'; p
++) {
177 if (isspace(0xff & (int)*p
))
193 bsdtar_strmode(struct archive_entry
*entry
, char *bp
)
195 static const char *perms
= "?rwxrwxrwx ";
196 static const mode_t permbits
[] =
197 { S_IRUSR
, S_IWUSR
, S_IXUSR
, S_IRGRP
, S_IWGRP
, S_IXGRP
,
198 S_IROTH
, S_IWOTH
, S_IXOTH
};
202 /* Fill in a default string, then selectively override. */
205 mode
= archive_entry_mode(entry
);
206 switch (mode
& S_IFMT
) {
207 case S_IFREG
: bp
[0] = '-'; break;
208 case S_IFBLK
: bp
[0] = 'b'; break;
209 case S_IFCHR
: bp
[0] = 'c'; break;
210 case S_IFDIR
: bp
[0] = 'd'; break;
211 case S_IFLNK
: bp
[0] = 'l'; break;
212 case S_IFSOCK
: bp
[0] = 's'; break;
214 case S_IFIFO
: bp
[0] = 'p'; break;
217 case S_IFWHT
: bp
[0] = 'w'; break;
221 for (i
= 0; i
< 9; i
++)
222 if (!(mode
& permbits
[i
]))
225 if (mode
& S_ISUID
) {
226 if (mode
& S_IXUSR
) bp
[3] = 's';
229 if (mode
& S_ISGID
) {
230 if (mode
& S_IXGRP
) bp
[6] = 's';
233 if (mode
& S_ISVTX
) {
234 if (mode
& S_IXOTH
) bp
[9] = 't';
237 if (archive_entry_acl_count(entry
, ARCHIVE_ENTRY_ACL_TYPE_ACCESS
))
243 * Read lines from file and do something with each one. If option_null
244 * is set, lines are terminated with zero bytes; otherwise, they're
245 * terminated with newlines.
247 * This uses a self-sizing buffer to handle arbitrarily-long lines.
248 * If the "process" function returns non-zero for any line, this
249 * function will return non-zero after attempting to process all
253 process_lines(struct bsdtar
*bsdtar
, const char *pathname
,
254 int (*process
)(struct bsdtar
*, const char *))
257 char *buff
, *buff_end
, *line_start
, *line_end
, *p
;
258 size_t buff_length
, bytes_read
, bytes_wanted
;
262 separator
= bsdtar
->option_null
? '\0' : '\n';
265 if (strcmp(pathname
, "-") == 0)
268 f
= fopen(pathname
, "r");
270 bsdtar_errc(bsdtar
, 1, errno
, "Couldn't open %s", pathname
);
272 buff
= malloc(buff_length
);
274 bsdtar_errc(bsdtar
, 1, ENOMEM
, "Can't read %s", pathname
);
275 line_start
= line_end
= buff_end
= buff
;
277 /* Get some more data into the buffer. */
278 bytes_wanted
= buff
+ buff_length
- buff_end
;
279 bytes_read
= fread(buff_end
, 1, bytes_wanted
, f
);
280 buff_end
+= bytes_read
;
281 /* Process all complete lines in the buffer. */
282 while (line_end
< buff_end
) {
283 if (*line_end
== separator
) {
285 if ((*process
)(bsdtar
, line_start
) != 0)
287 line_start
= line_end
+ 1;
288 line_end
= line_start
;
295 bsdtar_errc(bsdtar
, 1, errno
,
296 "Can't read %s", pathname
);
297 if (line_start
> buff
) {
298 /* Move a leftover fractional line to the beginning. */
299 memmove(buff
, line_start
, buff_end
- line_start
);
300 buff_end
-= line_start
- buff
;
301 line_end
-= line_start
- buff
;
304 /* Line is too big; enlarge the buffer. */
305 p
= realloc(buff
, buff_length
*= 2);
307 bsdtar_errc(bsdtar
, 1, ENOMEM
,
308 "Line too long in %s", pathname
);
309 buff_end
= p
+ (buff_end
- buff
);
310 line_end
= p
+ (line_end
- buff
);
311 line_start
= buff
= p
;
314 /* At end-of-file, handle the final line. */
315 if (line_end
> line_start
) {
317 if ((*process
)(bsdtar
, line_start
) != 0)
327 * The logic here for -C <dir> attempts to avoid
328 * chdir() as long as possible. For example:
329 * "-C /foo -C /bar file" needs chdir("/bar") but not chdir("/foo")
330 * "-C /foo -C bar file" needs chdir("/foo/bar")
331 * "-C /foo -C bar /file1" does not need chdir()
332 * "-C /foo -C bar /file1 file2" needs chdir("/foo/bar") before file2
334 * The only correct way to handle this is to record a "pending" chdir
335 * request and combine multiple requests intelligently until we
336 * need to process a non-absolute file. set_chdir() adds the new dir
337 * to the pending list; do_chdir() actually executes any pending chdir.
339 * This way, programs that build tar command lines don't have to worry
340 * about -C with non-existent directories; such requests will only
341 * fail if the directory must be accessed.
344 set_chdir(struct bsdtar
*bsdtar
, const char *newdir
)
346 if (newdir
[0] == '/') {
347 /* The -C /foo -C /bar case; dump first one. */
348 free(bsdtar
->pending_chdir
);
349 bsdtar
->pending_chdir
= NULL
;
351 if (bsdtar
->pending_chdir
== NULL
)
352 /* Easy case: no previously-saved dir. */
353 bsdtar
->pending_chdir
= strdup(newdir
);
355 /* The -C /foo -C bar case; concatenate */
356 char *old_pending
= bsdtar
->pending_chdir
;
357 size_t old_len
= strlen(old_pending
);
358 bsdtar
->pending_chdir
= malloc(old_len
+ strlen(newdir
) + 2);
359 if (old_pending
[old_len
- 1] == '/')
360 old_pending
[old_len
- 1] = '\0';
361 if (bsdtar
->pending_chdir
!= NULL
)
362 sprintf(bsdtar
->pending_chdir
, "%s/%s",
363 old_pending
, newdir
);
366 if (bsdtar
->pending_chdir
== NULL
)
367 bsdtar_errc(bsdtar
, 1, errno
, "No memory");
371 do_chdir(struct bsdtar
*bsdtar
)
373 if (bsdtar
->pending_chdir
== NULL
)
376 if (chdir(bsdtar
->pending_chdir
) != 0) {
377 bsdtar_errc(bsdtar
, 1, 0, "could not chdir to '%s'\n",
378 bsdtar
->pending_chdir
);
380 free(bsdtar
->pending_chdir
);
381 bsdtar
->pending_chdir
= NULL
;
385 * Handle --strip-components and any future path-rewriting options.
386 * Returns non-zero if the pathname should not be extracted.
389 edit_pathname(struct bsdtar
*bsdtar
, struct archive_entry
*entry
)
391 const char *name
= archive_entry_pathname(entry
);
393 /* Strip leading dir names as per --strip-components option. */
394 if (bsdtar
->strip_components
> 0) {
395 int r
= bsdtar
->strip_components
;
396 const char *p
= name
;
405 /* Path is too short, skip it. */
411 /* Strip redundant "./" from start of filename. */
412 if (name
[0] == '.' && name
[1] == '/' && name
[2] != '\0')
415 /* Strip redundant leading '/' characters. */
416 while (name
[0] == '/' && name
[1] == '/')
419 /* Strip leading '/' unless user has asked us not to. */
420 if (name
[0] == '/' && !bsdtar
->option_absolute_paths
) {
421 /* Generate a warning the first time this happens. */
422 if (!bsdtar
->warned_lead_slash
) {
423 bsdtar_warnc(bsdtar
, 0,
424 "Removing leading '/' from member names");
425 bsdtar
->warned_lead_slash
= 1;
428 /* Special case: Stripping leading '/' from "/" yields ".". */
433 /* Safely replace name in archive_entry. */
434 if (name
!= archive_entry_pathname(entry
)) {
435 char *q
= strdup(name
);
436 archive_entry_copy_pathname(entry
, q
);