|  | @@ -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)
 |  |  int mg_upload(struct mg_connection *conn, const char *destination_dir)
 | 
											
												
													
														|  |  {
 |  |  {
 | 
											
												
													
														|  |      const char *content_type_header, *boundary_start;
 |  |      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;
 |  |      FILE *fp;
 | 
											
												
													
														|  |      int bl, n, i, j, headers_len, boundary_len, eof,
 |  |      int bl, n, i, j, headers_len, boundary_len, eof,
 | 
											
												
													
														|  |          len = 0, num_uploaded_files = 0;
 |  |          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. */
 |  |          /* 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;
 |  |              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);
 |  |          } while (!eof && (n = mg_read(conn, buf + len, sizeof(buf) - len)) > 0);
 | 
											
												
													
														|  |          fclose(fp);
 |  |          fclose(fp);
 | 
											
												
													
														|  |          if (eof) {
 |  |          if (eof) {
 | 
											
												
													
														|  | 
 |  | +            remove(path);
 | 
											
												
													
														|  | 
 |  | +            rename(tmp_path, path);
 | 
											
												
													
														|  |              num_uploaded_files++;
 |  |              num_uploaded_files++;
 | 
											
												
													
														|  |              if (conn->ctx->callbacks.upload != NULL) {
 |  |              if (conn->ctx->callbacks.upload != NULL) {
 | 
											
												
													
														|  |                  conn->ctx->callbacks.upload(conn, path);
 |  |                  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)
 |  |  int mg_get_response(struct mg_connection *conn, char *ebuf, size_t ebuf_len)
 | 
											
												
													
														|  |  {
 |  |  {
 | 
											
												
													
														|  |      /* Implementation of API function for HTTP clients */
 |  |      /* 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,
 |  |  struct mg_connection *mg_download(const char *host, int port, int use_ssl,
 |