1/*-
2 * Copyright (c) 2003-2008 Tim Kientzle
3 * All rights reserved.
4 *
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.
13 *
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.
24 */
25#include "test.h"
26__FBSDID("$FreeBSD: src/usr.bin/tar/test/test_option_T.c,v 1.3 2008/08/15 06:12:02 kientzle Exp $");
27
28static int
29touch(const char *fn, int fail)
30{
31	FILE *f = fopen(fn, "w");
32	if (fail) {
33		failure("Couldn't create file '%s', errno=%d (%s)\n",
34		    fn, errno, strerror(errno));
35		if (!assert(f != NULL))
36			return (0); /* Failure. */
37	} else {
38		if (f == NULL)
39			return (0); /* Soft failure. */
40	}
41	fclose(f);
42	return (1); /* Success */
43}
44
45DEFINE_TEST(test_option_T_upper)
46{
47	FILE *f;
48	int r;
49	struct stat st;
50	int gnarlyFilesSupported;
51
52	/* Create a simple dir heirarchy; bail if anything fails. */
53	if (!assertMakeDir("d1", 0755)) return;
54	if (!assertMakeDir("d1/d2", 0755))	return;
55	if (!touch("f", 1)) return;
56	if (!touch("d1/f1", 1)) return;
57	if (!touch("d1/f2", 1)) return;
58	if (!touch("d1/d2/f3", 1)) return;
59	if (!touch("d1/d2/f4", 1)) return;
60	if (!touch("d1/d2/f5", 1)) return;
61	if (!touch("d1/d2/f6", 1)) return;
62	/* Some platforms don't permit such things; just skip it. */
63	gnarlyFilesSupported = touch("d1/d2/f\x0a", 0);
64
65	/* Populate a file list */
66	f = fopen("filelist", "w+");
67	if (!assert(f != NULL))
68		return;
69	/* Use a variety of text line endings. */
70	fprintf(f, "f\x0d"); /* CR */
71	fprintf(f, "d1/f1\x0d\x0a"); /* CRLF */
72	fprintf(f, "d1/d2/f4\x0a"); /* NL */
73	fprintf(f, "d1/d2/f6"); /* EOF */
74	fclose(f);
75
76	/* Populate a second file list */
77	f = fopen("filelist2", "w+");
78	if (!assert(f != NULL))
79		return;
80	/* Use null-terminated names. */
81	fprintf(f, "d1/d2/f3");
82	fwrite("\0", 1, 1, f);
83	fprintf(f, "d1/d2/f5");
84	fwrite("\0", 1, 1, f);
85	if (gnarlyFilesSupported) {
86		fprintf(f, "d1/d2/f\x0a");
87		fwrite("\0", 1, 1, f);
88	}
89	fclose(f);
90
91	/* Use -c -T to archive up the files. */
92	r = systemf("%s -c -f test1.tar -T filelist > test1.out 2> test1.err",
93	    testprog);
94	assert(r == 0);
95	assertEmptyFile("test1.out");
96	assertEmptyFile("test1.err");
97
98	/* Use -x -T to dearchive the files */
99	if (!assertMakeDir("test1", 0755)) return;
100	systemf("%s -x -f test1.tar -T filelist -C test1"
101	    " > test1b.out 2> test1b.err", testprog);
102	assertEmptyFile("test1b.out");
103	assertEmptyFile("test1b.err");
104
105	/* Verify the files were extracted. */
106	assertFileExists("test1/f");
107	assertFileExists("test1/d1/f1");
108	assertFileNotExists("test1/d1/f2");
109	assertFileNotExists("test1/d1/d2/f3");
110	assertFileExists("test1/d1/d2/f4");
111	assertFileNotExists("test1/d1/d2/f5");
112	assertFileExists("test1/d1/d2/f6");
113	if (gnarlyFilesSupported) {
114		assertFileNotExists("test1/d1/d2/f\x0a");
115	}
116
117	/* Use -r -T to add more files to the archive. */
118	systemf("%s -r -f test1.tar --null -T filelist2 > test2.out 2> test2.err",
119	    testprog);
120	assertEmptyFile("test2.out");
121	assertEmptyFile("test2.err");
122
123	/* Use -x without -T to dearchive the files (ensure -r worked) */
124	if (!assertMakeDir("test3", 0755)) return;
125	systemf("%s -x -f test1.tar -C test3"
126	    " > test3.out 2> test3.err", testprog);
127	assertEmptyFile("test3.out");
128	assertEmptyFile("test3.err");
129	/* Verify the files were extracted.*/
130	assertFileExists("test3/f");
131	assertFileExists("test3/d1/f1");
132	assertFileNotExists("test3/d1/f2");
133	assertFileExists("test3/d1/d2/f3");
134	assertFileExists("test3/d1/d2/f4");
135	assertFileExists("test3/d1/d2/f5");
136	assertFileExists("test3/d1/d2/f6");
137	if (gnarlyFilesSupported) {
138		assertFileExists("test3/d1/d2/f\x0a");
139	}
140
141	/* Use -x -T to dearchive the files (verify -x -T together) */
142	if (!assertMakeDir("test2", 0755)) return;
143	systemf("%s -x -f test1.tar -T filelist -C test2"
144	    " > test2b.out 2> test2b.err", testprog);
145	assertEmptyFile("test2b.out");
146	assertEmptyFile("test2b.err");
147	/* Verify the files were extracted.*/
148	assertFileExists("test2/f");
149	assertFileExists("test2/d1/f1");
150	assertFileNotExists("test2/d1/f2");
151	assertFileNotExists("test2/d1/d2/f3");
152	assertFileExists("test2/d1/d2/f4");
153	assertFileNotExists("test2/d1/d2/f5");
154	assertFileExists("test2/d1/d2/f6");
155	if (gnarlyFilesSupported) {
156		assertFileNotExists("test2/d1/d2/f\x0a");
157	}
158
159	assertMakeDir("test4", 0755);
160	assertMakeDir("test4_out", 0755);
161	assertMakeDir("test4_out2", 0755);
162	assertMakeDir("test4/d1", 0755);
163	assertEqualInt(1, touch("test4/d1/foo", 0));
164
165	/* Does bsdtar support -s option ? */
166	systemf("%s -cf - -s /foo/bar/ test4/d1/foo > check.out 2> check.err",
167	    testprog);
168	assertEqualInt(0, stat("check.err", &st));
169	if (st.st_size == 0) {
170		systemf("%s -cf - -s /foo/bar/ test4/d1/foo | %s -xf - -C test4_out",
171		    testprog, testprog);
172		assertEmptyFile("test4_out/test4/d1/bar");
173		systemf("%s -cf - -s /d1/d2/ test4/d1/foo | %s -xf - -C test4_out",
174		    testprog, testprog);
175		assertEmptyFile("test4_out/test4/d2/foo");
176		systemf("%s -cf - -s ,test4/d1/foo,, test4/d1/foo | %s -tvf - > test4.lst",
177		    testprog, testprog);
178		assertEmptyFile("test4.lst");
179		systemf("%s -cf - test4/d1/foo | %s -xf - -s /foo/bar/ -C test4_out2",
180		    testprog, testprog);
181		assertEmptyFile("test4_out2/test4/d1/bar");
182	} else {
183		skipping("bsdtar does not support -s option on this platform");
184	}
185
186	/* TODO: Include some use of -C directory-changing within the filelist. */
187	/* I'm pretty sure -C within the filelist is broken on extract. */
188}
189