From a8b4a28372645c93621008b2563c00ce6cdd739f Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Fri, 13 Sep 2019 09:17:58 +0200 Subject: [PATCH] cgi-io: use splice() to stream backup archive This improves the I/O performance when outputting large backups. Signed-off-by: Jo-Philipp Wich --- net/cgi-io/Makefile | 2 +- net/cgi-io/src/main.c | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/net/cgi-io/Makefile b/net/cgi-io/Makefile index eaf03b40d..211360905 100644 --- a/net/cgi-io/Makefile +++ b/net/cgi-io/Makefile @@ -8,7 +8,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=cgi-io -PKG_RELEASE:=11 +PKG_RELEASE:=12 PKG_LICENSE:=GPL-2.0-or-later diff --git a/net/cgi-io/src/main.c b/net/cgi-io/src/main.c index d19277d6a..ca1575842 100644 --- a/net/cgi-io/src/main.c +++ b/net/cgi-io/src/main.c @@ -16,6 +16,8 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#define _GNU_SOURCE /* splice(), SPLICE_F_MORE */ + #include #include #include @@ -736,7 +738,6 @@ main_backup(int argc, char **argv) int len; int status; int fds[2]; - char buf[4096]; char datestr[16] = { 0 }; char hostname[64] = { 0 }; char *fields[] = { "sessionid", NULL }; @@ -768,7 +769,6 @@ main_backup(int argc, char **argv) return -1; default: - fcntl(fds[0], F_SETFL, fcntl(fds[0], F_GETFL) | O_NONBLOCK); now = time(NULL); strftime(datestr, sizeof(datestr) - 1, "%Y-%m-%d", localtime(&now)); @@ -780,15 +780,13 @@ main_backup(int argc, char **argv) printf("Content-Disposition: attachment; " "filename=\"backup-%s-%s.tar.gz\"\r\n\r\n", hostname, datestr); - do { - waitpid(pid, &status, 0); + fflush(stdout); - while ((len = read(fds[0], buf, sizeof(buf))) > 0) { - fwrite(buf, len, 1, stdout); - fflush(stdout); - } + do { + len = splice(fds[0], NULL, 1, NULL, 4096, SPLICE_F_MORE); + } while (len > 0); - } while (!WIFEXITED(status)); + waitpid(pid, &status, 0); close(fds[0]); close(fds[1]);