fix(cli-create): Disregard nested directories during packing

This commit is contained in:
Rachel 2024-12-31 15:12:31 -08:00
parent a2e14a20b6
commit 2ee5bcf791
3 changed files with 35 additions and 3 deletions

View File

@ -31,4 +31,8 @@ size_t nrtrim(const char *s);
char *basename_extend(const char *path, const char *ext);
char *strcpy_fext(const char *path, const char *ext);
// Return true if "`parent`/`file`" is a directory, false otherwise.
// If file is not given, then `parent` will be tested without the trailing slash.
bool isdir(const char *parent, const char *file);
#endif // NARC_STRUTIL_H

View File

@ -24,6 +24,8 @@
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "api/pack.h"
#include "defs/narc.h"
#include "defs/vfs.h"
@ -85,7 +87,7 @@ struct options {
static int parse_opts(int *argc, const char ***argv, struct options *opts);
static int pack(struct options *opts);
static struct strvec *build_pack_list(DIR *dir, const char *order_fname, const char *ignore_fname);
static struct strvec *build_pack_list(DIR *dir, const char *dir_name, const char *order_fname, const char *ignore_fname);
static struct strbuild *start_index(const char *target_fname, char **out_guard);
static void add_to_index(struct strbuild *index, const char *fname, const size_t i);
static void finish_index(struct strbuild *index, const char *guard);
@ -173,7 +175,7 @@ static int pack(struct options *opts)
char guard[256] = {0};
char *guard_p = &guard[0]; // just to make the compiler happy
struct strbuild *index = start_index(naix, &guard_p);
struct strvec *to_pack = build_pack_list(dir, opts->order, opts->ignore);
struct strvec *to_pack = build_pack_list(dir, opts->input, opts->order, opts->ignore);
if (to_pack == NULL) {
fprintf(stderr, "narc create: failure while building packing list");
goto fail;
@ -247,7 +249,7 @@ fail:
return EXIT_FAILURE;
}
static struct strvec *build_pack_list(DIR *dir, const char *order_fname, const char *ignore_fname)
static struct strvec *build_pack_list(DIR *dir, const char *dir_name, const char *order_fname, const char *ignore_fname)
{
struct strvec *all_files = NULL, *ignored = NULL;
@ -271,6 +273,10 @@ static struct strvec *build_pack_list(DIR *dir, const char *order_fname, const c
continue;
}
if (isdir(dir_name, entry->d_name)) {
continue;
}
// Check the ignored list first, since it should be much smaller
bool exclude = false;
for (size_t i = 0; i < ignored->count; i++) {

View File

@ -24,6 +24,8 @@
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
char *basename(const char *path)
{
char *p = strrchr(path, '/');
@ -91,3 +93,23 @@ bool match_either(const char *s, const char *a, const char *b)
return (a != NULL && strcmp(s, a) == 0)
|| (b != NULL && strcmp(s, b) == 0);
}
bool isdir(const char *parent, const char *file)
{
char target[256];
size_t parent_len = strlen(parent);
strcpy(target, parent);
if (file != NULL) {
char *p = target + parent_len;
*p = '/';
strcpy(p + 1, file);
}
struct stat stbuf;
if (stat(target, &stbuf) != 0) {
return false;
}
return S_ISDIR(stbuf.st_mode);
}