Sync code with the original from mkdir(1).
authorclaudio <claudio@openbsd.org>
Mon, 17 May 2021 11:52:10 +0000 (11:52 +0000)
committerclaudio <claudio@openbsd.org>
Mon, 17 May 2021 11:52:10 +0000 (11:52 +0000)
OK benno@

usr.bin/rsync/mkpath.c

index c8c7bd4..1625508 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: mkpath.c,v 1.4 2019/05/08 21:30:11 benno Exp $ */
+/*     $OpenBSD: mkpath.c,v 1.5 2021/05/17 11:52:10 claudio Exp $ */
 /*
  * Copyright (c) 1983, 1992, 1993
  *     The Regents of the University of California.  All rights reserved.
@@ -46,29 +46,35 @@ mkpath(char *path)
 {
        struct stat sb;
        char *slash;
-       int done = 0;
+       int done;
 
        slash = path;
 
-       while (!done) {
+       for (;;) {
                slash += strspn(slash, "/");
                slash += strcspn(slash, "/");
 
                done = (*slash == '\0');
                *slash = '\0';
 
-               if (stat(path, &sb)) {
-                       if (errno != ENOENT || (mkdir(path, 0777) &&
-                           errno != EEXIST)) {
-                               ERR("%s: stat", path);
+               if (mkdir(path, 0777) != 0) {
+                       int mkdir_errno = errno;
+
+                       if (stat(path, &sb) == -1) {
+                               /* Not there; use mkdir()s errno */
+                               errno = mkdir_errno;
+                               return (-1);
+                       }
+                       if (!S_ISDIR(sb.st_mode)) {
+                               /* Is there, but isn't a directory */
+                               errno = ENOTDIR;
                                return (-1);
                        }
-               } else if (!S_ISDIR(sb.st_mode)) {
-                       errno = ENOTDIR;
-                       ERR("%s: stat", path);
-                       return (-1);
                }
 
+               if (done)
+                       break;
+
                *slash = '/';
        }