]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Multiple changes:
authorOliver Kurth <okurth@vmware.com>
Fri, 15 Sep 2017 18:23:25 +0000 (11:23 -0700)
committerOliver Kurth <okurth@vmware.com>
Fri, 15 Sep 2017 18:23:25 +0000 (11:23 -0700)
 lib/file/file.c: - Add new function File_ContainSymLink().
 lib/file/filePosix.c: - Add new function File_GetMountPath()
 lib/include/file.h: - Add new function declarations.

 lib/include/dynbuf.h:
  - Clean up documentation.
  - Rename DynBuf_ConcatString() to DynBuf_Strcat().
  - New inline static function DynBuf_GetString().
 lib/misc/dynbuf.c:
  - Clean up documentation comments.
  - New function DynBuf_DetachString().
 lib/misc/hostinfoPosix.c:
  - Utilize new DynBuf functions.
 lib/misc/strutil.c:
  - Clarify documentaion comments based on DynBuf changes.
 lib/misc/utilMem.c:
  - Adjustment to comments.

Common source file changeis; not applicable to open-vm-tools.

open-vm-tools/lib/file/file.c
open-vm-tools/lib/file/filePosix.c
open-vm-tools/lib/include/dynbuf.h
open-vm-tools/lib/include/file.h
open-vm-tools/lib/include/productState.h
open-vm-tools/lib/misc/dynbuf.c
open-vm-tools/lib/misc/hostinfoPosix.c
open-vm-tools/lib/misc/strutil.c
open-vm-tools/lib/misc/utilMem.c
open-vm-tools/lib/misc/util_misc.c

index 1739ad91f5c431d429a81fdaac3085db567aee42..456af1f62e918c5481ba721cc47ce587fd19f232 100644 (file)
@@ -2453,3 +2453,47 @@ File_GetFSMountInfo(const char *pathName,
 }
 
 
+/*
+ *----------------------------------------------------------------------
+ *
+ * File_ContainSymLink --
+ *
+ *      Check if the specified file path contains symbolic link.
+ *
+ * Results:
+ *      return TRUE if pathName contains a symlink,
+ *      return FALSE if pathName is not a symlink or error.
+ *
+ * Side effects:
+ *      None
+ *
+ *----------------------------------------------------------------------
+ */
+
+Bool
+File_ContainSymLink(const char *pathName)  // IN:
+{
+   char *path = NULL;
+   char *base = NULL;
+   Bool retValue = FALSE;
+
+   if (File_IsSymLink(pathName)) {
+      return TRUE;
+   }
+
+   File_GetPathName(pathName, &path, &base);
+
+   if (   (path != NULL)
+       && (base != NULL)
+       && (strcmp(path, "") != 0)
+       && (strcmp(base, "") != 0)) {
+      if (File_ContainSymLink(path)) {
+         retValue = TRUE;
+      }
+   }
+
+   free(path);
+   free(base);
+
+   return retValue;
+}
index 24f2a4a39edf878a74a21037b919eaf7d04d652c..4cfc31275318fca14c07782cda4c99345d92a9b6 100644 (file)
@@ -3262,3 +3262,32 @@ File_IsCharDevice(const char *pathName)  // IN:
    return (FileAttributes(pathName, &fileData) == 0) &&
            (fileData.fileType == FILE_TYPE_CHARDEVICE);
 }
+
+
+/*
+ *----------------------------------------------------------------------------
+ *
+ * File_GetMountPath --
+ *
+ *      This function translates the path for a symlink to the physical path.
+ *      If checkEntirePath is TRUE, this function will try to translate
+ *      every parent directory to physical path.
+ *      Caller must free the returned buffer if valid path is returned.
+ *
+ * Results:
+ *      return valid physical path if successfully.
+ *      return NULL if error.
+ *
+ * Side effects:
+ *      The result is allocated.
+ *
+ *----------------------------------------------------------------------------
+ */
+
+char *
+File_GetMountPath(const char *pathName,  // IN:
+                  Bool checkEntirePath)  // IN:
+{
+   NOT_IMPLEMENTED();
+   return NULL;
+}
index b2a96a6ca4a4ef55387f939176ccd1fef2a29347..55fc2d0627c2a31311f54863bb018c40e4675717 100644 (file)
@@ -22,8 +22,8 @@
  *    Dynamic buffers
  */
 
-#ifndef __DYNBUF_H__
-#   define __DYNBUF_H__
+#ifndef DYNBUF_H
+#   define DYNBUF_H
 
 #include <string.h>
 #include "vm_basic_types.h"
@@ -62,6 +62,9 @@ DynBuf_Attach(DynBuf *b,    // IN
 void *
 DynBuf_Detach(DynBuf *b); // IN/OUT
 
+char *
+DynBuf_DetachString(DynBuf *b); // IN/OUT
+
 Bool
 DynBuf_Enlarge(DynBuf *b,        // IN/OUT
                size_t min_size); // IN
@@ -118,6 +121,38 @@ DynBuf_Get(DynBuf const *b) // IN
 }
 
 
+/*
+ *-----------------------------------------------------------------------------
+ *
+ * DynBuf_GetString --
+ *
+ * Results:
+ *      Returns a pointer to the dynamic buffer data as a NUL-terminated
+ *      string.
+ *
+ * Side effects:
+ *      DynBuf might allocate additional memory and will panic if it fails to.
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+#if defined(SWIG)
+static char *
+#else
+static INLINE char *
+#endif
+DynBuf_GetString(DynBuf *b) // IN
+{
+   ASSERT(b);
+
+   if (b->size == b->allocated) {
+      ASSERT_MEM_ALLOC(DynBuf_Enlarge(b, b->size + 1));
+   }
+   b->data[b->size] = '\0';
+   return b->data;
+}
+
+
 /*
  *-----------------------------------------------------------------------------
  *
@@ -212,16 +247,17 @@ DynBuf_GetAllocatedSize(DynBuf const *b) // IN
  *
  * DynBuf_AppendString --
  *
- *     Append the string to the specified DynBuf object.  Basically a
- *     fancy strcat().
+ *     Appends the string to the specified DynBuf object, including its NUL
+ *     terminator.  Note that this is NOT like strcat; repeated calls will
+ *     leave embedded NULs in the middle of the buffer. (Compare to
+ *     DynBuf_Strcat.)
  *
  * Results:
  *      TRUE on success
  *      FALSE on failure (not enough memory)
  *
- *
  * Side effects:
- *     DynBuf may change its size or allocate additional memory.
+ *      DynBuf may change its size or allocate additional memory.
  *
  *----------------------------------------------------------------------------
  */
@@ -234,29 +270,26 @@ static INLINE Bool
 DynBuf_AppendString(DynBuf *buf,         // IN/OUT
                     const char *string)  // IN
 {
-   /*
-    * Make sure to copy the NUL.
-    */
-   return DynBuf_Append(buf, string, strlen(string) + 1);
+   return DynBuf_Append(buf, string, strlen(string) + 1 /* NUL */);
 }
 
 
 /*
  *----------------------------------------------------------------------------
  *
- * DynBuf_ConcatString --
+ * DynBuf_Strcat --
  *
- *     Append the string to the specified DynBuf object without adding a
- *     trailing NUL.  The caller is responsible for NUL-terminating if so
- *     desired.
+ *      A DynBuf version of strcat.  Unlike DynBuf_AppendString, does NOT
+ *      visibly NUL-terminate the DynBuf, thereby allowing future appends to
+ *      do proper string concatenation without leaving embedded NULs in the
+ *      middle.
  *
  * Results:
  *      TRUE on success
  *      FALSE on failure (not enough memory)
  *
- *
  * Side effects:
- *     DynBuf may change its size or allocate additional memory.
+ *      DynBuf may change its size or allocate additional memory.
  *
  *----------------------------------------------------------------------------
  */
@@ -266,10 +299,24 @@ static Bool
 #else
 static INLINE Bool
 #endif
-DynBuf_ConcatString(DynBuf *buf,         // IN/OUT
-                    const char *string)  // IN
+DynBuf_Strcat(DynBuf *buf,         // IN/OUT
+              const char *string)  // IN
 {
-   return DynBuf_Append(buf, string, strlen(string));
+   Bool success;
+
+   ASSERT(buf != NULL);
+   ASSERT(string != NULL);
+
+   /*
+    * We actually do NUL-terminate the buffer internally, but this is not
+    * visible to callers, and they should not rely on this.
+    */
+   success = DynBuf_AppendString(buf, string);
+   if (LIKELY(success)) {
+      ASSERT(buf->size > 0);
+      buf->size--;
+   }
+   return success;
 }
 
 
@@ -277,4 +324,4 @@ DynBuf_ConcatString(DynBuf *buf,         // IN/OUT
 }  // extern "C"
 #endif
 
-#endif /* __DYNBUF_H__ */
+#endif /* DYNBUF_H */
index b714ebaf4a2d6275534f90bc794c51c14b21d85d..26d872e2187c9ee1afb4daa53db05e70c006fbd8 100644 (file)
@@ -212,6 +212,8 @@ Bool File_IsFile(const char *pathName);
 
 Bool File_IsSymLink(const char *pathName);
 
+Bool File_ContainSymLink(const char *pathName);
+
 Bool File_IsCharDevice(const char *pathName);
 
 Bool File_GetParent(char **canPath);
@@ -249,6 +251,8 @@ char *File_GetModTimeString(const char *pathName);
 
 char *File_GetUniqueFileSystemID(const char *pathName);
 
+char *File_GetMountPath(const char *pathName, Bool checkEntirePath);
+
 #ifdef _WIN32
 char *File_GetVolumeGUID(const char *pathName);
 #endif
index 6570c3861451c946924fd4193303e1e23060f86c..7d8c6f202639474bbf310e3c1573c0efb3e16ffc 100644 (file)
@@ -51,6 +51,7 @@ typedef enum {
    PRODUCT_GANTRY = 1 << 9,
    PRODUCT_VMACORETESTS = 1 << 10,
    PRODUCT_SRM = 1 << 11,
+   PRODUCT_VIEWCRT = 1 << 12,
    /* etc */
 } Product;
 typedef uint64 ProductMask;
index 8d88ce6aea31279aced9a5aa1b42aebd9f5bd079..129384154a78a8e152e26ba9dda435fa873a2eed 100644 (file)
@@ -126,7 +126,8 @@ DynBuf_Destroy(DynBuf *b)  // IN/OUT:
  *      a copy of that data.
  *
  * Results:
- *      The pointer to the data.  NULL on out of memory failure.
+ *      The pointer to the data.  NULL on out of memory failure or if the
+ *      input DynBuf is empty.
  *
  * Side effects:
  *      Allocates memory.
@@ -140,6 +141,10 @@ DynBuf_AllocGet(DynBuf const *b)  // IN:
    void *new_data;
    ASSERT(b);
 
+   if (b->size == 0) {
+      return NULL;
+   }
+
    new_data = malloc(b->size);
    if (new_data) {
       memcpy(new_data, b->data, b->size);
@@ -185,11 +190,11 @@ DynBuf_Attach(DynBuf *b,    // IN/OUT:
  *
  * DynBuf_Detach --
  *
- *      Releases ownership of the buffer stored in the DynBuf object,
- *      and returns a pointer to it.
+ *      Transfers ownership of the buffer stored in the DynBuf object to the
+ *      caller.
  *
  * Results:
- *      The pointer to the data.
+ *      Returns a pointer to the data.  The caller must free it with free().
  *
  * Side effects:
  *      None
@@ -212,6 +217,33 @@ DynBuf_Detach(DynBuf *b)  // IN/OUT:
 }
 
 
+/*
+ *-----------------------------------------------------------------------------
+ *
+ * DynBuf_DetachString --
+ *
+ *      Transfers ownership of the buffer stored in the DynBuf object to the
+ *      caller.
+ *
+ * Results:
+ *      Returns a pointer to the data as a NUL-terminated string.  The caller
+ *      must free it with free().
+ *
+ * Side effects:
+ *      None
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+char *
+DynBuf_DetachString(DynBuf *b) // IN/OUT
+{
+   char *data = DynBuf_GetString(b);
+   DynBuf_Detach(b);
+   return data;
+}
+
+
 /*
  *-----------------------------------------------------------------------------
  *
@@ -360,7 +392,7 @@ DynBuf_Append(DynBuf *b,        // IN/OUT:
 
    if (new_size > b->allocated) {
       /* Not enough room */
-      if (DynBuf_Enlarge(b, new_size) == FALSE) {
+      if (!DynBuf_Enlarge(b, new_size)) {
          return FALSE;
       }
    }
index a0cdefb5e7a7036e7d2f0b54c531a5b07889f83f..472b207714610b6bb622681a26d873b75d1f63da 100644 (file)
@@ -841,14 +841,12 @@ HostinfoGetCmdOutput(const char *cmd)  // IN:
          break;
       }
 
-      /* size does -not- include the NUL terminator. */
-      DynBuf_Append(&db, line, size + 1);
+      /* size does not include the NUL terminator. */
+      DynBuf_Append(&db, line, size);
       free(line);
    }
 
-   if (DynBuf_Get(&db)) {
-      out = (char *) DynBuf_AllocGet(&db);
-   }
+   out = DynBuf_DetachString(&db);
 
  closeIt:
    pclose(stream);
index d00e560f5f3679509d06b0f14dcbfe1b38727017..1ff111aaf2330401ee2fe23dafa1b63133b6c195 100644 (file)
@@ -960,8 +960,7 @@ StrUtil_IsASCII(const char *s) // IN
  * StrUtil_VDynBufPrintf --
  *
  *      This is a vprintf() variant which appends directly into a
- *      dynbuf. The dynbuf is not NUL-terminated: The printf() result
- *      is written immediately after the last byte in the DynBuf.
+ *      DynBuf.  Does NOT visibly NUL-terminate the DynBuf.
  *
  *      This function does not use any temporary buffer. The printf()
  *      result can be arbitrarily large. This function automatically
@@ -1012,6 +1011,11 @@ StrUtil_VDynBufPrintf(DynBuf *b,        // IN/OUT
          va_list tmpArgs;
 
          va_copy(tmpArgs, args);
+
+         /*
+          * We actually do NUL-terminate the buffer internally, but this is not
+          * visible to callers, and they should not rely on this.
+          */
          i = Str_Vsnprintf((char *) DynBuf_Get(b) + size, allocSize - size,
                            fmt, tmpArgs);
          va_end(tmpArgs);
index 4c45cf52438ef4740421c0252a800e93e12d9d17..ca80ace54fb368d47a570c040830be5a16cf2423 100644 (file)
@@ -69,7 +69,8 @@ UtilAllocationFailure1(int bugNumber, const char *file, int lineno)
  *
  * UtilSafeMalloc0 --
  * UtilSafeMalloc1 --
- *      Helper function for malloc
+ *
+ *      Helper functions for Util_SafeMalloc.
  *
  * Results:
  *      Pointer to the dynamically allocated memory.
@@ -110,7 +111,8 @@ UtilSafeMalloc1(size_t size,            // IN:
  *
  * UtilSafeRealloc0 --
  * UtilSafeRealloc1 --
- *      Helper function for realloc
+ *
+ *      Helper functions for Util_SafeRealloc.
  *
  * Results:
  *      Pointer to the dynamically allocated memory.
@@ -153,7 +155,8 @@ UtilSafeRealloc1(void *ptr,            // IN:
  *
  * UtilSafeCalloc0 --
  * UtilSafeCalloc1 --
- *      Helper function for calloc
+ *
+ *      Helper functions for Util_SafeCalloc.
  *
  * Results:
  *      Pointer to the dynamically allocated memory.
@@ -194,8 +197,10 @@ UtilSafeCalloc1(size_t nmemb,         // IN:
 /*
  *-----------------------------------------------------------------------------
  *
- * Util_SafeStrdup --
- *      Helper function for strdup
+ * UtilSafeStrdup0 --
+ * UtilSafeStrdup1 --
+ *
+ *      Helper functions for Util_SafeStrdup.
  *
  * Results:
  *      Pointer to the dynamically allocated, duplicate string
@@ -248,7 +253,10 @@ UtilSafeStrdup1(const char *s,        // IN:
 /*
  *-----------------------------------------------------------------------------
  *
- * Util_SafeStrndup --
+ * UtilSafeStrndup0 --
+ * UtilSafeStrndup1 --
+ *
+ *      Helper functions for Util_SafeStrndup.
  *
  *      Returns a string consisting of first n characters of 's' if 's' has
  *      length >= 'n', otherwise returns a string duplicate of 's'.
index 00f83cb2462c8ba925077c04202cfb250591d1d4..98389d4de7e4cf443d2c5f92fddd71829b3765e3 100644 (file)
@@ -560,21 +560,26 @@ UtilDoTildeSubst(const char *user)  // IN: name of user
    if (*user == '\0') {
 #if defined(__APPLE__)
       /*
-       * The HOME environment variable is not always set on Mac OS.
-       * (was bug 841728)
+       * This check mimics the checks and order of CFCopyHomeDirectoryURL(),
+       * which is unfortunately not callable directly since Apple has marked it
+       * as only in iOS despite the fact that they clearly ship it on macOS.
        */
+      str = issetugid() ? NULL
+                        : Unicode_Duplicate(Posix_Getenv("CFFIXED_USER_HOME"));
+
       if (str == NULL) {
          pwd = Posix_Getpwuid(getuid());
          if (pwd == NULL) {
             Log("Could not get passwd for current user.\n");
          }
       }
-#else // !defined(__APPLE__)
-      str = Unicode_Duplicate(Posix_Getenv("HOME"));
-      if (str == NULL) {
-         Log("Could not expand environment variable HOME.\n");
+#endif // defined(__APPLE__)
+      if (str == NULL && pwd == NULL) {
+         str = Unicode_Duplicate(Posix_Getenv("HOME"));
+         if (str == NULL) {
+            Log("Could not expand environment variable HOME.\n");
+         }
       }
-#endif // !defined(__APPLE__)
    } else {
       pwd = Posix_Getpwnam(user);
       if (pwd == NULL) {