util/disk_cache: use stat() to check if entry is a directory

d_type is not supported on all systems.

Tested-by: Vinson Lee <vlee@freedesktop.org>
Reviewed-by: Emil Velikov <emil.velikov@collabora.com>
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=97967
This commit is contained in:
Timothy Arceri
2017-02-08 15:05:19 +11:00
parent 463236bd31
commit d7b3707c61
+25 -10
View File
@@ -375,7 +375,8 @@ make_cache_file_directory(struct disk_cache *cache, cache_key key)
*/ */
static char * static char *
choose_random_file_matching(const char *dir_path, choose_random_file_matching(const char *dir_path,
bool (*predicate)(struct dirent *)) bool (*predicate)(struct dirent *,
const char *dir_path))
{ {
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
@@ -392,7 +393,7 @@ choose_random_file_matching(const char *dir_path,
entry = readdir(dir); entry = readdir(dir);
if (entry == NULL) if (entry == NULL)
break; break;
if (! predicate(entry)) if (!predicate(entry, dir_path))
continue; continue;
count++; count++;
@@ -412,7 +413,7 @@ choose_random_file_matching(const char *dir_path,
entry = readdir(dir); entry = readdir(dir);
if (entry == NULL) if (entry == NULL)
break; break;
if (! predicate(entry)) if (!predicate(entry, dir_path))
continue; continue;
if (count == victim) if (count == victim)
break; break;
@@ -437,14 +438,20 @@ choose_random_file_matching(const char *dir_path,
* ".tmp" * ".tmp"
*/ */
static bool static bool
is_regular_non_tmp_file(struct dirent *entry) is_regular_non_tmp_file(struct dirent *entry, const char *path)
{ {
size_t len; char *filename;
if (asprintf(&filename, "%s/%s", path, entry->d_name) == -1)
if (entry->d_type != DT_REG)
return false; return false;
len = strlen (entry->d_name); struct stat sb;
stat(filename, &sb);
free(filename);
if (!S_ISREG(sb.st_mode))
return false;
size_t len = strlen (entry->d_name);
if (len >= 4 && strcmp(&entry->d_name[len-4], ".tmp") == 0) if (len >= 4 && strcmp(&entry->d_name[len-4], ".tmp") == 0)
return false; return false;
@@ -478,9 +485,17 @@ unlink_random_file_from_directory(const char *path)
* special name of "..") * special name of "..")
*/ */
static bool static bool
is_two_character_sub_directory(struct dirent *entry) is_two_character_sub_directory(struct dirent *entry, const char *path)
{ {
if (entry->d_type != DT_DIR) char *subdir;
if (asprintf(&subdir, "%s/%s", path, entry->d_name) == -1)
return false;
struct stat sb;
stat(path, &sb);
free(subdir);
if (!S_ISDIR(sb.st_mode))
return false; return false;
if (strlen(entry->d_name) != 2) if (strlen(entry->d_name) != 2)