implemented a simple untar method in hio-webs

This commit is contained in:
hyung-hwan 2022-12-13 16:17:39 +00:00
parent 7904964e0d
commit f917a0ec69
4 changed files with 53 additions and 14 deletions

View File

@ -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/config.guess $(top_srcdir)/ac/config.sub \
$(top_srcdir)/ac/install-sh $(top_srcdir)/ac/ltmain.sh \ $(top_srcdir)/ac/install-sh $(top_srcdir)/ac/ltmain.sh \
$(top_srcdir)/ac/missing $(top_srcdir)/pkgs/hio.spec.in \ $(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 ac/install-sh ac/ltmain.sh ac/missing
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
distdir = $(PACKAGE)-$(VERSION) distdir = $(PACKAGE)-$(VERSION)

View File

@ -11,11 +11,13 @@ struct htts_ext_t
}; };
typedef struct htts_ext_t 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) 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; 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); 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; goto done;
} }
#if 0 tar = hio_tar_open(hio, 0);
if (hio_extract_tarfd(hio, iop->rfd, ext->docroot + tfi->req_path) <= -1) 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; 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); write (iop->wfd, "Status: 200\r\n\r\n", 15);
done: done:
if (tar)
{
hio_tar_close (tar);
}
if (wfp) 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); fclose (wfp);
} }
} }
@ -178,6 +197,12 @@ int main (int argc, char* argv[])
} }
hio_setoption (hio, HIO_LOG_TARGET_BCSTR, "/dev/stderr"); 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; g_hio = hio;

View File

@ -85,6 +85,8 @@ struct hio_tar_t
struct struct
{ {
hio_bch_t root[2048];
hio_tar_state_t state; hio_tar_state_t state;
struct struct
{ {
@ -135,6 +137,11 @@ HIO_EXPORT int hio_tar_xfeed (
hio_oow_t len hio_oow_t len
); );
HIO_EXPORT void hio_tar_setxrootwithbcstr (
hio_tar_t* tar,
const hio_bch_t* root
);
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif

View File

@ -136,11 +136,13 @@ static int x_process_header (hio_tar_t* tar)
} }
hio_becs_clear (&tar->x.hi.filename); hio_becs_clear (&tar->x.hi.filename);
if (hio_becs_cat(&tar->x.hi.filename, hdr->prefix) == (hio_oow_t)-1 || if (tar->x.root[0] != '\0')
hio_becs_cat(&tar->x.hi.filename, hdr->name) == (hio_oow_t)-1)
{ {
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); filename = HIO_BECS_PTR(&tar->x.hi.filename);
switch (hdr->typeflag) 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)); mknod (filename, S_IFBLK | tar->x.hi.filemode, ((tar->x.hi.devmajor << 8) | tar->x.hi.devminor));
break; break;
case HIO_TAR_DIRTYPE: case HIO_TAR_DIRTYPE:
create_dir (hdr->name, tar->x.hi.filemode); create_dir (filename, tar->x.hi.filemode);
break; break;
case HIO_TAR_FIFOTYPE: case HIO_TAR_FIFOTYPE:
mkfifo (filename, tar->x.hi.filemode); 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; 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? */
}