From 997985d79e5813e8a33e82fb0cc0c0f08cf2c55d Mon Sep 17 00:00:00 2001 From: Erwan Velu Date: Tue, 15 Mar 2011 21:51:46 +0100 Subject: libupload: New library to manage uploads This commit creates a library to upload content via 3 backends (srec/ymodem/tftp). Code came from sysdump and got librarized for being used more easily by more other com32 modules. --- com32/libupload/cpio.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 com32/libupload/cpio.c (limited to 'com32/libupload/cpio.c') diff --git a/com32/libupload/cpio.c b/com32/libupload/cpio.c new file mode 100644 index 00000000..b3e1eb78 --- /dev/null +++ b/com32/libupload/cpio.c @@ -0,0 +1,75 @@ +/* + * cpio.c + * + * Write a compressed CPIO file + */ + +#include +#include +#include +#include +#include +#include "upload_backend.h" +#include "ctime.h" + +int cpio_pad(struct upload_backend *be) +{ + static char pad[4]; /* Up to 4 zero bytes */ + if (be->dbytes & 3) + return write_data(be, pad, -be->dbytes & 3); + else + return 0; +} + +int cpio_hdr(struct upload_backend *be, uint32_t mode, size_t datalen, + const char *filename) +{ + static uint32_t inode = 2; + char hdr[6+13*8+1]; + int nlen = strlen(filename)+1; + int rv = 0; + + cpio_pad(be); + + sprintf(hdr, "%06o%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x", + 070701, /* c_magic */ + inode++, /* c_ino */ + mode, /* c_mode */ + 0, /* c_uid */ + 0, /* c_gid */ + 1, /* c_nlink */ + be->now, /* c_mtime */ + datalen, /* c_filesize */ + 0, /* c_maj */ + 0, /* c_min */ + 0, /* c_rmaj */ + 0, /* c_rmin */ + nlen, /* c_namesize */ + 0); /* c_chksum */ + rv |= write_data(be, hdr, 6+13*8); + rv |= write_data(be, filename, nlen); + rv |= cpio_pad(be); + return rv; +} + +int cpio_mkdir(struct upload_backend *be, const char *filename) +{ + return cpio_hdr(be, MODE_DIR, 0, filename); +} + +int cpio_writefile(struct upload_backend *be, const char *filename, + const void *data, size_t len) +{ + int rv; + + rv = cpio_hdr(be, MODE_FILE, len, filename); + rv |= write_data(be, data, len); + rv |= cpio_pad(be); + + return rv; +} + +int cpio_close(struct upload_backend *be) +{ + return cpio_hdr(be, 0, 0, "TRAILER!!!"); +} -- cgit v1.2.1 From ccd3c55c47543ee0447f4a5cb545f9d2a22327d9 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Mon, 20 Jun 2011 21:31:51 -0700 Subject: libupload: use %zx to print a size_t argument To print an argument of type size_t we should use the %z size modifier. Signed-off-by: H. Peter Anvin --- com32/libupload/cpio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'com32/libupload/cpio.c') diff --git a/com32/libupload/cpio.c b/com32/libupload/cpio.c index b3e1eb78..25b464d4 100644 --- a/com32/libupload/cpio.c +++ b/com32/libupload/cpio.c @@ -31,7 +31,7 @@ int cpio_hdr(struct upload_backend *be, uint32_t mode, size_t datalen, cpio_pad(be); - sprintf(hdr, "%06o%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x", + sprintf(hdr, "%06o%08x%08x%08x%08x%08x%08x%08zx%08x%08x%08x%08x%08x%08x", 070701, /* c_magic */ inode++, /* c_ino */ mode, /* c_mode */ -- cgit v1.2.1