Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libarchive / dist / tar / test / test_option_T.c
blob6c9007b24a90e32fb993d3ed61768f7f016d0ef2
1 /*-
2 * Copyright (c) 2003-2008 Tim Kientzle
3 * All rights reserved.
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.
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.
25 #include "test.h"
26 __FBSDID("$FreeBSD: src/usr.bin/tar/test/test_option_T.c,v 1.2 2008/05/26 17:10:10 kientzle Exp $");
28 static int
29 touch(const char *fn)
31 int fd = open(fn, O_RDWR | O_CREAT);
32 failure("Couldn't create file '%s', fd=%d, errno=%d (%s)\n",
33 fn, fd, errno, strerror(errno));
34 if (!assert(fd > 0))
35 return (0); /* Failure. */
36 close(fd);
37 return (1); /* Success */
40 DEFINE_TEST(test_option_T)
42 FILE *f;
44 /* Create a simple dir heirarchy; bail if anything fails. */
45 if (!assertEqualInt(0, mkdir("d1", 0755))) return;
46 if (!assertEqualInt(0, mkdir("d1/d2", 0755))) return;
47 if (!touch("d1/f1")) return;
48 if (!touch("d1/f2")) return;
49 if (!touch("d1/d2/f3")) return;
50 if (!touch("d1/d2/f4")) return;
51 if (!touch("d1/d2/f5")) return;
53 /* Populate a file list */
54 f = fopen("filelist", "w+");
55 if (!assert(f != NULL))
56 return;
57 fprintf(f, "d1/f1\n");
58 fprintf(f, "d1/d2/f4\n");
59 fclose(f);
61 /* Populate a second file list */
62 f = fopen("filelist2", "w+");
63 if (!assert(f != NULL))
64 return;
65 fprintf(f, "d1/d2/f3\n");
66 fprintf(f, "d1/d2/f5\n");
67 fclose(f);
69 /* Use -c -T to archive up the files. */
70 systemf("%s -c -f test1.tar -T filelist > test1.out 2> test1.err",
71 testprog);
72 assertEmptyFile("test1.out");
73 assertEmptyFile("test1.err");
75 /* Use -x -T to dearchive the files */
76 if (!assertEqualInt(0, mkdir("test1", 0755))) return;
77 systemf("%s -x -f test1.tar -T filelist -C test1"
78 " > test1b.out 2> test1b.err", testprog);
79 assertEmptyFile("test1b.out");
80 assertEmptyFile("test1b.err");
82 /* Verify the files were extracted. */
83 assertFileExists("test1/d1/f1");
84 assertFileNotExists("test1/d1/f2");
85 assertFileNotExists("test1/d1/d2/f3");
86 assertFileExists("test1/d1/d2/f4");
87 assertFileNotExists("test1/d1/d2/f5");
89 /* Use -r -T to add more files to the archive. */
90 systemf("%s -r -f test1.tar -T filelist2 > test2.out 2> test2.err",
91 testprog);
92 assertEmptyFile("test2.out");
93 assertEmptyFile("test2.err");
95 /* Use -x without -T to dearchive the files (ensure -r worked) */
96 if (!assertEqualInt(0, mkdir("test3", 0755))) return;
97 systemf("%s -x -f test1.tar -C test3"
98 " > test3.out 2> test3.err", testprog);
99 assertEmptyFile("test3.out");
100 assertEmptyFile("test3.err");
101 /* Verify the files were extracted.*/
102 assertFileExists("test3/d1/f1");
103 assertFileNotExists("test3/d1/f2");
104 assertFileExists("test3/d1/d2/f3");
105 assertFileExists("test3/d1/d2/f4");
106 assertFileExists("test3/d1/d2/f5");
108 /* Use -x -T to dearchive the files (verify -x -T together) */
109 if (!assertEqualInt(0, mkdir("test2", 0755))) return;
110 systemf("%s -x -f test1.tar -T filelist -C test2"
111 " > test2b.out 2> test2b.err", testprog);
112 assertEmptyFile("test2b.out");
113 assertEmptyFile("test2b.err");
114 /* Verify the files were extracted.*/
115 assertFileExists("test2/d1/f1");
116 assertFileNotExists("test2/d1/f2");
117 assertFileNotExists("test2/d1/d2/f3");
118 assertFileExists("test2/d1/d2/f4");
119 assertFileNotExists("test2/d1/d2/f5");
121 assertEqualInt(0, mkdir("test4", 0755));
122 assertEqualInt(0, mkdir("test4_out", 0755));
123 assertEqualInt(0, mkdir("test4_out2", 0755));
124 assertEqualInt(0, mkdir("test4/d1", 0755));
125 assertEqualInt(1, touch("test4/d1/foo"));
127 systemf("%s -cf - -s /foo/bar/ test4/d1/foo | %s -xf - -C test4_out",
128 testprog, testprog);
129 assertEmptyFile("test4_out/test4/d1/bar");
130 systemf("%s -cf - -s /d1/d2/ test4/d1/foo | %s -xf - -C test4_out",
131 testprog, testprog);
132 assertEmptyFile("test4_out/test4/d2/foo");
133 systemf("%s -cf - -s ,test4/d1/foo,, test4/d1/foo | %s -tvf - > test4.lst",
134 testprog, testprog);
135 assertEmptyFile("test4.lst");
136 systemf("%s -cf - test4/d1/foo | %s -xf - -s /foo/bar/ -C test4_out2",
137 testprog, testprog);
138 assertEmptyFile("test4_out2/test4/d1/bar");
140 /* TODO: Include some use of -C directory-changing within the filelist. */
141 /* I'm pretty sure -C within the filelist is broken on extract. */