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