]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
lib/fs: Fix and simplify make_path()
authorPhil Sutter <phil@nwl.cc>
Thu, 24 Aug 2017 09:41:30 +0000 (11:41 +0200)
committerStephen Hemminger <stephen@networkplumber.org>
Thu, 24 Aug 2017 22:22:10 +0000 (15:22 -0700)
Calling stat() before mkdir() is racey: The entry might change in
between. Also, the call to stat() seems to exist only to check if the
directory exists already. So simply call mkdir() unconditionally and
catch only errors other than EEXIST.

Signed-off-by: Phil Sutter <phil@nwl.cc>
lib/fs.c

index 1ff881ecfcd8c5edb022fdf1e9d48bdef176bbbd..ebe05cd44e11bca1ff1481a3c628db61fd2a1fae 100644 (file)
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -102,7 +102,6 @@ out:
 int make_path(const char *path, mode_t mode)
 {
        char *dir, *delim;
-       struct stat sbuf;
        int rc = -1;
 
        delim = dir = strdup(path);
@@ -120,20 +119,11 @@ int make_path(const char *path, mode_t mode)
                if (delim)
                        *delim = '\0';
 
-               if (stat(dir, &sbuf) != 0) {
-                       if (errno != ENOENT) {
-                               fprintf(stderr,
-                                       "stat failed for %s: %s\n",
-                                       dir, strerror(errno));
-                               goto out;
-                       }
-
-                       if (mkdir(dir, mode) != 0) {
-                               fprintf(stderr,
-                                       "mkdir failed for %s: %s\n",
-                                       dir, strerror(errno));
-                               goto out;
-                       }
+               rc = mkdir(dir, mode);
+               if (mkdir(dir, mode) != 0 && errno != EEXIST) {
+                       fprintf(stderr, "mkdir failed for %s: %s\n",
+                               dir, strerror(errno));
+                       goto out;
                }
 
                if (delim == NULL)