2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2023 Klara, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #define PROGNAME "mktar"
43 #define SUBDIRNAME "directory"
44 #define SPARSEFILENAME "sparse_file"
45 #define HARDLINKNAME "hard_link"
46 #define SHORTLINKNAME "short_link"
47 #define LONGLINKNAME "long_link"
51 static void verbose(const char *fmt, ...)
57 fprintf(stderr, "%s: ", PROGNAME);
59 vfprintf(stderr, fmt, ap);
61 fprintf(stderr, "\n");
65 mksparsefile(const char *filename, mode_t mode)
71 if ((fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, mode)) < 0)
72 err(1, "%s", filename);
73 for (unsigned int i = 33; i <= 126; i++) {
74 memset(buf, i, sizeof(buf));
75 if (lseek(fd, 1048576LU * (i - 32), SEEK_SET) < 0)
76 err(1, "%s", filename);
77 res = write(fd, buf, sizeof(buf));
79 err(1, "%s", filename);
80 if (res != sizeof(buf))
81 errx(1, "%s: short write", filename);
87 mklonglinktarget(const char *dirname, const char *filename)
91 if (asprintf(&piece, "%1$s/../%1$s/../%1$s/../%1$s/../", dirname) < 0)
93 if (asprintf(&target, "%1$s%1$s%1$s%1$s%1$s%1$s%1$s%1$s%2$s", piece, filename) < 0)
104 /* create a subdirectory */
105 verbose("mkdir %s", SUBDIRNAME);
106 if (mkdir(SUBDIRNAME, 0755) != 0)
107 err(1, "%s", SUBDIRNAME);
109 /* create a sparse file */
110 verbose("creating %s", SPARSEFILENAME);
111 mksparsefile(SPARSEFILENAME, 0644);
112 chflags(SPARSEFILENAME, UF_NODUMP);
114 /* create a hard link */
115 verbose("link %s %s", SPARSEFILENAME, HARDLINKNAME);
116 if (link(SPARSEFILENAME, HARDLINKNAME) != 0)
117 err(1, "%s", HARDLINKNAME);
119 /* create a symbolic link with a short target */
120 verbose("symlink %s %s", SPARSEFILENAME, SHORTLINKNAME);
121 if (symlink(SPARSEFILENAME, SHORTLINKNAME) != 0)
122 err(1, "%s", SHORTLINKNAME);
124 /* create a symbolic link with a long target */
125 linktarget = mklonglinktarget(SUBDIRNAME, SPARSEFILENAME);
126 verbose("symlink %s %s", linktarget, LONGLINKNAME);
127 if (symlink(linktarget, LONGLINKNAME) != 0)
128 err(1, "%s", LONGLINKNAME);
136 fprintf(stderr, "usage: %s [-v] tarfile\n", PROGNAME);
141 main(int argc, char *argv[])
143 const char *tarfilename;
148 while ((opt = getopt(argc, argv, "v")) != -1)
164 if (asprintf(&dirname, "%s%s.XXXXXXXX", _PATH_TMP, PROGNAME) < 0)
165 err(1, "asprintf()");
166 if (mkdtemp(dirname) == NULL)
167 err(1, "%s", dirname);
168 verbose("mkdir %s", dirname);
170 /* fork a child to create the files */
171 if ((pid = fork()) < 0)
174 verbose("cd %s", dirname);
175 if (chdir(dirname) != 0)
176 err(1, "%s", dirname);
177 verbose("umask 022");
183 if (waitpid(pid, &wstatus, 0) < 0)
185 if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0)
186 errx(1, "child failed");
188 /* fork a child to create the tarball */
189 if ((pid = fork()) < 0)
192 verbose("creating tarball");
199 "--options", "zstd:frame-per-file",
205 if (waitpid(pid, &wstatus, 0) < 0)
207 if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0)
208 errx(1, "child failed");
210 /* fork a child to delete everything */
211 if ((pid = fork()) < 0)
214 verbose("cd %s", dirname);
215 if (chdir(dirname) != 0)
216 err(1, "%s", dirname);
217 verbose("rm %s", LONGLINKNAME);
218 (void)unlink(LONGLINKNAME);
219 verbose("rm %s", SHORTLINKNAME);
220 (void)unlink(SHORTLINKNAME);
221 verbose("rm %s", HARDLINKNAME);
222 (void)unlink(HARDLINKNAME);
223 verbose("rm %s", SPARSEFILENAME);
224 (void)unlink(SPARSEFILENAME);
225 verbose("rm %s", SUBDIRNAME);
226 (void)rmdir(SUBDIRNAME);
230 if (waitpid(pid, &wstatus, 0) < 0)
232 if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0)
233 errx(1, "child failed");
234 verbose("rmdir %s", dirname);
235 (void)rmdir(dirname);