From 778201fbc2bbc6e66a6df2de7f3270cce689131e Mon Sep 17 00:00:00 2001 From: "Darryl L. Miles" Date: Thu, 17 Oct 2024 21:15:30 +0100 Subject: [PATCH] DBio.c: HAVE_STRUCT_DIRENT_D_TYPE direct->d_type --- database/DBio.c | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/database/DBio.c b/database/DBio.c index 9f707de8..1dd389cb 100644 --- a/database/DBio.c +++ b/database/DBio.c @@ -167,6 +167,24 @@ file_is_not_writeable(name) return(0); } +static int +path_is_dir(const char *dirname, const char *filename) +{ + struct stat statbuf; + char path[PATH_MAX]; + const char *sep = filename ? "/" : ""; + if (!filename) + filename = ""; + size_t n = snprintf(path, sizeof(path), "%s%s%s", dirname, sep, filename); + ASSERT(n < sizeof(path), "path"); + if (n >= sizeof(path)) + return -1; + int err = stat(path, &statbuf); + if (err != 0) + return 0; + return S_ISDIR(statbuf.st_mode) ? 1 : 0; +} + /* Linked string record used to hold directory contents */ typedef struct _linkedDirent { @@ -252,7 +270,12 @@ DBSearchForTech(techname, techroot, pathroot, level) for (ld = dlist; ld; ld = ld->ld_next) { tdent = ld->ld_dirent; - if (tdent->d_type != DT_DIR) +#ifdef HAVE_STRUCT_DIRENT_D_TYPE + int is_dir = tdent->d_type == DT_DIR; +#else + int is_dir = path_is_dir(pathroot, tdent->d_name) > 0; /* treat error as false */ +#endif + if (!is_dir) { if (!strcmp(tdent->d_name, techname)) { @@ -324,7 +347,12 @@ DBAddStandardCellPaths(pathptr, level) while ((tdent = readdir(tdir)) != NULL) { - if ((tdent->d_type == DT_DIR) && +#ifdef HAVE_STRUCT_DIRENT_D_TYPE + int is_dir = tdent->d_type == DT_DIR; +#else + int is_dir = path_is_dir(pathptr, tdent->d_name) > 0; /* treat error as false */ +#endif + if (is_dir && (strcmp(tdent->d_name, ".") && strcmp(tdent->d_name, ".."))) { /* Scan the directory contents of tdir for more subdirectories */ @@ -333,7 +361,7 @@ DBAddStandardCellPaths(pathptr, level) paths += DBAddStandardCellPaths(newpath, level + 1); freeMagic(newpath); } - else if (tdent->d_type != DT_DIR) + else if (!is_dir) { /* Scan the directory contents of tdir for .mag files */ if (!strcmp(tdent->d_name + strlen(tdent->d_name) - 4, ".mag"))