Changeset View
Changeset View
Standalone View
Standalone View
tests/sys/fs/tarfs/mktar.c
Show All 35 Lines | |||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#define PROGNAME "mktar" | #define PROGNAME "mktar" | ||||
#define SUBDIRNAME "directory" | #define SUBDIRNAME "directory" | ||||
#define EMPTYDIRNAME "empty" | |||||
#define NORMALFILENAME "file" | |||||
#define SPARSEFILENAME "sparse_file" | #define SPARSEFILENAME "sparse_file" | ||||
#define HARDLINKNAME "hard_link" | #define HARDLINKNAME "hard_link" | ||||
#define SHORTLINKNAME "short_link" | #define SHORTLINKNAME "short_link" | ||||
#define LONGLINKNAME "long_link" | #define LONGLINKNAME "long_link" | ||||
static bool opt_v; | static bool opt_v; | ||||
static void verbose(const char *fmt, ...) | static void verbose(const char *fmt, ...) | ||||
{ | { | ||||
va_list ap; | va_list ap; | ||||
if (!opt_v) | if (!opt_v) | ||||
return; | return; | ||||
fprintf(stderr, "%s: ", PROGNAME); | fprintf(stderr, "%s: ", PROGNAME); | ||||
va_start(ap, fmt); | va_start(ap, fmt); | ||||
vfprintf(stderr, fmt, ap); | vfprintf(stderr, fmt, ap); | ||||
va_end(ap); | va_end(ap); | ||||
fprintf(stderr, "\n"); | fprintf(stderr, "\n"); | ||||
} | } | ||||
static void | static void | ||||
mknormalfile(const char *filename, mode_t mode) | |||||
{ | |||||
char buf[512]; | |||||
ssize_t res; | |||||
int fd; | |||||
if ((fd = open(filename, O_RDWR|O_CREAT|O_EXCL, mode)) < 0) | |||||
err(1, "%s", filename); | |||||
for (unsigned int i = 0; i < sizeof(buf); i++) | |||||
buf[i] = 32 + i % 64; | |||||
res = write(fd, buf, sizeof(buf)); | |||||
if (res < 0) | |||||
err(1, "%s", filename); | |||||
if (res != sizeof(buf)) | |||||
errx(1, "%s: short write", filename); | |||||
close(fd); | |||||
} | |||||
static void | |||||
mksparsefile(const char *filename, mode_t mode) | mksparsefile(const char *filename, mode_t mode) | ||||
{ | { | ||||
char buf[511]; | char buf[511]; | ||||
ssize_t res; | ssize_t res; | ||||
int fd; | int fd; | ||||
if ((fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, mode)) < 0) | if ((fd = open(filename, O_RDWR|O_CREAT|O_EXCL, mode)) < 0) | ||||
err(1, "%s", filename); | err(1, "%s", filename); | ||||
for (unsigned int i = 33; i <= 126; i++) { | for (unsigned int i = 33; i <= 126; i++) { | ||||
memset(buf, i, sizeof(buf)); | memset(buf, i, sizeof(buf)); | ||||
if (lseek(fd, 1048576LU * (i - 32), SEEK_SET) < 0) | if (lseek(fd, 1048576LU * (i - 32), SEEK_SET) < 0) | ||||
err(1, "%s", filename); | err(1, "%s", filename); | ||||
res = write(fd, buf, sizeof(buf)); | res = write(fd, buf, sizeof(buf)); | ||||
if (res < 0) | if (res < 0) | ||||
err(1, "%s", filename); | err(1, "%s", filename); | ||||
Show All 21 Lines | |||||
{ | { | ||||
char *linktarget; | char *linktarget; | ||||
/* create a subdirectory */ | /* create a subdirectory */ | ||||
verbose("mkdir %s", SUBDIRNAME); | verbose("mkdir %s", SUBDIRNAME); | ||||
if (mkdir(SUBDIRNAME, 0755) != 0) | if (mkdir(SUBDIRNAME, 0755) != 0) | ||||
err(1, "%s", SUBDIRNAME); | err(1, "%s", SUBDIRNAME); | ||||
/* create a second subdirectory which will remain empty */ | |||||
verbose("mkdir %s", EMPTYDIRNAME); | |||||
if (mkdir(EMPTYDIRNAME, 0755) != 0) | |||||
err(1, "%s", EMPTYDIRNAME); | |||||
/* create a normal file */ | |||||
verbose("creating %s", NORMALFILENAME); | |||||
mknormalfile(NORMALFILENAME, 0644); | |||||
/* create a sparse file */ | /* create a sparse file */ | ||||
verbose("creating %s", SPARSEFILENAME); | verbose("creating %s", SPARSEFILENAME); | ||||
mksparsefile(SPARSEFILENAME, 0644); | mksparsefile(SPARSEFILENAME, 0644); | ||||
chflags(SPARSEFILENAME, UF_NODUMP); | chflags(SPARSEFILENAME, UF_NODUMP); | ||||
/* create a hard link */ | /* create a hard link */ | ||||
verbose("link %s %s", SPARSEFILENAME, HARDLINKNAME); | verbose("link %s %s", SPARSEFILENAME, HARDLINKNAME); | ||||
if (link(SPARSEFILENAME, HARDLINKNAME) != 0) | if (link(SPARSEFILENAME, HARDLINKNAME) != 0) | ||||
▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | if (pid == 0) { | ||||
execlp("tar", "tar", | execlp("tar", "tar", | ||||
"-c", | "-c", | ||||
"-f", tarfilename, | "-f", tarfilename, | ||||
"-C", dirname, | "-C", dirname, | ||||
"--zstd", | "--zstd", | ||||
#if 0 | #if 0 | ||||
"--options", "zstd:frame-per-file", | "--options", "zstd:frame-per-file", | ||||
#endif | #endif | ||||
".", | "./" EMPTYDIRNAME "/../" NORMALFILENAME, | ||||
"./" SPARSEFILENAME, | |||||
"./" HARDLINKNAME, | |||||
"./" SHORTLINKNAME, | |||||
"./" SUBDIRNAME, | |||||
"./" LONGLINKNAME, | |||||
NULL); | NULL); | ||||
err(1, "execlp()"); | err(1, "execlp()"); | ||||
} | } | ||||
if (waitpid(pid, &wstatus, 0) < 0) | if (waitpid(pid, &wstatus, 0) < 0) | ||||
err(1, "waitpid()"); | err(1, "waitpid()"); | ||||
if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0) | if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0) | ||||
errx(1, "child failed"); | errx(1, "child failed"); | ||||
/* fork a child to delete everything */ | /* fork a child to delete everything */ | ||||
if ((pid = fork()) < 0) | if ((pid = fork()) < 0) | ||||
err(1, "fork()"); | err(1, "fork()"); | ||||
if (pid == 0) { | if (pid == 0) { | ||||
verbose("cd %s", dirname); | verbose("cd %s", dirname); | ||||
if (chdir(dirname) != 0) | if (chdir(dirname) != 0) | ||||
err(1, "%s", dirname); | err(1, "%s", dirname); | ||||
verbose("rm %s", LONGLINKNAME); | verbose("rm %s", LONGLINKNAME); | ||||
(void)unlink(LONGLINKNAME); | (void)unlink(LONGLINKNAME); | ||||
verbose("rm %s", SHORTLINKNAME); | verbose("rm %s", SHORTLINKNAME); | ||||
(void)unlink(SHORTLINKNAME); | (void)unlink(SHORTLINKNAME); | ||||
verbose("rm %s", HARDLINKNAME); | verbose("rm %s", HARDLINKNAME); | ||||
(void)unlink(HARDLINKNAME); | (void)unlink(HARDLINKNAME); | ||||
verbose("rm %s", SPARSEFILENAME); | verbose("rm %s", SPARSEFILENAME); | ||||
(void)unlink(SPARSEFILENAME); | (void)unlink(SPARSEFILENAME); | ||||
verbose("rm %s", SUBDIRNAME); | verbose("rmdir %s", EMPTYDIRNAME); | ||||
(void)rmdir(EMPTYDIRNAME); | |||||
verbose("rmdir %s", SUBDIRNAME); | |||||
(void)rmdir(SUBDIRNAME); | (void)rmdir(SUBDIRNAME); | ||||
verbose("cd -"); | verbose("cd -"); | ||||
exit(0); | exit(0); | ||||
} | } | ||||
if (waitpid(pid, &wstatus, 0) < 0) | if (waitpid(pid, &wstatus, 0) < 0) | ||||
err(1, "waitpid()"); | err(1, "waitpid()"); | ||||
if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0) | if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0) | ||||
errx(1, "child failed"); | errx(1, "child failed"); | ||||
verbose("rmdir %s", dirname); | verbose("rmdir %s", dirname); | ||||
(void)rmdir(dirname); | (void)rmdir(dirname); | ||||
exit(0); | exit(0); | ||||
} | } |