]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/tar/test/test_symlink_dir.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.bin / tar / test / test_symlink_dir.c
1 /*-
2  * Copyright (c) 2003-2007 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 /*
29  * tar -x -P should follow existing symlinks for dirs, but not other
30  * content.  Plain tar -x should remove symlinks when they're in the
31  * way of a dir extraction.
32  */
33
34 static int
35 mkfile(const char *name, int mode, const char *contents, ssize_t size)
36 {
37         int fd = open(name, O_CREAT | O_WRONLY, mode);
38         if (fd < 0)
39                 return (-1);
40         if (size != write(fd, contents, size)) {
41                 close(fd);
42                 return (-1);
43         }
44         close(fd);
45         return (0);
46 }
47
48 DEFINE_TEST(test_symlink_dir)
49 {
50         struct stat st;
51 #if !defined(_WIN32) || defined(__CYGWIN__)
52         struct stat st2;
53 #endif
54         int oldumask;
55
56         oldumask = umask(0);
57
58         assertEqualInt(0, mkdir("source", 0755));
59         assertEqualInt(0, mkfile("source/file", 0755, "a", 1));
60         assertEqualInt(0, mkfile("source/file2", 0755, "ab", 2));
61         assertEqualInt(0, mkdir("source/dir", 0755));
62         assertEqualInt(0, mkdir("source/dir/d", 0755));
63         assertEqualInt(0, mkfile("source/dir/f", 0755, "abc", 3));
64         assertEqualInt(0, mkdir("source/dir2", 0755));
65         assertEqualInt(0, mkdir("source/dir2/d2", 0755));
66         assertEqualInt(0, mkfile("source/dir2/f2", 0755, "abcd", 4));
67         assertEqualInt(0, mkdir("source/dir3", 0755));
68         assertEqualInt(0, mkdir("source/dir3/d3", 0755));
69         assertEqualInt(0, mkfile("source/dir3/f3", 0755, "abcde", 5));
70
71         assertEqualInt(0,
72             systemf("%s -cf test.tar -C source dir dir2 dir3 file file2",
73                 testprog));
74
75         /*
76          * Extract with -x and without -P.
77          */
78         assertEqualInt(0, mkdir("dest1", 0755));
79         /* "dir" is a symlink to an existing "real_dir" */
80         assertEqualInt(0, mkdir("dest1/real_dir", 0755));
81 #if !defined(_WIN32) || defined(__CYGWIN__)
82         assertEqualInt(0, symlink("real_dir", "dest1/dir"));
83         /* "dir2" is a symlink to a non-existing "real_dir2" */
84         assertEqualInt(0, symlink("real_dir2", "dest1/dir2"));
85 #else
86         skipping("symlink does not work on this platform");
87 #endif
88         /* "dir3" is a symlink to an existing "non_dir3" */
89         assertEqualInt(0, mkfile("dest1/non_dir3", 0755, "abcdef", 6));
90         assertEqualInt(0, symlink("non_dir3", "dest1/dir3"));
91         /* "file" is a symlink to existing "real_file" */
92         assertEqualInt(0, mkfile("dest1/real_file", 0755, "abcdefg", 7));
93         assertEqualInt(0, symlink("real_file", "dest1/file"));
94 #if !defined(_WIN32) || defined(__CYGWIN__)
95         /* "file2" is a symlink to non-existing "real_file2" */
96         assertEqualInt(0, symlink("real_file2", "dest1/file2"));
97 #else
98         skipping("symlink does not work on this platform");
99 #endif
100         assertEqualInt(0, systemf("%s -xf test.tar -C dest1", testprog));
101
102         /* dest1/dir symlink should be removed */
103         assertEqualInt(0, lstat("dest1/dir", &st));
104         failure("symlink to dir was followed when it shouldn't be");
105         assert(S_ISDIR(st.st_mode));
106         /* dest1/dir2 symlink should be removed */
107         assertEqualInt(0, lstat("dest1/dir2", &st));
108         failure("Broken symlink wasn't replaced with dir");
109         assert(S_ISDIR(st.st_mode));
110         /* dest1/dir3 symlink should be removed */
111         assertEqualInt(0, lstat("dest1/dir3", &st));
112         failure("Symlink to non-dir wasn't replaced with dir");
113         assert(S_ISDIR(st.st_mode));
114         /* dest1/file symlink should be removed */
115         assertEqualInt(0, lstat("dest1/file", &st));
116         failure("Symlink to existing file should be removed");
117         assert(S_ISREG(st.st_mode));
118         /* dest1/file2 symlink should be removed */
119         assertEqualInt(0, lstat("dest1/file2", &st));
120         failure("Symlink to non-existing file should be removed");
121         assert(S_ISREG(st.st_mode));
122
123         /*
124          * Extract with both -x and -P
125          */
126         assertEqualInt(0, mkdir("dest2", 0755));
127         /* "dir" is a symlink to existing "real_dir" */
128         assertEqualInt(0, mkdir("dest2/real_dir", 0755));
129 #if !defined(_WIN32) || defined(__CYGWIN__)
130         assertEqualInt(0, symlink("real_dir", "dest2/dir"));
131         /* "dir2" is a symlink to a non-existing "real_dir2" */
132         assertEqualInt(0, symlink("real_dir2", "dest2/dir2"));
133 #else
134         skipping("symlink does not work on this platform");
135 #endif
136         /* "dir3" is a symlink to an existing "non_dir3" */
137         assertEqualInt(0, mkfile("dest2/non_dir3", 0755, "abcdefgh", 8));
138         assertEqualInt(0, symlink("non_dir3", "dest2/dir3"));
139         /* "file" is a symlink to existing "real_file" */
140         assertEqualInt(0, mkfile("dest2/real_file", 0755, "abcdefghi", 9));
141         assertEqualInt(0, symlink("real_file", "dest2/file"));
142 #if !defined(_WIN32) || defined(__CYGWIN__)
143         /* "file2" is a symlink to non-existing "real_file2" */
144         assertEqualInt(0, symlink("real_file2", "dest2/file2"));
145 #else
146         skipping("symlink does not work on this platform");
147 #endif
148         assertEqualInt(0, systemf("%s -xPf test.tar -C dest2", testprog));
149
150         /* dest2/dir symlink should be followed */
151         assertEqualInt(0, lstat("dest2/dir", &st));
152         failure("tar -xP removed symlink instead of following it");
153 #if !defined(_WIN32) || defined(__CYGWIN__)
154         if (assert(S_ISLNK(st.st_mode))) {
155                 /* Only verify what the symlink points to if it
156                  * really is a symlink. */
157                 failure("The symlink should point to a directory");
158                 assertEqualInt(0, stat("dest2/dir", &st));
159                 assert(S_ISDIR(st.st_mode));
160                 failure("The pre-existing directory should still be there");
161                 assertEqualInt(0, lstat("dest2/real_dir", &st2));
162                 assert(S_ISDIR(st2.st_mode));
163                 assertEqualInt(st.st_dev, st2.st_dev);
164                 failure("symlink should still point to the existing directory");
165                 assertEqualInt(st.st_ino, st2.st_ino);
166         }
167 #else
168         skipping("symlink does not work on this platform");
169 #endif
170         /* Contents of 'dir' should be restored */
171         assertEqualInt(0, lstat("dest2/dir/d", &st));
172         assert(S_ISDIR(st.st_mode));
173         assertEqualInt(0, lstat("dest2/dir/f", &st));
174         assert(S_ISREG(st.st_mode));
175         assertEqualInt(3, st.st_size);
176         /* dest2/dir2 symlink should be removed */
177         assertEqualInt(0, lstat("dest2/dir2", &st));
178         failure("Broken symlink wasn't replaced with dir");
179         assert(S_ISDIR(st.st_mode));
180         /* dest2/dir3 symlink should be removed */
181         assertEqualInt(0, lstat("dest2/dir3", &st));
182         failure("Symlink to non-dir wasn't replaced with dir");
183         assert(S_ISDIR(st.st_mode));
184         /* dest2/file symlink should be removed;
185          * even -P shouldn't follow symlinks for files */
186         assertEqualInt(0, lstat("dest2/file", &st));
187         failure("Symlink to existing file should be removed");
188         assert(S_ISREG(st.st_mode));
189         /* dest2/file2 symlink should be removed */
190         assertEqualInt(0, lstat("dest2/file2", &st));
191         failure("Symlink to non-existing file should be removed");
192         assert(S_ISREG(st.st_mode));
193 }