[PATCH 06 of 12] Win32: reworked ngx_win32_rename_file() to check errors

Maxim Dounin mdounin at mdounin.ru
Thu Jan 12 21:35:29 UTC 2023


# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1673548968 -10800
#      Thu Jan 12 21:42:48 2023 +0300
# Node ID bed2302585d8647df3f8185085588395b1ce7b74
# Parent  fbe7d76fe0398ca674b5d62b4849cd524e35bf89
Win32: reworked ngx_win32_rename_file() to check errors.

Previously, ngx_win32_rename_file() retried on all errors returned by
MoveFile() to a temporary name.  It only make sense, however, to retry
when the destination file already exists, similarly to the condition
when ngx_win32_rename_file() is called.  Retrying on other errors is
meaningless and might result in an infinite loop.

diff -r fbe7d76fe039 -r bed2302585d8 src/os/win32/ngx_files.c
--- a/src/os/win32/ngx_files.c	Thu Jan 12 21:42:43 2023 +0300
+++ b/src/os/win32/ngx_files.c	Thu Jan 12 21:42:48 2023 +0300
@@ -235,10 +235,16 @@ ngx_win32_rename_file(ngx_str_t *from, n
             break;
         }
 
-        collision = 1;
+        err = ngx_errno;
 
-        ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
+        if (err == NGX_EEXIST || err == NGX_EEXIST_FILE) {
+            collision = 1;
+            continue;
+        }
+
+        ngx_log_error(NGX_LOG_CRIT, log, err,
                       "MoveFile() \"%s\" to \"%s\" failed", to->data, name);
+        goto failed;
     }
 
     if (MoveFile((const char *) from->data, (const char *) to->data) == 0) {
@@ -253,6 +259,8 @@ ngx_win32_rename_file(ngx_str_t *from, n
                       "DeleteFile() \"%s\" failed", name);
     }
 
+failed:
+
     /* mutex_unlock() */
 
     ngx_free(name);


More information about the nginx-devel mailing list