Opened 9 years ago

Closed 9 years ago

#910 closed defect (fixed)

CreateFile() "E:/tmp/0000000001" failed (80: The file exists)

Reported by: emawind84@… Owned by:
Priority: minor Milestone:
Component: nginx-core Version: 1.8.x
Keywords: client_body_in_file_only, CreateFile, 80 Cc:
uname -a: OS Microsoft Windows Server 2012 Standard
Version 6.2.9200 build 9200
nginx -V: nginx version: nginx/1.9.12
built by cl 16.00.30319.01 for 80x86
built with OpenSSL 1.0.2f 28 Jan 2016
TLS SNI support enabled
configure arguments: --with-cc=cl --builddir=objs.msvc8 --with-debug --prefix= --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid --http-log-pat
h=logs/access.log --error-log-path=logs/error.log --sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp --http-proxy-temp-path=t
emp/proxy_temp --http-fastcgi-temp-path=temp/fastcgi_temp --http-scgi-temp-path=temp/scgi_temp --http-uwsgi-temp-path=temp/uwsgi_temp --with-cc-opt
=-DFD_SETSIZE=1024 --with-pcre=objs.msvc8/lib/pcre-8.38 --with-zlib=objs.msvc8/lib/zlib-1.2.8 --with-select_module --with-http_realip_module --with
-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --
with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_modu
le --with-http_slice_module --with-mail --with-stream --with-openssl=objs.msvc8/lib/openssl-1.0.2f --with-openssl-opt=no-asm --with-http_ssl_module
--with-mail_ssl_module --with-stream_ssl_module --with-ipv6

Description

I am using nginx as a file upload handler.
The first time I run the server everything is fine,
I upload several files and nginx create some temporary files in the folder I set in the directive 'client_body_temp_path'.
The problem arise when I 'restart' the server.
If I try to upload a file it will fail with the following error:
'CreateFile() "E:/tmp/0000000001" failed (80: The file exists)'

So I should delete all the temporary files before restarting the server to avoid this problem.

On Linux however nginx looks smart enough and if a file with that sequence already exists he will create the file with a new random number much higher and will start from it.

This is the configuration for the handler

server {
    listen 8180;
    server_name '';
    root E:/tmp;

    location / { deny all; }

    location /upload {
        auth_basic "Restricted Upload";
        auth_basic_user_file .htpasswd.upload;

        add_header Pragma no-cache;
        add_header X-Content-Type-Options nosniff;

        limit_except POST          { deny all; }

        client_body_temp_path      E:/tmp/;
        client_body_in_file_only   on;
        client_body_buffer_size    1024K;
        client_max_body_size       4000M;

        proxy_pass_request_headers on;
        proxy_set_header           X-FILE $request_body_file;
        proxy_set_body             off;
        proxy_redirect             off;

        # do NOT use domain name in the proxy_pass directive only IP!
        #proxy_pass                 $scheme://$host/ws/fileupload$is_args$args;
        proxy_pass                 $scheme://$host/Common/TemporaryFile/fastupload.action$is_args$args;
    }

}

Change History (3)

comment:1 by Maxim Dounin, 9 years ago

Status: newaccepted

Please try the following patch:

# HG changeset patch
# User Maxim Dounin <mdounin@mdounin.ru>
# Date 1456965361 -10800
#      Thu Mar 03 03:36:01 2016 +0300
# Node ID 6103f02d07d5ee10e2bc480ca1e6cfa1a8f6f775
# Parent  8e6f34342eb652046fdfcd0d0677f0d20483c0a5
Win32: additional error code NGX_EEXIST_FILE (ticket #910).

On Windows there are two possible error codes which correspond to
the EEXIST error code: ERROR_FILE_EXISTS used by CreateFile(CREATE_NEW),
and ERROR_ALREADY_EXISTS used by CreateDirectory().

MoveFile() seems to use both: ERROR_ALREADY_EXISTS when moving within
one filesystem, and ERROR_FILE_EXISTS when copying a file to a different
drive.

diff --git a/src/core/ngx_file.c b/src/core/ngx_file.c
--- a/src/core/ngx_file.c
+++ b/src/core/ngx_file.c
@@ -187,7 +187,7 @@ ngx_create_temp_file(ngx_file_t *file, n
 
         err = ngx_errno;
 
-        if (err == NGX_EEXIST) {
+        if (err == NGX_EEXIST_FILE) {
             n = (uint32_t) ngx_next_temp_number(1);
             continue;
         }
@@ -683,7 +683,7 @@ ngx_ext_rename_file(ngx_str_t *src, ngx_
 
 #if (NGX_WIN32)
 
-    if (err == NGX_EEXIST) {
+    if (err == NGX_EEXIST || err == NGX_EEXIST_FILE) {
         err = ngx_win32_rename_file(src, to, ext->log);
 
         if (err == 0) {
diff --git a/src/os/unix/ngx_errno.h b/src/os/unix/ngx_errno.h
--- a/src/os/unix/ngx_errno.h
+++ b/src/os/unix/ngx_errno.h
@@ -25,6 +25,7 @@ typedef int               ngx_err_t;
 #define NGX_EACCES        EACCES
 #define NGX_EBUSY         EBUSY
 #define NGX_EEXIST        EEXIST
+#define NGX_EEXIST_FILE   EEXIST
 #define NGX_EXDEV         EXDEV
 #define NGX_ENOTDIR       ENOTDIR
 #define NGX_EISDIR        EISDIR
diff --git a/src/os/win32/ngx_errno.h b/src/os/win32/ngx_errno.h
--- a/src/os/win32/ngx_errno.h
+++ b/src/os/win32/ngx_errno.h
@@ -25,8 +25,14 @@ typedef DWORD                      ngx_e
 #define NGX_ENOPATH                ERROR_PATH_NOT_FOUND
 #define NGX_ENOMEM                 ERROR_NOT_ENOUGH_MEMORY
 #define NGX_EACCES                 ERROR_ACCESS_DENIED
-/* it's seems that ERROR_FILE_EXISTS is not appropriate error code */
+/*
+ * there are two EEXIST error codes:
+ * ERROR_FILE_EXISTS used by CreateFile(CREATE_NEW),
+ * and ERROR_ALREADY_EXISTS used by CreateDirectory();
+ * MoveFile() uses both
+ */
 #define NGX_EEXIST                 ERROR_ALREADY_EXISTS
+#define NGX_EEXIST_FILE            ERROR_FILE_EXISTS
 /*
  * could not found cross volume directory move error code,
  * so use ERROR_WRONG_DISK as stub one

comment:2 by Maxim Dounin <mdounin@…>, 9 years ago

In 6468:a5897d360977/nginx:

Win32: additional error code NGX_EEXIST_FILE (ticket #910).

On Windows there are two possible error codes which correspond to
the EEXIST error code: ERROR_FILE_EXISTS used by CreateFile(CREATE_NEW),
and ERROR_ALREADY_EXISTS used by CreateDirectory().

MoveFile() seems to use both: ERROR_ALREADY_EXISTS when moving within
one filesystem, and ERROR_FILE_EXISTS when copying a file to a different
drive.

comment:3 by Maxim Dounin, 9 years ago

Resolution: fixed
Status: acceptedclosed

Fix committed.

Note: See TracTickets for help on using tickets.