build - install should not complain if the mkdir() races
authorMatthew Dillon <dillon@apollo.backplane.com>
Tue, 29 Nov 2011 21:32:27 +0000 (13:32 -0800)
committerMatthew Dillon <dillon@apollo.backplane.com>
Tue, 29 Nov 2011 21:32:27 +0000 (13:32 -0800)
* install should not complain if the mkdir() races

* A make -j N can cause concurrent install's to race their mkdir() calls.

usr.bin/xinstall/xinstall.c

index a1add6d..0484383 100644 (file)
@@ -813,6 +813,25 @@ strip(const char *to_name)
 }
 
 /*
+ * When doing a concurrent make -j N multiple install's can race the mkdir.
+ */
+static
+int
+mkdir_race(const char *path, int nmode)
+{
+       int res;
+       struct stat sb;
+
+       res = mkdir(path, nmode);
+       if (res < 0 && errno == EEXIST) {
+               if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode))
+                       return(0);
+               res = mkdir(path, mode);
+       }
+       return (res);
+}
+
+/*
  * install_dir --
  *     build directory hierarchy
  */
@@ -828,7 +847,8 @@ install_dir(char *path)
                        ch = *p;
                        *p = '\0';
                        if (stat(path, &sb)) {
-                               if (errno != ENOENT || mkdir(path, 0755) < 0) {
+                               if (errno != ENOENT ||
+                                   mkdir_race(path, 0755) < 0) {
                                        err(EX_OSERR, "mkdir %s", path);
                                        /* NOTREACHED */
                                } else if (verbose)