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.

This commit is contained in:
Martin Schröder 2015-04-17 10:45:07 +02:00 committed by Martin Schröder
parent 34d2fed9d2
commit 100ada27f8

View file

@ -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 -");
}
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);
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");