]> code.citadel.org Git - citadel.git/blobdiff - citadel/file_ops.c
Fix hangups on binary blob reads from webcit stable
[citadel.git] / citadel / file_ops.c
index 0e01031ff1a434d53815413826af3eca50ddaee0..413e32da1aa6f62f6fc41fc6f0d4b3916c6f0466 100644 (file)
@@ -12,7 +12,6 @@
 #include <ctype.h>
 #include <string.h>
 #include <sys/stat.h>
-#include <sys/mman.h>
 
 #if TIME_WITH_SYS_TIME
 # include <sys/time.h>
@@ -234,7 +233,6 @@ void OpenCmdResult(char *filename, const char *mime_type)
        long filesize;
 
        fstat(fileno(CC->download_fp), &statbuf);
-       CC->download_fp_total = statbuf.st_size;
        filesize = (long) statbuf.st_size;
        modtime = (time_t) statbuf.st_mtime;
 
@@ -641,9 +639,9 @@ void cmd_read(char *cmdbuf)
 {
        long start_pos;
        size_t bytes;
-       size_t actual_bytes;
-       char *buf = NULL;
+       char buf[SIZ];
 
+       /* The client will transmit its requested offset and byte count */
        start_pos = extract_long(cmdbuf, 0);
        bytes = extract_int(cmdbuf, 1);
 
@@ -653,26 +651,24 @@ void cmd_read(char *cmdbuf)
                return;
        }
 
-       buf = mmap(NULL, 
-                  CC->download_fp_total, 
-                  PROT_READ, 
-                  MAP_PRIVATE,
-                  fileno(CC->download_fp), 
-                  0);
-       
-       actual_bytes = CC->download_fp_total - start_pos;
-       if ((actual_bytes > 0) && (buf != NULL)) {
-               cprintf("%d %d\n", BINARY_FOLLOWS, (int)actual_bytes);
-               client_write(buf + start_pos, actual_bytes);
+       /* If necessary, reduce the byte count to the size of our buffer */
+       if (bytes > sizeof(buf)) {
+               bytes = sizeof(buf);
+       }
+
+       fseek(CC->download_fp, start_pos, 0);
+       bytes = fread(buf, 1, bytes, CC->download_fp);
+       if (bytes > 0) {
+               /* Tell the client the actual byte count and transmit it */
+               cprintf("%d %d\n", BINARY_FOLLOWS, (int)bytes);
+               client_write(bufbytes);
        }
        else {
                cprintf("%d %s\n", ERROR, strerror(errno));
        }
-       munmap(buf, CC->download_fp_total);
 }
 
 
-
 /*
  * write to the upload file
  */
@@ -754,7 +750,6 @@ void cmd_ndop(char *cmdbuf)
        CC->dl_is_net = 1;
 
        stat(pathname, &statbuf);
-       CC->download_fp_total = statbuf.st_size;
        cprintf("%d %ld\n", CIT_OK, (long)statbuf.st_size);
 }