2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
26 __FBSDID("$FreeBSD$");
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.
35 mkfile(const char *name, int mode, const char *contents, ssize_t size)
37 int fd = open(name, O_CREAT | O_WRONLY, mode);
40 if (size != write(fd, contents, size)) {
48 DEFINE_TEST(test_symlink_dir)
55 assertEqualInt(0, mkdir("source", 0755));
56 assertEqualInt(0, mkfile("source/file", 0755, "a", 1));
57 assertEqualInt(0, mkfile("source/file2", 0755, "ab", 2));
58 assertEqualInt(0, mkdir("source/dir", 0755));
59 assertEqualInt(0, mkdir("source/dir/d", 0755));
60 assertEqualInt(0, mkfile("source/dir/f", 0755, "abc", 3));
61 assertEqualInt(0, mkdir("source/dir2", 0755));
62 assertEqualInt(0, mkdir("source/dir2/d2", 0755));
63 assertEqualInt(0, mkfile("source/dir2/f2", 0755, "abcd", 4));
64 assertEqualInt(0, mkdir("source/dir3", 0755));
65 assertEqualInt(0, mkdir("source/dir3/d3", 0755));
66 assertEqualInt(0, mkfile("source/dir3/f3", 0755, "abcde", 5));
69 systemf("%s -cf test.tar -C source dir dir2 dir3 file file2",
73 * Extract with -x and without -P.
75 assertEqualInt(0, mkdir("dest1", 0755));
76 /* "dir" is a symlink to an existing "real_dir" */
77 assertEqualInt(0, mkdir("dest1/real_dir", 0755));
78 assertEqualInt(0, symlink("real_dir", "dest1/dir"));
79 /* "dir2" is a symlink to a non-existing "real_dir2" */
80 assertEqualInt(0, symlink("real_dir2", "dest1/dir2"));
81 /* "dir3" is a symlink to an existing "non_dir3" */
82 assertEqualInt(0, mkfile("dest1/non_dir3", 0755, "abcdef", 6));
83 assertEqualInt(0, symlink("non_dir3", "dest1/dir3"));
84 /* "file" is a symlink to existing "real_file" */
85 assertEqualInt(0, mkfile("dest1/real_file", 0755, "abcdefg", 7));
86 assertEqualInt(0, symlink("real_file", "dest1/file"));
87 /* "file2" is a symlink to non-existing "real_file2" */
88 assertEqualInt(0, symlink("real_file2", "dest1/file2"));
90 assertEqualInt(0, systemf("%s -xf test.tar -C dest1", testprog));
92 /* dest1/dir symlink should be removed */
93 assertEqualInt(0, lstat("dest1/dir", &st));
94 failure("symlink to dir was followed when it shouldn't be");
95 assert(S_ISDIR(st.st_mode));
96 /* dest1/dir2 symlink should be removed */
97 assertEqualInt(0, lstat("dest1/dir2", &st));
98 failure("Broken symlink wasn't replaced with dir");
99 assert(S_ISDIR(st.st_mode));
100 /* dest1/dir3 symlink should be removed */
101 assertEqualInt(0, lstat("dest1/dir3", &st));
102 failure("Symlink to non-dir wasn't replaced with dir");
103 assert(S_ISDIR(st.st_mode));
104 /* dest1/file symlink should be removed */
105 assertEqualInt(0, lstat("dest1/file", &st));
106 failure("Symlink to existing file should be removed");
107 assert(S_ISREG(st.st_mode));
108 /* dest1/file2 symlink should be removed */
109 assertEqualInt(0, lstat("dest1/file2", &st));
110 failure("Symlink to non-existing file should be removed");
111 assert(S_ISREG(st.st_mode));
114 * Extract with both -x and -P
116 assertEqualInt(0, mkdir("dest2", 0755));
117 /* "dir" is a symlink to existing "real_dir" */
118 assertEqualInt(0, mkdir("dest2/real_dir", 0755));
119 assertEqualInt(0, symlink("real_dir", "dest2/dir"));
120 /* "dir2" is a symlink to a non-existing "real_dir2" */
121 assertEqualInt(0, symlink("real_dir2", "dest2/dir2"));
122 /* "dir3" is a symlink to an existing "non_dir3" */
123 assertEqualInt(0, mkfile("dest2/non_dir3", 0755, "abcdefgh", 8));
124 assertEqualInt(0, symlink("non_dir3", "dest2/dir3"));
125 /* "file" is a symlink to existing "real_file" */
126 assertEqualInt(0, mkfile("dest2/real_file", 0755, "abcdefghi", 9));
127 assertEqualInt(0, symlink("real_file", "dest2/file"));
128 /* "file2" is a symlink to non-existing "real_file2" */
129 assertEqualInt(0, symlink("real_file2", "dest2/file2"));
131 assertEqualInt(0, systemf("%s -xPf test.tar -C dest2", testprog));
133 /* dest2/dir symlink should be followed */
134 assertEqualInt(0, lstat("dest2/dir", &st));
135 failure("tar -xP removed symlink instead of following it");
136 if (assert(S_ISLNK(st.st_mode))) {
137 /* Only verify what the symlink points to if it
138 * really is a symlink. */
139 failure("The symlink should point to a directory");
140 assertEqualInt(0, stat("dest2/dir", &st));
141 assert(S_ISDIR(st.st_mode));
142 failure("The pre-existing directory should still be there");
143 assertEqualInt(0, lstat("dest2/real_dir", &st2));
144 assert(S_ISDIR(st2.st_mode));
145 assertEqualInt(st.st_dev, st2.st_dev);
146 failure("symlink should still point to the existing directory");
147 assertEqualInt(st.st_ino, st2.st_ino);
149 /* Contents of 'dir' should be restored */
150 assertEqualInt(0, lstat("dest2/dir/d", &st));
151 assert(S_ISDIR(st.st_mode));
152 assertEqualInt(0, lstat("dest2/dir/f", &st));
153 assert(S_ISREG(st.st_mode));
154 assertEqualInt(3, st.st_size);
155 /* dest2/dir2 symlink should be removed */
156 assertEqualInt(0, lstat("dest2/dir2", &st));
157 failure("Broken symlink wasn't replaced with dir");
158 assert(S_ISDIR(st.st_mode));
159 /* dest2/dir3 symlink should be removed */
160 assertEqualInt(0, lstat("dest2/dir3", &st));
161 failure("Symlink to non-dir wasn't replaced with dir");
162 assert(S_ISDIR(st.st_mode));
163 /* dest2/file symlink should be removed;
164 * even -P shouldn't follow symlinks for files */
165 assertEqualInt(0, lstat("dest2/file", &st));
166 failure("Symlink to existing file should be removed");
167 assert(S_ISREG(st.st_mode));
168 /* dest2/file2 symlink should be removed */
169 assertEqualInt(0, lstat("dest2/file2", &st));
170 failure("Symlink to non-existing file should be removed");
171 assert(S_ISREG(st.st_mode));