src/server/webdav/webdav.c

changeset 58
66c22e54aa90
parent 56
c6cf20b09043
child 59
ab25c0a231d0
--- a/src/server/webdav/webdav.c	Sun Mar 17 19:19:57 2013 +0100
+++ b/src/server/webdav/webdav.c	Tue Mar 19 17:38:32 2013 +0100
@@ -35,6 +35,7 @@
 #include "../util/pool.h"
 #include "../util/pblock.h"
 #include "../util/date.h"
+#include "../util/util.h"
 
 #include "../daemon/vfs.h"
 #include "../daemon/protocol.h"
@@ -77,24 +78,20 @@
     
     printf("PUT length: %d\n", length);
     
-    int status = 201;
-    FILE *out = fopen(ppath, "w");
+    //int status = 201;
+    //FILE *out = fopen(ppath, "w");
+    
+    VFSContext *vfs = vfs_request_context(sn, rq);
+    SYS_FILE out = vfs_openWO(vfs, ppath);
     if(out == NULL) {
-        fprintf(stderr, "fopen(%s, \"w\") failed\n", ppath);
-        protocol_status(sn, rq, 500, NULL);
+        fprintf(stderr, "vfs_openWO(%s, \"w\") failed\n", ppath);
+        //protocol_status(sn, rq, 500, NULL);
         return REQ_ABORTED;
     }
     
     if(length > 0) {
-        FILE *out = fopen(ppath, "w");
-        if(out == NULL) {
-            fprintf(stderr, "fopen(%s, \"w\") failed\n", ppath);
-            return REQ_ABORTED;
-        }
-        setvbuf(out, NULL, _IONBF, 0);
-        
         size_t len = (length > 4096) ? (4096) : (length);
-        char *buffer = malloc(len);
+        char *buffer = pool_malloc(sn->pool, len);
         
         int r;
         int r2 = 0;
@@ -103,18 +100,18 @@
             if(r == NETBUF_EOF) {
                 break;
             }
-            fwrite(buffer, 1, r, out);
+            system_fwrite(out, buffer, r);
             
             r2 += r;
         }
         
-        free(buffer);
+        pool_free(sn->pool, buffer);
     } else {
         
     }
-    fclose(out);
+    vfs_close(out);
     
-    protocol_status(sn, rq, status, NULL);
+    protocol_status(sn, rq, 201, NULL);
     pblock_removekey(pb_key_content_type, rq->srvhdrs);
     pblock_nninsert("content-length", 0, rq->srvhdrs);
     http_start_response(sn, rq);
@@ -126,12 +123,13 @@
     char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
     char *ppath = pblock_findkeyval(pb_key_ppath, rq->vars);
     
+    VFSContext *vfs = vfs_request_context(sn, rq);
+    
     int status = 204;
     
     struct stat st;
-    if(stat(ppath, &st) != 0) {
-        /* ERROR */
-        status = 403; /* TODO: check errno */
+    if(vfs_stat(vfs, ppath, &st)) {
+        return REQ_ABORTED;
     }
     
     if(!strcmp(uri, "/")) {
@@ -142,9 +140,9 @@
             status = 403;
         }
     } else {
-        if(unlink(ppath) != 0) {
+        if(vfs_unlink(vfs, ppath)) {
             /* ERROR */
-            status = 403; /* TODO: check errno */
+            return REQ_ABORTED;
         }
     }
     
@@ -233,17 +231,10 @@
     char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
     char *ppath = pblock_findkeyval(pb_key_ppath, rq->vars);
     
+    VFSContext *vfs = vfs_request_context(sn, rq);
+    
     struct stat st;
-    if(stat(ppath, &st) != 0) {
-        perror("webdav_propfind: stat");
-        fprintf(stderr, "   file: %s\n", ppath);
-        
-        /* TODO: check errno only set status */
-        protocol_status(sn, rq, 404, NULL);
-        pblock_removekey(pb_key_content_type, rq->srvhdrs);
-        pblock_nninsert("content-length", 0, rq->srvhdrs);
-        //http_start_response(sn, rq);
-        
+    if(vfs_stat(vfs, ppath, &st) != 0) {
         return REQ_ABORTED;
     } 
     
@@ -252,54 +243,16 @@
      * a response for every child
      */
     if(S_ISDIR(st.st_mode)) {
-        DIR *dir = opendir(ppath);
+        VFS_DIR dir = vfs_opendir(vfs, ppath);
         if(dir == NULL) {
-            protocol_status(sn, rq, 500, NULL);
-            printf("webdav_propfind: DIR is null\n");
             return REQ_ABORTED;
         }
         
-        struct dirent *f;
-        while((f = readdir(dir)) != NULL) {
-            if(strcmp(f->d_name, ".") == 0 || strcmp(f->d_name, "..") == 0) {
-                continue;
-            }
-            
-            sstr_t filename = sstr(f->d_name);
-            sstr_t _path = sstr(ppath); 
-            sstr_t _uri = sstr(uri);
-            sstr_t ps;
-            sstr_t us;
-            ps.length = 0;
-            ps.ptr = NULL;
-            us.length = 0;
-            us.ptr = NULL;
-            if(_path.ptr[_path.length - 1] != '/') {
-                ps = sstrn("/", 1);
-            }
-            if(_uri.ptr[_uri.length - 1] != '/') {
-                us = sstrn("/", 1);
-            }
-            
-            sstr_t newuri;
-            newuri.length = filename.length + _uri.length + us.length;
-            newuri.ptr = alloca(newuri.length + 1);
-            if(us.length == 1) {
-                newuri = sstrncat(3, newuri, _uri, us, filename);
-            } else {
-                newuri = sstrncat(2, newuri, _uri, filename);
-            }
-            
-            sstr_t newpath;
-            newpath.length = _path.length + filename.length + ps.length;
-            newpath.ptr = alloca(newpath.length + 1);
-            if(ps.length == 1) {
-                newpath = sstrncat(3, newpath, _path, ps, filename);
-            } else {
-                newpath = sstrncat(2, newpath, _path, filename);
-            }
-            
-            /* child response */
+        VFS_ENTRY entry;
+        while(vfs_readdir(dir, &entry)) {
+            sstr_t newpath = util_path_append(sn->pool, ppath, entry.name);
+            sstr_t newuri = util_path_append(sn->pool, uri, entry.name); 
+            // child response
             dav_resource_response(davrq, newpath, newuri);
         }
     }

mercurial