From: VMware, Inc <> Date: Thu, 2 Aug 2012 06:53:59 +0000 (-0700) Subject: lib/file: make File_Cwd() handle directories of any length X-Git-Tag: 2012.10.14-874563~54 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=aaa80d4caf24de0961a3cc0355d4b81ea9c04e5c;p=thirdparty%2Fopen-vm-tools.git lib/file: make File_Cwd() handle directories of any length 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 --- diff --git a/open-vm-tools/lib/file/filePosix.c b/open-vm-tools/lib/file/filePosix.c index 7c7149bea..f714dc8aa 100644 --- a/open-vm-tools/lib/file/filePosix.c +++ b/open-vm-tools/lib/file/filePosix.c @@ -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; }