Browse Source

Use temporary file name for partially uploaded files

bel 10 năm trước cách đây
mục cha
commit
a0c10cd94b
2 tập tin đã thay đổi với 12 bổ sung4 xóa
  1. 2 0
      RELEASE_NOTES.md
  2. 10 4
      src/civetweb.c

+ 2 - 0
RELEASE_NOTES.md

@@ -5,6 +5,8 @@ Release Notes v1.7 (Under Development)
 Changes
 -------
 
+- Use temporary file name for partially uploaded files
+- Some new API functions for C and C++
 - Add a websocket client example
 - Add a websocket client API
 - Update websocket example

+ 10 - 4
src/civetweb.c

@@ -5469,7 +5469,7 @@ static uint32_t get_remote_ip(const struct mg_connection *conn)
 int mg_upload(struct mg_connection *conn, const char *destination_dir)
 {
     const char *content_type_header, *boundary_start;
-    char buf[MG_BUF_LEN], path[PATH_MAX], fname[1024], boundary[100], *s;
+    char buf[MG_BUF_LEN], path[PATH_MAX], tmp_path[PATH_MAX], fname[1024], boundary[100], *s;
     FILE *fp;
     int bl, n, i, j, headers_len, boundary_len, eof,
         len = 0, num_uploaded_files = 0;
@@ -5549,8 +5549,10 @@ int mg_upload(struct mg_connection *conn, const char *destination_dir)
         }
 
         /* Open file in binary mode. TODO: set an exclusive lock. */
-        snprintf(path, sizeof(path), "%s/%s", destination_dir, s);
-        if ((fp = fopen(path, "wb")) == NULL) {
+        snprintf(path, sizeof(path)-1, "%s/%s", destination_dir, s);
+        strcpy(tmp_path, path);
+        strcat(tmp_path, "~");
+        if ((fp = fopen(tmp_path, "wb")) == NULL) {
             break;
         }
 
@@ -5577,10 +5579,14 @@ int mg_upload(struct mg_connection *conn, const char *destination_dir)
         } while (!eof && (n = mg_read(conn, buf + len, sizeof(buf) - len)) > 0);
         fclose(fp);
         if (eof) {
+            remove(path);
+            rename(tmp_path, path);
             num_uploaded_files++;
             if (conn->ctx->callbacks.upload != NULL) {
                 conn->ctx->callbacks.upload(conn, path);
             }
+        } else {
+            remove(tmp_path);
         }
     }
 
@@ -6513,7 +6519,7 @@ static int getreq(struct mg_connection *conn, char *ebuf, size_t ebuf_len)
 int mg_get_response(struct mg_connection *conn, char *ebuf, size_t ebuf_len)
 {
     /* Implementation of API function for HTTP clients */
-    return getreq(struct mg_connection *conn, char *ebuf, size_t ebuf_len);
+    return getreq(conn, ebuf, ebuf_len);
 }
 
 struct mg_connection *mg_download(const char *host, int port, int use_ssl,