[Bf-blender-cvs] [0c2b2cdb78c] master: BLI_filelist: minor changes to bli_builddir behavior

Campbell Barton noreply at git.blender.org
Wed Jan 25 00:51:33 CET 2023


Commit: 0c2b2cdb78c4a03386b36ab69491fa4f0cd22993
Author: Campbell Barton
Date:   Wed Jan 25 10:48:59 2023 +1100
Branches: master
https://developer.blender.org/rB0c2b2cdb78c4a03386b36ab69491fa4f0cd22993

BLI_filelist: minor changes to bli_builddir behavior

- Don't call exit() when memory allocation fails, while unlikely
  internal failures should not be exiting the application.
- Don't print a message when the directory is empty as it's
  unnecessarily noisy.
- Print errors the the stderr & include the reason for opendir failing.

===================================================================

M	source/blender/blenlib/intern/BLI_filelist.c

===================================================================

diff --git a/source/blender/blenlib/intern/BLI_filelist.c b/source/blender/blenlib/intern/BLI_filelist.c
index 69d07e52b08..ae7f6f79a17 100644
--- a/source/blender/blenlib/intern/BLI_filelist.c
+++ b/source/blender/blenlib/intern/BLI_filelist.c
@@ -4,6 +4,7 @@
  * \ingroup bli
  */
 
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
@@ -109,7 +110,10 @@ static void bli_builddir(struct BuildDirCtx *dir_ctx, const char *dirname)
 {
   DIR *dir = opendir(dirname);
   if (UNLIKELY(dir == NULL)) {
-    printf("%s non-existent directory\n", dirname);
+    fprintf(stderr,
+            "Failed to open dir (%s): %s\n",
+            errno ? strerror(errno) : "unknown error",
+            dirname);
     return;
   }
 
@@ -172,7 +176,7 @@ static void bli_builddir(struct BuildDirCtx *dir_ctx, const char *dirname)
       if (tmp) {
         dir_ctx->files = (struct direntry *)tmp;
       }
-      else { /* realloc fail */
+      else { /* Reallocation may fail. */
         MEM_freeN(dir_ctx->files);
         dir_ctx->files = NULL;
       }
@@ -182,7 +186,11 @@ static void bli_builddir(struct BuildDirCtx *dir_ctx, const char *dirname)
       dir_ctx->files = (struct direntry *)MEM_mallocN(newnum * sizeof(struct direntry), __func__);
     }
 
-    if (dir_ctx->files) {
+    if (UNLIKELY(dir_ctx->files == NULL)) {
+      fprintf(stderr, "Couldn't get memory for dir: %s\n", dirname);
+      dir_ctx->files_num = 0;
+    }
+    else {
       struct dirlink *dlink = (struct dirlink *)dirbase.first;
       struct direntry *file = &dir_ctx->files[dir_ctx->files_num];
 
@@ -194,30 +202,22 @@ static void bli_builddir(struct BuildDirCtx *dir_ctx, const char *dirname)
           file->type = file->s.st_mode;
         }
         else if (FILENAME_IS_CURRPAR(file->relname)) {
-          /* Hack around for UNC paths on windows:
-           * does not support stat on '\\SERVER\foo\..', sigh... */
+          /* Unfortunately a hack around UNC paths on WIN32,
+           * which does not support `stat` on `\\SERVER\foo\..`. */
           file->type |= S_IFDIR;
         }
         dir_ctx->files_num++;
         file++;
         dlink = dlink->next;
       }
-    }
-    else {
-      printf("Couldn't get memory for dir\n");
-      exit(1);
-    }
 
-    BLI_freelist(&dirbase);
-    if (dir_ctx->files) {
       qsort(dir_ctx->files,
             dir_ctx->files_num,
             sizeof(struct direntry),
             (int (*)(const void *, const void *))direntry_cmp);
     }
-  }
-  else {
-    printf("%s empty directory\n", dirname);
+
+    BLI_freelist(&dirbase);
   }
 
   closedir(dir);



More information about the Bf-blender-cvs mailing list