]> code.citadel.org Git - citadel.git/commitdiff
* File/image uploads now implemented in terms of the new IPC code.
authorMichael Hampton <io_error@uncensored.citadel.org>
Wed, 2 Oct 2002 04:06:02 +0000 (04:06 +0000)
committerMichael Hampton <io_error@uncensored.citadel.org>
Wed, 2 Oct 2002 04:06:02 +0000 (04:06 +0000)
citadel/citadel_ipc.c
citadel/citadel_ipc.h
citadel/routines.c
citadel/routines2.c

index 07f13ac9f77933f3634bd0a6993f8c5cefa4fb67..2377445120e5341da886a097386c5d78cc94f1fa 100644 (file)
@@ -1159,50 +1159,62 @@ int CtdlIPCImageDownload(CtdlIPC *ipc, const char *filename, void **buf,
 
 
 /* UOPN */
-int CtdlIPCFileUpload(CtdlIPC *ipc, const char *filename, const char *comment, void *buf,
-               size_t bytes, char *cret)
+int CtdlIPCFileUpload(CtdlIPC *ipc, const char *save_as, const char *comment,
+               const char *path, void (*progress_gauge_callback)(long, long),
+               char *cret)
 {
        register int ret;
        char *aaa;
 
        if (!cret) return -1;
-       if (!filename) return -1;
+       if (!save_as) return -1;
        if (!comment) return -1;
+       if (!path) return -1;
+       if (!*path) return -1;
        if (ipc->uploading) return -1;
 
-       aaa = (char *)malloc(strlen(filename) + strlen(comment) + 7);
+       aaa = (char *)malloc(strlen(save_as) + strlen(comment) + 7);
        if (!aaa) return -1;
 
-       sprintf(aaa, "UOPN %s|%s", filename, comment);
+       sprintf(aaa, "UOPN %s|%s", save_as, comment);
        ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
        free(aaa);
-       if (ret / 100 == 2)
+       if (ret / 100 == 2) {
                ipc->uploading = 1;
-       ret = CtdlIPCWriteUpload(ipc, buf, bytes, cret);
-       ret = CtdlIPCEndUpload(ipc, cret);
+               ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
+               ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
+               ipc->uploading = 0;
+       }
        return ret;
 }
 
 
 /* UIMG */
-int CtdlIPCImageUpload(CtdlIPC *ipc, int for_real, const char *filename, size_t bytes,
-               char *cret)
+int CtdlIPCImageUpload(CtdlIPC *ipc, int for_real, const char *path,
+               const char *save_as,
+               void (*progress_gauge_callback)(long, long), char *cret)
 {
        register int ret;
        char *aaa;
 
        if (!cret) return -1;
-       if (!filename) return -1;
+       if (!save_as) return -1;
+       if (!path && for_real) return -1;
+       if (!*path && for_real) return -1;
        if (ipc->uploading) return -1;
 
-       aaa = (char *)malloc(strlen(filename) + 17);
+       aaa = (char *)malloc(strlen(save_as) + 17);
        if (!aaa) return -1;
 
-       sprintf(aaa, "UIMG %d|%s", for_real, filename);
+       sprintf(aaa, "UIMG %d|%s", for_real, save_as);
        ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
        free(aaa);
-       if (ret / 100 == 2)
+       if (ret / 100 == 2 && for_real) {
                ipc->uploading = 1;
+               ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
+               ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
+               ipc->uploading = 0;
+       }
        return ret;
 }
 
@@ -2008,49 +2020,75 @@ int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
 
 
 /* UCLS */
-int CtdlIPCEndUpload(CtdlIPC *ipc, char *cret)
+int CtdlIPCEndUpload(CtdlIPC *ipc, int discard, char *cret)
 {
        register int ret;
+       char cmd[8];
 
        if (!cret) return -1;
        if (!ipc->uploading) return -1;
 
-       ret = CtdlIPCGenericCommand(ipc, "UCLS", NULL, 0, NULL, NULL, cret);
-       if (ret / 100 == 2)
-               ipc->uploading = 0;
+       sprintf(cmd, "UCLS %d", discard ? 0 : 1);
+       ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
+       ipc->uploading = 0;
        return ret;
 }
 
 
 /* WRIT */
-int CtdlIPCWriteUpload(CtdlIPC *ipc, void *buf, size_t bytes, char *cret)
+int CtdlIPCWriteUpload(CtdlIPC *ipc, const char *path,
+               void (*progress_gauge_callback)(long, long), char *cret)
 {
        register int ret = -1;
-       register size_t offset;
+       register size_t offset = 0;
+       size_t bytes;
        char aaa[SIZ];
+       char buf[4096];
+       FILE *fd;
 
        if (!cret) return -1;
-       if (!buf) return -1;
-       if (bytes < 1) return -1;
+       if (!path) return -1;
+       if (!*path) return -1;
+
+       fd = fopen(path, "r");
+       if (!fd) return -2;
+
+       fseek(fd, 0L, SEEK_END);
+       bytes = ftell(fd);
+       rewind(fd);
+
+       if (progress_gauge_callback)
+               progress_gauge_callback(0, bytes);
 
-       offset = 0;
        while (offset < bytes) {
-               sprintf(aaa, "WRIT %d", bytes - offset);
+               register size_t to_write;
+
+               /* Read some data in */
+               to_write = fread(buf, 1, 4096, fd);
+               if (!to_write) {
+                       if (feof(fd) || ferror(fd)) break;
+               }
+               sprintf(aaa, "WRIT %d", to_write);
                CtdlIPC_putline(ipc, aaa);
                CtdlIPC_getline(ipc, aaa);
                strcpy(cret, &aaa[4]);
                ret = atoi(aaa);
                if (aaa[0] == '7') {
-                       register size_t to_write;
-
                        to_write = extract_long(&aaa[4], 0);
-                       serv_write(ipc, buf + offset, to_write);
+                       
+                       serv_write(ipc, buf, to_write);
                        offset += to_write;
+                       if (progress_gauge_callback)
+                               progress_gauge_callback(offset, bytes);
+                       /* Detect short reads and back up if needed */
+                       fseek(fd, offset, SEEK_SET);
                } else {
                        break;
                }
        }
-       return ret;
+       if (progress_gauge_callback)
+               progress_gauge_callback(1, 1);
+       return (!ferror(fd) ? ret : -2);
 }
 
 
index 3b45e9ee55b6ab5983918556eb086e00738b95b8..2f834d200bd4c63b8cfd766f861a7982359071fc 100644 (file)
@@ -113,10 +113,12 @@ int CtdlIPCAttachmentDownload(CtdlIPC *ipc, long msgnum, const char *part, void
                void (*progress_gauge_callback)(long, long), char *cret);
 int CtdlIPCImageDownload(CtdlIPC *ipc, const char *filename, void **buf,
                void (*progress_gauge_callback)(long, long), char *cret);
-int CtdlIPCFileUpload(CtdlIPC *ipc, const char *filename, const char *comment, void *buf,
-               size_t bytes, char *cret);
-int CtdlIPCImageUpload(CtdlIPC *ipc, int for_real, const char *filename, size_t bytes,
+int CtdlIPCFileUpload(CtdlIPC *ipc, const char *save_as, const char *comment,
+               const char *path, void (*progress_gauge_callback)(long, long),
                char *cret);
+int CtdlIPCImageUpload(CtdlIPC *ipc, int for_real, const char *path,
+               const char *save_as,
+               void (*progress_gauge_callback)(long, long), char *cret);
 int CtdlIPCQueryUsername(CtdlIPC *ipc, const char *username, char *cret);
 int CtdlIPCFloorListing(CtdlIPC *ipc, char **listing, char *cret);
 int CtdlIPCCreateFloor(CtdlIPC *ipc, int for_real, const char *name, char *cret);
@@ -170,8 +172,9 @@ inline void CtdlIPC_unlock(CtdlIPC *ipc);
 char *CtdlIPCReadListing(CtdlIPC *ipc, char *dest);
 int CtdlIPCSendListing(CtdlIPC *ipc, const char *listing);
 size_t CtdlIPCPartialRead(CtdlIPC *ipc, void **buf, size_t offset, size_t bytes, char *cret);
-int CtdlIPCEndUpload(CtdlIPC *ipc, char *cret);
-int CtdlIPCWriteUpload(CtdlIPC *ipc, void *buf, size_t bytes, char *cret);
+int CtdlIPCEndUpload(CtdlIPC *ipc, int discard, char *cret);
+int CtdlIPCWriteUpload(CtdlIPC *ipc, const char *path,
+               void (*progress_gauge_callback)(long, long), char *cret);
 int CtdlIPCEndDownload(CtdlIPC *ipc, char *cret);
 int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
               void (*progress_gauge_callback)(long, long), char *cret);
index 7c44fc566c0a88b06a91a0976a844a07814db807..89de17cbd20357a8544122698d6059793507cb08 100644 (file)
@@ -398,6 +398,8 @@ void progress(long int curr, long int cmax)
 
        if (curr >= cmax) {
                sln_printf("\r%79s\r","");
+               status_line(serv_info.serv_humannode, serv_info.serv_bbs_city,
+                       room_name, secure, 0);
        } else {
                /* a will be range 0-50 rather than 0-100 */
                a=(curr * 50) / cmax;
index 2bee0b63a2a8fa7d2fa4c06c68d78d7bb97a816e..6a8c4f551363965cc32a311602635d9af2a2d569 100644 (file)
@@ -218,48 +218,6 @@ void updatelsa(CtdlIPC *ipc)
 }
 
 
-/*
- * This routine completes a client upload
- */
-void do_upload(CtdlIPC *ipc, int fd)
-{
-       char buf[SIZ];
-       char tbuf[4096];
-       long transmitted_bytes, total_bytes;
-       int bytes_to_send;
-       int bytes_expected;
-
-       /* learn the size of the file */
-       total_bytes = lseek(fd, 0L, 2);
-       lseek(fd, 0L, 0);
-
-       transmitted_bytes = 0L;
-       progress(transmitted_bytes, total_bytes);
-       do {
-               bytes_to_send = read(fd, tbuf, 4096);
-               if (bytes_to_send > 0) {
-                       snprintf(buf, sizeof buf, "WRIT %d", bytes_to_send);
-                       CtdlIPC_putline(ipc, buf);
-                       CtdlIPC_getline(ipc, buf);
-                       if (buf[0] == '7') {
-                               bytes_expected = atoi(&buf[4]);
-                               serv_write(ipc, tbuf, bytes_expected);
-                       } else {
-                               scr_printf("%s\n", &buf[4]);
-                       }
-               }
-               transmitted_bytes = transmitted_bytes + (long) bytes_to_send;
-               progress(transmitted_bytes, total_bytes);
-       } while (bytes_to_send > 0);
-
-       /* close the upload file, locally and at the server */
-       close(fd);
-       CtdlIPC_putline(ipc, "UCLS 1");
-       CtdlIPC_getline(ipc, buf);
-       scr_printf("%s\n", &buf[4]);
-}
-
-
 /*
  * client-based uploads (for users with their own clientware)
  */
@@ -269,6 +227,7 @@ void cli_upload(CtdlIPC *ipc)
        char desc[151];
        char buf[SIZ];
        char tbuf[SIZ];
+       int r;          /* IPC response code */
        int a;
        int fd;
 
@@ -285,28 +244,25 @@ void cli_upload(CtdlIPC *ipc)
        scr_printf("Enter a description of this file:\n");
        newprompt(": ", desc, 75);
 
-       /* keep generating filenames in hope of finding a unique one */
+       /* Keep generating filenames in hope of finding a unique one */
        a = 0;
-       do {
-               if (a == 10)
-                       return; /* fail if tried 10 times */
-               strcpy(buf, flnm);
-               while ((strlen(buf) > 0) && (haschar(buf, '/')))
-                       strcpy(buf, &buf[1]);
+       while (a < 10) {
+               /* basename of filename */
+               strcpy(tbuf, flnm);
+               if (haschar(tbuf, '/'))
+                       strcpy(tbuf, strrchr(tbuf, '/'));
+               /* filename.1, filename.2, etc */
                if (a > 0) {
-                       size_t tmp = strlen(buf);
-                       snprintf(&buf[tmp], sizeof buf - tmp, "%d", a);
+                       sprintf(buf + strlen(buf), ".%d", a);
                }
-               snprintf(tbuf, sizeof tbuf, "UOPN %s|%s", buf, desc);
-               CtdlIPC_putline(ipc, tbuf);
-               CtdlIPC_getline(ipc, buf);
-               if (buf[0] != '2')
-                       scr_printf("%s\n", &buf[4]);
+               /* Try upload */
+               r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
+               if (r / 100 == 5 || r < 0)
+                       scr_printf("%s\n", buf);
+               else
+                       break;
                ++a;
-       } while (buf[0] != '2');
-
-       /* at this point we have an open upload file at the server */
-       do_upload(ipc, fd);
+       };
 }
 
 
@@ -317,29 +273,22 @@ void cli_image_upload(CtdlIPC *ipc, char *keyname)
 {
        char flnm[SIZ];
        char buf[SIZ];
-       int fd;
+       int r;
 
-       snprintf(buf, sizeof buf, "UIMG 0|%s", keyname);
-       CtdlIPC_putline(ipc, buf);
-       CtdlIPC_getline(ipc, buf);
-       if (buf[0] != '2') {
-               scr_printf("%s\n", &buf[4]);
+       /* Can we upload this image? */
+       r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
+       if (r / 100 != 2) {
+               err_printf("%s\n", buf);
                return;
        }
        newprompt("Image file to be uploaded: ", flnm, 55);
-       fd = open(flnm, O_RDONLY);
-       if (fd < 0) {
-               scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
-               return;
+       r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
+       if (r / 100 == 5) {
+               err_printf("%s\n", buf);
+       } else if (r < 0) {
+               err_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
        }
-       snprintf(buf, sizeof buf, "UIMG 1|%s", keyname);
-       CtdlIPC_putline(ipc, buf);
-       CtdlIPC_getline(ipc, buf);
-       if (buf[0] != '2') {
-               scr_printf("%s\n", &buf[4]);
-               return;
-       }
-       do_upload(ipc, fd);
+       /* else upload succeeded */
 }
 
 
@@ -355,7 +304,7 @@ void upload(CtdlIPC *ipc, int c)
        int xfer_pid;
        int a, b;
        FILE *fp, *lsfp;
-       int fd;
+       int r;
 
        if ((room_flags & QR_UPLOAD) == 0) {
                scr_printf("*** You cannot upload to this room.\n");
@@ -373,8 +322,6 @@ void upload(CtdlIPC *ipc, int c)
                    || (flnm[a] == ';') || (flnm[a] == '&'))
                        flnm[a] = '_';
 
-       newprompt("Enter a short description of the file:\n: ", desc, 150);
-
        /* create a temporary directory... */
        if (mkdir(tempdir, 0700) != 0) {
                scr_printf("*** Could not create temporary directory %s: %s\n",
@@ -430,44 +377,19 @@ void upload(CtdlIPC *ipc, int c)
                nukedir(tempdir);
                return;
        }
-       scr_printf("\r*** Transfer successful.  Sending file(s) to server...\n");
+       scr_printf("\r*** Transfer successful.\n");
        snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
        lsfp = popen(buf, "r");
        if (lsfp != NULL) {
                while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
-                       flnm[strlen(flnm) - 1] = 0;
+                       flnm[strlen(flnm) - 1] = 0;     /* chop newline */
+                       snprintf(buf, sizeof buf,
+                                "Enter a short description of '%s':\n: ",
+                                flnm);
+                       newprompt(buf, desc, 150);
                        snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
-                       fd = open(buf, O_RDONLY);
-                       if (fd >= 0) {
-                               a = 0;
-                               do {
-                                       snprintf(buf, sizeof buf, "UOPN %s|%s", flnm, desc);
-                                       if (a > 0) {
-                                               size_t tmp = strlen(buf);
-                                               snprintf(&buf[tmp],
-                                                       sizeof buf - tmp,
-                                                       ".%d", a);
-                                       }
-                                       ++a;
-                                       CtdlIPC_putline(ipc, buf);
-                                       CtdlIPC_getline(ipc, buf);
-                               } while ((buf[0] != '2') && (a < 100));
-                               if (buf[0] == '2')
-                                       do {
-                                               a = read(fd, tbuf, 4096);
-                                               if (a > 0) {
-                                                       snprintf(buf, sizeof buf, "WRIT %d", a);
-                                                       CtdlIPC_putline(ipc, buf);
-                                                       CtdlIPC_getline(ipc, buf);
-                                                       if (buf[0] == '7')
-                                                               serv_write(ipc, tbuf, a);
-                                               }
-                                       } while (a > 0);
-                               close(fd);
-                               CtdlIPC_putline(ipc, "UCLS 1");
-                               CtdlIPC_getline(ipc, buf);
-                               scr_printf("%s\n", &buf[4]);
-                       }
+                       r = CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
+                       scr_printf("%s\n", tbuf);
                }
                pclose(lsfp);
        }