bin/xbps-create: cleanup filetype handling

This commit is contained in:
Duncan Overbruck 2022-12-22 22:00:47 +01:00
parent 9a46051499
commit c70c55b19a
No known key found for this signature in database
GPG Key ID: 335C1D17EC3D6E35

View File

@ -51,10 +51,19 @@
#define _PROGNAME "xbps-create" #define _PROGNAME "xbps-create"
enum entry_type {
ENTRY_TYPE_METADATA = 1,
ENTRY_TYPE_LINKS,
ENTRY_TYPE_DIRS,
ENTRY_TYPE_FILES,
ENTRY_TYPE_CONF_FILES,
};
struct xentry { struct xentry {
TAILQ_ENTRY(xentry) entries; TAILQ_ENTRY(xentry) entries;
uint64_t size; uint64_t size;
char *file, *type, *target; enum entry_type type;
char *file, *target;
char sha256[XBPS_SHA256_SIZE]; char sha256[XBPS_SHA256_SIZE];
ino_t inode; ino_t inode;
}; };
@ -139,6 +148,24 @@ die_archive(struct archive *ar, const char *fmt, ...)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
static const char *
entry_type_str(enum entry_type type)
{
switch (type) {
case ENTRY_TYPE_LINKS:
return "links";
case ENTRY_TYPE_DIRS:
return "dirs";
case ENTRY_TYPE_FILES:
return "files";
case ENTRY_TYPE_CONF_FILES:
return "conf_files";
case ENTRY_TYPE_METADATA:
return "metadata";
}
diex("unknown entry type");
}
static void static void
process_array(const char *key, const char *val) process_array(const char *key, const char *val)
{ {
@ -330,7 +357,9 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED
filep = strchr(fpath, '.') + 1; filep = strchr(fpath, '.') + 1;
fileinfo = xbps_dictionary_create(); fileinfo = xbps_dictionary_create();
xe = calloc(1, sizeof(*xe)); xe = calloc(1, sizeof(*xe));
assert(xe); if (xe == NULL)
die("calloc:");
/* XXX: fileinfo contains the sanatized path, whereas xe contains the /* XXX: fileinfo contains the sanatized path, whereas xe contains the
* unsanatized path! * unsanatized path!
* *
@ -346,8 +375,7 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED
(strcmp(fpath, "./REMOVE") == 0)) { (strcmp(fpath, "./REMOVE") == 0)) {
/* metadata file */ /* metadata file */
xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "metadata"); xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "metadata");
xe->type = strdup("metadata"); xe->type = ENTRY_TYPE_METADATA;
assert(xe->type);
goto out; goto out;
} }
@ -361,9 +389,7 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED
* Find out target file. * Find out target file.
*/ */
xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "links"); xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "links");
xe->type = strdup("links"); xe->type = ENTRY_TYPE_LINKS;
assert(xe->type);
xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "links");
len = readlink(fpath, buf, sizeof(buf)); len = readlink(fpath, buf, sizeof(buf));
if (len < 0 || len >= (int)sizeof(buf)) if (len < 0 || len >= (int)sizeof(buf))
@ -466,10 +492,10 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED
*/ */
if (entry_is_conf_file(filep)) { if (entry_is_conf_file(filep)) {
xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "conf_files"); xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "conf_files");
xe->type = strdup("conf_files"); xe->type = ENTRY_TYPE_CONF_FILES;
} else { } else {
xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "files"); xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "files");
xe->type = strdup("files"); xe->type = ENTRY_TYPE_FILES;
} }
assert(xe->type); assert(xe->type);
@ -484,8 +510,7 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED
} else if (S_ISDIR(sb->st_mode)) { } else if (S_ISDIR(sb->st_mode)) {
/* directory */ /* directory */
xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "dirs"); xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "dirs");
xe->type = strdup("dirs"); xe->type = ENTRY_TYPE_DIRS;
assert(xe->type);
} else if (S_ISFIFO(sb->st_mode)) { } else if (S_ISFIFO(sb->st_mode)) {
errno = 0; errno = 0;
die("cannot package fifo %s", fpath); die("cannot package fifo %s", fpath);
@ -546,7 +571,7 @@ walk_dir(const char *path,
} }
static void static void
process_xentry(const char *key, const char *mutable_files) process_xentry(enum entry_type type, const char *mutable_files)
{ {
xbps_array_t a; xbps_array_t a;
xbps_dictionary_t d; xbps_dictionary_t d;
@ -558,7 +583,7 @@ process_xentry(const char *key, const char *mutable_files)
assert(a); assert(a);
TAILQ_FOREACH_REVERSE(xe, &xentry_list, xentry_head, entries) { TAILQ_FOREACH_REVERSE(xe, &xentry_list, xentry_head, entries) {
if (strcmp(xe->type, key)) if (xe->type != type)
continue; continue;
found = true; found = true;
@ -603,7 +628,7 @@ process_xentry(const char *key, const char *mutable_files)
xbps_object_release(d); xbps_object_release(d);
} }
if (found) if (found)
xbps_dictionary_set(pkg_filesd, key, a); xbps_dictionary_set(pkg_filesd, entry_type_str(type), a);
xbps_object_release(a); xbps_object_release(a);
} }
@ -615,16 +640,16 @@ process_destdir(const char *mutable_files)
die("failed to process destdir files (nftw):"); die("failed to process destdir files (nftw):");
/* Process regular files */ /* Process regular files */
process_xentry("files", mutable_files); process_xentry(ENTRY_TYPE_FILES, mutable_files);
/* Process configuration files */ /* Process configuration files */
process_xentry("conf_files", NULL); process_xentry(ENTRY_TYPE_CONF_FILES, NULL);
/* Process symlinks */ /* Process symlinks */
process_xentry("links", NULL); process_xentry(ENTRY_TYPE_LINKS, NULL);
/* Process directories */ /* Process directories */
process_xentry("dirs", NULL); process_xentry(ENTRY_TYPE_DIRS, NULL);
} }
static void static void
@ -757,8 +782,7 @@ process_archive(struct archive *ar,
/* Add all package data files and release resources */ /* Add all package data files and release resources */
while ((xe = TAILQ_FIRST(&xentry_list)) != NULL) { while ((xe = TAILQ_FIRST(&xentry_list)) != NULL) {
TAILQ_REMOVE(&xentry_list, xe, entries); TAILQ_REMOVE(&xentry_list, xe, entries);
if ((strcmp(xe->type, "metadata") == 0) || if (xe->type == ENTRY_TYPE_METADATA || xe->type == ENTRY_TYPE_DIRS)
(strcmp(xe->type, "dirs") == 0))
continue; continue;
if (!quiet) { if (!quiet) {