]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.bin/tar/test/test_option_T.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.bin / tar / test / test_option_T.c
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$");
27
28 static int
29 touch(const char *fn)
30 {
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 */
38 }
39
40 DEFINE_TEST(test_option_T)
41 {
42         FILE *f;
43         int r;
44
45         /* Create a simple dir heirarchy; bail if anything fails. */
46         if (!assertEqualInt(0, mkdir("d1", 0755))) return;
47         if (!assertEqualInt(0, mkdir("d1/d2", 0755)))   return;
48         if (!touch("d1/f1")) return;
49         if (!touch("d1/f2")) return;
50         if (!touch("d1/d2/f3")) return;
51         if (!touch("d1/d2/f4")) return;
52         if (!touch("d1/d2/f5")) return;
53
54         /* Populate a file list */
55         f = fopen("filelist", "w+");
56         if (!assert(f != NULL))
57                 return;
58         fprintf(f, "d1/f1\n");
59         fprintf(f, "d1/d2/f4\n");
60         fclose(f);
61
62         /* Populate a second file list */
63         f = fopen("filelist2", "w+");
64         if (!assert(f != NULL))
65                 return;
66         fprintf(f, "d1/d2/f3\n");
67         fprintf(f, "d1/d2/f5\n");
68         fclose(f);
69
70         /* Use -c -T to archive up the files. */
71         r = systemf("%s -c -f test1.tar -T filelist > test1.out 2> test1.err",
72             testprog);
73         failure("Failure here probably means that tar can't archive zero-length files without reading them");
74         assert(r == 0);
75         assertEmptyFile("test1.out");
76         assertEmptyFile("test1.err");
77
78         /* Use -x -T to dearchive the files */
79         if (!assertEqualInt(0, mkdir("test1", 0755))) return;
80         systemf("%s -x -f test1.tar -T filelist -C test1"
81             " > test1b.out 2> test1b.err", testprog);
82         assertEmptyFile("test1b.out");
83         assertEmptyFile("test1b.err");
84
85         /* Verify the files were extracted. */
86         assertFileExists("test1/d1/f1");
87         assertFileNotExists("test1/d1/f2");
88         assertFileNotExists("test1/d1/d2/f3");
89         assertFileExists("test1/d1/d2/f4");
90         assertFileNotExists("test1/d1/d2/f5");
91
92         /* Use -r -T to add more files to the archive. */
93         systemf("%s -r -f test1.tar -T filelist2 > test2.out 2> test2.err",
94             testprog);
95         assertEmptyFile("test2.out");
96         assertEmptyFile("test2.err");
97
98         /* Use -x without -T to dearchive the files (ensure -r worked) */
99         if (!assertEqualInt(0, mkdir("test3", 0755))) return;
100         systemf("%s -x -f test1.tar -C test3"
101             " > test3.out 2> test3.err", testprog);
102         assertEmptyFile("test3.out");
103         assertEmptyFile("test3.err");
104         /* Verify the files were extracted.*/
105         assertFileExists("test3/d1/f1");
106         assertFileNotExists("test3/d1/f2");
107         assertFileExists("test3/d1/d2/f3");
108         assertFileExists("test3/d1/d2/f4");
109         assertFileExists("test3/d1/d2/f5");
110
111         /* Use -x -T to dearchive the files (verify -x -T together) */
112         if (!assertEqualInt(0, mkdir("test2", 0755))) return;
113         systemf("%s -x -f test1.tar -T filelist -C test2"
114             " > test2b.out 2> test2b.err", testprog);
115         assertEmptyFile("test2b.out");
116         assertEmptyFile("test2b.err");
117         /* Verify the files were extracted.*/
118         assertFileExists("test2/d1/f1");
119         assertFileNotExists("test2/d1/f2");
120         assertFileNotExists("test2/d1/d2/f3");
121         assertFileExists("test2/d1/d2/f4");
122         assertFileNotExists("test2/d1/d2/f5");
123
124         assertEqualInt(0, mkdir("test4", 0755));
125         assertEqualInt(0, mkdir("test4_out", 0755));
126         assertEqualInt(0, mkdir("test4_out2", 0755));
127         assertEqualInt(0, mkdir("test4/d1", 0755));
128         assertEqualInt(1, touch("test4/d1/foo"));
129
130         systemf("%s -cf - -s /foo/bar/ test4/d1/foo | %s -xf - -C test4_out",
131             testprog, testprog);
132         assertEmptyFile("test4_out/test4/d1/bar");
133         systemf("%s -cf - -s /d1/d2/ test4/d1/foo | %s -xf - -C test4_out",
134             testprog, testprog);
135         assertEmptyFile("test4_out/test4/d2/foo");
136         systemf("%s -cf - -s ,test4/d1/foo,, test4/d1/foo | %s -tvf - > test4.lst",
137             testprog, testprog);
138         assertEmptyFile("test4.lst");
139         systemf("%s -cf - test4/d1/foo | %s -xf - -s /foo/bar/ -C test4_out2",
140             testprog, testprog);
141         assertEmptyFile("test4_out2/test4/d1/bar");
142
143         /* TODO: Include some use of -C directory-changing within the filelist. */
144         /* I'm pretty sure -C within the filelist is broken on extract. */
145 }