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$");
30 basic_tar(const char *target, const char *pack_options,
31 const char *unpack_options, const char *flist)
34 #if !defined(_WIN32) || defined(__CYGWIN__)
39 assertEqualInt(0, mkdir(target, 0775));
41 /* Use the tar program to create an archive. */
42 #if !defined(_WIN32) || defined(__CYGWIN__)
43 r = systemf("%s cf - %s `cat %s` >%s/archive 2>%s/pack.err", testprog, pack_options, flist, target, target);
45 r = systemf("%s cf - %s %s >%s/archive 2>%s/pack.err", testprog, pack_options, flist, target, target);
47 failure("Error invoking %s cf -", testprog, pack_options);
52 /* Verify that nothing went to stderr. */
53 assertEmptyFile("pack.err");
56 * Use tar to unpack the archive into another directory.
58 r = systemf("%s xf archive %s >unpack.out 2>unpack.err", testprog, unpack_options);
59 failure("Error invoking %s xf archive %s", testprog, unpack_options);
62 /* Verify that nothing went to stderr. */
63 assertEmptyFile("unpack.err");
66 * Verify unpacked files.
69 /* Regular file with 2 links. */
70 r = lstat("file", &st);
71 failure("Failed to stat file %s/file, errno=%d", target, errno);
74 assert(S_ISREG(st.st_mode));
75 #if !defined(_WIN32) || defined(__CYGWIN__)
76 assertEqualInt(0644, st.st_mode & 0777);
78 assertEqualInt(0600, st.st_mode & 0700);
80 assertEqualInt(10, st.st_size);
81 failure("file %s/file", target);
82 assertEqualInt(2, st.st_nlink);
85 /* Another name for the same file. */
86 r = lstat("linkfile", &st2);
87 failure("Failed to stat file %s/linkfile, errno=%d", target, errno);
90 assert(S_ISREG(st2.st_mode));
91 #if !defined(_WIN32) || defined(__CYGWIN__)
92 assertEqualInt(0644, st2.st_mode & 0777);
94 assertEqualInt(0600, st2.st_mode & 0700);
96 assertEqualInt(10, st2.st_size);
97 failure("file %s/linkfile", target);
98 assertEqualInt(2, st2.st_nlink);
99 /* Verify that the two are really hardlinked. */
100 assertEqualInt(st.st_dev, st2.st_dev);
101 failure("%s/linkfile and %s/file aren't really hardlinks", target, target);
102 assertEqualInt(st.st_ino, st2.st_ino);
105 #if !defined(_WIN32) || defined(__CYGWIN__)
107 r = lstat("symlink", &st);
108 failure("Failed to stat file %s/symlink, errno=%d", target, errno);
109 assertEqualInt(r, 0);
111 failure("symlink should be a symlink; actual mode is %o",
113 assert(S_ISLNK(st.st_mode));
114 if (S_ISLNK(st.st_mode)) {
115 r = readlink("symlink", buff, sizeof(buff));
116 assertEqualInt(r, 4);
118 assertEqualString(buff, "file");
124 r = lstat("dir", &st);
126 assertEqualInt(r, 0);
127 assert(S_ISDIR(st.st_mode));
128 #if !defined(_WIN32) || defined(__CYGWIN__)
129 assertEqualInt(0775, st.st_mode & 0777);
131 assertEqualInt(0700, st.st_mode & 0700);
138 DEFINE_TEST(test_basic)
148 * Create an assortment of files on disk.
150 filelist = open("filelist", O_CREAT | O_WRONLY, 0644);
152 /* File with 10 bytes content. */
153 fd = open("file", O_CREAT | O_WRONLY, 0644);
155 assertEqualInt(10, write(fd, "123456789", 10));
157 write(filelist, "file\n", 5);
159 /* hardlink to above file. */
160 assertEqualInt(0, link("file", "linkfile"));
161 write(filelist, "linkfile\n", 9);
163 /* Symlink to above file. */
164 assertEqualInt(0, symlink("file", "symlink"));
165 write(filelist, "symlink\n", 8);
168 assertEqualInt(0, mkdir("dir", 0775));
169 write(filelist, "dir\n", 4);
173 #if !defined(_WIN32) || defined(__CYGWIN__)
176 flist = "file linkfile symlink dir";
178 /* Archive/dearchive with a variety of options. */
179 basic_tar("copy", "", "", flist);
180 /* tar doesn't handle cpio symlinks correctly */
181 /* basic_tar("copy_odc", "--format=odc", ""); */
182 basic_tar("copy_ustar", "--format=ustar", "", flist);