Browse Source

Intermediate commit for #246 (still needs heavy revision)

bel 8 years ago
parent
commit
c51c0adf71
4 changed files with 83 additions and 6 deletions
  1. 0 4
      include/civetweb.h
  2. 6 0
      src/civetweb.c
  3. 75 0
      src/file_ops.inl
  4. 2 2
      src/mod_lua.inl

+ 0 - 4
include/civetweb.h

@@ -176,9 +176,6 @@ struct mg_callbacks {
 	   mg_set_websocket_handler instead. */
 	void (*connection_close)(const struct mg_connection *);
 
-#if 0 /* This interface is a part of the problem causing #246 and others.      \
-         It will be replaced. */
-
 	/* Called when civetweb tries to open a file. Used to intercept file open
 	   calls, and serve file data from memory instead.
 	   Parameters:
@@ -192,7 +189,6 @@ struct mg_callbacks {
 	const char *(*open_file)(const struct mg_connection *,
 	                         const char *path,
 	                         size_t *data_len);
-#endif
 
 	/* Called when civetweb is about to serve Lua server page, if
 	   Lua support is enabled.

+ 6 - 0
src/civetweb.c

@@ -402,6 +402,10 @@ static void path_to_unicode(const struct mg_connection *conn,
 /* All file operations need to be rewritten to solve #246. */
 #include "file_ops.inl"
 
+static const char *
+mg_fgets(char *buf, size_t size, struct mg_file *filep, char **p);
+
+#define HAVE_STDINT
 #if defined(HAVE_STDINT)
 #include <stdint.h>
 #else
@@ -8612,6 +8616,7 @@ delete_file(struct mg_connection *conn, const char *path)
 		return;
 	}
 
+#if 0 /* Ignore if a file in memory is inside a folder */
 	if (de.access.membuf != NULL) {
 		/* the file is cached in memory */
 		send_http_error(
@@ -8621,6 +8626,7 @@ delete_file(struct mg_connection *conn, const char *path)
 		    path);
 		return;
 	}
+#endif
 
 	if (de.file.is_directory) {
 		if (remove_directory(conn, path)) {

+ 75 - 0
src/file_ops.inl

@@ -0,0 +1,75 @@
+/* Interface */
+#include <stdint.h>
+
+/* Handle */
+typedef struct cv_file *CV_FILE;
+
+/* Callbacks for memory mapped files */
+typedef struct cv_filesys *CV_FILESYS;
+
+/* Get handle for file "path", using CV_FILESYS options. Out: file */
+CV_FILE cvf_openhandle(const char *path_utf8, CV_FILESYS fsys);
+
+/* Release handle information */
+int cvf_closehandle(CV_FILE file);
+
+/* If file does not exist, return 0 */
+int cvf_file_location(CV_FILE file);
+
+/* File size, -1 on error */
+int64_t cvf_size(CV_FILE file);
+
+/* Is directory */
+int cvf_is_directory(CV_FILE file);
+
+/* Can be written / is not read only */
+int cvf_is_writable(CV_FILE file);
+
+/* Can be deleted / is not read only */
+int cvf_is_deletable(CV_FILE file);
+
+/* File date, =0 on error */
+int64_t cvf_last_modified(CV_FILE file);
+
+/* Is zipped */
+int cvf_is_gzipped(CV_FILE file);
+
+/* Open file for reading, writing or appending. Use filename from cvf_info. */
+int cvf_openfile(CV_FILE file, int access_mode);
+
+/* Close file */
+int cvf_closefile(CV_FILE file);
+
+/* Write data to file. Return -1 on error. */
+int32_t cvf_write(CV_FILE file, const void *block_ptr, int32_t block_size);
+
+/* Read data from file. Return -1 on error. */
+int32_t cvf_read(CV_FILE file, void *block_ptr, int32_t block_size);
+
+/* Callbacks for file system */
+struct cv_filesys {
+	int (*cv_get_file_disp)(const char *path_utf8, void *user_arg);
+
+	void *user_arg;
+};
+
+
+/* Implementation */
+struct cv_file {
+	int64_t size;
+
+	int disp; /* 0 = not exist, 1 = on disk */
+
+#if defined(_WIN32)
+	const wchar_t *fname;
+#else
+	const char *fname;
+#endif
+};
+
+
+#if defined(_WIN32)
+#else
+
+
+#endif

+ 2 - 2
src/mod_lua.inl

@@ -1607,7 +1607,7 @@ handle_lsp_request(struct mg_connection *conn,
 	 * indicate. They should not fill in different members of the same
 	 * struct mg_file.
 	 * See Github issue #225 */
-	filep->size = filesize.stat.size;
+	filep->stat.size = filesize.stat.size;
 
 	if (filep->access.membuf == NULL
 	    && (p = mmap(NULL,
@@ -1669,7 +1669,7 @@ cleanup_handle_lsp_request:
 		lua_close(L);
 	if (p != NULL)
 		munmap(p, filep->stat.size);
-	mg_fclose(filep);
+	mg_fclose(&filep->access);
 
 	return error;
 }