]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
lib/file: prep change before fix
authorVMware, Inc <>
Tue, 26 Apr 2011 21:27:00 +0000 (14:27 -0700)
committerMarcelo Vanzin <mvanzin@vmware.com>
Tue, 26 Apr 2011 21:27:00 +0000 (14:27 -0700)
Move the "make temp" functions from file.c to fileTemp.c.
Once properly collected and under the same roof, further
considation, simplification and a fix can be done

Signed-off-by: Marcelo Vanzin <mvanzin@vmware.com>
open-vm-tools/lib/file/file.c
open-vm-tools/lib/file/fileTemp.c

index 7387db7127f798fe1cbad1269217acf8c94a653b..9c367bc21d8fe87200693fa315ea61bd97010a4a 100644 (file)
@@ -69,9 +69,6 @@
 
 #include "unicodeOperations.h"
 
-#if !defined(O_BINARY)
-#define O_BINARY 0
-#endif
 
 /*
  *----------------------------------------------------------------------
@@ -373,7 +370,7 @@ static const char *
 GetOldMachineID(void)
 {
    static Atomic_Ptr atomic; /* Implicitly initialized to NULL. --mbellon */
-   const char        *machineID;
+   const char *machineID;
 
    machineID = Atomic_ReadPtr(&atomic);
 
@@ -441,7 +438,7 @@ const char *
 FileLockGetMachineID(void)
 {
    static Atomic_Ptr atomic; /* Implicitly initialized to NULL. --mbellon */
-   const char        *machineID;
+   const char *machineID;
 
    machineID = Atomic_ReadPtr(&atomic);
 
@@ -664,203 +661,6 @@ File_IsFile(ConstUnicode pathName)  // IN:
 }
 
 
-/*
- *----------------------------------------------------------------------
- *
- *  File_MakeTempEx2 --
- *
- *      Create a temporary file or a directory.
- *      If a temporary file is created successfully, then return an open file
- *      descriptor to that file.
- *
- *      'dir' specifies the directory in which to create the file. It
- *      must not end in a slash.
- *
- *      'createTempFile', if TRUE, then a temporary file will be created. If
- *      FALSE, then a temporary directory will be created.
- *
- *      'createNameFunc' specifies the user-specified callback function that
- *      will be called to construct the fileName. 'createNameFuncData' will be
- *      passed everytime 'createNameFunc' is called. 'createNameFunc'
- *      should return the proper fileName.
- *
- *      Check the documentation for File_MakeTempHelperFunc.
- *
- * Results:
- *      if a temporary file is created, then Open file descriptor or -1;
- *      if a temporary directory is created, then 0 or -1;
- *      If successful then presult points to a dynamically allocated
- *      string with the pathname of the temp file.
- *
- * Side effects:
- *      Creates a file if successful. Errno is set on error
- *
- *----------------------------------------------------------------------
- */
-
-int
-File_MakeTempEx2(ConstUnicode dir,                             // IN:
-                 Bool createTempFile,                          // IN:
-                 File_MakeTempCreateNameFunc *createNameFunc,  // IN:
-                 void *createNameFuncData,                     // IN:
-                 Unicode *presult)                             // OUT:
-{
-   int fd = -1;
-   int var;
-   uint32 i;
-
-   Unicode path = NULL;
-
-   if ((dir == NULL) || (createNameFunc == NULL)) {
-      errno = EFAULT;
-      return -1;
-   }
-
-   ASSERT(presult);
-
-   *presult = NULL;
-
-   for (i = 0, var = 0; i < 0xFFFFFFFF;
-        i++, var += (FileSimpleRandom() >> 8) & 0xFF) {
-      Unicode fileName;
-
-      /* construct suffixed pathname to use */
-      Unicode_Free(path);
-      path = NULL;
-
-      fileName = (*createNameFunc)(var, createNameFuncData);
-      ASSERT(fileName);
-
-      /* construct base full pathname to use */
-      path = Unicode_Join(dir, DIRSEPS, fileName, NULL);
-
-      Unicode_Free(fileName);
-
-      if (createTempFile) {
-         fd = Posix_Open(path, O_CREAT | O_EXCL | O_BINARY | O_RDWR, 0600);
-#if defined(_WIN32)
-         /*
-          * On windows, Posix_Open() fails with EACCES if there is any
-          * access violation while creating the file. Also, EACCES is returned
-          * if a directory already exists with the same name. In such case,
-          * we need to check if a file already exists and ignore EACCES error.
-          */
-
-         if ((fd == -1) && (errno == EACCES) && (File_Exists(path))) {
-            continue;
-         }
-#endif
-      } else {
-         fd = Posix_Mkdir(path, 0600);
-      }
-
-      if (fd != -1) {
-         *presult = path;
-         path = NULL;
-         break;
-      }
-
-      if (errno != EEXIST) {
-         Log(LGPFX" Failed to create temporary %s \"%s\": %s.\n",
-             createTempFile ? "file" : "directory",
-             UTF8(path), strerror(errno));
-         goto exit;
-      }
-   }
-
-   if (fd == -1) {
-      Warning(LGPFX" Failed to create temporary %s \"%s\": "
-              "The name space is full.\n",
-              createTempFile ? "file" : "directory", UTF8(path));
-
-      errno = EAGAIN;
-   }
-
-  exit:
-   Unicode_Free(path);
-
-   return fd;
-}
-
-
-/*
- *----------------------------------------------------------------------------
- *
- * FileMakeTempExCreateNameFunc --
- *
- *      This is a helper function designed for File_MakeTempEx function.
- *      Everytime this function is called, this creates a fileName with the
- *      format <num><fileName> and returns back to the caller.
- *
- *      'num' specifies the nth time this function is called.
- *
- *      'data' specifies the payload that is specified when File_MakeTempEx2()
- *      function is called. This points to a Unicode string.
- *
- * Results:
- *      if successful, a dynamically allocated string with the basename of
- *      the temp file. NULL otherwise.
- *
- * Side effects:
- *      None
- *
- *----------------------------------------------------------------------------
- */
-
-static Unicode
-FileMakeTempExCreateNameFunc(int num,     // IN:
-                             void *data)  // IN:
-{
-   Unicode filePath;
-
-   if (data == NULL) {
-      return NULL;
-   }
-
-   filePath = Unicode_Format("%s%d", (Unicode) data, num);
-
-   return filePath;
-}
-
-
-/*
- *----------------------------------------------------------------------
- *
- *  File_MakeTempEx --
- *
- *      Create a temporary file and, if successful, return an open file
- *      descriptor to that file.
- *
- *      'dir' specifies the directory in which to create the file. It
- *      must not end in a slash.
- *
- *      'fileName' specifies the base filename of the created file.
- *
- * Results:
- *      Open file descriptor or -1; if successful then presult points
- *      to a dynamically allocated string with the pathname of the temp
- *      file.
- *
- * Side effects:
- *      Creates a file if successful. Errno is set on error
- *
- *----------------------------------------------------------------------
- */
-
-int
-File_MakeTempEx(ConstUnicode dir,       // IN:
-                ConstUnicode fileName,  // IN:
-                Unicode *presult)       // OUT:
-{
-   return File_MakeTempEx2(dir,
-                           TRUE,
-                           FileMakeTempExCreateNameFunc,
-                           (void *) fileName,
-                           presult);
-
-}
-
-
 /*
  *----------------------------------------------------------------------
  *
index 71107628393c0e6068bd532578fb73385c51eb49..10dab5f0b8fa9efbbdebd904a078924897fe8bb1 100644 (file)
 #include <windows.h>
 #endif
 
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <fcntl.h>
+
 #include "vmware.h"
 #include "log.h"
 #include "file.h"
+#include "fileInt.h"
 #include "util.h"
 #include "unicodeOperations.h"
 
+#if !defined(O_BINARY)
+#define O_BINARY 0
+#endif
+
+
+/*
+ *----------------------------------------------------------------------
+ *
+ *  File_MakeTempEx2 --
+ *
+ *      Create a temporary file or a directory.
+ *      If a temporary file is created successfully, then return an open file
+ *      descriptor to that file.
+ *
+ *      'dir' specifies the directory in which to create the file. It
+ *      must not end in a slash.
+ *
+ *      'createTempFile', if TRUE, then a temporary file will be created. If
+ *      FALSE, then a temporary directory will be created.
+ *
+ *      'createNameFunc' specifies the user-specified callback function that
+ *      will be called to construct the fileName. 'createNameFuncData' will be
+ *      passed everytime 'createNameFunc' is called. 'createNameFunc'
+ *      should return the proper fileName.
+ *
+ *      Check the documentation for File_MakeTempHelperFunc.
+ *
+ * Results:
+ *      if a temporary file is created, then Open file descriptor or -1;
+ *      if a temporary directory is created, then 0 or -1;
+ *      If successful then presult points to a dynamically allocated
+ *      string with the pathname of the temp file.
+ *
+ * Side effects:
+ *      Creates a file if successful. Errno is set on error
+ *
+ *----------------------------------------------------------------------
+ */
+
+int
+File_MakeTempEx2(ConstUnicode dir,                             // IN:
+                 Bool createTempFile,                          // IN:
+                 File_MakeTempCreateNameFunc *createNameFunc,  // IN:
+                 void *createNameFuncData,                     // IN:
+                 Unicode *presult)                             // OUT:
+{
+   int fd = -1;
+   int var;
+   uint32 i;
+
+   Unicode path = NULL;
+
+   if ((dir == NULL) || (createNameFunc == NULL)) {
+      errno = EFAULT;
+      return -1;
+   }
+
+   ASSERT(presult);
+
+   *presult = NULL;
+
+   for (i = 0, var = 0; i < 0xFFFFFFFF;
+        i++, var += (FileSimpleRandom() >> 8) & 0xFF) {
+      Unicode fileName;
+
+      /* construct suffixed pathname to use */
+      Unicode_Free(path);
+      path = NULL;
+
+      fileName = (*createNameFunc)(var, createNameFuncData);
+      ASSERT(fileName);
+
+      /* construct base full pathname to use */
+      path = Unicode_Join(dir, DIRSEPS, fileName, NULL);
+
+      Unicode_Free(fileName);
+
+      if (createTempFile) {
+         fd = Posix_Open(path, O_CREAT | O_EXCL | O_BINARY | O_RDWR, 0600);
+#if defined(_WIN32)
+         /*
+          * On Windows, Posix_Open() fails with EACCES if there is any
+          * access violation while creating the file. Also, EACCES is returned
+          * if a directory already exists with the same name. In such case,
+          * we need to check if a file already exists and ignore EACCES error.
+          */
+
+         if ((fd == -1) && (errno == EACCES) && (File_Exists(path))) {
+            continue;
+         }
+#endif
+      } else {
+         fd = Posix_Mkdir(path, 0600);
+      }
+
+      if (fd != -1) {
+         *presult = path;
+         path = NULL;
+         break;
+      }
+
+      if (errno != EEXIST) {
+         Log(LGPFX" Failed to create temporary %s \"%s\": %s.\n",
+             createTempFile ? "file" : "directory",
+             UTF8(path), strerror(errno));
+         goto exit;
+      }
+   }
+
+   if (fd == -1) {
+      Warning(LGPFX" Failed to create temporary %s \"%s\": "
+              "The name space is full.\n",
+              createTempFile ? "file" : "directory", UTF8(path));
+
+      errno = EAGAIN;
+   }
+
+  exit:
+   Unicode_Free(path);
+
+   return fd;
+}
+
+
+/*
+ *----------------------------------------------------------------------------
+ *
+ * FileMakeTempExCreateNameFunc --
+ *
+ *      This is a helper function designed for File_MakeTempEx function.
+ *      Everytime this function is called, this creates a fileName with the
+ *      format <num><fileName> and returns back to the caller.
+ *
+ *      'num' specifies the nth time this function is called.
+ *
+ *      'data' specifies the payload that is specified when File_MakeTempEx2()
+ *      function is called. This points to a Unicode string.
+ *
+ * Results:
+ *      if successful, a dynamically allocated string with the basename of
+ *      the temp file. NULL otherwise.
+ *
+ * Side effects:
+ *      None
+ *
+ *----------------------------------------------------------------------------
+ */
+
+static Unicode
+FileMakeTempExCreateNameFunc(int num,     // IN:
+                             void *data)  // IN:
+{
+   Unicode filePath;
+
+   if (data == NULL) {
+      return NULL;
+   }
+
+   filePath = Unicode_Format("%s%d", (Unicode) data, num);
+
+   return filePath;
+}
+
+
+/*
+ *----------------------------------------------------------------------
+ *
+ *  File_MakeTempEx --
+ *
+ *      Create a temporary file and, if successful, return an open file
+ *      descriptor to that file.
+ *
+ *      'dir' specifies the directory in which to create the file. It
+ *      must not end in a slash.
+ *
+ *      'fileName' specifies the base filename of the created file.
+ *
+ * Results:
+ *      Open file descriptor or -1; if successful then presult points
+ *      to a dynamically allocated string with the pathname of the temp
+ *      file.
+ *
+ * Side effects:
+ *      Creates a file if successful. Errno is set on error
+ *
+ *----------------------------------------------------------------------
+ */
+
+int
+File_MakeTempEx(ConstUnicode dir,       // IN:
+                ConstUnicode fileName,  // IN:
+                Unicode *presult)       // OUT:
+{
+   return File_MakeTempEx2(dir, TRUE, FileMakeTempExCreateNameFunc,
+                           (void *) fileName, presult);
+}
+
 
 /*
  *----------------------------------------------------------------------
@@ -47,7 +253,7 @@ int
 File_MakeSafeTemp(ConstUnicode tag,  // IN (OPT):
                   Unicode *presult)  // OUT:
 {
-   int fd = -1;
+   int fd;
    Unicode dir = NULL;
    Unicode fileName = NULL;
 
@@ -114,7 +320,7 @@ File_DoesVolumeSupportAcls(ConstUnicode path)  // IN:
          goto exit;
       }
    } else {
-      Log("%s: GetVolumeInformation failed: %d\n", __FUNCTION__,
+      Log(LGPFX" %s: GetVolumeInformation failed: %d\n", __FUNCTION__,
           GetLastError());
       goto exit;
    }