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 ARCHIVE_VERSION_NUMBER >= 1009000
32 static void create(struct archive_entry *ae, const char *msg)
37 /* Write the entry to disk. */
38 assert((ad = archive_write_disk_new()) != NULL);
40 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
41 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
42 #if ARCHIVE_VERSION_NUMBER < 2000000
43 archive_write_finish(ad);
45 assertEqualInt(0, archive_write_finish(ad));
47 /* Test the entries on disk. */
48 assert(0 == stat(archive_entry_pathname(ae), &st));
49 failure("st.st_mode=%o archive_entry_mode(ae)=%o",
50 st.st_mode, archive_entry_mode(ae));
51 /* When verifying a dir, ignore the S_ISGID bit, as some systems set
52 * that automatically. */
53 if (archive_entry_filetype(ae) == AE_IFDIR)
54 st.st_mode &= ~S_ISGID;
55 assertEqualInt(st.st_mode, archive_entry_mode(ae) & ~UMASK);
58 static void create_reg_file(struct archive_entry *ae, const char *msg)
60 static const char data[]="abcdefghijklmnopqrstuvwxyz";
65 /* Write the entry to disk. */
66 assert((ad = archive_write_disk_new()) != NULL);
67 archive_write_disk_set_options(ad, ARCHIVE_EXTRACT_TIME);
70 * A touchy API design issue: archive_write_data() does (as of
71 * 2.4.12) enforce the entry size as a limit on the data
72 * written to the file. This was not enforced prior to
73 * 2.4.12. The change was prompted by the refined
74 * hardlink-restore semantics introduced at that time. In
75 * short, libarchive needs to know whether a "hardlink entry"
76 * is going to overwrite the contents so that it can know
77 * whether or not to open the file for writing. This implies
78 * that there is a fundamental semantic difference between an
79 * entry with a zero size and one with a non-zero size in the
80 * case of hardlinks and treating the hardlink case
81 * differently from the regular file case is just asking for
82 * trouble. So, a zero size must always mean that no data
83 * will be accepted, which is consistent with the file size in
84 * the entry being a maximum size.
86 archive_entry_set_size(ae, sizeof(data));
87 archive_entry_set_mtime(ae, 123456789, 0);
88 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
89 assertEqualInt(sizeof(data), archive_write_data(ad, data, sizeof(data)));
90 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
91 #if ARCHIVE_VERSION_NUMBER < 2000000
92 archive_write_finish(ad);
94 assertEqualInt(0, archive_write_finish(ad));
96 /* Test the entries on disk. */
97 assert(0 == stat(archive_entry_pathname(ae), &st));
98 failure("st.st_mode=%o archive_entry_mode(ae)=%o",
99 st.st_mode, archive_entry_mode(ae));
100 assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK));
101 assertEqualInt(st.st_size, sizeof(data));
102 /* test_write_disk_times has more detailed tests of this area. */
103 assertEqualInt(st.st_mtime, 123456789);
104 failure("No atime was specified, so atime should get set to current time");
106 assert(st.st_atime <= now && st.st_atime > now - 5);
109 static void create_reg_file2(struct archive_entry *ae, const char *msg)
111 const int datasize = 100000;
118 data = malloc(datasize);
119 for (i = 0; i < datasize; i++)
120 data[i] = (char)(i % 256);
122 /* Write the entry to disk. */
123 assert((ad = archive_write_disk_new()) != NULL);
126 * See above for an explanation why this next call
129 archive_entry_set_size(ae, datasize);
130 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
131 for (i = 0; i < datasize - 999; i += 1000) {
132 assertEqualIntA(ad, ARCHIVE_OK,
133 archive_write_data_block(ad, data + i, 1000, i));
135 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
136 #if ARCHIVE_VERSION_NUMBER < 2000000
137 archive_write_finish(ad);
139 assertEqualInt(0, archive_write_finish(ad));
141 /* Test the entries on disk. */
142 assert(0 == stat(archive_entry_pathname(ae), &st));
143 failure("st.st_mode=%o archive_entry_mode(ae)=%o",
144 st.st_mode, archive_entry_mode(ae));
145 assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK));
146 assertEqualInt(st.st_size, i);
148 compare = malloc(datasize);
149 fd = open(archive_entry_pathname(ae), O_RDONLY);
150 assertEqualInt(datasize, read(fd, compare, datasize));
152 assert(memcmp(compare, data, datasize) == 0);
157 static void create_reg_file3(struct archive_entry *ae, const char *msg)
159 static const char data[]="abcdefghijklmnopqrstuvwxyz";
163 /* Write the entry to disk. */
164 assert((ad = archive_write_disk_new()) != NULL);
166 /* Set the size smaller than the data and verify the truncation. */
167 archive_entry_set_size(ae, 5);
168 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
169 assertEqualInt(5, archive_write_data(ad, data, sizeof(data)));
170 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
171 #if ARCHIVE_VERSION_NUMBER < 2000000
172 archive_write_finish(ad);
174 assertEqualInt(0, archive_write_finish(ad));
176 /* Test the entry on disk. */
177 assert(0 == stat(archive_entry_pathname(ae), &st));
178 failure("st.st_mode=%o archive_entry_mode(ae)=%o",
179 st.st_mode, archive_entry_mode(ae));
180 assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK));
181 assertEqualInt(st.st_size, 5);
185 static void create_reg_file4(struct archive_entry *ae, const char *msg)
187 static const char data[]="abcdefghijklmnopqrstuvwxyz";
191 /* Write the entry to disk. */
192 assert((ad = archive_write_disk_new()) != NULL);
193 /* Leave the size unset. The data should not be truncated. */
194 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
195 assertEqualInt(ARCHIVE_OK,
196 archive_write_data_block(ad, data, sizeof(data), 0));
197 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
198 #if ARCHIVE_VERSION_NUMBER < 2000000
199 archive_write_finish(ad);
201 assertEqualInt(0, archive_write_finish(ad));
203 /* Test the entry on disk. */
204 assert(0 == stat(archive_entry_pathname(ae), &st));
205 failure("st.st_mode=%o archive_entry_mode(ae)=%o",
206 st.st_mode, archive_entry_mode(ae));
207 assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK));
209 assertEqualInt(st.st_size, sizeof(data));
213 DEFINE_TEST(test_write_disk)
215 #if ARCHIVE_VERSION_NUMBER < 1009000
216 skipping("archive_write_disk interface");
218 struct archive_entry *ae;
220 /* Force the umask to something predictable. */
223 /* A regular file. */
224 assert((ae = archive_entry_new()) != NULL);
225 archive_entry_copy_pathname(ae, "file");
226 archive_entry_set_mode(ae, S_IFREG | 0755);
227 create_reg_file(ae, "Test creating a regular file");
228 archive_entry_free(ae);
230 /* Another regular file. */
231 assert((ae = archive_entry_new()) != NULL);
232 archive_entry_copy_pathname(ae, "file2");
233 archive_entry_set_mode(ae, S_IFREG | 0755);
234 create_reg_file2(ae, "Test creating another regular file");
235 archive_entry_free(ae);
237 /* A regular file with a size restriction */
238 assert((ae = archive_entry_new()) != NULL);
239 archive_entry_copy_pathname(ae, "file3");
240 archive_entry_set_mode(ae, S_IFREG | 0755);
241 create_reg_file3(ae, "Regular file with size restriction");
242 archive_entry_free(ae);
244 /* A regular file with an unspecified size */
245 assert((ae = archive_entry_new()) != NULL);
246 archive_entry_copy_pathname(ae, "file3");
247 archive_entry_set_mode(ae, S_IFREG | 0755);
248 create_reg_file4(ae, "Regular file with unspecified size");
249 archive_entry_free(ae);
251 /* A regular file over an existing file */
252 assert((ae = archive_entry_new()) != NULL);
253 archive_entry_copy_pathname(ae, "file");
254 archive_entry_set_mode(ae, S_IFREG | 0724);
255 create(ae, "Test creating a file over an existing file.");
256 archive_entry_free(ae);
259 assert((ae = archive_entry_new()) != NULL);
260 archive_entry_copy_pathname(ae, "dir");
261 archive_entry_set_mode(ae, S_IFDIR | 0555);
262 create(ae, "Test creating a regular dir.");
263 archive_entry_free(ae);
265 /* A directory over an existing file. */
266 assert((ae = archive_entry_new()) != NULL);
267 archive_entry_copy_pathname(ae, "file");
268 archive_entry_set_mode(ae, S_IFDIR | 0742);
269 create(ae, "Test creating a dir over an existing file.");
270 archive_entry_free(ae);
272 /* A file over an existing dir. */
273 assert((ae = archive_entry_new()) != NULL);
274 archive_entry_copy_pathname(ae, "file");
275 archive_entry_set_mode(ae, S_IFREG | 0744);
276 create(ae, "Test creating a file over an existing dir.");
277 archive_entry_free(ae);