]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
lib/file: Enhance File_EnsureDirectoryEx to do more checking
authorOliver Kurth <okurth@vmware.com>
Tue, 24 Oct 2017 21:07:33 +0000 (14:07 -0700)
committerOliver Kurth <okurth@vmware.com>
Tue, 24 Oct 2017 21:07:33 +0000 (14:07 -0700)
When creating a directory, distinguish whether an EEXIST error is
because the directory already exists or because there is a file with
that path name.  Return the appropriate error.

open-vm-tools/lib/file/file.c
open-vm-tools/lib/include/file.h

index 275614853aa25ecfa9687b000b5df1188ce0c032..1ad13fdcc85b88f2cd4034e6b7367101a14063c1 100644 (file)
@@ -365,9 +365,9 @@ File_UnlinkRetry(const char *pathName,       // IN:
 
 Bool
 File_CreateDirectoryEx(const char *pathName,  // IN:
-                       int mask)              // IN:
+                       int mode)              // IN:
 {
-   int err = FileCreateDirectory(pathName, mask);
+   int err = FileCreateDirectory(pathName, mode);
 
    return err == 0;
 }
@@ -385,7 +385,7 @@ File_CreateDirectoryEx(const char *pathName,  // IN:
  * Results:
  *      TRUE   Directory was created
  *      FALSE  Directory creation failed.
- *             See File_EnsureDirectoryEx for dealing with directories that
+ *             See File_EnsureDirectory for dealing with directories that
  *             may exist.
  *
  * Side effects:
@@ -424,11 +424,28 @@ File_CreateDirectory(const char *pathName)  // IN:
 
 Bool
 File_EnsureDirectoryEx(const char *pathName,  // IN:
-                       int mask)              // IN:
+                       int mode)              // IN:
 {
-   int err = FileCreateDirectory(pathName, mask);
+   int err = FileCreateDirectory(pathName, mode);
+
+   if (err == EEXIST) {
+      FileData fileData;
+
+      err = FileAttributes(pathName, &fileData);
+
+      if (err == 0) {
+         if (fileData.fileType != FILE_TYPE_DIRECTORY) {
+            err = ENOTDIR;
+            errno = ENOTDIR;
+
+#if defined(_WIN32)
+            SetLastError(ERROR_DIRECTORY);
+#endif
+         }
+      }
+   }
 
-   return ((err == 0) || (err == EEXIST));
+   return err == 0;
 }
 
 
@@ -1679,7 +1696,7 @@ FileFirstSlashIndex(const char *pathName,     // IN:
 
 Bool
 File_CreateDirectoryHierarchyEx(const char *pathName,   // IN:
-                                int mask,               // IN:
+                                int mode,               // IN:
                                 char **topmostCreated)  // OUT/OPT:
 {
    char *volume;
@@ -1734,7 +1751,7 @@ File_CreateDirectoryHierarchyEx(const char *pathName,   // IN:
        * confusing. We avoid this by attempting to create the directory before
        * checking the type.
        */
-      err = FileCreateDirectory(temp, mask);
+      err = FileCreateDirectory(temp, mode);
 
       if (err == 0) {
          if (topmostCreated != NULL && *topmostCreated == NULL) {
index 7cf44eea00408e5dd753fba259e2821e688bfcda..d0bbe6d937b23b3cb6a6ace540fbf21301e96393 100644 (file)
@@ -1,5 +1,5 @@
 /*********************************************************
- * Copyright (C) 1998-2016 VMware, Inc. All rights reserved.
+ * Copyright (C) 1998-2017 VMware, Inc. All rights reserved.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as published
@@ -151,17 +151,20 @@ char *File_PathJoin(const char *dirName,
 Bool File_CreateDirectory(const char *pathName);
 
 Bool File_CreateDirectoryEx(const char *pathName,
-                            int mask);
+                            int mode);
 
 Bool File_EnsureDirectory(const char *pathName);
 
+Bool File_EnsureDirectoryEx(const char *pathName,
+                            int mode);
+
 Bool File_DeleteEmptyDirectory(const char *pathName);
 
 Bool File_CreateDirectoryHierarchy(const char *pathName,
                                    char **topmostCreated);
 
 Bool File_CreateDirectoryHierarchyEx(const char *pathName,
-                                     int mask,
+                                     int mode,
                                      char **topmostCreated);
 
 Bool File_DeleteDirectoryContent(const char *pathName);