From: VMware, Inc <> Date: Tue, 26 Apr 2011 21:27:00 +0000 (-0700) Subject: lib/file: prep change before fix X-Git-Tag: 2011.04.25-402641~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=856d2e7da570c4452a85eb051965ed95ca840e9a;p=thirdparty%2Fopen-vm-tools.git lib/file: prep change before fix 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 --- diff --git a/open-vm-tools/lib/file/file.c b/open-vm-tools/lib/file/file.c index 7387db712..9c367bc21 100644 --- a/open-vm-tools/lib/file/file.c +++ b/open-vm-tools/lib/file/file.c @@ -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 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); - -} - - /* *---------------------------------------------------------------------- * diff --git a/open-vm-tools/lib/file/fileTemp.c b/open-vm-tools/lib/file/fileTemp.c index 711076283..10dab5f0b 100644 --- a/open-vm-tools/lib/file/fileTemp.c +++ b/open-vm-tools/lib/file/fileTemp.c @@ -20,12 +20,218 @@ #include #endif +#include +#include +#include +#include +#include +#include +#include + #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 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; }