util/disk_cache: Handle OS'es without d_type in struct dirent

Needed to build on Solaris

Fixes: f58e6fee74 ("util/disk_cache: delete more cache items in one go when full")
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Reviewed-by: Eric Engestrom <eric@igalia.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21110>
This commit is contained in:
Alan Coopersmith 2023-02-03 13:12:38 -08:00 committed by Marge Bot
parent 067545eb9a
commit 289eb50abf

View file

@ -200,12 +200,24 @@ choose_lru_file_matching(const char *dir_path,
if (dir == NULL)
return NULL;
const int dir_fd = dirfd(dir);
/* First count the number of files in the directory */
unsigned total_file_count = 0;
while ((dir_ent = readdir(dir)) != NULL) {
#ifdef HAVE_DIRENT_D_TYPE
if (dir_ent->d_type == DT_REG) { /* If the entry is a regular file */
total_file_count++;
}
#else
struct stat st;
if (fstatat(dir_fd, dir_ent->d_name, &st, AT_SYMLINK_NOFOLLOW) == 0) {
if (S_ISREG(st.st_mode)) {
total_file_count++;
}
}
#endif
}
/* Reset to the start of the directory */
@ -225,7 +237,7 @@ choose_lru_file_matching(const char *dir_path,
break;
struct stat sb;
if (fstatat(dirfd(dir), dir_ent->d_name, &sb, 0) == 0) {
if (fstatat(dir_fd, dir_ent->d_name, &sb, 0) == 0) {
struct lru_file *entry = NULL;
if (!list_is_empty(lru_file_list))
entry = list_first_entry(lru_file_list, struct lru_file, node);