From f917a0ec6909c24990dd7e68793b1399a34c7f52 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 13 Dec 2022 16:17:39 +0000 Subject: [PATCH] implemented a simple untar method in hio-webs --- Makefile.in | 2 +- bin/webs.c | 43 ++++++++++++++++++++++++++++++++++--------- lib/hio-tar.h | 7 +++++++ lib/tar.c | 15 +++++++++++---- 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/Makefile.in b/Makefile.in index 365ea22..50d0354 100644 --- a/Makefile.in +++ b/Makefile.in @@ -165,7 +165,7 @@ am__DIST_COMMON = $(srcdir)/Dockerfile.in $(srcdir)/Makefile.in \ $(top_srcdir)/ac/config.guess $(top_srcdir)/ac/config.sub \ $(top_srcdir)/ac/install-sh $(top_srcdir)/ac/ltmain.sh \ $(top_srcdir)/ac/missing $(top_srcdir)/pkgs/hio.spec.in \ - ac/ar-lib ac/compile ac/config.guess ac/config.sub \ + ac/ar-lib ac/compile ac/config.guess ac/config.sub ac/depcomp \ ac/install-sh ac/ltmain.sh ac/missing DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) distdir = $(PACKAGE)-$(VERSION) diff --git a/bin/webs.c b/bin/webs.c index 0e9369f..0344583 100644 --- a/bin/webs.c +++ b/bin/webs.c @@ -11,11 +11,13 @@ struct htts_ext_t }; typedef struct htts_ext_t htts_ext_t; - void untar (hio_t* hio, hio_dev_thr_iopair_t* iop, hio_svc_htts_thr_func_info_t* tfi, void* ctx) { - FILE* wfp = NULL; + FILE* wfp = HIO_NULL; htts_ext_t* ext; + hio_tar_t* tar = HIO_NULL; + hio_uint8_t buf[4096]; + ssize_t n; ext = hio_svc_htts_getxtn(tfi->htts); @@ -26,21 +28,38 @@ void untar (hio_t* hio, hio_dev_thr_iopair_t* iop, hio_svc_htts_thr_func_info_t* goto done; } -#if 0 - if (hio_extract_tarfd(hio, iop->rfd, ext->docroot + tfi->req_path) <= -1) + tar = hio_tar_open(hio, 0); + if (!tar) { -/* TODO: better error message */ - write (iop->wfd, "Status: 500\r\n\r\n", 15); + write (iop->wfd, "Status: 500\r\n\r\n", 15); goto done; } -#endif + hio_tar_setxrootwithbcstr (tar, ext->docroot); + + while (1) + { + n = read(iop->rfd, buf, HIO_COUNTOF(buf)); + if (n <= 0) break; /* eof or error */ + + if (hio_tar_xfeed(tar, buf, n) <= -1) + { + write (iop->wfd, "Status: 500\r\n\r\n", 20); + goto done; + } + } + + hio_tar_endxfeed (tar); write (iop->wfd, "Status: 200\r\n\r\n", 15); done: + if (tar) + { + hio_tar_close (tar); + } if (wfp) { - iop->wfd = HIO_SYSHND_INVALID; /* prevent double close by the main library */ + iop->wfd = HIO_SYSHND_INVALID; /* prevent double close by the main library since this function closes it with fclose() */ fclose (wfp); } } @@ -178,7 +197,13 @@ int main (int argc, char* argv[]) } hio_setoption (hio, HIO_LOG_TARGET_BCSTR, "/dev/stderr"); - + { + hio_bitmask_t logmask; + hio_getoption (hio, HIO_LOG_MASK, &logmask); + logmask |= HIO_LOG_GUARDED; + hio_setoption (hio, HIO_LOG_MASK, &logmask); + } + g_hio = hio; if (webs_start(hio, argv[1], argv[2]) <= -1) goto oops; diff --git a/lib/hio-tar.h b/lib/hio-tar.h index dbc21d7..6c05eea 100644 --- a/lib/hio-tar.h +++ b/lib/hio-tar.h @@ -85,6 +85,8 @@ struct hio_tar_t struct { + hio_bch_t root[2048]; + hio_tar_state_t state; struct { @@ -135,6 +137,11 @@ HIO_EXPORT int hio_tar_xfeed ( hio_oow_t len ); +HIO_EXPORT void hio_tar_setxrootwithbcstr ( + hio_tar_t* tar, + const hio_bch_t* root +); + #if defined(__cplusplus) } #endif diff --git a/lib/tar.c b/lib/tar.c index aad13d2..dbe57b8 100644 --- a/lib/tar.c +++ b/lib/tar.c @@ -136,11 +136,13 @@ static int x_process_header (hio_tar_t* tar) } hio_becs_clear (&tar->x.hi.filename); - if (hio_becs_cat(&tar->x.hi.filename, hdr->prefix) == (hio_oow_t)-1 || - hio_becs_cat(&tar->x.hi.filename, hdr->name) == (hio_oow_t)-1) + if (tar->x.root[0] != '\0') { - return -1; + if (hio_becs_cat(&tar->x.hi.filename, tar->x.root) == (hio_oow_t)-1) return -1; + if (HIO_BECS_LASTCHAR(&tar->x.hi.filename) != '/' && hio_becs_ccat(&tar->x.hi.filename, '/') == (hio_oow_t)-1) return -1; } + if (hio_becs_cat(&tar->x.hi.filename, hdr->prefix) == (hio_oow_t)-1 || + hio_becs_cat(&tar->x.hi.filename, hdr->name) == (hio_oow_t)-1) return -1; filename = HIO_BECS_PTR(&tar->x.hi.filename); switch (hdr->typeflag) @@ -158,7 +160,7 @@ static int x_process_header (hio_tar_t* tar) mknod (filename, S_IFBLK | tar->x.hi.filemode, ((tar->x.hi.devmajor << 8) | tar->x.hi.devminor)); break; case HIO_TAR_DIRTYPE: - create_dir (hdr->name, tar->x.hi.filemode); + create_dir (filename, tar->x.hi.filemode); break; case HIO_TAR_FIFOTYPE: mkfifo (filename, tar->x.hi.filemode); @@ -272,3 +274,8 @@ int hio_tar_xfeed (hio_tar_t* tar, const void* ptr, hio_oow_t len) return 0; } + +void hio_tar_setxrootwithbcstr (hio_tar_t* tar, const hio_bch_t* root) +{ + hio_copy_bcstr (tar->x.root, HIO_COUNTOF(tar->x.root), root); /* TOOD: handle truncation. make tar->x.root dyanmic? */ +} \ No newline at end of file