[FIX] configure.c: Improve recursion rule

So far, collect_sources was called recursively when the entry name did
not match a pre-defined list. However, this assumption broke with files
such as `etc/media/README`, that were not in the list.

Therefore, a deterministic way to distinguish files from directories is
to rely on the S_ISDIR macro. This avoids the need for a pre-defined
list.
This commit is contained in:
Xavier Del Campo Romero 2024-10-17 06:45:42 +02:00
commit a66021bf46
No known key found for this signature in database
GPG key ID: 84FF3612A9BF43F2

View file

@ -11,6 +11,7 @@
#include <sys/stat.h>
#include <stdbool.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
@ -306,22 +307,29 @@ collect_sources(char *dir, bool head, char *ext)
free(na);
continue;
}
if (!strchr(name, '.') &&
strcmp(name, "out") &&
strcmp(name, "Makefile"))
{
str_array_t *sub;
char *d1 = string_cat(dir, "/");
char *d2 = string_cat(d1, name);
size_t i;
struct stat sb;
sub = collect_sources(d2, false, ext);
for (i = 0; i < str_array_len(sub); i++)
if (stat(d2, &sb))
{
char *file = str_array_get(sub, i);
str_array_add(ret, file);
fprintf(stderr, "stat(2) %s: %s\n", d2, strerror(errno));
free(d2);
free(d1);
}
str_array_free(sub);
else if (S_ISDIR(sb.st_mode))
{
str_array_t *sub = collect_sources(d2, false, ext);
for (i = 0; i < str_array_len(sub); i++)
{
char *file = str_array_get(sub, i);
str_array_add(ret, file);
}
str_array_free(sub);
}
free(d2);
free(d1);
}