فهرست منبع

Test another fix for mg_fopen (see #363, #246)

bel 8 سال پیش
والد
کامیت
ce2620f74c
1فایلهای تغییر یافته به همراه19 افزوده شده و 21 حذف شده
  1. 19 21
      src/civetweb.c

+ 19 - 21
src/civetweb.c

@@ -1901,14 +1901,7 @@ is_file_in_memory(const struct mg_connection *conn,
 
 
 static int
-is_file_opened(const struct file *filep)
-{
-	if (!filep) {
-		return 0;
-	}
-
-	return filep->membuf != NULL || filep->fp != NULL;
-}
+mg_stat(struct mg_connection *conn, const char *path, struct file *filep);
 
 
 /* mg_fopen will open a file either in memory or on the disk.
@@ -1922,27 +1915,35 @@ mg_fopen(const struct mg_connection *conn,
          const char *mode,
          struct file *filep)
 {
+	int found;
+
 	if (!filep) {
 		return 0;
 	}
 
-    /* filep is initialized in mg_stat: all fields with memset to, 
-     * some fields like size and modification date with values */
-	mg_stat(conn, path, &filep);
+	/* filep is initialized in mg_stat: all fields with memset to,
+	 * some fields like size and modification date with values */
+	found = mg_stat(conn, path, filep);
 
 	if (!is_file_in_memory(conn, path, filep)) {
+		if (found) {
 #ifdef _WIN32
-		wchar_t wbuf[PATH_MAX], wmode[20];
-		path_to_unicode(conn, path, wbuf, ARRAY_SIZE(wbuf));
-		MultiByteToWideChar(CP_UTF8, 0, mode, -1, wmode, ARRAY_SIZE(wmode));
-		filep->fp = _wfopen(wbuf, wmode);
+			wchar_t wbuf[PATH_MAX], wmode[20];
+			path_to_unicode(conn, path, wbuf, ARRAY_SIZE(wbuf));
+			MultiByteToWideChar(CP_UTF8, 0, mode, -1, wmode, ARRAY_SIZE(wmode));
+			filep->fp = _wfopen(wbuf, wmode);
 #else
-		/* Linux et al already use unicode. No need to convert. */
-		filep->fp = fopen(path, mode);
+			/* Linux et al already use unicode. No need to convert. */
+			filep->fp = fopen(path, mode);
 #endif
+		}
+	} else {
+		/* file is in memory */
+		return (filep->membuf != NULL);
 	}
 
-	return is_file_opened(filep);
+	/* file is on disk */
+	return (filep->fp != NULL);
 }
 
 
@@ -2686,9 +2687,6 @@ static void handle_file_based_request(struct mg_connection *conn,
                                       const char *path,
                                       struct file *filep);
 
-static int
-mg_stat(struct mg_connection *conn, const char *path, struct file *filep);
-
 
 const char *
 mg_get_response_code_text(struct mg_connection *conn, int response_code)