Skip to content

Commit 3349dbf

Browse files
committed
compat UPDATE check for dirent.d_type
1 parent f3642f6 commit 3349dbf

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

CMakeModules/UseCompat.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ include(CheckSymbolExists)
1818
include(CheckFunctionExists)
1919
include(CheckIncludeFile)
2020
include(TestBigEndian)
21+
include(CheckStructHasMember)
2122
if(POLICY CMP0075)
2223
cmake_policy(SET CMP0075 NEW)
2324
endif()
@@ -63,6 +64,8 @@ macro(USE_COMPAT)
6364

6465
check_include_file("byteswap.h" HAVE_BYTESWAP_H)
6566

67+
check_struct_has_member("struct dirent" d_type "dirent.h" HAVE_DIRENT_D_TYPE)
68+
6669
list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_POSIX_C_SOURCE=200809L)
6770
list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
6871
list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D__BSD_VISIBLE=1)

compat/compat.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
#cmakedefine HAVE_MMAP
8787
#cmakedefine HAVE_STRCASECMP
8888
#cmakedefine HAVE_SETENV
89+
#cmakedefine HAVE_DIRENT_D_TYPE
8990

9091
#ifndef bswap64
9192
#define bswap64(val) \

src/tree_schema.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,13 +2349,28 @@ lys_search_localfile_file_type(const struct dirent *file, const char *wd, struct
23492349
{
23502350
LY_ERR rc = LY_SUCCESS;
23512351
char *str = NULL;
2352-
ly_bool is_dir = 0, is_reg = 0;
2352+
ly_bool is_dir = 0, is_reg = 0, need_stat = 1;
23532353
struct stat st;
23542354

23552355
*skip = 0;
23562356

2357-
if ((file->d_type == DT_UNKNOWN) || (file->d_type == DT_LNK)) {
2358-
/* FS does not support this field or its a symbolic link, need to call stat */
2357+
#ifdef HAVE_DIRENT_D_TYPE
2358+
if (file->d_type == DT_DIR) {
2359+
/* dirent - dir */
2360+
is_dir = 1;
2361+
need_stat = 0;
2362+
} else if (file->d_type == DT_REG) {
2363+
/* dirent - file */
2364+
is_reg = 1;
2365+
need_stat = 0;
2366+
} else if ((file->d_type != DT_UNKNOWN) && (file->d_type != DT_LNK)) {
2367+
/* it is a known type, but not dir or regular file, so trust d_type and just skip it */
2368+
need_stat = 0;
2369+
}
2370+
#endif // HAVE_DIRENT_D_TYPE
2371+
2372+
if (need_stat) {
2373+
/* need to use stat to determine the file type */
23592374
if (asprintf(&str, "%s/%s", wd, file->d_name) == -1) {
23602375
LOGMEM(NULL);
23612376
rc = LY_EMEM;
@@ -2371,12 +2386,6 @@ lys_search_localfile_file_type(const struct dirent *file, const char *wd, struct
23712386
/* stat - file */
23722387
is_reg = 1;
23732388
}
2374-
} else if (file->d_type == DT_DIR) {
2375-
/* dirent - dir */
2376-
is_dir = 1;
2377-
} else if (file->d_type == DT_REG) {
2378-
/* dirent - file */
2379-
is_reg = 1;
23802389
}
23812390

23822391
if (is_dir && (dirs->count || !implicit_cwd)) {

0 commit comments

Comments
 (0)