OSDN Git Service

make_ext4fs: workaround for a glibc scandir bug
authorMihai Serban <mihai.serban@intel.com>
Wed, 7 Jan 2015 10:44:24 +0000 (12:44 +0200)
committerElliott Hughes <enh@google.com>
Thu, 8 Jan 2015 17:44:15 +0000 (17:44 +0000)
Due to a bug in glibc scandir the make_ext4fs tool fails sometimes
with error message:
"error: build_directory_structure: scandir: Cannot allocate memory"
A simple retry of the failed scandir in case errno is ENOMEM
greatly increases the chances to finish the operation successfully.

The scandir bug is reported here:
https://sourceware.org/bugzilla/show_bug.cgi?id=17804

Change-Id: I87fa283106c0215e4b358459022497d9ec1edb60
Signed-off-by: Mihai Serban <mihai.serban@intel.com>
ext4_utils/make_ext4fs.c

index 2f89ae8..62a3f1a 100644 (file)
@@ -143,8 +143,18 @@ static u32 build_directory_structure(const char *full_path, const char *dir_path
        if (full_path) {
                entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
                if (entries < 0) {
-                       error_errno("scandir");
-                       return EXT4_ALLOCATE_FAILED;
+#ifdef __GLIBC__
+                       /* The scandir function implemented in glibc has a bug that makes it
+                          erroneously fail with ENOMEM under certain circumstances.
+                          As a workaround we can retry the scandir call with the same arguments.
+                          GLIBC BZ: https://sourceware.org/bugzilla/show_bug.cgi?id=17804 */
+                       if (errno == ENOMEM)
+                               entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
+#endif
+                       if (entries < 0) {
+                               error_errno("scandir");
+                               return EXT4_ALLOCATE_FAILED;
+                       }
                }
        }