From 100ada27f86ed47669e6a789c645a65f83479eae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Schr=C3=B6der?= Date: Fri, 17 Apr 2015 10:45:07 +0200 Subject: [PATCH] Replaced original forked version with a piped one. Original version for some reason resulted in incomplete file transfer. This works better. Just needs to be optimized. --- luci2/luci2/src/io/main.c | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/luci2/luci2/src/io/main.c b/luci2/luci2/src/io/main.c index b08efc636..cc9e9f870 100644 --- a/luci2/luci2/src/io/main.c +++ b/luci2/luci2/src/io/main.c @@ -571,7 +571,7 @@ main_upload(int argc, char *argv[]) return 0; } -/* + static int exec_command(const char *cmd, char **out){ FILE *fd; int buffer_size = 4096; @@ -594,18 +594,6 @@ static int exec_command(const char *cmd, char **out){ } *out = data; return (int)(pdata - data); -}*/ - -static void run_child(const char *cmd){ - FILE *fd; - int c; - - if ((fd = popen(cmd, "r"))) { - while((c = fgetc(fd)) != EOF){ - putc(c, stdout); - } - pclose(fd); - } } static int @@ -618,9 +606,9 @@ main_backup(int argc, char **argv) char buf[4096];*/ char datestr[16] = { 0 }; char hostname[64] = { 0 }; - char *fields[] = { "sessionid", NULL }; + char *fields[] = { "sessionid", NULL, "password", NULL }; - if (!postdecode(fields, 1) || !session_access(fields[1], "backup", "read")) + if (!postdecode(fields, 2) || !session_access(fields[1], "backup", "read")) return failure(0, "Backup permission denied"); now = time(NULL); @@ -632,19 +620,27 @@ main_backup(int argc, char **argv) printf("Status: 200 OK\r\n"); char cmd[256]; + char *data; if(fields[3]){ snprintf(cmd, sizeof(cmd), "sysupgrade --create-backup - --password %s", fields[3]); } else { snprintf(cmd, sizeof(cmd), "sysupgrade --create-backup -"); } - - printf("Content-Type: application/x-targz\r\n"); - printf("Content-Disposition: attachment; " - "filename=\"backup-%s-%s.tar.gz\"\r\n\r\n", hostname, datestr); + int size = exec_command(cmd, &data); + if(size){ + printf("Content-Type: application/x-targz\r\n"); + printf("Content-Disposition: attachment; " + "filename=\"backup-%s-%s.tar.gz\"\r\n\r\n", hostname, datestr); - run_child(cmd); - + fwrite(data, size, 1, stdout); + fflush(stdout); + free(data); + } else { + // TODO: make sure that we return some kind of error code so we can show it in the gui. + // otherwise the form post just silently fails without showing this message! + printf("No data!\n"); + } /* if (pipe(fds)) return failure(errno, "Failed to spawn pipe");