From: John Wolfe Date: Mon, 5 Apr 2021 16:01:41 +0000 (-0700) Subject: Don't follow symlinks in File_Rotate_ByRename(). X-Git-Tag: stable-11.3.0~88 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=761631d7c7d7c0210dafd6f4e4d5281a6d46a52a;p=thirdparty%2Fopen-vm-tools.git Don't follow symlinks in File_Rotate_ByRename(). Delete the directory entries, not where they point to in the case of symlinks. Also, clean up some of the related and similar source code. --- diff --git a/open-vm-tools/lib/file/file.c b/open-vm-tools/lib/file/file.c index 54e850ce8..46933c594 100644 --- a/open-vm-tools/lib/file/file.c +++ b/open-vm-tools/lib/file/file.c @@ -328,10 +328,13 @@ File_UnlinkRetry(const char *pathName, // IN: do { ret = FileDeletion(pathName, TRUE); + if (ret != EBUSY || waitMilliSec >= maxWaitTimeMilliSec) { break; } + Log(LGPFX" %s: %s after %u ms\n", __FUNCTION__, pathName, unlinkWait); + Util_Usleep(unlinkWait * 1000); waitMilliSec += unlinkWait; } while (TRUE); @@ -2361,22 +2364,18 @@ FileRotateByRename(const char *fileName, // IN: full path to file Str_SafeAsprintf(NULL, "%s-%d%s", baseName, i - 1, ext); if (dst == NULL) { - result = File_UnlinkIfExists(src); + result = FileDeletion(src, FALSE); // Don't follow a symlink! - if (result == -1) { + if ((result != 0) && (result != ENOENT)) { Log(LGPFX" %s: failed to remove %s: %s\n", __FUNCTION__, - src, Msg_ErrString()); + src, Err_Errno2String(Err_Errno())); } } else { - result = Posix_Rename(src, dst); + result = File_Rename(src, dst); - if (result == -1) { - int error = Err_Errno(); - - if (error != ENOENT) { - Log(LGPFX" %s: failed to rename %s -> %s: %s\n", src, dst, - __FUNCTION__, Err_Errno2String(error)); - } + if ((result != 0) && (result != ENOENT)) { + Log(LGPFX" %s: rename of %s -> %s failed: %s\n", src, dst, + __FUNCTION__, Err_Errno2String(Err_Errno())); } } @@ -2547,11 +2546,11 @@ FileRotateByRenumber(const char *filePath, // IN: full path to file char *from = Str_SafeAsprintf(NULL, "%s/%s-%u%s", baseDir, baseName, fileNumbers[i], ext); - if (Posix_Rename(from, to) == -1) { - int error = Err_Errno(); + result = File_Rename(from, to); - Log(LGPFX" %s: failed to rename %s -> %s failed: %s\n", __FUNCTION__, - from, to, Err_Errno2String(error)); + if (result != 0) { + Log(LGPFX" %s: rename of %s -> %s failed: %s\n", __FUNCTION__, + from, to, Err_Errno2String(Err_Errno())); } free(to); @@ -2565,21 +2564,16 @@ FileRotateByRenumber(const char *filePath, // IN: full path to file } /* Rename the existing file to the next number */ - tmp = Str_SafeAsprintf(NULL, "%s/%s-%u%s", baseDir, baseName, - maxNr + 1, ext); + tmp = Str_SafeAsprintf(NULL, "%s/%s-%u%s", baseDir, baseName, maxNr + 1, ext); - result = Posix_Rename(filePath, tmp); + result = File_Rename(filePath, tmp); - if (result == -1) { - int error = Err_Errno(); - - if (error != ENOENT) { - Log(LGPFX" %s: failed to rename %s -> %s failed: %s\n", __FUNCTION__, - filePath, tmp, Err_Errno2String(error)); - } + if ((result != 0) && (result != ENOENT)) { + Log(LGPFX" %s: rename of %s -> %s failed: %s\n", __FUNCTION__, + filePath, tmp, Err_Errno2String(Err_Errno())); } - if (newFilePath == NULL || result == -1) { + if (newFilePath == NULL || result != 0) { Posix_Free(tmp); } else { *newFilePath = tmp; @@ -2591,10 +2585,13 @@ FileRotateByRenumber(const char *filePath, // IN: full path to file tmp = Str_SafeAsprintf(NULL, "%s/%s-%u%s", baseDir, baseName, fileNumbers[i], ext); - if (Posix_Unlink(tmp) == -1) { - Log(LGPFX" %s: failed to remove %s: %s\n", __FUNCTION__, tmp, - Msg_ErrString()); + result = FileDeletion(tmp, FALSE); // Don't follow a symlink! + + if (result != 0) { + Log(LGPFX" %s: failed to remove %s: %s\n", __FUNCTION__, + tmp, Err_Errno2String(Err_Errno())); } + Posix_Free(tmp); } }