fix musl problem with dirname, now for all users of bb_make_directory()
authorDenys Vlasenko <vda.linux@googlemail.com>
Sun, 4 Dec 2016 09:42:07 +0000 (10:42 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 4 Dec 2016 09:42:07 +0000 (10:42 +0100)
function                                             old     new   delta
bb_make_directory                                    412     419      +7
install_main                                         793     769     -24
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 7/-24)             Total: -17 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
coreutils/install.c
libbb/make_directory.c

index d0dcd0e8957663e7f3f173d5f649bf6ae5e69ee6..831f9b802924c1aa93df97905c954e8f8a0fe854 100644 (file)
@@ -209,16 +209,10 @@ int install_main(int argc, char **argv)
                        dest = last;
                        if (opts & OPT_MKDIR_LEADING) {
                                char *ddir = xstrdup(dest);
-                               char *dn = dirname(ddir);
-                               /* musl can return read-only "/" or "." string.
-                                * bb_make_directory needs writable string.
+                               bb_make_directory(dirname(ddir), 0755, mkdir_flags);
+                               /* errors are not checked. copy_file
+                                * will fail if dir is not created.
                                 */
-                               if ((dn[0] != '/' && dn[0] != '.') || dn[1] != '\0') {
-                                       bb_make_directory(dn, 0755, mkdir_flags);
-                                       /* errors are not checked. copy_file
-                                        * will fail if dir is not created.
-                                        */
-                               }
                                free(ddir);
                        }
                        if (isdir)
index 89352ca1f180d782dd08465f7923e23d169a35ee..a6b7c28df06c7366afc51d3a2ce528f3b8858be5 100644 (file)
@@ -35,9 +35,20 @@ int FAST_FUNC bb_make_directory(char *path, long mode, int flags)
        char c;
        struct stat st;
 
-       /* Happens on bb_make_directory(dirname("no_slashes"),...) */
-       if (LONE_CHAR(path, '.'))
+       /* "path" can be a result of dirname().
+        * dirname("no_slashes") returns ".", possibly read-only.
+        * musl dirname() can return read-only "/" too.
+        * We need writable string. And for "/", "." (and ".."?)
+        * nothing needs to be created anyway.
+        */
+       if (LONE_CHAR(path, '/'))
                return 0;
+       if (path[0] == '.') {
+               if (path[1] == '\0')
+                       return 0; /* "." */
+//             if (path[1] == '.' && path[2] == '\0')
+//                     return 0; /* ".." */
+       }
 
        org_mask = cur_mask = (mode_t)-1L;
        s = path;