From be8eedc9c7c3ce24000f69c7d6d969ee2477d599 Mon Sep 17 00:00:00 2001 From: simon987 Date: Sun, 7 Nov 2021 11:56:09 -0500 Subject: [PATCH] Skip subtree of excluded directories --- src/io/walk.c | 20 +++++++++++++++----- src/sist.h | 2 ++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/io/walk.c b/src/io/walk.c index 96a59da..3379f55 100644 --- a/src/io/walk.c +++ b/src/io/walk.c @@ -43,26 +43,36 @@ int sub_strings[30]; int handle_entry(const char *filepath, const struct stat *info, int typeflag, struct FTW *ftw) { - if (typeflag == FTW_F && S_ISREG(info->st_mode) && ftw->level <= ScanCtx.depth) { + if (ftw->level > ScanCtx.depth) { + if (typeflag == FTW_D) { + return FTW_SKIP_SUBTREE; + } + return FTW_CONTINUE; + } - if (ScanCtx.exclude != NULL && EXCLUDED(filepath)) { - LOG_DEBUGF("walk.c", "Excluded: %s", filepath) + if (ScanCtx.exclude != NULL && EXCLUDED(filepath)) { + LOG_DEBUGF("walk.c", "Excluded: %s", filepath) + if (typeflag == FTW_F && S_ISREG(info->st_mode)) { pthread_mutex_lock(&ScanCtx.dbg_file_counts_mu); ScanCtx.dbg_excluded_files_count += 1; pthread_mutex_unlock(&ScanCtx.dbg_file_counts_mu); return 0; + } else if (typeflag == FTW_D) { + return FTW_SKIP_SUBTREE; } + } + if (typeflag == FTW_F && S_ISREG(info->st_mode)) { parse_job_t *job = create_fs_parse_job(filepath, info, ftw->base); tpool_add_work(ScanCtx.pool, parse, job); } - return 0; + return FTW_CONTINUE; } #define MAX_FILE_DESCRIPTORS 64 int walk_directory_tree(const char *dirpath) { - return nftw(dirpath, handle_entry, MAX_FILE_DESCRIPTORS, FTW_PHYS | FTW_DEPTH); + return nftw(dirpath, handle_entry, MAX_FILE_DESCRIPTORS, FTW_PHYS | FTW_ACTIONRETVAL); } diff --git a/src/sist.h b/src/sist.h index 64f867f..db958e8 100644 --- a/src/sist.h +++ b/src/sist.h @@ -1,6 +1,8 @@ #ifndef SIST_H #define SIST_H +#define _GNU_SOURCE + #ifndef FALSE #define FALSE (0) #define BOOL int