[Mesa-dev] [PATCH] util/disk_cache: use stat() to check if entry is a directory

Timothy Arceri tarceri at itsqueeze.com
Thu Feb 9 23:05:46 UTC 2017


d_type is not supported on all systems.

Tested-by: Vinson Lee <vlee at freedesktop.org>
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=97967
---
 src/util/disk_cache.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)

diff --git a/src/util/disk_cache.c b/src/util/disk_cache.c
index 75c7334..660fa4f 100644
--- a/src/util/disk_cache.c
+++ b/src/util/disk_cache.c
@@ -436,7 +436,8 @@ make_cache_file_directory(struct disk_cache *cache, cache_key key)
  */
 static char *
 choose_random_file_matching(const char *dir_path,
-                            bool (*predicate)(struct dirent *))
+                            bool (*predicate)(struct dirent *,
+                                              const char *dir_path))
 {
    DIR *dir;
    struct dirent *entry;
@@ -453,7 +454,7 @@ choose_random_file_matching(const char *dir_path,
       entry = readdir(dir);
       if (entry == NULL)
          break;
-      if (! predicate(entry))
+      if (!predicate(entry, dir_path))
          continue;
 
       count++;
@@ -473,7 +474,7 @@ choose_random_file_matching(const char *dir_path,
       entry = readdir(dir);
       if (entry == NULL)
          break;
-      if (! predicate(entry))
+      if (!predicate(entry, dir_path))
          continue;
       if (count == victim)
          break;
@@ -498,14 +499,20 @@ choose_random_file_matching(const char *dir_path,
  * ".tmp"
  */
 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)
+      return false;
+
+   struct stat sb;
+   stat(filename, &sb);
+   free(filename);
 
-   if (entry->d_type != DT_REG)
+   if (!S_ISREG(sb.st_mode))
       return false;
 
-   len = strlen (entry->d_name);
+   size_t len = strlen (entry->d_name);
    if (len >= 4 && strcmp(&entry->d_name[len-4], ".tmp") == 0)
       return false;
 
@@ -539,9 +546,17 @@ unlink_random_file_from_directory(const char *path)
  * special name of "..")
  */
 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;
 
    if (strlen(entry->d_name) != 2)
-- 
2.9.3



More information about the mesa-dev mailing list