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$");
28 #if defined(_WIN32) && !defined(__CYGWIN__)
29 /* Execution bits, Group members bits and others bits do not work. */
31 #define E_MASK (~0177)
38 * Exercise hardlink recreation.
40 * File permissions are chosen so that the authoritative entry
41 * has the correct permission and the non-authoritative versions
42 * are just writeable files.
44 DEFINE_TEST(test_write_disk_hardlink)
46 #if defined(__HAIKU__)
47 skipping("archive_write_disk_hardlink; hardlinks are not supported on bfs");
49 static const char data[]="abcdefghijklmnopqrstuvwxyz";
51 struct archive_entry *ae;
54 /* Force the umask to something predictable. */
57 /* Write entries to disk. */
58 assert((ad = archive_write_disk_new()) != NULL);
61 * First, use a tar-like approach; a regular file, then
62 * a separate "hardlink" entry.
66 assert((ae = archive_entry_new()) != NULL);
67 archive_entry_copy_pathname(ae, "link1a");
68 archive_entry_set_mode(ae, S_IFREG | 0755);
69 archive_entry_set_size(ae, sizeof(data));
70 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
71 assertEqualInt(sizeof(data),
72 archive_write_data(ad, data, sizeof(data)));
73 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
74 archive_entry_free(ae);
76 /* Link. Size of zero means this doesn't carry data. */
77 assert((ae = archive_entry_new()) != NULL);
78 archive_entry_copy_pathname(ae, "link1b");
79 archive_entry_set_mode(ae, S_IFREG | 0642);
80 archive_entry_set_size(ae, 0);
81 archive_entry_copy_hardlink(ae, "link1a");
82 assertEqualIntA(ad, 0, r = archive_write_header(ad, ae));
83 if (r >= ARCHIVE_WARN) {
84 assertEqualInt(ARCHIVE_WARN,
85 archive_write_data(ad, data, sizeof(data)));
86 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
88 archive_entry_free(ae);
91 * Repeat tar approach test, but use unset to mark the
92 * hardlink as having no data.
96 assert((ae = archive_entry_new()) != NULL);
97 archive_entry_copy_pathname(ae, "link2a");
98 archive_entry_set_mode(ae, S_IFREG | 0755);
99 archive_entry_set_size(ae, sizeof(data));
100 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
101 assertEqualInt(sizeof(data),
102 archive_write_data(ad, data, sizeof(data)));
103 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
104 archive_entry_free(ae);
106 /* Link. Unset size means this doesn't carry data. */
107 assert((ae = archive_entry_new()) != NULL);
108 archive_entry_copy_pathname(ae, "link2b");
109 archive_entry_set_mode(ae, S_IFREG | 0642);
110 archive_entry_unset_size(ae);
111 archive_entry_copy_hardlink(ae, "link2a");
112 assertEqualIntA(ad, 0, r = archive_write_header(ad, ae));
113 if (r >= ARCHIVE_WARN) {
114 assertEqualInt(ARCHIVE_WARN,
115 archive_write_data(ad, data, sizeof(data)));
116 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
118 archive_entry_free(ae);
121 * Second, try an old-cpio-like approach; a regular file, then
122 * another identical one (which has been marked hardlink).
126 assert((ae = archive_entry_new()) != NULL);
127 archive_entry_copy_pathname(ae, "link3a");
128 archive_entry_set_mode(ae, S_IFREG | 0600);
129 archive_entry_set_size(ae, sizeof(data));
130 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
131 assertEqualInt(sizeof(data), archive_write_data(ad, data, sizeof(data)));
132 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
133 archive_entry_free(ae);
136 assert((ae = archive_entry_new()) != NULL);
137 archive_entry_copy_pathname(ae, "link3b");
138 archive_entry_set_mode(ae, S_IFREG | 0755);
139 archive_entry_set_size(ae, sizeof(data));
140 archive_entry_copy_hardlink(ae, "link3a");
141 assertEqualIntA(ad, 0, r = archive_write_header(ad, ae));
142 if (r > ARCHIVE_WARN) {
143 assertEqualInt(sizeof(data),
144 archive_write_data(ad, data, sizeof(data)));
145 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
147 archive_entry_free(ae);
150 * Finally, try a new-cpio-like approach, where the initial
151 * regular file is empty and the hardlink has the data.
155 assert((ae = archive_entry_new()) != NULL);
156 archive_entry_copy_pathname(ae, "link4a");
157 archive_entry_set_mode(ae, S_IFREG | 0600);
158 archive_entry_set_size(ae, 0);
159 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
160 assertEqualInt(ARCHIVE_WARN, archive_write_data(ad, data, 1));
161 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
162 archive_entry_free(ae);
165 assert((ae = archive_entry_new()) != NULL);
166 archive_entry_copy_pathname(ae, "link4b");
167 archive_entry_set_mode(ae, S_IFREG | 0755);
168 archive_entry_set_size(ae, sizeof(data));
169 archive_entry_copy_hardlink(ae, "link4a");
170 assertEqualIntA(ad, 0, r = archive_write_header(ad, ae));
171 if (r > ARCHIVE_FAILED) {
172 assertEqualInt(sizeof(data),
173 archive_write_data(ad, data, sizeof(data)));
174 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
176 archive_entry_free(ae);
177 assertEqualInt(0, archive_write_free(ad));
179 /* Test the entries on disk. */
182 /* If the hardlink was successfully created and the archive
183 * doesn't carry data for it, we consider it to be
184 * non-authoritative for meta data as well. This is consistent
185 * with GNU tar and BSD pax. */
186 assertIsReg("link1a", 0755 & ~UMASK);
187 assertFileSize("link1a", sizeof(data));
188 assertFileNLinks("link1a", 2);
189 assertIsHardlink("link1a", "link1b");
191 /* Test #2: Should produce identical results to test #1 */
192 /* Note that marking a hardlink with size = 0 is treated the
193 * same as having an unset size. This is partly for backwards
194 * compatibility (we used to not have unset tracking, so
195 * relied on size == 0) and partly to match the model used by
196 * common file formats that store a size of zero for
198 assertIsReg("link2a", 0755 & ~UMASK);
199 assertFileSize("link2a", sizeof(data));
200 assertFileNLinks("link2a", 2);
201 assertIsHardlink("link2a", "link2b");
204 assertIsReg("link3a", 0755 & ~UMASK);
205 assertFileSize("link3a", sizeof(data));
206 assertFileNLinks("link3a", 2);
207 assertIsHardlink("link3a", "link3b");
210 assertIsReg("link4a", 0755 & ~UMASK);
211 assertFileNLinks("link4a", 2);
212 assertFileSize("link4a", sizeof(data));
213 assertIsHardlink("link4a", "link4b");