[Mesa-dev] [PATCH 03/65] glsl: Add initial functions to implement an on-disk cache

Erik Faye-Lund kusmabite at gmail.com
Sat Apr 30 09:05:59 UTC 2016


On Fri, Apr 29, 2016 at 3:33 PM, Timothy Arceri
<timothy.arceri at collabora.com> wrote:
> +/* Create a directory named 'path' if it does not already exist.
> + *
> + * Returns: 0 if path already exists as a directory or if created.
> + *         -1 in all other cases.
> + */
> +static int
> +mkdir_if_needed(char *path)
> +{
> +   struct stat sb;
> +
> +   /* If the path exists already, then our work is done if it's a
> +    * directory, but it's an error if it is not.
> +    */
> +   if (stat(path, &sb) == 0) {
> +      if (S_ISDIR(sb.st_mode)) {
> +         return 0;
> +      } else {
> +         _mesa_warning(NULL,
> +                       "Cannot use %s for shader cache (not a directory)"
> +                       "---disabling.\n", path);
> +         return -1;
> +      }
> +   }
> +
> +   if (mkdir(path, 0755) == 0)
> +      return 0;
> +
> +   _mesa_warning(NULL,
> +                 "Failed to create %s for shader cache (%s)---disabling.\n",
> +                 path, strerror(errno));
> +
> +   return -1;
> +}
> +
<snip>
> +   /* Determine path for cache based on the first defined name as follows:
> +    *
> +    *   $MESA_GLSL_CACHE_DIR
> +    *   $XDG_CACHE_HOME/mesa
> +    *   <pwd.pw_dir>/.cache/mesa
> +    */
> +   path = getenv("MESA_GLSL_CACHE_DIR");
> +   if (path && mkdir_if_needed(path) == -1) {
> +      goto fail;
> +   }
> +

Hmm, doesn't this only create the last path component (if
nonexistent)? Shouldn't we create *all* leading directories in this
case?

I would have expected to see something along the lines of this:

static int create_leading_dirs(const char *base)
{
   char *pos, buf[FILENAME_MAX];

   strncpy(buf, base, sizeof(buf));
   buf[sizeof(buf) - 1] = '\0';
   pos = buf + (*buf == '/');

   while (1) {
      struct stat st;

      pos = strchr(pos, '/');
      if (!pos)
         break;
      *pos = '\0';

      if (!stat(buf, &st)) {
         if (!S_ISDIR(st.st_mode))
            return -1;
      } else {
         if (mkdir(buf, 0777))
            return -1;
      }

      *pos++ = '/';
   }

   return 0;
}


More information about the mesa-dev mailing list