]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Don't follow symlinks in File_Rotate_ByRename().
authorJohn Wolfe <jwolfe@vmware.com>
Mon, 5 Apr 2021 16:01:41 +0000 (09:01 -0700)
committerJohn Wolfe <jwolfe@vmware.com>
Mon, 5 Apr 2021 16:01:41 +0000 (09:01 -0700)
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.

open-vm-tools/lib/file/file.c

index 54e850ce81f10a1ed23b040c9fad5c6d9e6977d3..46933c594c006b96771fe1642ffc78946efcd0f5 100644 (file)
@@ -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);
       }
    }