Skip to content

SDL_GlobDirectory(): Don't descend into sub-directories, if not requested by pattern #15180

@Sackzement

Description

@Sackzement

When calling SDL_GlobDirectory(), it pattern-matches after descending into a sub-directory to check if it is a match.

This causes the function to fail, when globbing with the pattern "*" on a directory, that has permissions, but its sub-directories don't.
This happens for example with the root directory "/".

Edit:
A different alternative would be, instead of stopping on error, to just skip a directory.

example:

#include <SDL3/SDL.h>

const char *path = "/";
const char *pattern = "*";

SDL_EnumerationResult enum_dir_func(void *userdata, const char *dirname, const char *fname)
{
    SDL_Log("%s", fname);
    
    return SDL_ENUM_CONTINUE;
}

int main(void)
{
    SDL_Log("SDL_EnumerateDirectory(\"%s\")", path);
    if (!SDL_EnumerateDirectory(path, enum_dir_func, NULL)) {
        SDL_Log("ERROR: SDL_EnumerateDirectory() %s", SDL_GetError());
    }
    
    SDL_Log("");
    
    SDL_Log("SDL_GlobDirectory(\"%s\",\"%s\")", path, pattern);
    char **globs = SDL_GlobDirectory(path, pattern, 0, NULL);
    if (!globs) {
        SDL_Log("ERROR: SDL_GlobDirectory() %s", SDL_GetError());
    } else {
        for(char **it = globs; *it; ++it) {
            SDL_Log("%s", *it);
        }
    }
    
    return 0;
}

output:

./a.out

SDL_EnumerateDirectory("/")
tmp
var
efi
root
bin
dev
lost+found
media
mnt
run
lib64
proc
sbin
opt
lib
srv
etc
home
sys
boot
usr

SDL_GlobDirectory("/","*")
ERROR: SDL_GlobDirectory() Can't open directory: Permission denied

output with sudo:

sudo ./a.out

SDL_EnumerateDirectory("/")
tmp
var
efi
root
bin
dev
lost+found
media
mnt
run
lib64
proc
sbin
opt
lib
srv
etc
home
sys
boot
usr

SDL_GlobDirectory("/","*")
tmp
var
efi
root
bin
dev
lost+found
media
mnt
run
lib64
proc
sbin
opt
lib
srv
etc
home
sys
boot
usr

Edit: Here is the code where it descends into a sub-directory

if (matched_to_dir) {
SDL_PathInfo info;
if (data->getpathinfo(fullpath, &info, data->fsuserdata) && (info.type == SDL_PATHTYPE_DIRECTORY)) {
//SDL_Log("GlobDirectoryCallback: Descending into subdir '%s'", fname);
if (!data->enumerator(fullpath, GlobDirectoryCallback, data, data->fsuserdata)) {
result = SDL_ENUM_FAILURE;
}
}
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions