xref: /freebsd/contrib/libarchive/tar/test/test_symlink_dir.c (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
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 DEFINE_TEST(test_symlink_dir)
35 {
36 	assertUmask(0);
37 
38 	assertMakeDir("source", 0755);
39 	assertMakeFile("source/file", 0755, "a");
40 	assertMakeFile("source/file2", 0755, "ab");
41 	assertMakeDir("source/dir", 0755);
42 	assertMakeDir("source/dir/d", 0755);
43 	assertMakeFile("source/dir/f", 0755, "abc");
44 	assertMakeDir("source/dir2", 0755);
45 	assertMakeDir("source/dir2/d2", 0755);
46 	assertMakeFile("source/dir2/f2", 0755, "abcd");
47 	assertMakeDir("source/dir3", 0755);
48 	assertMakeDir("source/dir3/d3", 0755);
49 	assertMakeFile("source/dir3/f3", 0755, "abcde");
50 	assertMakeDir("source/dir4", 0755);
51 	assertMakeFile("source/dir4/file3", 0755, "abcdef");
52 	assertMakeHardlink("source/dir4/file4", "source/dir4/file3");
53 
54 	assertEqualInt(0,
55 	    systemf("%s -cf test.tar -C source dir dir2 dir3 file file2",
56 		testprog));
57 
58 	/* Second archive with hardlinks */
59 	assertEqualInt(0,
60 	    systemf("%s -cf test2.tar -C source dir4", testprog));
61 
62 	/*
63 	 * Extract with -x and without -P.
64 	 */
65 	assertMakeDir("dest1", 0755);
66 	/* "dir" is a symlink to an existing "dest1/real_dir" */
67 	assertMakeDir("dest1/real_dir", 0755);
68 	if (canSymlink()) {
69 		assertMakeSymlink("dest1/dir", "real_dir", 1);
70 		/* "dir2" is a symlink to a non-existing "real_dir2" */
71 		assertMakeSymlink("dest1/dir2", "real_dir2", 1);
72 	} else {
73 		skipping("Symlinks are not supported on this platform");
74 	}
75 	/* "dir3" is a symlink to an existing "non_dir3" */
76 	assertMakeFile("dest1/non_dir3", 0755, "abcdef");
77 	if (canSymlink())
78 		assertMakeSymlink("dest1/dir3", "non_dir3", 1);
79 	/* "file" is a symlink to existing "real_file" */
80 	assertMakeFile("dest1/real_file", 0755, "abcdefg");
81 	if (canSymlink()) {
82 		assertMakeSymlink("dest1/file", "real_file", 0);
83 		/* "file2" is a symlink to non-existing "real_file2" */
84 		assertMakeSymlink("dest1/file2", "real_file2", 0);
85 	}
86 	assertEqualInt(0, systemf("%s -xf test.tar -C dest1", testprog));
87 
88 	/* dest1/dir symlink should be replaced */
89 	failure("symlink to dir was followed when it shouldn't be");
90 	assertIsDir("dest1/dir", -1);
91 	/* dest1/dir2 symlink should be replaced */
92 	failure("Broken symlink wasn't replaced with dir");
93 	assertIsDir("dest1/dir2", -1);
94 	/* dest1/dir3 symlink should be replaced */
95 	failure("Symlink to non-dir wasn't replaced with dir");
96 	assertIsDir("dest1/dir3", -1);
97 	/* dest1/file symlink should be replaced */
98 	failure("Symlink to existing file should be replaced");
99 	assertIsReg("dest1/file", -1);
100 	/* dest1/file2 symlink should be replaced */
101 	failure("Symlink to non-existing file should be replaced");
102 	assertIsReg("dest1/file2", -1);
103 
104 	/*
105 	 * Extract with both -x and -P
106 	 */
107 	assertMakeDir("dest2", 0755);
108 	/* "dir" is a symlink to existing "real_dir" */
109 	assertMakeDir("dest2/real_dir", 0755);
110 	if (canSymlink())
111 		assertMakeSymlink("dest2/dir", "real_dir", 1);
112 	/* "dir2" is a symlink to a non-existing "real_dir2" */
113 	if (canSymlink())
114 		assertMakeSymlink("dest2/dir2", "real_dir2", 1);
115 	/* "dir3" is a symlink to an existing "non_dir3" */
116 	assertMakeFile("dest2/non_dir3", 0755, "abcdefgh");
117 	if (canSymlink())
118 		assertMakeSymlink("dest2/dir3", "non_dir3", 1);
119 	/* "file" is a symlink to existing "real_file" */
120 	assertMakeFile("dest2/real_file", 0755, "abcdefghi");
121 	if (canSymlink())
122 		assertMakeSymlink("dest2/file", "real_file", 0);
123 	/* "file2" is a symlink to non-existing "real_file2" */
124 	if (canSymlink())
125 		assertMakeSymlink("dest2/file2", "real_file2", 0);
126 	assertEqualInt(0, systemf("%s -xPf test.tar -C dest2", testprog));
127 
128 	/* "dir4" is a symlink to existing "real_dir" */
129 	if (canSymlink())
130 		assertMakeSymlink("dest2/dir4", "real_dir", 1);
131 	assertEqualInt(0, systemf("%s -xPf test2.tar -C dest2", testprog));
132 
133 	/* dest2/dir and dest2/dir4 symlinks should be followed */
134 	if (canSymlink()) {
135 		assertIsSymlink("dest2/dir", "real_dir", 1);
136 		assertIsSymlink("dest2/dir4", "real_dir", 1);
137 		assertIsDir("dest2/real_dir", -1);
138 	}
139 
140 	/* Contents of 'dir' should be restored */
141 	assertIsDir("dest2/dir/d", -1);
142 	assertIsReg("dest2/dir/f", -1);
143 	assertFileSize("dest2/dir/f", 3);
144 	/* dest2/dir2 symlink should be removed */
145 	failure("Broken symlink wasn't replaced with dir");
146 	assertIsDir("dest2/dir2", -1);
147 	/* dest2/dir3 symlink should be removed */
148 	failure("Symlink to non-dir wasn't replaced with dir");
149 	assertIsDir("dest2/dir3", -1);
150 	/* dest2/file symlink should be removed;
151 	 * even -P shouldn't follow symlinks for files */
152 	failure("Symlink to existing file should be removed");
153 	assertIsReg("dest2/file", -1);
154 	/* dest2/file2 symlink should be removed */
155 	failure("Symlink to non-existing file should be removed");
156 	assertIsReg("dest2/file2", -1);
157 
158 	/* dest2/dir4/file3 and dest2/dir4/file4 should be hard links */
159 	assertIsHardlink("dest2/dir4/file3", "dest2/dir4/file4");
160 }
161