Skip to content

Commit

Permalink
Drop local printexit() helper in favour of errx()
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Larsson <[email protected]>
  • Loading branch information
alexlarsson committed Sep 25, 2023
1 parent 0c63ee0 commit f9df66f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 51 deletions.
27 changes: 8 additions & 19 deletions tools/cfs-fuse.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "config.h"

#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
Expand Down Expand Up @@ -48,18 +49,6 @@ uint64_t erofs_build_time;
uint32_t erofs_build_time_nsec;
int basedir_fd;

static void printexit(const char *format, ...) __attribute__((noreturn));
static void printexit(const char *format, ...)
{
va_list args;

va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);

exit(1);
}

struct cfs_data {
const char *source;
const char *basedir;
Expand Down Expand Up @@ -1171,34 +1160,34 @@ int main(int argc, char *argv[])

fd = open(data.source, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
printexit("Failed to open %s\n", data.source);
errx(EXIT_FAILURE, "Failed to open %s\n", data.source);
}

r = fstat(fd, &s);
if (r < 0) {
printexit("Failed to stat %s\n", data.source);
errx(EXIT_FAILURE, "Failed to stat %s\n", data.source);
}
erofs_data_size = s.st_size;

if (erofs_data_size < EROFS_BLKSIZ) {
printexit("To small image\n");
errx(EXIT_FAILURE, "To small image\n");
}

/* Memory-map the file. */
erofs_data = mmap(0, erofs_data_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (erofs_data == MAP_FAILED) {
printexit("Failed to mmap %s\n", argv[1]);
errx(EXIT_FAILURE, "Failed to mmap %s\n", argv[1]);
}
close(fd);

basedir_fd = open(data.basedir, O_RDONLY | O_PATH);
if (basedir_fd < 0) {
printexit("Failed to open basedir %s\n", data.basedir);
errx(EXIT_FAILURE, "Failed to open basedir %s\n", data.basedir);
}

cfs_header = (struct lcfs_erofs_header_s *)(erofs_data);
if (lcfs_u32_from_file(cfs_header->magic) != LCFS_EROFS_MAGIC) {
printexit("Wrong cfs magic");
errx(EXIT_FAILURE, "Wrong cfs magic");
}

cfs_flags = lcfs_u32_from_file(cfs_header->flags);
Expand All @@ -1208,7 +1197,7 @@ int main(int argc, char *argv[])
erofs_super = (struct erofs_super_block *)(erofs_data + EROFS_SUPER_OFFSET);

if (lcfs_u32_from_file(erofs_super->magic) != EROFS_SUPER_MAGIC_V1) {
printexit("Wrong erofs magic");
errx(EXIT_FAILURE, "Wrong erofs magic");
}

erofs_metadata = erofs_data + lcfs_u32_from_file(erofs_super->meta_blkaddr) *
Expand Down
64 changes: 32 additions & 32 deletions tools/mountcomposefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "config.h"

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
Expand All @@ -37,18 +38,6 @@

#include "libcomposefs/lcfs-mount.h"

static void printexit(const char *format, ...) __attribute__((noreturn));
static void printexit(const char *format, ...)
{
va_list args;

va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);

exit(1);
}

static void usage(const char *argv0)
{
const char *bin = basename(argv0);
Expand Down Expand Up @@ -136,7 +125,8 @@ int main(int argc, char **argv)
switch (opt) {
case 't':
if (strcmp(optarg, "composefs") != 0)
printexit("Unsupported fs type '%s'\n", optarg);
errx(EXIT_FAILURE, "Unsupported fs type '%s'\n",
optarg);
break;
case 'o':
mount_options = optarg;
Expand Down Expand Up @@ -173,32 +163,37 @@ int main(int argc, char **argv)

if (strcmp("basedir", key) == 0) {
if (value == NULL)
printexit("No value specified for basedir option\n");
errx(EXIT_FAILURE,
"No value specified for basedir option\n");
opt_basedir = value;
} else if (strcmp("digest", key) == 0) {
if (value == NULL)
printexit("No value specified for digest option\n");
errx(EXIT_FAILURE,
"No value specified for digest option\n");
opt_digest = value;
} else if (strcmp("verity", key) == 0) {
opt_verity = true;
} else if (strcmp("upperdir", key) == 0) {
if (value == NULL)
printexit("No value specified for upperdir option\n");
errx(EXIT_FAILURE,
"No value specified for upperdir option\n");
opt_upperdir = value;
} else if (strcmp("workdir", key) == 0) {
if (value == NULL)
printexit("No value specified for workdir option\n");
errx(EXIT_FAILURE,
"No value specified for workdir option\n");
opt_workdir = value;
} else if (strcmp("idmap", key) == 0) {
if (value == NULL)
printexit("No value specified for workdir option\n");
errx(EXIT_FAILURE,
"No value specified for workdir option\n");
opt_idmap = value;
} else if (strcmp("rw", key) == 0) {
opt_ro = false;
} else if (strcmp("ro", key) == 0) {
opt_ro = true;
} else {
printexit("Unsupported option %s\n", key);
errx(EXIT_FAILURE, "Unsupported option %s\n", key);
}
}

Expand All @@ -214,7 +209,7 @@ int main(int argc, char **argv)

options.objdirs = calloc(options.n_objdirs, sizeof(char *));
if (options.objdirs == NULL)
printexit("Out of memory\n");
errx(EXIT_FAILURE, "Out of memory\n");

for (i = 0, str = (char *)opt_basedir;; i++, str = NULL) {
token = strtok_r(str, ":", &saveptr);
Expand All @@ -231,7 +226,8 @@ int main(int argc, char **argv)
}

if ((opt_upperdir && !opt_workdir) || (!opt_upperdir && opt_workdir)) {
printexit("Both workdir and upperdir must be specified if used\n");
errx(EXIT_FAILURE,
"Both workdir and upperdir must be specified if used\n");
}
options.upperdir = opt_upperdir;
options.workdir = opt_workdir;
Expand All @@ -246,32 +242,36 @@ int main(int argc, char **argv)
if (opt_idmap != NULL) {
userns_fd = open(opt_idmap, O_RDONLY | O_CLOEXEC | O_NOCTTY);
if (userns_fd < 0)
printexit("Failed to open userns %s: %s\n", opt_idmap,
strerror(errno));
errx(EXIT_FAILURE, "Failed to open userns %s: %s\n",
opt_idmap, strerror(errno));
options.flags |= LCFS_MOUNT_FLAGS_IDMAP;
options.idmap_fd = userns_fd;
}

fd = open(image_path, O_RDONLY | O_CLOEXEC);
if (fd < 0)
printexit("Failed to open %s: %s\n", image_path, strerror(errno));
errx(EXIT_FAILURE, "Failed to open %s: %s\n", image_path,
strerror(errno));

res = lcfs_mount_fd(fd, mount_path, &options);
if (res < 0) {
int errsv = errno;

if (errsv == ENOVERITY)
printexit("Failed to mount composefs %s: Image has no fs-verity\n",
image_path);
errx(EXIT_FAILURE,
"Failed to mount composefs %s: Image has no fs-verity\n",
image_path);
else if (errsv == EWRONGVERITY)
printexit("Failed to mount composefs %s: Image has wrong fs-verity\n",
image_path);
errx(EXIT_FAILURE,
"Failed to mount composefs %s: Image has wrong fs-verity\n",
image_path);
else if (errsv == ENOSIGNATURE)
printexit("Failed to mount composefs %s: Image was not signed\n",
image_path);
errx(EXIT_FAILURE,
"Failed to mount composefs %s: Image was not signed\n",
image_path);

printexit("Failed to mount composefs %s: %s\n", image_path,
strerror(errno));
errx(EXIT_FAILURE, "Failed to mount composefs %s: %s\n",
image_path, strerror(errno));
}

free(options.objdirs);
Expand Down

0 comments on commit f9df66f

Please sign in to comment.