浏览代码

Replace fopen by mg_fopen

mg_fopen uses unicode (UTF-8) file names in windows, while fopen uses code page depending string encoding
bel 9 年之前
父节点
当前提交
d2c64184d9
共有 1 个文件被更改,包括 43 次插入35 次删除
  1. 43 35
      src/handle_form.inl

+ 43 - 35
src/handle_form.inl

@@ -171,7 +171,7 @@ mg_handle_form_request(struct mg_connection *conn,
 	int buf_fill = 0;
 	int buf_fill = 0;
 	int r;
 	int r;
 	int field_count = 0;
 	int field_count = 0;
-	FILE *fstore = NULL;
+	struct file fstore;
 	size_t file_size = 0; /* init here, to a avoid a false positive
 	size_t file_size = 0; /* init here, to a avoid a false positive
 	                         "uninitialized variable used" warning */
 	                         "uninitialized variable used" warning */
 
 
@@ -258,23 +258,26 @@ mg_handle_form_request(struct mg_connection *conn,
 			}
 			}
 			if (field_storage == FORM_FIELD_STORAGE_STORE) {
 			if (field_storage == FORM_FIELD_STORAGE_STORE) {
 				/* Store the content to a file */
 				/* Store the content to a file */
-				fstore = fopen(path, "wb");
+				if (mg_fopen(conn, path, "wb", &fstore) == 0) {
+					fstore.fp = NULL;
+				}
 				file_size = 0;
 				file_size = 0;
-				if (fstore != NULL) {
-					size_t n = (size_t)fwrite(val, 1, (size_t)vallen, fstore);
-					if ((n != (size_t)vallen) || (ferror(fstore))) {
+				if (fstore.fp != NULL) {
+					size_t n =
+					    (size_t)fwrite(val, 1, (size_t)vallen, fstore.fp);
+					if ((n != (size_t)vallen) || (ferror(fstore.fp))) {
 						mg_cry(conn,
 						mg_cry(conn,
 						       "%s: Cannot write file %s",
 						       "%s: Cannot write file %s",
 						       __func__,
 						       __func__,
 						       path);
 						       path);
-						fclose(fstore);
-						fstore = NULL;
+						fclose(fstore.fp);
+						fstore.fp = NULL;
 						remove_bad_file(conn, path);
 						remove_bad_file(conn, path);
 					}
 					}
 					file_size += (size_t)n;
 					file_size += (size_t)n;
 
 
-					if (fstore) {
-						r = fclose(fstore);
+					if (fstore.fp) {
+						r = fclose(fstore.fp);
 						if (r == 0) {
 						if (r == 0) {
 							/* stored successfully */
 							/* stored successfully */
 							field_stored(conn, path, file_size, fdh);
 							field_stored(conn, path, file_size, fdh);
@@ -285,7 +288,7 @@ mg_handle_form_request(struct mg_connection *conn,
 							       path);
 							       path);
 							remove_bad_file(conn, path);
 							remove_bad_file(conn, path);
 						}
 						}
-						fstore = NULL;
+						fstore.fp = NULL;
 					}
 					}
 
 
 				} else {
 				} else {
@@ -388,9 +391,11 @@ mg_handle_form_request(struct mg_connection *conn,
 			}
 			}
 
 
 			if (field_storage == FORM_FIELD_STORAGE_STORE) {
 			if (field_storage == FORM_FIELD_STORAGE_STORE) {
-				fstore = fopen(path, "wb");
+				if (mg_fopen(conn, path, "wb", &fstore) == 0) {
+					fstore.fp = NULL;
+				}
 				file_size = 0;
 				file_size = 0;
-				if (!fstore) {
+				if (!fstore.fp) {
 					mg_cry(conn, "%s: Cannot create file %s", __func__, path);
 					mg_cry(conn, "%s: Cannot create file %s", __func__, path);
 				}
 				}
 			}
 			}
@@ -407,15 +412,16 @@ mg_handle_form_request(struct mg_connection *conn,
 					next = val + vallen;
 					next = val + vallen;
 				}
 				}
 
 
-				if (fstore) {
-					size_t n = (size_t)fwrite(val, 1, (size_t)vallen, fstore);
-					if ((n != (size_t)vallen) || (ferror(fstore))) {
+				if (fstore.fp) {
+					size_t n =
+					    (size_t)fwrite(val, 1, (size_t)vallen, fstore.fp);
+					if ((n != (size_t)vallen) || (ferror(fstore.fp))) {
 						mg_cry(conn,
 						mg_cry(conn,
 						       "%s: Cannot write file %s",
 						       "%s: Cannot write file %s",
 						       __func__,
 						       __func__,
 						       path);
 						       path);
-						fclose(fstore);
-						fstore = NULL;
+						fclose(fstore.fp);
+						fstore.fp = NULL;
 						remove_bad_file(conn, path);
 						remove_bad_file(conn, path);
 					}
 					}
 					file_size += (size_t)n;
 					file_size += (size_t)n;
@@ -440,8 +446,8 @@ mg_handle_form_request(struct mg_connection *conn,
 
 
 			} while (!end_of_key_value_pair_found);
 			} while (!end_of_key_value_pair_found);
 
 
-			if (fstore) {
-				r = fclose(fstore);
+			if (fstore.fp) {
+				r = fclose(fstore.fp);
 				if (r == 0) {
 				if (r == 0) {
 					/* stored successfully */
 					/* stored successfully */
 					field_stored(conn, path, file_size, fdh);
 					field_stored(conn, path, file_size, fdh);
@@ -449,7 +455,7 @@ mg_handle_form_request(struct mg_connection *conn,
 					mg_cry(conn, "%s: Error saving file %s", __func__, path);
 					mg_cry(conn, "%s: Error saving file %s", __func__, path);
 					remove_bad_file(conn, path);
 					remove_bad_file(conn, path);
 				}
 				}
-				fstore = NULL;
+				fstore.fp = NULL;
 			}
 			}
 
 
 			/* Proceed to next entry */
 			/* Proceed to next entry */
@@ -627,10 +633,12 @@ mg_handle_form_request(struct mg_connection *conn,
 			if (field_storage == FORM_FIELD_STORAGE_STORE) {
 			if (field_storage == FORM_FIELD_STORAGE_STORE) {
 				/* Store the content to a file */
 				/* Store the content to a file */
 				size_t towrite, n;
 				size_t towrite, n;
-				fstore = fopen(path, "wb");
+				if (mg_fopen(conn, path, "wb", &fstore) == 0) {
+					fstore.fp = NULL;
+				}
 				file_size = 0;
 				file_size = 0;
 
 
-				if (!fstore) {
+				if (!fstore.fp) {
 					mg_cry(conn, "%s: Cannot create file %s", __func__, path);
 					mg_cry(conn, "%s: Cannot create file %s", __func__, path);
 				}
 				}
 
 
@@ -643,17 +651,17 @@ mg_handle_form_request(struct mg_connection *conn,
 					 * in the buffer. */
 					 * in the buffer. */
 					towrite -= bl + 4;
 					towrite -= bl + 4;
 
 
-					if (fstore) {
+					if (fstore.fp) {
 
 
 						/* Store the content of the buffer. */
 						/* Store the content of the buffer. */
-						n = (size_t)fwrite(hend, 1, towrite, fstore);
-						if ((n != towrite) || (ferror(fstore))) {
+						n = (size_t)fwrite(hend, 1, towrite, fstore.fp);
+						if ((n != towrite) || (ferror(fstore.fp))) {
 							mg_cry(conn,
 							mg_cry(conn,
 							       "%s: Cannot write file %s",
 							       "%s: Cannot write file %s",
 							       __func__,
 							       __func__,
 							       path);
 							       path);
-							fclose(fstore);
-							fstore = NULL;
+							fclose(fstore.fp);
+							fstore.fp = NULL;
 							remove_bad_file(conn, path);
 							remove_bad_file(conn, path);
 						}
 						}
 						file_size += (size_t)n;
 						file_size += (size_t)n;
@@ -682,23 +690,23 @@ mg_handle_form_request(struct mg_connection *conn,
 					next = search_boundary(buf, (size_t)buf_fill, boundary, bl);
 					next = search_boundary(buf, (size_t)buf_fill, boundary, bl);
 				}
 				}
 
 
-				if (fstore) {
+				if (fstore.fp) {
 					towrite = (size_t)(next - hend);
 					towrite = (size_t)(next - hend);
-					n = (size_t)fwrite(hend, 1, towrite, fstore);
-					if ((n != towrite) || (ferror(fstore))) {
+					n = (size_t)fwrite(hend, 1, towrite, fstore.fp);
+					if ((n != towrite) || (ferror(fstore.fp))) {
 						mg_cry(conn,
 						mg_cry(conn,
 						       "%s: Cannot write file %s",
 						       "%s: Cannot write file %s",
 						       __func__,
 						       __func__,
 						       path);
 						       path);
-						fclose(fstore);
-						fstore = NULL;
+						fclose(fstore.fp);
+						fstore.fp = NULL;
 						remove_bad_file(conn, path);
 						remove_bad_file(conn, path);
 					}
 					}
 					file_size += (size_t)n;
 					file_size += (size_t)n;
 				}
 				}
 
 
-				if (fstore) {
-					r = fclose(fstore);
+				if (fstore.fp) {
+					r = fclose(fstore.fp);
 					if (r == 0) {
 					if (r == 0) {
 						/* stored successfully */
 						/* stored successfully */
 						field_stored(conn, path, file_size, fdh);
 						field_stored(conn, path, file_size, fdh);
@@ -709,7 +717,7 @@ mg_handle_form_request(struct mg_connection *conn,
 						       path);
 						       path);
 						remove_bad_file(conn, path);
 						remove_bad_file(conn, path);
 					}
 					}
-					fstore = NULL;
+					fstore.fp = NULL;
 				}
 				}
 			}
 			}