160b4ad09SPeter Avalos /*- 260b4ad09SPeter Avalos * Copyright (c) 2003-2007 Tim Kientzle 360b4ad09SPeter Avalos * All rights reserved. 460b4ad09SPeter Avalos * 560b4ad09SPeter Avalos * Redistribution and use in source and binary forms, with or without 660b4ad09SPeter Avalos * modification, are permitted provided that the following conditions 760b4ad09SPeter Avalos * are met: 860b4ad09SPeter Avalos * 1. Redistributions of source code must retain the above copyright 960b4ad09SPeter Avalos * notice, this list of conditions and the following disclaimer. 1060b4ad09SPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright 1160b4ad09SPeter Avalos * notice, this list of conditions and the following disclaimer in the 1260b4ad09SPeter Avalos * documentation and/or other materials provided with the distribution. 1360b4ad09SPeter Avalos * 1460b4ad09SPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 1560b4ad09SPeter Avalos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1660b4ad09SPeter Avalos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1760b4ad09SPeter Avalos * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 1860b4ad09SPeter Avalos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1960b4ad09SPeter Avalos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2060b4ad09SPeter Avalos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2160b4ad09SPeter Avalos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2260b4ad09SPeter Avalos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2360b4ad09SPeter Avalos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2460b4ad09SPeter Avalos */ 2560b4ad09SPeter Avalos 2660b4ad09SPeter Avalos #include "bsdtar_platform.h" 278029ab02SPeter Avalos __FBSDID("$FreeBSD: src/usr.bin/tar/util.c,v 1.23 2008/12/15 06:00:25 kientzle Exp $"); 2860b4ad09SPeter Avalos 2960b4ad09SPeter Avalos #ifdef HAVE_SYS_STAT_H 3060b4ad09SPeter Avalos #include <sys/stat.h> 3160b4ad09SPeter Avalos #endif 3260b4ad09SPeter Avalos #ifdef HAVE_SYS_TYPES_H 3360b4ad09SPeter Avalos #include <sys/types.h> /* Linux doesn't define mode_t, etc. in sys/stat.h. */ 3460b4ad09SPeter Avalos #endif 3560b4ad09SPeter Avalos #include <ctype.h> 3660b4ad09SPeter Avalos #ifdef HAVE_ERRNO_H 3760b4ad09SPeter Avalos #include <errno.h> 3860b4ad09SPeter Avalos #endif 399c82a63eSPeter Avalos #ifdef HAVE_IO_H 409c82a63eSPeter Avalos #include <io.h> 419c82a63eSPeter Avalos #endif 4260b4ad09SPeter Avalos #ifdef HAVE_STDARG_H 4360b4ad09SPeter Avalos #include <stdarg.h> 4460b4ad09SPeter Avalos #endif 459c82a63eSPeter Avalos #ifdef HAVE_STDINT_H 469c82a63eSPeter Avalos #include <stdint.h> 479c82a63eSPeter Avalos #endif 4860b4ad09SPeter Avalos #include <stdio.h> 4960b4ad09SPeter Avalos #ifdef HAVE_STDLIB_H 5060b4ad09SPeter Avalos #include <stdlib.h> 5160b4ad09SPeter Avalos #endif 5260b4ad09SPeter Avalos #ifdef HAVE_STRING_H 5360b4ad09SPeter Avalos #include <string.h> 5460b4ad09SPeter Avalos #endif 558029ab02SPeter Avalos #ifdef HAVE_WCTYPE_H 568029ab02SPeter Avalos #include <wctype.h> 578029ab02SPeter Avalos #else 588029ab02SPeter Avalos /* If we don't have wctype, we need to hack up some version of iswprint(). */ 598029ab02SPeter Avalos #define iswprint isprint 608029ab02SPeter Avalos #endif 6160b4ad09SPeter Avalos 6260b4ad09SPeter Avalos #include "bsdtar.h" 639c82a63eSPeter Avalos #include "err.h" 6460b4ad09SPeter Avalos 658029ab02SPeter Avalos static size_t bsdtar_expand_char(char *, size_t, char); 668029ab02SPeter Avalos static const char *strip_components(const char *path, int elements); 678029ab02SPeter Avalos 689c82a63eSPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__) 699c82a63eSPeter Avalos #define read _read 709c82a63eSPeter Avalos #endif 719c82a63eSPeter Avalos 728029ab02SPeter Avalos /* TODO: Hack up a version of mbtowc for platforms with no wide 738029ab02SPeter Avalos * character support at all. I think the following might suffice, 748029ab02SPeter Avalos * but it needs careful testing. 758029ab02SPeter Avalos * #if !HAVE_MBTOWC 768029ab02SPeter Avalos * #define mbtowc(wcp, p, n) ((*wcp = *p), 1) 778029ab02SPeter Avalos * #endif 788029ab02SPeter Avalos */ 7960b4ad09SPeter Avalos 8060b4ad09SPeter Avalos /* 8160b4ad09SPeter Avalos * Print a string, taking care with any non-printable characters. 828029ab02SPeter Avalos * 838029ab02SPeter Avalos * Note that we use a stack-allocated buffer to receive the formatted 848029ab02SPeter Avalos * string if we can. This is partly performance (avoiding a call to 858029ab02SPeter Avalos * malloc()), partly out of expedience (we have to call vsnprintf() 868029ab02SPeter Avalos * before malloc() anyway to find out how big a buffer we need; we may 878029ab02SPeter Avalos * as well point that first call at a small local buffer in case it 888029ab02SPeter Avalos * works), but mostly for safety (so we can use this to print messages 898029ab02SPeter Avalos * about out-of-memory conditions). 9060b4ad09SPeter Avalos */ 9160b4ad09SPeter Avalos 9260b4ad09SPeter Avalos void 9360b4ad09SPeter Avalos safe_fprintf(FILE *f, const char *fmt, ...) 9460b4ad09SPeter Avalos { 958029ab02SPeter Avalos char fmtbuff_stack[256]; /* Place to format the printf() string. */ 968029ab02SPeter Avalos char outbuff[256]; /* Buffer for outgoing characters. */ 978029ab02SPeter Avalos char *fmtbuff_heap; /* If fmtbuff_stack is too small, we use malloc */ 988029ab02SPeter Avalos char *fmtbuff; /* Pointer to fmtbuff_stack or fmtbuff_heap. */ 998029ab02SPeter Avalos int fmtbuff_length; 1009c82a63eSPeter Avalos int length, n; 10160b4ad09SPeter Avalos va_list ap; 1028029ab02SPeter Avalos const char *p; 10360b4ad09SPeter Avalos unsigned i; 1048029ab02SPeter Avalos wchar_t wc; 1058029ab02SPeter Avalos char try_wc; 10660b4ad09SPeter Avalos 10760b4ad09SPeter Avalos /* Use a stack-allocated buffer if we can, for speed and safety. */ 1088029ab02SPeter Avalos fmtbuff_heap = NULL; 1098029ab02SPeter Avalos fmtbuff_length = sizeof(fmtbuff_stack); 1108029ab02SPeter Avalos fmtbuff = fmtbuff_stack; 11160b4ad09SPeter Avalos 1128029ab02SPeter Avalos /* Try formatting into the stack buffer. */ 11360b4ad09SPeter Avalos va_start(ap, fmt); 1148029ab02SPeter Avalos length = vsnprintf(fmtbuff, fmtbuff_length, fmt, ap); 11560b4ad09SPeter Avalos va_end(ap); 1168029ab02SPeter Avalos 1178029ab02SPeter Avalos /* If the result was too large, allocate a buffer on the heap. */ 1188029ab02SPeter Avalos if (length >= fmtbuff_length) { 1198029ab02SPeter Avalos fmtbuff_length = length+1; 1208029ab02SPeter Avalos fmtbuff_heap = malloc(fmtbuff_length); 1218029ab02SPeter Avalos 1228029ab02SPeter Avalos /* Reformat the result into the heap buffer if we can. */ 1238029ab02SPeter Avalos if (fmtbuff_heap != NULL) { 1248029ab02SPeter Avalos fmtbuff = fmtbuff_heap; 12560b4ad09SPeter Avalos va_start(ap, fmt); 1268029ab02SPeter Avalos length = vsnprintf(fmtbuff, fmtbuff_length, fmt, ap); 12760b4ad09SPeter Avalos va_end(ap); 1288029ab02SPeter Avalos } else { 1298029ab02SPeter Avalos /* Leave fmtbuff pointing to the truncated 1308029ab02SPeter Avalos * string in fmtbuff_stack. */ 1318029ab02SPeter Avalos length = sizeof(fmtbuff_stack) - 1; 13260b4ad09SPeter Avalos } 13360b4ad09SPeter Avalos } 13460b4ad09SPeter Avalos 1358029ab02SPeter Avalos /* Note: mbrtowc() has a cleaner API, but mbtowc() seems a bit 1368029ab02SPeter Avalos * more portable, so we use that here instead. */ 1379c82a63eSPeter Avalos n = mbtowc(NULL, NULL, 1); /* Reset the shift state. */ 1388029ab02SPeter Avalos 13960b4ad09SPeter Avalos /* Write data, expanding unprintable characters. */ 1408029ab02SPeter Avalos p = fmtbuff; 14160b4ad09SPeter Avalos i = 0; 1428029ab02SPeter Avalos try_wc = 1; 14360b4ad09SPeter Avalos while (*p != '\0') { 14460b4ad09SPeter Avalos 1458029ab02SPeter Avalos /* Convert to wide char, test if the wide 1468029ab02SPeter Avalos * char is printable in the current locale. */ 1478029ab02SPeter Avalos if (try_wc && (n = mbtowc(&wc, p, length)) != -1) { 1488029ab02SPeter Avalos length -= n; 1498029ab02SPeter Avalos if (iswprint(wc) && wc != L'\\') { 1508029ab02SPeter Avalos /* Printable, copy the bytes through. */ 1518029ab02SPeter Avalos while (n-- > 0) 1528029ab02SPeter Avalos outbuff[i++] = *p++; 1538029ab02SPeter Avalos } else { 1548029ab02SPeter Avalos /* Not printable, format the bytes. */ 1558029ab02SPeter Avalos while (n-- > 0) 1569c82a63eSPeter Avalos i += (unsigned)bsdtar_expand_char( 1578029ab02SPeter Avalos outbuff, i, *p++); 1588029ab02SPeter Avalos } 1598029ab02SPeter Avalos } else { 1608029ab02SPeter Avalos /* After any conversion failure, don't bother 1618029ab02SPeter Avalos * trying to convert the rest. */ 1629c82a63eSPeter Avalos i += (unsigned)bsdtar_expand_char(outbuff, i, *p++); 1638029ab02SPeter Avalos try_wc = 0; 1648029ab02SPeter Avalos } 1658029ab02SPeter Avalos 1668029ab02SPeter Avalos /* If our output buffer is full, dump it and keep going. */ 1678029ab02SPeter Avalos if (i > (sizeof(outbuff) - 20)) { 1689c82a63eSPeter Avalos outbuff[i] = '\0'; 1698029ab02SPeter Avalos fprintf(f, "%s", outbuff); 1708029ab02SPeter Avalos i = 0; 1718029ab02SPeter Avalos } 1728029ab02SPeter Avalos } 1739c82a63eSPeter Avalos outbuff[i] = '\0'; 1748029ab02SPeter Avalos fprintf(f, "%s", outbuff); 1758029ab02SPeter Avalos 1768029ab02SPeter Avalos /* If we allocated a heap-based formatting buffer, free it now. */ 1778029ab02SPeter Avalos if (fmtbuff_heap != NULL) 1788029ab02SPeter Avalos free(fmtbuff_heap); 1798029ab02SPeter Avalos } 1808029ab02SPeter Avalos 1818029ab02SPeter Avalos /* 1828029ab02SPeter Avalos * Render an arbitrary sequence of bytes into printable ASCII characters. 1838029ab02SPeter Avalos */ 1848029ab02SPeter Avalos static size_t 1858029ab02SPeter Avalos bsdtar_expand_char(char *buff, size_t offset, char c) 1868029ab02SPeter Avalos { 1878029ab02SPeter Avalos size_t i = offset; 1888029ab02SPeter Avalos 1898029ab02SPeter Avalos if (isprint((unsigned char)c) && c != '\\') 1908029ab02SPeter Avalos buff[i++] = c; 19160b4ad09SPeter Avalos else { 1928029ab02SPeter Avalos buff[i++] = '\\'; 19360b4ad09SPeter Avalos switch (c) { 1948029ab02SPeter Avalos case '\a': buff[i++] = 'a'; break; 1958029ab02SPeter Avalos case '\b': buff[i++] = 'b'; break; 1968029ab02SPeter Avalos case '\f': buff[i++] = 'f'; break; 1978029ab02SPeter Avalos case '\n': buff[i++] = 'n'; break; 19860b4ad09SPeter Avalos #if '\r' != '\n' 19960b4ad09SPeter Avalos /* On some platforms, \n and \r are the same. */ 2008029ab02SPeter Avalos case '\r': buff[i++] = 'r'; break; 20160b4ad09SPeter Avalos #endif 2028029ab02SPeter Avalos case '\t': buff[i++] = 't'; break; 2038029ab02SPeter Avalos case '\v': buff[i++] = 'v'; break; 2048029ab02SPeter Avalos case '\\': buff[i++] = '\\'; break; 20560b4ad09SPeter Avalos default: 2068029ab02SPeter Avalos sprintf(buff + i, "%03o", 0xFF & (int)c); 20760b4ad09SPeter Avalos i += 3; 20860b4ad09SPeter Avalos } 20960b4ad09SPeter Avalos } 21060b4ad09SPeter Avalos 2118029ab02SPeter Avalos return (i - offset); 21260b4ad09SPeter Avalos } 21360b4ad09SPeter Avalos 21460b4ad09SPeter Avalos int 21560b4ad09SPeter Avalos yes(const char *fmt, ...) 21660b4ad09SPeter Avalos { 21760b4ad09SPeter Avalos char buff[32]; 21860b4ad09SPeter Avalos char *p; 21960b4ad09SPeter Avalos ssize_t l; 22060b4ad09SPeter Avalos 22160b4ad09SPeter Avalos va_list ap; 22260b4ad09SPeter Avalos va_start(ap, fmt); 22360b4ad09SPeter Avalos vfprintf(stderr, fmt, ap); 22460b4ad09SPeter Avalos va_end(ap); 22560b4ad09SPeter Avalos fprintf(stderr, " (y/N)? "); 22660b4ad09SPeter Avalos fflush(stderr); 22760b4ad09SPeter Avalos 22860b4ad09SPeter Avalos l = read(2, buff, sizeof(buff) - 1); 229*c09f92d2SPeter Avalos if (l < 0) { 230*c09f92d2SPeter Avalos fprintf(stderr, "Keyboard read failed\n"); 231*c09f92d2SPeter Avalos exit(1); 232*c09f92d2SPeter Avalos } 233*c09f92d2SPeter Avalos if (l == 0) 23460b4ad09SPeter Avalos return (0); 23560b4ad09SPeter Avalos buff[l] = 0; 23660b4ad09SPeter Avalos 23760b4ad09SPeter Avalos for (p = buff; *p != '\0'; p++) { 2388029ab02SPeter Avalos if (isspace((unsigned char)*p)) 23960b4ad09SPeter Avalos continue; 24060b4ad09SPeter Avalos switch(*p) { 24160b4ad09SPeter Avalos case 'y': case 'Y': 24260b4ad09SPeter Avalos return (1); 24360b4ad09SPeter Avalos case 'n': case 'N': 24460b4ad09SPeter Avalos return (0); 24560b4ad09SPeter Avalos default: 24660b4ad09SPeter Avalos return (0); 24760b4ad09SPeter Avalos } 24860b4ad09SPeter Avalos } 24960b4ad09SPeter Avalos 25060b4ad09SPeter Avalos return (0); 25160b4ad09SPeter Avalos } 25260b4ad09SPeter Avalos 25360b4ad09SPeter Avalos /*- 25460b4ad09SPeter Avalos * The logic here for -C <dir> attempts to avoid 25560b4ad09SPeter Avalos * chdir() as long as possible. For example: 25660b4ad09SPeter Avalos * "-C /foo -C /bar file" needs chdir("/bar") but not chdir("/foo") 25760b4ad09SPeter Avalos * "-C /foo -C bar file" needs chdir("/foo/bar") 25860b4ad09SPeter Avalos * "-C /foo -C bar /file1" does not need chdir() 25960b4ad09SPeter Avalos * "-C /foo -C bar /file1 file2" needs chdir("/foo/bar") before file2 26060b4ad09SPeter Avalos * 26160b4ad09SPeter Avalos * The only correct way to handle this is to record a "pending" chdir 26260b4ad09SPeter Avalos * request and combine multiple requests intelligently until we 26360b4ad09SPeter Avalos * need to process a non-absolute file. set_chdir() adds the new dir 26460b4ad09SPeter Avalos * to the pending list; do_chdir() actually executes any pending chdir. 26560b4ad09SPeter Avalos * 26660b4ad09SPeter Avalos * This way, programs that build tar command lines don't have to worry 26760b4ad09SPeter Avalos * about -C with non-existent directories; such requests will only 26860b4ad09SPeter Avalos * fail if the directory must be accessed. 2699c82a63eSPeter Avalos * 27060b4ad09SPeter Avalos */ 27160b4ad09SPeter Avalos void 27260b4ad09SPeter Avalos set_chdir(struct bsdtar *bsdtar, const char *newdir) 27360b4ad09SPeter Avalos { 274*c09f92d2SPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__) 275*c09f92d2SPeter Avalos if (newdir[0] == '/' || newdir[0] == '\\' || 276*c09f92d2SPeter Avalos /* Detect this type, for example, "C:\" or "C:/" */ 277*c09f92d2SPeter Avalos (((newdir[0] >= 'a' && newdir[0] <= 'z') || 278*c09f92d2SPeter Avalos (newdir[0] >= 'A' && newdir[0] <= 'Z')) && 279*c09f92d2SPeter Avalos newdir[1] == ':' && (newdir[2] == '/' || newdir[2] == '\\'))) { 280*c09f92d2SPeter Avalos #else 28160b4ad09SPeter Avalos if (newdir[0] == '/') { 282*c09f92d2SPeter Avalos #endif 28360b4ad09SPeter Avalos /* The -C /foo -C /bar case; dump first one. */ 28460b4ad09SPeter Avalos free(bsdtar->pending_chdir); 28560b4ad09SPeter Avalos bsdtar->pending_chdir = NULL; 28660b4ad09SPeter Avalos } 28760b4ad09SPeter Avalos if (bsdtar->pending_chdir == NULL) 28860b4ad09SPeter Avalos /* Easy case: no previously-saved dir. */ 28960b4ad09SPeter Avalos bsdtar->pending_chdir = strdup(newdir); 29060b4ad09SPeter Avalos else { 29160b4ad09SPeter Avalos /* The -C /foo -C bar case; concatenate */ 29260b4ad09SPeter Avalos char *old_pending = bsdtar->pending_chdir; 29360b4ad09SPeter Avalos size_t old_len = strlen(old_pending); 29460b4ad09SPeter Avalos bsdtar->pending_chdir = malloc(old_len + strlen(newdir) + 2); 29560b4ad09SPeter Avalos if (old_pending[old_len - 1] == '/') 29660b4ad09SPeter Avalos old_pending[old_len - 1] = '\0'; 29760b4ad09SPeter Avalos if (bsdtar->pending_chdir != NULL) 29860b4ad09SPeter Avalos sprintf(bsdtar->pending_chdir, "%s/%s", 29960b4ad09SPeter Avalos old_pending, newdir); 30060b4ad09SPeter Avalos free(old_pending); 30160b4ad09SPeter Avalos } 30260b4ad09SPeter Avalos if (bsdtar->pending_chdir == NULL) 3039c82a63eSPeter Avalos lafe_errc(1, errno, "No memory"); 30460b4ad09SPeter Avalos } 30560b4ad09SPeter Avalos 30660b4ad09SPeter Avalos void 30760b4ad09SPeter Avalos do_chdir(struct bsdtar *bsdtar) 30860b4ad09SPeter Avalos { 30960b4ad09SPeter Avalos if (bsdtar->pending_chdir == NULL) 31060b4ad09SPeter Avalos return; 31160b4ad09SPeter Avalos 31260b4ad09SPeter Avalos if (chdir(bsdtar->pending_chdir) != 0) { 3139c82a63eSPeter Avalos lafe_errc(1, 0, "could not chdir to '%s'\n", 31460b4ad09SPeter Avalos bsdtar->pending_chdir); 31560b4ad09SPeter Avalos } 31660b4ad09SPeter Avalos free(bsdtar->pending_chdir); 31760b4ad09SPeter Avalos bsdtar->pending_chdir = NULL; 31860b4ad09SPeter Avalos } 31960b4ad09SPeter Avalos 3209c82a63eSPeter Avalos static const char * 3219c82a63eSPeter Avalos strip_components(const char *p, int elements) 3228029ab02SPeter Avalos { 3239c82a63eSPeter Avalos /* Skip as many elements as necessary. */ 3248029ab02SPeter Avalos while (elements > 0) { 3258029ab02SPeter Avalos switch (*p++) { 3268029ab02SPeter Avalos case '/': 3279c82a63eSPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__) 3289c82a63eSPeter Avalos case '\\': /* Support \ path sep on Windows ONLY. */ 3299c82a63eSPeter Avalos #endif 3308029ab02SPeter Avalos elements--; 3318029ab02SPeter Avalos break; 3328029ab02SPeter Avalos case '\0': 3338029ab02SPeter Avalos /* Path is too short, skip it. */ 3348029ab02SPeter Avalos return (NULL); 3358029ab02SPeter Avalos } 3368029ab02SPeter Avalos } 3378029ab02SPeter Avalos 3389c82a63eSPeter Avalos /* Skip any / characters. This handles short paths that have 3399c82a63eSPeter Avalos * additional / termination. This also handles the case where 3409c82a63eSPeter Avalos * the logic above stops in the middle of a duplicate // 3419c82a63eSPeter Avalos * sequence (which would otherwise get converted to an 3429c82a63eSPeter Avalos * absolute path). */ 3439c82a63eSPeter Avalos for (;;) { 3449c82a63eSPeter Avalos switch (*p) { 3459c82a63eSPeter Avalos case '/': 3469c82a63eSPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__) 3479c82a63eSPeter Avalos case '\\': /* Support \ path sep on Windows ONLY. */ 3489c82a63eSPeter Avalos #endif 3499c82a63eSPeter Avalos ++p; 3509c82a63eSPeter Avalos break; 3519c82a63eSPeter Avalos case '\0': 3528029ab02SPeter Avalos return (NULL); 3539c82a63eSPeter Avalos default: 3549c82a63eSPeter Avalos return (p); 3559c82a63eSPeter Avalos } 3569c82a63eSPeter Avalos } 3578029ab02SPeter Avalos } 3588029ab02SPeter Avalos 35960b4ad09SPeter Avalos /* 36060b4ad09SPeter Avalos * Handle --strip-components and any future path-rewriting options. 36160b4ad09SPeter Avalos * Returns non-zero if the pathname should not be extracted. 36260b4ad09SPeter Avalos * 36360b4ad09SPeter Avalos * TODO: Support pax-style regex path rewrites. 36460b4ad09SPeter Avalos */ 36560b4ad09SPeter Avalos int 36660b4ad09SPeter Avalos edit_pathname(struct bsdtar *bsdtar, struct archive_entry *entry) 36760b4ad09SPeter Avalos { 36860b4ad09SPeter Avalos const char *name = archive_entry_pathname(entry); 36960b4ad09SPeter Avalos #if HAVE_REGEX_H 37060b4ad09SPeter Avalos char *subst_name; 37160b4ad09SPeter Avalos int r; 37260b4ad09SPeter Avalos 373*c09f92d2SPeter Avalos r = apply_substitution(bsdtar, name, &subst_name, 0, 0); 37460b4ad09SPeter Avalos if (r == -1) { 3759c82a63eSPeter Avalos lafe_warnc(0, "Invalid substitution, skipping entry"); 37660b4ad09SPeter Avalos return 1; 37760b4ad09SPeter Avalos } 37860b4ad09SPeter Avalos if (r == 1) { 37960b4ad09SPeter Avalos archive_entry_copy_pathname(entry, subst_name); 38060b4ad09SPeter Avalos if (*subst_name == '\0') { 38160b4ad09SPeter Avalos free(subst_name); 38260b4ad09SPeter Avalos return -1; 38360b4ad09SPeter Avalos } else 38460b4ad09SPeter Avalos free(subst_name); 38560b4ad09SPeter Avalos name = archive_entry_pathname(entry); 38660b4ad09SPeter Avalos } 38760b4ad09SPeter Avalos 38860b4ad09SPeter Avalos if (archive_entry_hardlink(entry)) { 389*c09f92d2SPeter Avalos r = apply_substitution(bsdtar, archive_entry_hardlink(entry), &subst_name, 0, 1); 39060b4ad09SPeter Avalos if (r == -1) { 3919c82a63eSPeter Avalos lafe_warnc(0, "Invalid substitution, skipping entry"); 39260b4ad09SPeter Avalos return 1; 39360b4ad09SPeter Avalos } 39460b4ad09SPeter Avalos if (r == 1) { 39560b4ad09SPeter Avalos archive_entry_copy_hardlink(entry, subst_name); 39660b4ad09SPeter Avalos free(subst_name); 39760b4ad09SPeter Avalos } 39860b4ad09SPeter Avalos } 39960b4ad09SPeter Avalos if (archive_entry_symlink(entry) != NULL) { 400*c09f92d2SPeter Avalos r = apply_substitution(bsdtar, archive_entry_symlink(entry), &subst_name, 1, 0); 40160b4ad09SPeter Avalos if (r == -1) { 4029c82a63eSPeter Avalos lafe_warnc(0, "Invalid substitution, skipping entry"); 40360b4ad09SPeter Avalos return 1; 40460b4ad09SPeter Avalos } 40560b4ad09SPeter Avalos if (r == 1) { 40660b4ad09SPeter Avalos archive_entry_copy_symlink(entry, subst_name); 40760b4ad09SPeter Avalos free(subst_name); 40860b4ad09SPeter Avalos } 40960b4ad09SPeter Avalos } 41060b4ad09SPeter Avalos #endif 41160b4ad09SPeter Avalos 41260b4ad09SPeter Avalos /* Strip leading dir names as per --strip-components option. */ 4138029ab02SPeter Avalos if (bsdtar->strip_components > 0) { 4148029ab02SPeter Avalos const char *linkname = archive_entry_hardlink(entry); 41560b4ad09SPeter Avalos 4168029ab02SPeter Avalos name = strip_components(name, bsdtar->strip_components); 4178029ab02SPeter Avalos if (name == NULL) 4188029ab02SPeter Avalos return (1); 4198029ab02SPeter Avalos 4208029ab02SPeter Avalos if (linkname != NULL) { 4218029ab02SPeter Avalos linkname = strip_components(linkname, 4228029ab02SPeter Avalos bsdtar->strip_components); 4238029ab02SPeter Avalos if (linkname == NULL) 4248029ab02SPeter Avalos return (1); 4258029ab02SPeter Avalos archive_entry_copy_hardlink(entry, linkname); 4268029ab02SPeter Avalos } 4278029ab02SPeter Avalos } 4288029ab02SPeter Avalos 4298029ab02SPeter Avalos /* By default, don't write or restore absolute pathnames. */ 4308029ab02SPeter Avalos if (!bsdtar->option_absolute_paths) { 4318029ab02SPeter Avalos const char *rp, *p = name; 4328029ab02SPeter Avalos int slashonly = 1; 4338029ab02SPeter Avalos 4348029ab02SPeter Avalos /* Remove leading "//./" or "//?/" or "//?/UNC/" 4358029ab02SPeter Avalos * (absolute path prefixes used by Windows API) */ 4368029ab02SPeter Avalos if ((p[0] == '/' || p[0] == '\\') && 4378029ab02SPeter Avalos (p[1] == '/' || p[1] == '\\') && 4388029ab02SPeter Avalos (p[2] == '.' || p[2] == '?') && 4398029ab02SPeter Avalos (p[3] == '/' || p[3] == '\\')) 4408029ab02SPeter Avalos { 4418029ab02SPeter Avalos if (p[2] == '?' && 4428029ab02SPeter Avalos (p[4] == 'U' || p[4] == 'u') && 4438029ab02SPeter Avalos (p[5] == 'N' || p[5] == 'n') && 4448029ab02SPeter Avalos (p[6] == 'C' || p[6] == 'c') && 4458029ab02SPeter Avalos (p[7] == '/' || p[7] == '\\')) 4468029ab02SPeter Avalos p += 8; 4478029ab02SPeter Avalos else 4488029ab02SPeter Avalos p += 4; 4498029ab02SPeter Avalos slashonly = 0; 4508029ab02SPeter Avalos } 4518029ab02SPeter Avalos do { 4528029ab02SPeter Avalos rp = p; 4538029ab02SPeter Avalos /* Remove leading drive letter from archives created 4548029ab02SPeter Avalos * on Windows. */ 4558029ab02SPeter Avalos if (((p[0] >= 'a' && p[0] <= 'z') || 4568029ab02SPeter Avalos (p[0] >= 'A' && p[0] <= 'Z')) && 4578029ab02SPeter Avalos p[1] == ':') { 4588029ab02SPeter Avalos p += 2; 4598029ab02SPeter Avalos slashonly = 0; 4608029ab02SPeter Avalos } 4618029ab02SPeter Avalos /* Remove leading "/../", "//", etc. */ 4628029ab02SPeter Avalos while (p[0] == '/' || p[0] == '\\') { 4638029ab02SPeter Avalos if (p[1] == '.' && p[2] == '.' && 4648029ab02SPeter Avalos (p[3] == '/' || p[3] == '\\')) { 4658029ab02SPeter Avalos p += 3; /* Remove "/..", leave "/" 4668029ab02SPeter Avalos * for next pass. */ 4678029ab02SPeter Avalos slashonly = 0; 4688029ab02SPeter Avalos } else 4698029ab02SPeter Avalos p += 1; /* Remove "/". */ 4708029ab02SPeter Avalos } 4718029ab02SPeter Avalos } while (rp != p); 4728029ab02SPeter Avalos 4738029ab02SPeter Avalos if (p != name && !bsdtar->warned_lead_slash) { 4748029ab02SPeter Avalos /* Generate a warning the first time this happens. */ 4758029ab02SPeter Avalos if (slashonly) 4769c82a63eSPeter Avalos lafe_warnc(0, 4778029ab02SPeter Avalos "Removing leading '%c' from member names", 4788029ab02SPeter Avalos name[0]); 4798029ab02SPeter Avalos else 4809c82a63eSPeter Avalos lafe_warnc(0, 4818029ab02SPeter Avalos "Removing leading drive letter from " 4828029ab02SPeter Avalos "member names"); 4838029ab02SPeter Avalos bsdtar->warned_lead_slash = 1; 4848029ab02SPeter Avalos } 4858029ab02SPeter Avalos 4868029ab02SPeter Avalos /* Special case: Stripping everything yields ".". */ 4878029ab02SPeter Avalos if (*p == '\0') 4888029ab02SPeter Avalos name = "."; 4898029ab02SPeter Avalos else 49060b4ad09SPeter Avalos name = p; 4918029ab02SPeter Avalos } else { 49260b4ad09SPeter Avalos /* Strip redundant leading '/' characters. */ 49360b4ad09SPeter Avalos while (name[0] == '/' && name[1] == '/') 49460b4ad09SPeter Avalos name++; 49560b4ad09SPeter Avalos } 49660b4ad09SPeter Avalos 49760b4ad09SPeter Avalos /* Safely replace name in archive_entry. */ 49860b4ad09SPeter Avalos if (name != archive_entry_pathname(entry)) { 49960b4ad09SPeter Avalos char *q = strdup(name); 50060b4ad09SPeter Avalos archive_entry_copy_pathname(entry, q); 50160b4ad09SPeter Avalos free(q); 50260b4ad09SPeter Avalos } 50360b4ad09SPeter Avalos return (0); 50460b4ad09SPeter Avalos } 50560b4ad09SPeter Avalos 50660b4ad09SPeter Avalos /* 5079c82a63eSPeter Avalos * It would be nice to just use printf() for formatting large numbers, 5089c82a63eSPeter Avalos * but the compatibility problems are quite a headache. Hence the 5099c82a63eSPeter Avalos * following simple utility function. 5109c82a63eSPeter Avalos */ 5119c82a63eSPeter Avalos const char * 5129c82a63eSPeter Avalos tar_i64toa(int64_t n0) 5139c82a63eSPeter Avalos { 5149c82a63eSPeter Avalos static char buff[24]; 515*c09f92d2SPeter Avalos uint64_t n = n0 < 0 ? -n0 : n0; 5169c82a63eSPeter Avalos char *p = buff + sizeof(buff); 5179c82a63eSPeter Avalos 5189c82a63eSPeter Avalos *--p = '\0'; 5199c82a63eSPeter Avalos do { 5209c82a63eSPeter Avalos *--p = '0' + (int)(n % 10); 521*c09f92d2SPeter Avalos } while (n /= 10); 5229c82a63eSPeter Avalos if (n0 < 0) 5239c82a63eSPeter Avalos *--p = '-'; 5249c82a63eSPeter Avalos return p; 5259c82a63eSPeter Avalos } 5269c82a63eSPeter Avalos 5279c82a63eSPeter Avalos /* 52860b4ad09SPeter Avalos * Like strcmp(), but try to be a little more aware of the fact that 52960b4ad09SPeter Avalos * we're comparing two paths. Right now, it just handles leading 53060b4ad09SPeter Avalos * "./" and trailing '/' specially, so that "a/b/" == "./a/b" 53160b4ad09SPeter Avalos * 53260b4ad09SPeter Avalos * TODO: Make this better, so that "./a//b/./c/" == "a/b/c" 53360b4ad09SPeter Avalos * TODO: After this works, push it down into libarchive. 53460b4ad09SPeter Avalos * TODO: Publish the path normalization routines in libarchive so 53560b4ad09SPeter Avalos * that bsdtar can normalize paths and use fast strcmp() instead 53660b4ad09SPeter Avalos * of this. 5379c82a63eSPeter Avalos * 5389c82a63eSPeter Avalos * Note: This is currently only used within write.c, so should 5399c82a63eSPeter Avalos * not handle \ path separators. 54060b4ad09SPeter Avalos */ 54160b4ad09SPeter Avalos 54260b4ad09SPeter Avalos int 54360b4ad09SPeter Avalos pathcmp(const char *a, const char *b) 54460b4ad09SPeter Avalos { 54560b4ad09SPeter Avalos /* Skip leading './' */ 54660b4ad09SPeter Avalos if (a[0] == '.' && a[1] == '/' && a[2] != '\0') 54760b4ad09SPeter Avalos a += 2; 54860b4ad09SPeter Avalos if (b[0] == '.' && b[1] == '/' && b[2] != '\0') 54960b4ad09SPeter Avalos b += 2; 55060b4ad09SPeter Avalos /* Find the first difference, or return (0) if none. */ 55160b4ad09SPeter Avalos while (*a == *b) { 55260b4ad09SPeter Avalos if (*a == '\0') 55360b4ad09SPeter Avalos return (0); 55460b4ad09SPeter Avalos a++; 55560b4ad09SPeter Avalos b++; 55660b4ad09SPeter Avalos } 55760b4ad09SPeter Avalos /* 55860b4ad09SPeter Avalos * If one ends in '/' and the other one doesn't, 55960b4ad09SPeter Avalos * they're the same. 56060b4ad09SPeter Avalos */ 56160b4ad09SPeter Avalos if (a[0] == '/' && a[1] == '\0' && b[0] == '\0') 56260b4ad09SPeter Avalos return (0); 56360b4ad09SPeter Avalos if (a[0] == '\0' && b[0] == '/' && b[1] == '\0') 56460b4ad09SPeter Avalos return (0); 56560b4ad09SPeter Avalos /* They're really different, return the correct sign. */ 56660b4ad09SPeter Avalos return (*(const unsigned char *)a - *(const unsigned char *)b); 56760b4ad09SPeter Avalos } 568