From: John Wolfe Date: Mon, 7 Jun 2021 15:25:08 +0000 (-0700) Subject: Changes to common source files not applicable to open-vm-tools. X-Git-Tag: stable-12.0.0~189 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5acc2a69256aa351e38d7abe1914b7785de6d6b2;p=thirdparty%2Fopen-vm-tools.git Changes to common source files not applicable to open-vm-tools. --- diff --git a/open-vm-tools/lib/file/fileIO.c b/open-vm-tools/lib/file/fileIO.c index 4d7917571..77f3fb360 100644 --- a/open-vm-tools/lib/file/fileIO.c +++ b/open-vm-tools/lib/file/fileIO.c @@ -48,6 +48,7 @@ #include #endif #if defined(VMX86_SERVER) +#include "config.h" #include "fs_public.h" #endif @@ -1054,3 +1055,109 @@ FileIO_AtomicUpdate(FileIODescriptor *newFD, // IN/OUT: file IO descriptor { return FileIO_AtomicUpdateEx(newFD, currFD, TRUE) == 1; } + + +/* + *----------------------------------------------------------------------------- + * + * FileIOGetChoiceOnConfig -- + * + * Get the choice based on the "answer.msg.%s.file.open" in config file. + * If it's not set, will return the default 0x20. + * + * Results: + * The first character of the value or 0x20 by default. + * + * Side effects: + * None + * + *----------------------------------------------------------------------------- + */ + +static char +FileIOGetChoiceOnConfig(const char *device, // IN: device name + const char *pathName) // IN: file path +{ + char choice = 0x20; + +#if defined(VMX86_SERVER) + char *answer = Config_GetString(NULL, "answer.msg.%s.file.open", device); + + if (answer == NULL) { + Log("Appending %s output to %s. Use answer.msg.%s.file.open=\"replace\" " + "to replace file instead.", device, pathName, device); + } else { + const char *opt = answer; + + while (*opt == '_') { + opt++; + } + choice = *opt | 0x20; // tolower() for ascii + free(answer); + } +#endif + return choice; +} + + +/* + *----------------------------------------------------------------------------- + * + * FileIO_CreateDeviceFileNoPrompt -- + * + * If file does not exist, new file is created. If file does exist, + * configuration option answer.msg..file.open is consulted to + * decide whether to truncate file (replace), append data at the end of + * the file (append, the default), or fail open (cancel). + * + * Results: + * FileIOResult of call: FILEIO_SUCCESS or FILEIO_CANCELLED or other value. + * + * Side effects: + * None + * + *----------------------------------------------------------------------------- + */ + +FileIOResult +FileIO_CreateDeviceFileNoPrompt(FileIODescriptor *fd, // IN: file IO description + const char *pathName, // IN: file path + int openMode, // IN: + FileIOOpenAction action,// IN: + int perms, // IN: + const char *device) // IN: +{ + FileIOResult fret; + char choice = '\0'; + int time = 0; + + ASSERT(fd != NULL); + ASSERT(pathName != NULL); + ASSERT(device != NULL); + + while ((fret = FileIO_Create(fd, pathName, openMode, action, perms)) + == FILEIO_OPEN_ERROR_EXIST) { + if (choice == '\0') { + choice = FileIOGetChoiceOnConfig(device, pathName); + + switch (choice) { + case 'c': // Cancel + fret = FILEIO_CANCELLED; + break; + case 'r': // Replace + case 'o': // Overwrite + action = FILEIO_OPEN_CREATE_EMPTY; + break; + default: // Append, nothing, empty string + action = FILEIO_OPEN_CREATE; + break; + } + } + + if (fret == FILEIO_CANCELLED || time++ == 3) { + break; + } + } + + return fret; +} diff --git a/open-vm-tools/lib/include/fileIO.h b/open-vm-tools/lib/include/fileIO.h index 03165f93c..ba206ab5b 100644 --- a/open-vm-tools/lib/include/fileIO.h +++ b/open-vm-tools/lib/include/fileIO.h @@ -493,6 +493,15 @@ FILE *FileIO_DescriptorToStream(FileIODescriptor *fd, const char *FileIO_Filename(FileIODescriptor *fd); +#ifdef VMX86_SERVER +FileIOResult FileIO_CreateDeviceFileNoPrompt(FileIODescriptor *fd, + const char *pathName, + int openMode, + FileIOOpenAction action, + int perms, + const char *device); +#endif + /* *------------------------------------------------------------------------- *