hio/lib/tar.c

275 lines
6.2 KiB
C
Raw Normal View History

2022-10-25 22:47:40 +00:00
#include <hio-tar.h>
#include <hio-utl.h>
#include <hio-prv.h>
2022-11-15 14:31:14 +00:00
2022-10-25 22:47:40 +00:00
#include <sys/types.h>
#include <sys/stat.h>
2022-12-13 14:21:19 +00:00
#include <fcntl.h>
#include <unistd.h>
2022-11-15 14:31:14 +00:00
#include <stdio.h>
#include <errno.h>
2022-10-25 22:47:40 +00:00
static hio_uint8_t _end_block[HIO_TAR_BLKSIZE] = { 0, };
#if 0
static int verify_checksum(const char *p)
{
int n, u = 0;
for (n = 0; n < 512; n++)
{
if (n < 148 || n > 155)
/* Standard tar checksum adds unsigned bytes. */
u += ((hio_uint8_t*)p)[n];
else
u += 0x20;
}
return (u == parseoct(p + 148, 8));
}
#endif
static int create_dir (hio_bch_t *pathname, int mode)
{
char *p;
int n;
hio_oow_t pathlen;
pathlen = hio_count_bcstr(pathname);
/* Strip trailing '/' TODO: improve ...*/
if (pathname[pathlen - 1] == '/')
pathname[pathlen - 1] = '\0';
n = mkdir(pathname, mode);
if (n <= -1)
{
/* On failure, try creating parent directory. */
p = hio_rfind_bchar_in_bcstr(pathname, '/');
if (p)
{
*p = '\0';
create_dir(pathname, 0755);
*p = '/';
n = mkdir(pathname, mode);
}
}
return n;
}
hio_tar_t* hio_tar_open (hio_t* hio, hio_oow_t xtnsize)
{
hio_tar_t* tar;
tar = (hio_tar_t*)hio_callocmem(hio, HIO_SIZEOF(*tar) + xtnsize);
if (tar)
{
2022-12-04 16:51:10 +00:00
if (hio_tar_init(tar, hio) <= -1)
2022-10-25 22:47:40 +00:00
{
hio_freemem (hio, tar);
tar = HIO_NULL;
}
}
return tar;
}
void hio_tar_close (hio_tar_t* tar)
{
hio_tar_fini (tar);
hio_freemem (tar->hio, tar);
}
2022-12-04 16:51:10 +00:00
int hio_tar_init (hio_tar_t* tar, hio_t* hio)
2022-10-25 22:47:40 +00:00
{
2022-12-04 16:51:10 +00:00
tar->hio = hio;
2022-12-13 14:21:19 +00:00
tar->x.state = HIO_TAR_STATE_START;
tar->x.blk.len = 0;
hio_becs_init (&tar->x.hi.filename, tar->hio, 0); /* won't fail with the capacity of 0 */
2022-10-25 22:47:40 +00:00
return 0;
}
void hio_tar_fini (hio_tar_t* tar)
{
2022-12-13 14:21:19 +00:00
hio_becs_fini (&tar->x.hi.filename);
if (tar->x.hi.fp)
2022-11-29 16:23:53 +00:00
{
/* clean up */
2022-12-13 14:21:19 +00:00
fclose (tar->x.hi.fp);
tar->x.hi.fp = HIO_NULL;
2022-11-29 16:23:53 +00:00
}
2022-10-25 22:47:40 +00:00
}
2022-12-13 14:21:19 +00:00
/* extraction - the implementation is still far from completion */
static int x_process_header (hio_tar_t* tar)
2022-10-25 22:47:40 +00:00
{
hio_tar_hdr_t* hdr;
2022-12-13 14:21:19 +00:00
HIO_ASSERT (tar->hio, tar->x.state == HIO_TAR_STATE_START);
HIO_ASSERT (tar->hio, tar->x.blk.len == HIO_TAR_BLKSIZE);
hdr = (hio_tar_hdr_t*)tar->x.blk.buf;
2022-10-25 22:47:40 +00:00
/* all-zero byte block ends the archive */
if (HIO_MEMCMP(hdr, _end_block, HIO_TAR_BLKSIZE) == 0)
{
2022-12-05 16:04:11 +00:00
/* two all-zero blocks are expected as the EOF indicator */
2022-12-13 14:21:19 +00:00
tar->x.state = HIO_TAR_STATE_END;
2022-11-14 23:21:42 +00:00
}
else
{
int is_sober;
const hio_bch_t* endptr;
2022-12-04 16:51:10 +00:00
const hio_bch_t* filename;
2022-11-14 23:21:42 +00:00
2022-12-13 14:21:19 +00:00
tar->x.hi.filesize = hio_bchars_to_uintmax(hdr->size, HIO_COUNTOF(hdr->size), HIO_BCHARS_TO_UINTMAX_MAKE_OPTION(0,0,0,8), &endptr, &is_sober);
tar->x.hi.filemode = hio_bchars_to_uintmax(hdr->mode, HIO_COUNTOF(hdr->mode), HIO_BCHARS_TO_UINTMAX_MAKE_OPTION(0,0,0,8), &endptr, &is_sober);
tar->x.hi.devmajor = hio_bchars_to_uintmax(hdr->devmajor, HIO_COUNTOF(hdr->devmajor), HIO_BCHARS_TO_UINTMAX_MAKE_OPTION(0,0,0,8), &endptr, &is_sober);
tar->x.hi.devminor = hio_bchars_to_uintmax(hdr->devminor, HIO_COUNTOF(hdr->devminor), HIO_BCHARS_TO_UINTMAX_MAKE_OPTION(0,0,0,8), &endptr, &is_sober);
2022-11-14 23:21:42 +00:00
2022-12-13 14:21:19 +00:00
if (tar->x.hi.fp)
2022-11-14 23:21:42 +00:00
{
2022-11-29 16:23:53 +00:00
/* just in case */
2022-12-13 14:21:19 +00:00
fclose (tar->x.hi.fp);
tar->x.hi.fp = HIO_NULL;
2022-11-14 23:21:42 +00:00
}
2022-11-15 14:31:14 +00:00
2022-12-13 14:21:19 +00:00
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)
2022-12-04 16:51:10 +00:00
{
return -1;
}
2022-11-30 05:01:25 +00:00
2022-12-13 14:21:19 +00:00
filename = HIO_BECS_PTR(&tar->x.hi.filename);
2022-11-29 16:23:53 +00:00
switch (hdr->typeflag)
2022-11-14 23:21:42 +00:00
{
2022-11-29 16:23:53 +00:00
case HIO_TAR_LNKTYPE:
2022-12-13 14:21:19 +00:00
link (hdr->linkname, filename);
2022-11-29 16:23:53 +00:00
break;
case HIO_TAR_SYMTYPE:
2022-12-13 14:21:19 +00:00
symlink (hdr->linkname, filename); /* TODO: error check */
2022-11-29 16:23:53 +00:00
break;
2022-12-13 14:21:19 +00:00
case HIO_TAR_CHRTYPE:
mknod (filename, S_IFCHR | tar->x.hi.filemode, ((tar->x.hi.devmajor << 8) | tar->x.hi.devminor));
2022-11-29 16:23:53 +00:00
break;
case HIO_TAR_BLKTYPE:
2022-12-13 14:21:19 +00:00
mknod (filename, S_IFBLK | tar->x.hi.filemode, ((tar->x.hi.devmajor << 8) | tar->x.hi.devminor));
2022-11-29 16:23:53 +00:00
break;
case HIO_TAR_DIRTYPE:
2022-12-13 14:21:19 +00:00
create_dir (hdr->name, tar->x.hi.filemode);
2022-11-29 16:23:53 +00:00
break;
case HIO_TAR_FIFOTYPE:
2022-12-13 14:21:19 +00:00
mkfifo (filename, tar->x.hi.filemode);
2022-11-29 16:23:53 +00:00
break;
2022-12-13 14:21:19 +00:00
case HIO_TAR_CONTTYPE: /* treate it like REGTYPE for now */
2022-11-29 16:23:53 +00:00
default: /* HIO_TAR_REGTYPE */
2022-11-15 14:31:14 +00:00
{
2022-11-29 16:23:53 +00:00
FILE* fp;
2022-12-04 16:51:10 +00:00
fp = fopen(filename, "wb+");
2022-11-29 16:23:53 +00:00
if (!fp)
{
hio_seterrwithsyserr (tar->hio, 0, errno);
return -1;
}
2022-12-13 14:21:19 +00:00
fchmod (fileno(fp), tar->x.hi.filemode);
2022-11-29 16:23:53 +00:00
2022-12-13 14:21:19 +00:00
tar->x.hi.fp = fp;
tar->x.state = HIO_TAR_STATE_FILE;
2022-11-29 16:23:53 +00:00
goto done;
2022-11-15 14:31:14 +00:00
}
2022-11-14 23:21:42 +00:00
}
2022-11-29 16:23:53 +00:00
2022-12-13 14:21:19 +00:00
tar->x.state = HIO_TAR_STATE_START;
2022-10-25 22:47:40 +00:00
}
2022-11-29 16:23:53 +00:00
done:
2022-10-25 22:47:40 +00:00
return 0;
}
2022-12-13 14:21:19 +00:00
static int x_process_content (hio_tar_t* tar)
2022-11-14 23:21:42 +00:00
{
hio_oow_t chunksize;
2022-12-13 14:21:19 +00:00
HIO_ASSERT (tar->hio, tar->x.blk.len == HIO_TAR_BLKSIZE);
HIO_ASSERT (tar->hio, tar->x.hi.filesize > 0);
HIO_ASSERT (tar->hio, tar->x.hi.fp != HIO_NULL);
2022-11-14 23:21:42 +00:00
2022-12-13 14:21:19 +00:00
chunksize = tar->x.hi.filesize < tar->x.blk.len? tar->x.hi.filesize: tar->x.blk.len;
2022-11-14 23:21:42 +00:00
2022-11-15 14:31:14 +00:00
/* TODO: error check */
2022-12-13 14:21:19 +00:00
fwrite (tar->x.blk.buf, 1, chunksize, tar->x.hi.fp);
2022-11-14 23:21:42 +00:00
2022-12-13 14:21:19 +00:00
tar->x.hi.filesize -= chunksize;
if (tar->x.hi.filesize <= 0)
2022-11-14 23:21:42 +00:00
{
/* end of file */
2022-12-13 14:21:19 +00:00
fclose (tar->x.hi.fp);
tar->x.hi.fp = HIO_NULL;
2022-12-13 14:21:19 +00:00
tar->x.state = HIO_TAR_STATE_START;
2022-11-14 23:21:42 +00:00
}
2022-11-15 14:31:14 +00:00
return 0;
2022-11-14 23:21:42 +00:00
}
2022-10-25 22:47:40 +00:00
2022-12-13 14:21:19 +00:00
int hio_tar_xfeed (hio_tar_t* tar, const void* ptr, hio_oow_t len)
2022-10-25 22:47:40 +00:00
{
if (!ptr)
{
/* EOF indicator */
2022-12-13 14:21:19 +00:00
if (tar->x.state != HIO_TAR_STATE_END || tar->x.blk.len > 0)
2022-10-25 22:47:40 +00:00
{
/* ERROR - premature end of file */
2022-11-14 23:21:42 +00:00
hio_seterrbfmt (tar->hio, HIO_EINVAL, "premature end of feed");
2022-10-25 22:47:40 +00:00
return -1;
}
}
while (len > 0)
{
2022-11-14 23:21:42 +00:00
hio_oow_t cplen;
2022-12-13 14:21:19 +00:00
cplen = HIO_COUNTOF(tar->x.blk.buf) - tar->x.blk.len; /* required length to fill a block */
2022-11-14 23:21:42 +00:00
if (len < cplen) cplen = len; /* not enough to fill a block */
2022-12-13 14:21:19 +00:00
HIO_MEMCPY (&tar->x.blk.buf[tar->x.blk.len], ptr, cplen);
tar->x.blk.len += cplen;
2022-11-14 23:21:42 +00:00
len -= cplen;
2022-11-30 05:01:25 +00:00
ptr += cplen;
2022-11-14 23:21:42 +00:00
2022-12-13 14:21:19 +00:00
if (tar->x.blk.len == HIO_COUNTOF(tar->x.blk.buf))
2022-10-25 22:47:40 +00:00
{
2022-11-14 23:21:42 +00:00
/* on a complete block */
2022-12-13 14:21:19 +00:00
switch (tar->x.state)
2022-10-25 22:47:40 +00:00
{
2022-11-14 23:21:42 +00:00
case HIO_TAR_STATE_START:
2022-12-13 14:21:19 +00:00
if (x_process_header(tar) <= -1) return -1;
2022-11-14 23:21:42 +00:00
break;
2022-10-25 22:47:40 +00:00
2022-11-14 23:21:42 +00:00
case HIO_TAR_STATE_FILE:
2022-12-13 14:21:19 +00:00
if (x_process_content(tar) <= -1) return -1;
2022-11-14 23:21:42 +00:00
break;
2022-10-25 22:47:40 +00:00
2022-12-13 14:21:19 +00:00
case HIO_TAR_STATE_END:
if (HIO_MEMCMP(tar->x.blk.buf, _end_block, HIO_TAR_BLKSIZE) != 0)
2022-12-05 16:04:11 +00:00
{
2022-12-13 14:21:19 +00:00
hio_seterrbfmt (tar->hio, HIO_EINVAL, "trailing garbage at the end of feed");
return -1;
2022-12-05 16:04:11 +00:00
}
2022-12-13 14:21:19 +00:00
/* there may come multiple EOF marker blocks depending on the logical record size.
* this implementation doesn't care how much such blocks are given */
2022-12-05 16:04:11 +00:00
break;
2022-11-14 23:21:42 +00:00
}
2022-11-30 05:01:25 +00:00
2022-12-13 14:21:19 +00:00
tar->x.blk.len = 0;
2022-10-25 22:47:40 +00:00
}
}
return 0;
}