Skip to content

Commit 26487aa

Browse files
committed
faster filesearch again
1 parent 60bcb4a commit 26487aa

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/filesrch.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,16 @@ char *refreshdirname = NULL;
378378
#define isuptree(dirent) ((dirent)[0]=='.' && ((dirent)[1]=='\0' || ((dirent)[1]=='.' && (dirent)[2]=='\0')))
379379

380380
#ifdef __SWITCH__
381+
static int scandir_filter(const struct dirent *entry) {
382+
return (entry->d_type == DT_DIR &&
383+
!(entry->d_name[0]=='.' &&
384+
(entry->d_name[1]=='\0' ||
385+
(entry->d_name[1]=='.' &&
386+
entry->d_name[2]=='\0'))));
387+
388+
}
381389
filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *wantedmd5sum, boolean completepath, int maxsearchdepth) {
382390
struct dirent **namelist;
383-
int n = scandir(startpath, &namelist, NULL, alphasort);
384-
if (n < 0) return FS_NOTFOUND;
385391
char filepath[1024];
386392
struct stat fsstat;
387393
filestatus_t retval = FS_NOTFOUND;
@@ -402,18 +408,14 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want
402408
}
403409
// Only recurse if file not found and depth available
404410
if (retval != FS_FOUND && maxsearchdepth > 0) {
411+
int n = scandir(startpath, &namelist, scandir_filter, alphasort);
405412
for (int i = 0; i < n && retval != FS_FOUND; i++) {
406-
if (namelist[i]->d_type == DT_DIR &&
407-
strcmp(namelist[i]->d_name, ".") != 0 &&
408-
strcmp(namelist[i]->d_name, "..") != 0) {
409-
410-
snprintf(filepath, sizeof(filepath), "%s/%s", startpath, namelist[i]->d_name);
411-
retval = filesearch(filename, filepath, wantedmd5sum, completepath, maxsearchdepth - 1);
412-
}
413+
snprintf(filepath, sizeof(filepath), "%s/%s", startpath, namelist[i]->d_name);
414+
retval = filesearch(filename, filepath, wantedmd5sum, completepath, maxsearchdepth - 1);
413415
free(namelist[i]);
414416
}
417+
free(namelist);
415418
}
416-
free(namelist);
417419
return retval;
418420
}
419421
#else

0 commit comments

Comments
 (0)