From cc28606e6f5b37d89d565e5c0e687d2bb109c2e6 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 14 Aug 2024 19:58:44 -0400 Subject: Handle destination directories more gracefully * The storage.output_dir is now the storage.root to avoid generating a sub-directory beneath the temporary working directory * The destination directory is created, then resolved by realpath to avoid generating the destination directory within the temporary working directory when a relative path is used as input --- src/stasis_indexer.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/stasis_indexer.c') diff --git a/src/stasis_indexer.c b/src/stasis_indexer.c index a7b0fce..bc1e6fe 100644 --- a/src/stasis_indexer.c +++ b/src/stasis_indexer.c @@ -508,7 +508,7 @@ void indexer_init_dirs(struct Delivery *ctx, const char *workdir) { fprintf(stderr, "Failed to configure temporary storage directory\n"); exit(1); } - path_store(&ctx->storage.output_dir, PATH_MAX, ctx->storage.root, "output"); + path_store(&ctx->storage.output_dir, PATH_MAX, ctx->storage.root, ""); path_store(&ctx->storage.tools_dir, PATH_MAX, ctx->storage.output_dir, "tools"); path_store(&globals.conda_install_prefix, PATH_MAX, ctx->storage.tools_dir, "conda"); path_store(&ctx->storage.cfgdump_dir, PATH_MAX, ctx->storage.output_dir, "config"); @@ -542,7 +542,13 @@ int main(int argc, char *argv[]) { usage(path_basename(argv[0])); exit(0); case 'd': - destdir = strdup(optarg); + if (mkdir(optarg, 0755)) { + if (errno != 0 && errno != EEXIST) { + SYSERROR("Unable to create destination directory, '%s': %s", optarg, strerror(errno)); + exit(1); + } + } + destdir = realpath(optarg, NULL); break; case 'U': fflush(stdout); @@ -573,7 +579,13 @@ int main(int argc, char *argv[]) { } if (isempty(destdir)) { - destdir = strdup("output"); + if (mkdir("output", 0755)) { + if (errno != 0 && errno != EEXIST) { + SYSERROR("Unable to create destination directory, '%s': %s", "output", strerror(errno)); + exit(1); + } + } + destdir = realpath("output", NULL); } if (!rootdirs || !rootdirs_total) { -- cgit