From 9ab299b136d96059e3903cf712eadfd82dddc3ac Mon Sep 17 00:00:00 2001 From: kientzle Date: Sun, 8 Mar 2009 05:47:21 +0000 Subject: [PATCH] Merger r629-631,633-646,648,654,678,681,682 from libarchive.googlecode.com: Many changes for Windows compatibility. bsdtar_test now runs successfully on both POSIX platforms and Windows. --- usr.bin/tar/bsdtar.c | 17 +- usr.bin/tar/bsdtar_platform.h | 6 + usr.bin/tar/getdate.y | 4 + usr.bin/tar/read.c | 8 + usr.bin/tar/siginfo.c | 4 + usr.bin/tar/test/main.c | 31 ++- usr.bin/tar/test/test.h | 6 + usr.bin/tar/test/test_0.c | 9 +- usr.bin/tar/test/test_basic.c | 35 +++- usr.bin/tar/test/test_copy.c | 10 +- usr.bin/tar/test/test_option_T.c | 34 ++-- usr.bin/tar/test/test_option_s.c | 11 ++ usr.bin/tar/test/test_patterns.c | 12 +- usr.bin/tar/test/test_patterns_2.tar.uu | 232 +++++++++++++++++++++++ usr.bin/tar/test/test_patterns_2.tgz.uu | 9 - usr.bin/tar/test/test_patterns_3.tar.uu | 232 +++++++++++++++++++++++ usr.bin/tar/test/test_patterns_3.tgz.uu | 8 - usr.bin/tar/test/test_strip_components.c | 4 + usr.bin/tar/test/test_symlink_dir.c | 27 ++- usr.bin/tar/test/test_version.c | 17 +- usr.bin/tar/tree.c | 33 ++++ usr.bin/tar/util.c | 45 +++-- usr.bin/tar/write.c | 5 + 23 files changed, 733 insertions(+), 66 deletions(-) create mode 100644 usr.bin/tar/test/test_patterns_2.tar.uu delete mode 100644 usr.bin/tar/test/test_patterns_2.tgz.uu create mode 100644 usr.bin/tar/test/test_patterns_3.tar.uu delete mode 100644 usr.bin/tar/test/test_patterns_3.tgz.uu diff --git a/usr.bin/tar/bsdtar.c b/usr.bin/tar/bsdtar.c index 24aaad7e658d..af762eacdbeb 100644 --- a/usr.bin/tar/bsdtar.c +++ b/usr.bin/tar/bsdtar.c @@ -73,6 +73,9 @@ __FBSDID("$FreeBSD$"); #ifdef __linux #define _PATH_DEFTAPE "/dev/st0" #endif +#ifdef _WIN32 +#define _PATH_DEFTAPE "\\\\.\\tape0" +#endif #ifndef _PATH_DEFTAPE #define _PATH_DEFTAPE "/dev/tape" @@ -109,12 +112,20 @@ main(int argc, char **argv) memset(bsdtar, 0, sizeof(*bsdtar)); bsdtar->fd = -1; /* Mark as "unused" */ option_o = 0; +#ifdef _WIN32 + /* open() function is always with a binary mode. */ + _set_fmode(_O_BINARY); +#endif /* Need bsdtar->progname before calling bsdtar_warnc. */ if (*argv == NULL) bsdtar->progname = "bsdtar"; else { +#if _WIN32 + bsdtar->progname = strrchr(*argv, '\\'); +#else bsdtar->progname = strrchr(*argv, '/'); +#endif if (bsdtar->progname != NULL) bsdtar->progname++; else @@ -143,7 +154,7 @@ main(int argc, char **argv) bsdtar->extract_flags |= SECURITY; /* Defaults for root user: */ - if (bsdtar->user_uid == 0) { + if (bsdtar_is_privileged(bsdtar)) { /* --same-owner */ bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER; /* -p */ @@ -152,6 +163,10 @@ main(int argc, char **argv) bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR; bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS; } +#ifdef _WIN32 + /* Windows cannot set UNIX like uid/gid. */ + bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER; +#endif bsdtar->argv = argv; bsdtar->argc = argc; diff --git a/usr.bin/tar/bsdtar_platform.h b/usr.bin/tar/bsdtar_platform.h index a641f5b72e2c..828eb38fa67b 100644 --- a/usr.bin/tar/bsdtar_platform.h +++ b/usr.bin/tar/bsdtar_platform.h @@ -164,4 +164,10 @@ #define __LA_DEAD #endif +#ifdef _WIN32 +#include "bsdtar_windows.h" +#else +#define bsdtar_is_privileged(bsdtar) (bsdtar->user_uid == 0) +#endif + #endif /* !BSDTAR_PLATFORM_H_INCLUDED */ diff --git a/usr.bin/tar/getdate.y b/usr.bin/tar/getdate.y index 16162c35424b..28b402ef179f 100644 --- a/usr.bin/tar/getdate.y +++ b/usr.bin/tar/getdate.y @@ -35,6 +35,10 @@ __FBSDID("$FreeBSD$"); #include #include +#ifdef _MSC_VER +#define __STDC__ /* for a bug of bison 2.1 on Windows */ +#endif + #define yyparse getdate_yyparse #define yylex getdate_yylex #define yyerror getdate_yyerror diff --git a/usr.bin/tar/read.c b/usr.bin/tar/read.c index 35cea89a95a2..3aa164feb608 100644 --- a/usr.bin/tar/read.c +++ b/usr.bin/tar/read.c @@ -384,10 +384,18 @@ list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry) /* Format the time using 'ls -l' conventions. */ tim = (time_t)st->st_mtime; +#ifdef _WIN32 + /* Windows' strftime function does not support %e format. */ + if (abs(tim - now) > (365/2)*86400) + fmt = bsdtar->day_first ? "%d %b %Y" : "%b %d %Y"; + else + fmt = bsdtar->day_first ? "%d %b %H:%M" : "%b %d %H:%M"; +#else if (abs(tim - now) > (365/2)*86400) fmt = bsdtar->day_first ? "%e %b %Y" : "%b %e %Y"; else fmt = bsdtar->day_first ? "%e %b %H:%M" : "%b %e %H:%M"; +#endif strftime(tmp, sizeof(tmp), fmt, localtime(&tim)); fprintf(out, " %s ", tmp); safe_fprintf(out, "%s", archive_entry_pathname(entry)); diff --git a/usr.bin/tar/siginfo.c b/usr.bin/tar/siginfo.c index f0269a10f67d..e0881ac1a1db 100644 --- a/usr.bin/tar/siginfo.c +++ b/usr.bin/tar/siginfo.c @@ -82,8 +82,10 @@ siginfo_init(struct bsdtar *bsdtar) /* We want to catch SIGINFO, if it exists. */ bsdtar->siginfo->siginfo_old = signal(SIGINFO, siginfo_handler); #endif +#ifdef SIGUSR1 /* ... and treat SIGUSR1 the same way as SIGINFO. */ bsdtar->siginfo->sigusr1_old = signal(SIGUSR1, siginfo_handler); +#endif } void @@ -135,8 +137,10 @@ siginfo_done(struct bsdtar *bsdtar) /* Restore old SIGINFO handler. */ signal(SIGINFO, bsdtar->siginfo->siginfo_old); #endif +#ifdef SIGUSR1 /* And the old SIGUSR1 handler, too. */ signal(SIGUSR1, bsdtar->siginfo->sigusr1_old); +#endif /* Free strings. */ free(bsdtar->siginfo->path); diff --git a/usr.bin/tar/test/main.c b/usr.bin/tar/test/main.c index 7f114ecf0159..ad434aa5ac3d 100644 --- a/usr.bin/tar/test/main.c +++ b/usr.bin/tar/test/main.c @@ -504,6 +504,7 @@ test_assert_empty_file(const char *f1fmt, ...) (ssize_t)sizeof(buff) : (ssize_t)st.st_size; s = read(fd, buff, s); hexdump(buff, NULL, s, 0); + close(fd); } report_failure(NULL); return (0); @@ -564,11 +565,16 @@ test_assert_equal_file(const char *f1, const char *f2pattern, ...) n2 = read(fd2, buff2, sizeof(buff2)); if (n1 != n2) break; - if (n1 == 0 && n2 == 0) + if (n1 == 0 && n2 == 0) { + close(fd1); + close(fd2); return (1); + } if (memcmp(buff1, buff2, n1) != 0) break; } + close(fd1); + close(fd2); failures ++; if (!verbose && previous_failures(test_filename, test_line)) return (0); @@ -648,6 +654,7 @@ test_assert_file_contents(const void *buff, int s, const char *fpattern, ...) } contents = malloc(s * 2); n = read(fd, contents, s * 2); + close(fd); if (n == s && memcmp(buff, contents, s) == 0) { free(contents); return (1); @@ -802,7 +809,11 @@ static int test_run(int i, const char *tmpdir) /* If there were no failures, we can remove the work dir. */ if (failures == failures_before) { if (!keep_temp_files && chdir(tmpdir) == 0) { +#ifdef _WIN32 + systemf("rmdir /S /Q %s", tests[i].name); +#else systemf("rm -rf %s", tests[i].name); +#endif } } /* Return appropriate status. */ @@ -901,6 +912,9 @@ int main(int argc, char **argv) int i, tests_run = 0, tests_failed = 0, opt; time_t now; char *refdir_alloc = NULL; +#ifdef _WIN32 + char *testprg; +#endif const char *opt_arg, *progname, *p; char tmpdir[256]; char tmpdir_timestamp[256]; @@ -913,7 +927,8 @@ int main(int argc, char **argv) */ progname = p = argv[0]; while (*p != '\0') { - if (*p == '/') + /* Support \ or / dir separators for Windows compat. */ + if (*p == '/' || *p == '\\') progname = p + 1; ++p; } @@ -995,6 +1010,18 @@ int main(int argc, char **argv) if (testprog == NULL) usage(progname); #endif +#ifdef _WIN32 + /* + * command.com cannot accept the command used '/' with drive + * name such as c:/xxx/command.exe when use '|' pipe handling. + */ + testprg = strdup(testprog); + for (i = 0; testprg[i] != '\0'; i++) { + if (testprg[i] == '/') + testprg[i] = '\\'; + } + testprog = testprg; +#endif /* * Create a temp directory for the following tests. diff --git a/usr.bin/tar/test/test.h b/usr.bin/tar/test/test.h index ab568c252a4a..27a693520114 100644 --- a/usr.bin/tar/test/test.h +++ b/usr.bin/tar/test/test.h @@ -45,7 +45,13 @@ #error Oops: No config.h and no pre-built configuration in test.h. #endif +#ifndef _WIN32 #include +#else +#define dirent direct +#include "../bsdtar_windows.h" +#include +#endif #include #include #include diff --git a/usr.bin/tar/test/test_0.c b/usr.bin/tar/test/test_0.c index 7a72af1c8edb..1bafc05a2cad 100644 --- a/usr.bin/tar/test/test_0.c +++ b/usr.bin/tar/test/test_0.c @@ -29,6 +29,11 @@ __FBSDID("$FreeBSD$"); * This first test does basic sanity checks on the environment. For * most of these, we just exit on failure. */ +#ifndef _WIN32 +#define DEV_NULL "/dev/null" +#else +#define DEV_NULL "NUL" +#endif DEFINE_TEST(test_0) { @@ -46,9 +51,9 @@ DEFINE_TEST(test_0) * Try to succesfully run the program; this requires that * we know some option that will succeed. */ - if (0 == systemf("%s --version >/dev/null", testprog)) { + if (0 == systemf("%s --version >" DEV_NULL, testprog)) { /* This worked. */ - } else if (0 == systemf("%s -W version >/dev/null", testprog)) { + } else if (0 == systemf("%s -W version >" DEV_NULL, testprog)) { /* This worked. */ } else { failure("Unable to successfully run any of the following:\n" diff --git a/usr.bin/tar/test/test_basic.c b/usr.bin/tar/test/test_basic.c index 50be2890c0e3..b454723abd23 100644 --- a/usr.bin/tar/test/test_basic.c +++ b/usr.bin/tar/test/test_basic.c @@ -27,16 +27,23 @@ __FBSDID("$FreeBSD$"); static void -basic_tar(const char *target, const char *pack_options, const char *unpack_options) +basic_tar(const char *target, const char *pack_options, + const char *unpack_options, const char *flist) { struct stat st, st2; +#ifndef _WIN32 char buff[128]; +#endif int r; assertEqualInt(0, mkdir(target, 0775)); /* Use the tar program to create an archive. */ - r = systemf("%s cf - %s `cat filelist` >%s/archive 2>%s/pack.err", testprog, pack_options, target, target); +#ifndef _WIN32 + r = systemf("%s cf - %s `cat %s` >%s/archive 2>%s/pack.err", testprog, pack_options, flist, target, target); +#else + r = systemf("%s cf - %s %s >%s/archive 2>%s/pack.err", testprog, pack_options, flist, target, target); +#endif failure("Error invoking %s cf -", testprog, pack_options); assertEqualInt(r, 0); @@ -65,7 +72,11 @@ basic_tar(const char *target, const char *pack_options, const char *unpack_optio assertEqualInt(r, 0); if (r == 0) { assert(S_ISREG(st.st_mode)); +#ifndef _WIN32 assertEqualInt(0644, st.st_mode & 0777); +#else + assertEqualInt(0600, st.st_mode & 0700); +#endif assertEqualInt(10, st.st_size); failure("file %s/file", target); assertEqualInt(2, st.st_nlink); @@ -77,7 +88,11 @@ basic_tar(const char *target, const char *pack_options, const char *unpack_optio assertEqualInt(r, 0); if (r == 0) { assert(S_ISREG(st2.st_mode)); +#ifndef _WIN32 assertEqualInt(0644, st2.st_mode & 0777); +#else + assertEqualInt(0600, st2.st_mode & 0700); +#endif assertEqualInt(10, st2.st_size); failure("file %s/linkfile", target); assertEqualInt(2, st2.st_nlink); @@ -87,6 +102,7 @@ basic_tar(const char *target, const char *pack_options, const char *unpack_optio assertEqualInt(st.st_ino, st2.st_ino); } +#ifndef _WIN32 /* Symlink */ r = lstat("symlink", &st); failure("Failed to stat file %s/symlink, errno=%d", target, errno); @@ -102,13 +118,18 @@ basic_tar(const char *target, const char *pack_options, const char *unpack_optio assertEqualString(buff, "file"); } } +#endif /* dir */ r = lstat("dir", &st); if (r == 0) { assertEqualInt(r, 0); assert(S_ISDIR(st.st_mode)); +#ifndef _WIN32 assertEqualInt(0775, st.st_mode & 0777); +#else + assertEqualInt(0700, st.st_mode & 0700); +#endif } chdir(".."); @@ -119,6 +140,7 @@ DEFINE_TEST(test_basic) int fd; int filelist; int oldumask; + const char *flist; oldumask = umask(0); @@ -148,11 +170,16 @@ DEFINE_TEST(test_basic) /* All done. */ close(filelist); +#ifndef _WIN32 + flist = "filelist"; +#else + flist = "file linkfile symlink dir"; +#endif /* Archive/dearchive with a variety of options. */ - basic_tar("copy", "", ""); + basic_tar("copy", "", "", flist); /* tar doesn't handle cpio symlinks correctly */ /* basic_tar("copy_odc", "--format=odc", ""); */ - basic_tar("copy_ustar", "--format=ustar", ""); + basic_tar("copy_ustar", "--format=ustar", "", flist); umask(oldumask); } diff --git a/usr.bin/tar/test/test_copy.c b/usr.bin/tar/test/test_copy.c index 7f12f9c89bb2..9aff9169a277 100644 --- a/usr.bin/tar/test/test_copy.c +++ b/usr.bin/tar/test/test_copy.c @@ -64,6 +64,7 @@ create_tree(void) buff2[0] = 'm'; assertEqualInt(0, link(buff, buff2)); +#ifndef _WIN32 /* Create a symlink named "s/abcdef..." to the above. */ strcpy(buff2 + 3, buff); buff[0] = 's'; @@ -71,7 +72,9 @@ create_tree(void) buff2[1] = '.'; buff2[2] = '/'; assertEqualInt(0, symlink(buff2, buff)); - +#else + skipping("create a symlink to the above"); +#endif /* Create a dir named "d/abcdef...". */ buff[0] = 'd'; assertEqualInt(0, mkdir(buff, 0775)); @@ -153,6 +156,7 @@ verify_tree(int limit) } } +#ifndef _WIN32 /* * Symlink text doesn't include the 'original/' prefix, * so the limit here is 100 characters. @@ -174,7 +178,9 @@ verify_tree(int limit) } } } - +#else + skipping("verify symlink"); +#endif /* Verify dir "d/abcdef...". */ strcpy(name1, "d/"); strcat(name1, filename); diff --git a/usr.bin/tar/test/test_option_T.c b/usr.bin/tar/test/test_option_T.c index 493dcc11dea5..9f4a2b34c6cf 100644 --- a/usr.bin/tar/test/test_option_T.c +++ b/usr.bin/tar/test/test_option_T.c @@ -41,6 +41,7 @@ DEFINE_TEST(test_option_T) { FILE *f; int r; + struct stat st; /* Create a simple dir heirarchy; bail if anything fails. */ if (!assertEqualInt(0, mkdir("d1", 0755))) return; @@ -127,19 +128,26 @@ DEFINE_TEST(test_option_T) assertEqualInt(0, mkdir("test4/d1", 0755)); assertEqualInt(1, touch("test4/d1/foo")); - systemf("%s -cf - -s /foo/bar/ test4/d1/foo | %s -xf - -C test4_out", - testprog, testprog); - assertEmptyFile("test4_out/test4/d1/bar"); - systemf("%s -cf - -s /d1/d2/ test4/d1/foo | %s -xf - -C test4_out", - testprog, testprog); - assertEmptyFile("test4_out/test4/d2/foo"); - systemf("%s -cf - -s ,test4/d1/foo,, test4/d1/foo | %s -tvf - > test4.lst", - testprog, testprog); - assertEmptyFile("test4.lst"); - systemf("%s -cf - test4/d1/foo | %s -xf - -s /foo/bar/ -C test4_out2", - testprog, testprog); - assertEmptyFile("test4_out2/test4/d1/bar"); - + /* Does bsdtar support -s option ? */ + systemf("%s -cf - -s /foo/bar/ test4/d1/foo > NUL 2> check.err", + testprog); + assertEqualInt(0, stat("check.err", &st)); + if (st.st_size == 0) { + systemf("%s -cf - -s /foo/bar/ test4/d1/foo | %s -xf - -C test4_out", + testprog, testprog); + assertEmptyFile("test4_out/test4/d1/bar"); + systemf("%s -cf - -s /d1/d2/ test4/d1/foo | %s -xf - -C test4_out", + testprog, testprog); + assertEmptyFile("test4_out/test4/d2/foo"); + systemf("%s -cf - -s ,test4/d1/foo,, test4/d1/foo | %s -tvf - > test4.lst", + testprog, testprog); + assertEmptyFile("test4.lst"); + systemf("%s -cf - test4/d1/foo | %s -xf - -s /foo/bar/ -C test4_out2", + testprog, testprog); + assertEmptyFile("test4_out2/test4/d1/bar"); + } else { + skipping("bsdtar does not support -s option on this platform"); + } /* TODO: Include some use of -C directory-changing within the filelist. */ /* I'm pretty sure -C within the filelist is broken on extract. */ } diff --git a/usr.bin/tar/test/test_option_s.c b/usr.bin/tar/test/test_option_s.c index b256e2aaa71f..1059066fd5cc 100644 --- a/usr.bin/tar/test/test_option_s.c +++ b/usr.bin/tar/test/test_option_s.c @@ -42,12 +42,23 @@ mkfile(const char *fn, const char *contents) DEFINE_TEST(test_option_s) { + struct stat st; + /* Create a sample file heirarchy. */ assertEqualInt(0, mkdir("in", 0755)); assertEqualInt(0, mkdir("in/d1", 0755)); assertEqualInt(0, mkfile("in/d1/foo", "foo")); assertEqualInt(0, mkfile("in/d1/bar", "bar")); + /* Does bsdtar support -s option ? */ + systemf("%s -cf - -s /foo/bar/ in/d1/foo > NUL 2> check.err", + testprog); + assertEqualInt(0, stat("check.err", &st)); + if (st.st_size != 0) { + skipping("bsdtar does not support -s option on this platform"); + return; + } + /* * Test 1: Filename substitution when creating archives. */ diff --git a/usr.bin/tar/test/test_patterns.c b/usr.bin/tar/test/test_patterns.c index 281133aa4c00..36d4542450d4 100644 --- a/usr.bin/tar/test/test_patterns.c +++ b/usr.bin/tar/test/test_patterns.c @@ -28,8 +28,8 @@ __FBSDID("$FreeBSD$"); DEFINE_TEST(test_patterns) { int fd, r; - const char *reffile2 = "test_patterns_2.tgz"; - const char *reffile3 = "test_patterns_3.tgz"; + const char *reffile2 = "test_patterns_2.tar"; + const char *reffile3 = "test_patterns_3.tar"; const char *p; /* @@ -45,9 +45,9 @@ DEFINE_TEST(test_patterns) fd = open("foo", O_CREAT | O_WRONLY, 0644); assert(fd >= 0); close(fd); - r = systemf("%s zcfv tar1.tgz foo > tar1a.out 2> tar1a.err", testprog); + r = systemf("%s cfv tar1.tgz foo > tar1a.out 2> tar1a.err", testprog); assertEqualInt(r, 0); - r = systemf("%s zxfv tar1.tgz foo bar > tar1b.out 2> tar1b.err", testprog); + r = systemf("%s xfv tar1.tgz foo bar > tar1b.out 2> tar1b.err", testprog); failure("tar should return non-zero because a file was given on the command line that's not in the archive"); assert(r != 0); @@ -59,7 +59,11 @@ DEFINE_TEST(test_patterns) r = systemf("%s tf %s /tmp/foo/bar > tar2a.out 2> tar2a.err", testprog, reffile2); assertEqualInt(r, 0); +#ifndef _WIN32 p = "/tmp/foo/bar/\n/tmp/foo/bar/baz\n"; +#else + p = "/tmp/foo/bar/\r\n/tmp/foo/bar/baz\r\n"; +#endif assertFileContents(p, strlen(p), "tar2a.out"); assertEmptyFile("tar2a.err"); diff --git a/usr.bin/tar/test/test_patterns_2.tar.uu b/usr.bin/tar/test/test_patterns_2.tar.uu new file mode 100644 index 000000000000..1ed9a7d1aea0 --- /dev/null +++ b/usr.bin/tar/test/test_patterns_2.tar.uu @@ -0,0 +1,232 @@ +$FreeBSD$ +begin 644 test_patterns_2.tar +M+W1M<"]F;V\O```````````````````````````````````````````````` +M```````````````````````````````````````````````````````````` +M`````````````#`P,#@`````````````````````````````````````````````````````````` +M```````````````````````````````````````````````````````````P +M,#`V-#0@`#`P,3@`````````````````````````````` +M```````````````````````````````````````````````````````````` +M`````````````````````#`P,#8T-"``,#`Q-S4P(``P,#`P,#`@`#`P,#`P +M,#`P,#`P(#$Q,#4Q,C$R-C4S(#`Q,S8V-P`@,``````````````````````` +M```````````````````````````````````````````````````````````` +M``````````````````````````````````````````````````!U.:W8X?V;/==9XM\1*0*P:2J59"QCN8ZO:A*4*Z`]OR?\Y_C!7Y`P``````````````?.,! -(*>E$>P`H```` -` -end diff --git a/usr.bin/tar/test/test_patterns_3.tar.uu b/usr.bin/tar/test/test_patterns_3.tar.uu new file mode 100644 index 000000000000..e8d487ed259b --- /dev/null +++ b/usr.bin/tar/test/test_patterns_3.tar.uu @@ -0,0 +1,232 @@ +$FreeBSD$ +begin 644 test_patterns_3.tar +M+W1M<"]F;V\O8F%R+P`````````````````````````````````````````` +M```````````````````````````````````````````````````````````` +M`````````````#`P,#65]>2CUUQX+TO/\F_@H<0_BWG^I;Z`#?D'X?T#``````````````!\Y0FE&!YR`"@````` -` -end diff --git a/usr.bin/tar/test/test_strip_components.c b/usr.bin/tar/test/test_strip_components.c index 0f98dfc08632..6fc4b301c520 100644 --- a/usr.bin/tar/test/test_strip_components.c +++ b/usr.bin/tar/test/test_strip_components.c @@ -64,7 +64,11 @@ DEFINE_TEST(test_strip_components) failure("d0/d1/ is too short and should not get restored"); assertEqualInt(-1, lstat("target/d1", &st)); failure("d0/d1/s2 is a symlink to something that won't be extracted"); +#ifndef _WIN32 assertEqualInt(-1, stat("target/s2", &st)); +#else + skipping("symlink with stat()"); +#endif assertEqualInt(0, lstat("target/s2", &st)); failure("d0/d1/d2 should be extracted"); assertEqualInt(0, lstat("target/d2", &st)); diff --git a/usr.bin/tar/test/test_symlink_dir.c b/usr.bin/tar/test/test_symlink_dir.c index e256c940bb9e..d5c493db28e2 100644 --- a/usr.bin/tar/test/test_symlink_dir.c +++ b/usr.bin/tar/test/test_symlink_dir.c @@ -47,7 +47,10 @@ mkfile(const char *name, int mode, const char *contents, ssize_t size) DEFINE_TEST(test_symlink_dir) { - struct stat st, st2; + struct stat st; +#ifndef _WIN32 + struct stat st2; +#endif int oldumask; oldumask = umask(0); @@ -75,18 +78,25 @@ DEFINE_TEST(test_symlink_dir) assertEqualInt(0, mkdir("dest1", 0755)); /* "dir" is a symlink to an existing "real_dir" */ assertEqualInt(0, mkdir("dest1/real_dir", 0755)); +#ifndef _WIN32 assertEqualInt(0, symlink("real_dir", "dest1/dir")); /* "dir2" is a symlink to a non-existing "real_dir2" */ assertEqualInt(0, symlink("real_dir2", "dest1/dir2")); +#else + skipping("symlink does not work on this platform"); +#endif /* "dir3" is a symlink to an existing "non_dir3" */ assertEqualInt(0, mkfile("dest1/non_dir3", 0755, "abcdef", 6)); assertEqualInt(0, symlink("non_dir3", "dest1/dir3")); /* "file" is a symlink to existing "real_file" */ assertEqualInt(0, mkfile("dest1/real_file", 0755, "abcdefg", 7)); assertEqualInt(0, symlink("real_file", "dest1/file")); +#ifndef _WIN32 /* "file2" is a symlink to non-existing "real_file2" */ assertEqualInt(0, symlink("real_file2", "dest1/file2")); - +#else + skipping("symlink does not work on this platform"); +#endif assertEqualInt(0, systemf("%s -xf test.tar -C dest1", testprog)); /* dest1/dir symlink should be removed */ @@ -116,23 +126,31 @@ DEFINE_TEST(test_symlink_dir) assertEqualInt(0, mkdir("dest2", 0755)); /* "dir" is a symlink to existing "real_dir" */ assertEqualInt(0, mkdir("dest2/real_dir", 0755)); +#ifndef _WIN32 assertEqualInt(0, symlink("real_dir", "dest2/dir")); /* "dir2" is a symlink to a non-existing "real_dir2" */ assertEqualInt(0, symlink("real_dir2", "dest2/dir2")); +#else + skipping("symlink does not work on this platform"); +#endif /* "dir3" is a symlink to an existing "non_dir3" */ assertEqualInt(0, mkfile("dest2/non_dir3", 0755, "abcdefgh", 8)); assertEqualInt(0, symlink("non_dir3", "dest2/dir3")); /* "file" is a symlink to existing "real_file" */ assertEqualInt(0, mkfile("dest2/real_file", 0755, "abcdefghi", 9)); assertEqualInt(0, symlink("real_file", "dest2/file")); +#ifndef _WIN32 /* "file2" is a symlink to non-existing "real_file2" */ assertEqualInt(0, symlink("real_file2", "dest2/file2")); - +#else + skipping("symlink does not work on this platform"); +#endif assertEqualInt(0, systemf("%s -xPf test.tar -C dest2", testprog)); /* dest2/dir symlink should be followed */ assertEqualInt(0, lstat("dest2/dir", &st)); failure("tar -xP removed symlink instead of following it"); +#ifndef _WIN32 if (assert(S_ISLNK(st.st_mode))) { /* Only verify what the symlink points to if it * really is a symlink. */ @@ -146,6 +164,9 @@ DEFINE_TEST(test_symlink_dir) failure("symlink should still point to the existing directory"); assertEqualInt(st.st_ino, st2.st_ino); } +#else + skipping("symlink does not work on this platform"); +#endif /* Contents of 'dir' should be restored */ assertEqualInt(0, lstat("dest2/dir/d", &st)); assert(S_ISDIR(st.st_mode)); diff --git a/usr.bin/tar/test/test_version.c b/usr.bin/tar/test/test_version.c index 6f2f6a0b5a0c..4f249a517b22 100644 --- a/usr.bin/tar/test/test_version.c +++ b/usr.bin/tar/test/test_version.c @@ -51,7 +51,7 @@ DEFINE_TEST(test_version) q = p = slurpfile(&s, "version.stdout"); /* Version message should start with name of program, then space. */ assert(s > 6); - failure("Version: %s", p); + failure("Version must start with 'bsdtar': ``%s''", p); assertEqualMem(q, "bsdtar ", 7); q += 7; s -= 7; /* Version number is a series of digits and periods. */ @@ -60,22 +60,22 @@ DEFINE_TEST(test_version) --s; } /* Version number terminated by space. */ - failure("Version: %s", p); + failure("No space after bsdtar version: ``%s''", p); assert(s > 1); /* Skip a single trailing a,b,c, or d. */ if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd') ++q; - failure("Version: %s", p); + failure("No space after bsdtar version: ``%s''", p); assert(*q == ' '); ++q; --s; /* Separator. */ - failure("Version: %s", p); + failure("No `-' between bsdtar and libarchive versions: ``%s''", p); assertEqualMem(q, "- ", 2); q += 2; s -= 2; /* libarchive name and version number */ - failure("Version: %s", p); + failure("Not long enough for libarchive version: ``%s''", p); assert(s > 11); - failure("Version: %s", p); + failure("Libarchive version must start with `libarchive': ``%s''", p); assertEqualMem(q, "libarchive ", 11); q += 11; s -= 11; /* Version number is a series of digits and periods. */ @@ -86,8 +86,11 @@ DEFINE_TEST(test_version) /* Skip a single trailing a,b,c, or d. */ if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd') ++q; - /* All terminated by a newline. */ + /* All terminated by end-of-line. */ assert(s >= 1); + /* Skip an optional CR character (e.g., Windows) */ + failure("Version output must end with \\n or \\r\\n"); + if (*q == '\r') { ++q; --s; } assertEqualMem(q, "\n", 1); free(p); } diff --git a/usr.bin/tar/tree.c b/usr.bin/tar/tree.c index de2e9ef7b32e..f47330b2c97f 100644 --- a/usr.bin/tar/tree.c +++ b/usr.bin/tar/tree.c @@ -83,6 +83,9 @@ struct tree_entry { dev_t dev; ino_t ino; int fd; +#ifdef _WIN32 + char *fullpath; +#endif int flags; }; @@ -100,6 +103,9 @@ struct tree { struct tree_entry *current; DIR *d; int initialDirFd; +#ifdef _WIN32 + char *initialDir; +#endif int flags; int visit_type; int tree_errno; /* Error code from last failed operation. */ @@ -164,6 +170,9 @@ tree_push(struct tree *t, const char *path) te->next = t->stack; t->stack = te; te->fd = -1; +#ifdef _WIN32 + te->fullpath = NULL; +#endif te->name = strdup(path); te->flags = needsPreVisit | needsPostVisit; te->dirname_length = t->dirname_length; @@ -214,6 +223,10 @@ tree_open(const char *path) memset(t, 0, sizeof(*t)); tree_append(t, path, strlen(path)); t->initialDirFd = open(".", O_RDONLY); +#ifdef _WIN32 + if (t->initialDirFd >= 0) + t->initialDir = getcwd(NULL, 0); +#endif /* * During most of the traversal, items are set up and then * returned immediately from tree_next(). That doesn't work @@ -236,10 +249,20 @@ tree_ascend(struct tree *t) te = t->stack; t->depth--; if (te->flags & isDirLink) { +#ifdef HAVE_FCHDIR if (fchdir(te->fd) != 0) { t->tree_errno = errno; r = TREE_ERROR_FATAL; } +#endif +#ifdef _WIN32 + if (te->fullpath != NULL && chdir(te->fullpath) != 0) { + t->tree_errno = errno; + r = TREE_ERROR_FATAL; + } + free(te->fullpath); + te->fullpath = NULL; +#endif close(te->fd); t->openCount--; } else { @@ -332,6 +355,9 @@ tree_next(struct tree *t) /* If it is a link, set up fd for the ascent. */ if (t->stack->flags & isDirLink) { t->stack->fd = open(".", O_RDONLY); +#ifdef _WIN32 + t->stack->fullpath = getcwd(NULL, 0); +#endif t->openCount++; if (t->openCount > t->maxOpenCount) t->maxOpenCount = t->openCount; @@ -555,7 +581,14 @@ tree_close(struct tree *t) free(t->buff); /* chdir() back to where we started. */ if (t->initialDirFd >= 0) { +#ifdef HAVE_FCHDIR fchdir(t->initialDirFd); +#endif +#ifdef _WIN32 + chdir(t->initialDir); + free(t->initialDir); + t->initialDir = NULL; +#endif close(t->initialDirFd); t->initialDirFd = -1; } diff --git a/usr.bin/tar/util.c b/usr.bin/tar/util.c index 367409b321f8..442a95c99c49 100644 --- a/usr.bin/tar/util.c +++ b/usr.bin/tar/util.c @@ -517,18 +517,41 @@ edit_pathname(struct bsdtar *bsdtar, struct archive_entry *entry) while (name[0] == '/' && name[1] == '/') name++; - /* Strip leading '/' unless user has asked us not to. */ - if (name[0] == '/' && !bsdtar->option_absolute_paths) { - /* Generate a warning the first time this happens. */ - if (!bsdtar->warned_lead_slash) { - bsdtar_warnc(bsdtar, 0, - "Removing leading '/' from member names"); - bsdtar->warned_lead_slash = 1; + /* By default, don't write or restore absolute pathnames. */ + if (!bsdtar->option_absolute_paths) { + /* Strip Windows drive letters. */ + if (((name[0] >= 'A' && name[0] <= 'Z') + || (name[0] >= 'a' && name[0] <= 'z')) + && name[1] == ':' + && (name[2] == '/' || name[2] == '\\')) + { + /* Generate a warning the first time this happens. */ + if (!bsdtar->warned_lead_slash) { + bsdtar_warnc(bsdtar, 0, + "Removing leading drive letter from member names"); + bsdtar->warned_lead_slash = 1; + } + name += 3; + while (*name == '/' || *name == '\\') + ++name; + /* Special case: Stripping everything yields ".". */ + if (*name == '\0') + name = "."; + } + + /* Strip leading '/'. */ + if (name[0] == '/') { + /* Generate a warning the first time this happens. */ + if (!bsdtar->warned_lead_slash) { + bsdtar_warnc(bsdtar, 0, + "Removing leading '/' from member names"); + bsdtar->warned_lead_slash = 1; + } + name++; + /* Special case: Stripping everything yields ".". */ + if (*name == '\0') + name = "."; } - name++; - /* Special case: Stripping leading '/' from "/" yields ".". */ - if (*name == '\0') - name = "."; } /* Safely replace name in archive_entry. */ diff --git a/usr.bin/tar/write.c b/usr.bin/tar/write.c index b64de1837730..dd534536c0ec 100644 --- a/usr.bin/tar/write.c +++ b/usr.bin/tar/write.c @@ -440,7 +440,12 @@ write_archive(struct archive *a, struct bsdtar *bsdtar) arg + 1) != 0) break; } else +#ifdef _WIN32 + write_hierarchy_win(bsdtar, a, arg, + write_hierarchy); +#else write_hierarchy(bsdtar, a, arg); +#endif } bsdtar->argv++; }