mirror of
https://github.com/KartKrewDev/RingRacers.git
synced 2026-04-27 20:41:46 +00:00
filesearch: Ignore certain high-content, low-utility folders
A blacklist for "logs", "media", and "luafiles" at the root of the search, and ".git" in all subfolders Also some minor code cleanup for improved readability
This commit is contained in:
parent
4d6fe8f233
commit
8230bbbca4
1 changed files with 64 additions and 30 deletions
|
|
@ -435,6 +435,13 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut)
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
static const char *filesearch_exclude[] = {
|
||||||
|
"media",
|
||||||
|
"logs",
|
||||||
|
"luafiles",
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *wantedmd5sum, boolean completepath, int maxsearchdepth)
|
filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *wantedmd5sum, boolean completepath, int maxsearchdepth)
|
||||||
{
|
{
|
||||||
filestatus_t retval = FS_NOTFOUND;
|
filestatus_t retval = FS_NOTFOUND;
|
||||||
|
|
@ -442,7 +449,7 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want
|
||||||
struct dirent *dent;
|
struct dirent *dent;
|
||||||
struct stat fsstat = {0};
|
struct stat fsstat = {0};
|
||||||
int found = 0;
|
int found = 0;
|
||||||
char *searchname = strdup(filename);
|
char *searchname;
|
||||||
int depthleft = maxsearchdepth;
|
int depthleft = maxsearchdepth;
|
||||||
char searchpath[1024];
|
char searchpath[1024];
|
||||||
size_t *searchpathindex;
|
size_t *searchpathindex;
|
||||||
|
|
@ -457,12 +464,13 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want
|
||||||
|
|
||||||
if (dirhandle[depthleft] == NULL)
|
if (dirhandle[depthleft] == NULL)
|
||||||
{
|
{
|
||||||
free(searchname);
|
|
||||||
free(dirhandle);
|
free(dirhandle);
|
||||||
free(searchpathindex);
|
free(searchpathindex);
|
||||||
return FS_NOTFOUND;
|
return FS_NOTFOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
searchname = strdup(filename);
|
||||||
|
|
||||||
if (searchpath[searchpathindex[depthleft]-2] != PATHSEP[0])
|
if (searchpath[searchpathindex[depthleft]-2] != PATHSEP[0])
|
||||||
{
|
{
|
||||||
searchpath[searchpathindex[depthleft]-1] = PATHSEP[0];
|
searchpath[searchpathindex[depthleft]-1] = PATHSEP[0];
|
||||||
|
|
@ -492,42 +500,68 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want
|
||||||
}
|
}
|
||||||
|
|
||||||
// okay, now we actually want searchpath to incorporate d_name
|
// okay, now we actually want searchpath to incorporate d_name
|
||||||
strcpy(&searchpath[searchpathindex[depthleft]],dent->d_name);
|
strcpy(&searchpath[searchpathindex[depthleft]], dent->d_name);
|
||||||
|
|
||||||
if (stat(searchpath,&fsstat) < 0) // do we want to follow symlinks? if not: change it to lstat
|
if (stat(searchpath,&fsstat) < 0) // do we want to follow symlinks? if not: change it to lstat
|
||||||
; // was the file (re)moved? can't stat it
|
continue; // was the file (re)moved? can't stat it
|
||||||
else if (S_ISDIR(fsstat.st_mode) && depthleft)
|
|
||||||
|
if (S_ISDIR(fsstat.st_mode))
|
||||||
{
|
{
|
||||||
searchpathindex[--depthleft] = strlen(searchpath) + 1;
|
// I am a folder!
|
||||||
dirhandle[depthleft] = opendir(searchpath);
|
|
||||||
if (!dirhandle[depthleft])
|
if (!depthleft)
|
||||||
|
continue; // No additional folder delving permitted...
|
||||||
|
|
||||||
|
const char **path = filesearch_exclude;
|
||||||
|
|
||||||
|
if (depthleft == maxsearchdepth-1)
|
||||||
{
|
{
|
||||||
// can't open it... maybe no read-permissions
|
// When we're at the root of the search, we exclude certain folders.
|
||||||
// go back to previous dir
|
for (; *path; path++)
|
||||||
depthleft++;
|
{
|
||||||
|
if (strcasecmp(*path, dent->d_name))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*path)
|
||||||
|
continue; // This folder is excluded
|
||||||
}
|
}
|
||||||
|
|
||||||
searchpath[searchpathindex[depthleft]-1] = PATHSEP[0];
|
if (strcasecmp(".git", dent->d_name) // sanity if you're weird like me
|
||||||
searchpath[searchpathindex[depthleft]] = 0;
|
&& (dirhandle[depthleft-1] = opendir(searchpath)) != NULL)
|
||||||
}
|
|
||||||
else if (!strcasecmp(searchname, dent->d_name))
|
|
||||||
{
|
|
||||||
switch (checkfilemd5(searchpath, wantedmd5sum))
|
|
||||||
{
|
{
|
||||||
case FS_FOUND:
|
// Got read permissions!
|
||||||
if (completepath)
|
searchpathindex[--depthleft] = strlen(searchpath) + 1;
|
||||||
strcpy(filename,searchpath);
|
|
||||||
else
|
searchpath[searchpathindex[depthleft]-1] = PATHSEP[0];
|
||||||
strcpy(filename,dent->d_name);
|
searchpath[searchpathindex[depthleft]] = 0;
|
||||||
retval = FS_FOUND;
|
|
||||||
found = 1;
|
|
||||||
break;
|
|
||||||
case FS_MD5SUMBAD:
|
|
||||||
retval = FS_MD5SUMBAD;
|
|
||||||
break;
|
|
||||||
default: // prevent some compiler warnings
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// I am a file!
|
||||||
|
|
||||||
|
if (strcasecmp(searchname, dent->d_name))
|
||||||
|
continue; // Not what we're looking for!
|
||||||
|
|
||||||
|
switch (checkfilemd5(searchpath, wantedmd5sum))
|
||||||
|
{
|
||||||
|
case FS_FOUND:
|
||||||
|
if (completepath)
|
||||||
|
strcpy(filename,searchpath);
|
||||||
|
else
|
||||||
|
strcpy(filename,dent->d_name);
|
||||||
|
retval = FS_FOUND;
|
||||||
|
found = 1;
|
||||||
|
break;
|
||||||
|
case FS_MD5SUMBAD:
|
||||||
|
retval = FS_MD5SUMBAD;
|
||||||
|
break;
|
||||||
|
default: // prevent some compiler warnings
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue