Use DT_* values directly for scan entry types

This commit is contained in:
Andrea Mazzoleni 2026-09-25 09:21:04 +02:00
parent a92b19d9c2
commit 099f3c7429
3 changed files with 35 additions and 19 deletions

View file

@ -1563,7 +1563,7 @@ static int scan_sub(struct snapraid_scan* scan, int level, int is_diff, char* pa
struct dirent_sorted* dd = node->data;
const char* name = dd->d_name;
struct stat* st;
int type;
int d_type;
#if !HAVE_STRUCT_DIRENT_D_STAT
struct stat st_buf;
#endif
@ -1571,23 +1571,17 @@ static int scan_sub(struct snapraid_scan* scan, int level, int is_diff, char* pa
pathcatl(path_next, path_len, PATH_MAX, name);
pathcatl(sub_next, sub_len, PATH_MAX, name);
/* start with an unknown type */
type = -1;
st = 0;
/* if dirent has the type, use it */
#if HAVE_STRUCT_DIRENT_D_TYPE
switch (dd->d_type) {
case DT_UNKNOWN : break;
case DT_REG : type = 0; break;
case DT_LNK : type = 1; break;
case DT_DIR : type = 2; break;
default : type = 3; break;
}
d_type = dd->d_type;
#else
d_type = DT_UNKNOWN;
#endif
/* if type is still unknown */
if (type < 0) {
if (d_type == DT_UNKNOWN) {
/* get the type from stat */
st = DSTAT(path_next, dd, &st_buf);
@ -1611,16 +1605,14 @@ static int scan_sub(struct snapraid_scan* scan, int level, int is_diff, char* pa
#endif
if (S_ISREG(st->st_mode))
type = 0;
d_type = DT_REG;
else if (S_ISLNK(st->st_mode))
type = 1;
d_type = DT_LNK;
else if (S_ISDIR(st->st_mode))
type = 2;
else
type = 3;
d_type = DT_DIR;
}
if (type == 0) { /* REG */
if (d_type == DT_REG) {
/*
* Note that .snapraidignore is an exception from filtering because it
* is a control file that must be preserved as array data.
@ -1661,7 +1653,7 @@ static int scan_sub(struct snapraid_scan* scan, int level, int is_diff, char* pa
} else {
msg_verbose("Excluding file '%s' for rule '%s'\n", path_next, filter_type(reason, tmp, PATH_MAX));
}
} else if (type == 1) { /* LNK */
} else if (d_type == DT_LNK) {
/*
* Note that .snapraidignore is an exception from filtering because it
* is a control file that must be preserved as array data.
@ -1698,7 +1690,7 @@ static int scan_sub(struct snapraid_scan* scan, int level, int is_diff, char* pa
} else {
msg_verbose("Excluding link '%s' for rule '%s'\n", path_next, filter_type(reason, tmp, PATH_MAX));
}
} else if (type == 2) { /* DIR */
} else if (d_type == DT_DIR) {
if (filter_subdir(&state->filterlist, &reason, disk->name, sub_next) == 0
&& filter_subdir(&scan->local_filter_list, &reason, disk->name, sub_next) == 0) {
#ifndef _WIN32

View file

@ -134,6 +134,14 @@ wchar_t* u8tou16_mayfail(wchar_t* conv_buf, size_t number_of_wchar, const char*
#define rmdir windows_rmdir
#undef dirent
#define dirent windows_dirent
#define DT_UNKNOWN 0
#define DT_FIFO 1
#define DT_CHR 2
#define DT_DIR 4
#define DT_BLK 6
#define DT_REG 8
#define DT_LNK 10
#define DT_SOCK 12
#undef DIR
#define DIR windows_dir
#undef opendir

View file

@ -19,6 +19,22 @@
#define OS_LVL_INFO LOG_INFO
#define OS_LVL_DEBUG LOG_DEBUG
#if !HAVE_STRUCT_DIRENT_D_TYPE
/* without d_type these values are internal markers populated from lstat(). */
#ifndef DT_UNKNOWN
#define DT_UNKNOWN 0
#endif
#ifndef DT_DIR
#define DT_DIR 4
#endif
#ifndef DT_REG
#define DT_REG 8
#endif
#ifndef DT_LNK
#define DT_LNK 10
#endif
#endif
/* implement close_range for glibc 2.33 or earlier */
#if defined(__linux__) && !defined(HAVE_CLOSE_RANGE)
#ifndef __NR_close_range