Skip to content

Commit c70c55b

Browse files
committed
bin/xbps-create: cleanup filetype handling
1 parent 9a46051 commit c70c55b

1 file changed

Lines changed: 44 additions & 20 deletions

File tree

bin/xbps-create/main.c

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,19 @@
5151

5252
#define _PROGNAME "xbps-create"
5353

54+
enum entry_type {
55+
ENTRY_TYPE_METADATA = 1,
56+
ENTRY_TYPE_LINKS,
57+
ENTRY_TYPE_DIRS,
58+
ENTRY_TYPE_FILES,
59+
ENTRY_TYPE_CONF_FILES,
60+
};
61+
5462
struct xentry {
5563
TAILQ_ENTRY(xentry) entries;
5664
uint64_t size;
57-
char *file, *type, *target;
65+
enum entry_type type;
66+
char *file, *target;
5867
char sha256[XBPS_SHA256_SIZE];
5968
ino_t inode;
6069
};
@@ -139,6 +148,24 @@ die_archive(struct archive *ar, const char *fmt, ...)
139148
exit(EXIT_FAILURE);
140149
}
141150

151+
static const char *
152+
entry_type_str(enum entry_type type)
153+
{
154+
switch (type) {
155+
case ENTRY_TYPE_LINKS:
156+
return "links";
157+
case ENTRY_TYPE_DIRS:
158+
return "dirs";
159+
case ENTRY_TYPE_FILES:
160+
return "files";
161+
case ENTRY_TYPE_CONF_FILES:
162+
return "conf_files";
163+
case ENTRY_TYPE_METADATA:
164+
return "metadata";
165+
}
166+
diex("unknown entry type");
167+
}
168+
142169
static void
143170
process_array(const char *key, const char *val)
144171
{
@@ -330,7 +357,9 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED
330357
filep = strchr(fpath, '.') + 1;
331358
fileinfo = xbps_dictionary_create();
332359
xe = calloc(1, sizeof(*xe));
333-
assert(xe);
360+
if (xe == NULL)
361+
die("calloc:");
362+
334363
/* XXX: fileinfo contains the sanatized path, whereas xe contains the
335364
* unsanatized path!
336365
*
@@ -346,8 +375,7 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED
346375
(strcmp(fpath, "./REMOVE") == 0)) {
347376
/* metadata file */
348377
xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "metadata");
349-
xe->type = strdup("metadata");
350-
assert(xe->type);
378+
xe->type = ENTRY_TYPE_METADATA;
351379
goto out;
352380
}
353381

@@ -361,9 +389,7 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED
361389
* Find out target file.
362390
*/
363391
xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "links");
364-
xe->type = strdup("links");
365-
assert(xe->type);
366-
xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "links");
392+
xe->type = ENTRY_TYPE_LINKS;
367393

368394
len = readlink(fpath, buf, sizeof(buf));
369395
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
466492
*/
467493
if (entry_is_conf_file(filep)) {
468494
xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "conf_files");
469-
xe->type = strdup("conf_files");
495+
xe->type = ENTRY_TYPE_CONF_FILES;
470496
} else {
471497
xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "files");
472-
xe->type = strdup("files");
498+
xe->type = ENTRY_TYPE_FILES;
473499
}
474500

475501
assert(xe->type);
@@ -484,8 +510,7 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED
484510
} else if (S_ISDIR(sb->st_mode)) {
485511
/* directory */
486512
xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "dirs");
487-
xe->type = strdup("dirs");
488-
assert(xe->type);
513+
xe->type = ENTRY_TYPE_DIRS;
489514
} else if (S_ISFIFO(sb->st_mode)) {
490515
errno = 0;
491516
die("cannot package fifo %s", fpath);
@@ -546,7 +571,7 @@ walk_dir(const char *path,
546571
}
547572

548573
static void
549-
process_xentry(const char *key, const char *mutable_files)
574+
process_xentry(enum entry_type type, const char *mutable_files)
550575
{
551576
xbps_array_t a;
552577
xbps_dictionary_t d;
@@ -558,7 +583,7 @@ process_xentry(const char *key, const char *mutable_files)
558583
assert(a);
559584

560585
TAILQ_FOREACH_REVERSE(xe, &xentry_list, xentry_head, entries) {
561-
if (strcmp(xe->type, key))
586+
if (xe->type != type)
562587
continue;
563588

564589
found = true;
@@ -603,7 +628,7 @@ process_xentry(const char *key, const char *mutable_files)
603628
xbps_object_release(d);
604629
}
605630
if (found)
606-
xbps_dictionary_set(pkg_filesd, key, a);
631+
xbps_dictionary_set(pkg_filesd, entry_type_str(type), a);
607632

608633
xbps_object_release(a);
609634
}
@@ -615,16 +640,16 @@ process_destdir(const char *mutable_files)
615640
die("failed to process destdir files (nftw):");
616641

617642
/* Process regular files */
618-
process_xentry("files", mutable_files);
643+
process_xentry(ENTRY_TYPE_FILES, mutable_files);
619644

620645
/* Process configuration files */
621-
process_xentry("conf_files", NULL);
646+
process_xentry(ENTRY_TYPE_CONF_FILES, NULL);
622647

623648
/* Process symlinks */
624-
process_xentry("links", NULL);
649+
process_xentry(ENTRY_TYPE_LINKS, NULL);
625650

626651
/* Process directories */
627-
process_xentry("dirs", NULL);
652+
process_xentry(ENTRY_TYPE_DIRS, NULL);
628653
}
629654

630655
static void
@@ -757,8 +782,7 @@ process_archive(struct archive *ar,
757782
/* Add all package data files and release resources */
758783
while ((xe = TAILQ_FIRST(&xentry_list)) != NULL) {
759784
TAILQ_REMOVE(&xentry_list, xe, entries);
760-
if ((strcmp(xe->type, "metadata") == 0) ||
761-
(strcmp(xe->type, "dirs") == 0))
785+
if (xe->type == ENTRY_TYPE_METADATA || xe->type == ENTRY_TYPE_DIRS)
762786
continue;
763787

764788
if (!quiet) {

0 commit comments

Comments
 (0)