]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
lib/file: make File_Cwd() handle directories of any length
authorVMware, Inc <>
Thu, 2 Aug 2012 06:53:59 +0000 (23:53 -0700)
committerDmitry Torokhov <dtor@vmware.com>
Thu, 2 Aug 2012 18:23:09 +0000 (11:23 -0700)
In the I18n world the current working directory can be essentially
unlimited in length. Using FILE_MAXPATH is outdated. Make File_Cwd
handle a current working directory of any length.

Signed-off-by: Dmitry Torokhov <dtor@vmware.com>
open-vm-tools/lib/file/filePosix.c

index 7c7149beaec058f20062e24e4c1e5bc0b2bb2be3..f714dc8aa57e76bfbc02a773fbd493b9ca2b6e23 100644 (file)
@@ -125,6 +125,9 @@ struct WalkDirContextImpl
 #define NO_ENDMNTENT
 #endif
 
+/* Long path chunk growth size */
+#define FILE_PATH_GROW_SIZE 1024
+
 
 /*
  *-----------------------------------------------------------------------------
@@ -459,9 +462,8 @@ File_IsSymLink(ConstUnicode pathName)  // IN:
  *
  * File_Cwd --
  *
- *      Find the current directory on drive DRIVE.
- *      DRIVE is either NULL (current drive) or a string
- *      starting with [A-Za-z].
+ *      Find the current directory on drive DRIVE. DRIVE is either NULL
+ *      (current drive) or a string starting with [A-Za-z].
  *
  * Results:
  *      NULL if error.
@@ -475,26 +477,51 @@ File_IsSymLink(ConstUnicode pathName)  // IN:
 Unicode
 File_Cwd(ConstUnicode drive)  // IN:
 {
-   char buffer[FILE_MAXPATH];
+   size_t size;
+   char *buffer;
+   Unicode path;
 
    if ((drive != NULL) && !Unicode_IsEmpty(drive)) {
       Warning(LGPFX" %s: Drive letter %s on Linux?\n", __FUNCTION__,
               UTF8(drive));
    }
 
-   if (getcwd(buffer, FILE_MAXPATH) == NULL) {
+   size = FILE_PATH_GROW_SIZE;
+   buffer = Util_SafeMalloc(size);
+
+   while (TRUE) {
+      if (getcwd(buffer, size) != NULL) {
+         break;
+      }
+
+      free(buffer);
+      buffer = NULL;
+
+      if (errno != ENAMETOOLONG) {
+         break;
+      }
+
+      size += FILE_PATH_GROW_SIZE;
+      buffer = Util_SafeMalloc(size);
+   }
+
+   if (buffer == NULL) {
       Msg_Append(MSGID(filePosix.getcwd)
                  "Unable to retrieve the current working directory: %s. "
-                 "Check if the directory has been deleted or "
-                 "unmounted.\n",
+                 "Check if the directory has been deleted or unmounted.\n",
                  Msg_ErrString());
+
       Warning(LGPFX" %s: getcwd() failed: %s\n", __FUNCTION__,
               Msg_ErrString());
 
       return NULL;
-   };
+   }
+
+   path = Unicode_Alloc(buffer, STRING_ENCODING_DEFAULT);
+
+   free(buffer);
 
-   return Unicode_Alloc(buffer, STRING_ENCODING_DEFAULT);
+   return path;
 }