From b992e1e0043738c42a35957e890fb7392aadd095 Mon Sep 17 00:00:00 2001 From: Danny Abukalam Date: Tue, 13 Dec 2011 14:46:32 +0000 Subject: Added library to control reading and writing to streams Added Cross-endian platform support Changed gids and uids to respective variables Forced time_t to be written as a uint64_t to support cross-architecture --- Makefile | 7 +-- libtbd_apply.c | 135 +++++++++++++++++++++++++------------------------- libtbd_create.c | 33 +++++++------ libtbd_io.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ libtbd_io.h | 42 ++++++++++++++++ libtbd_stat.c | 6 +-- libtbd_stat.h | 6 +-- libtbd_xattrs.c | 6 +-- libtbd_xattrs.h | 3 +- 9 files changed, 291 insertions(+), 96 deletions(-) create mode 100644 libtbd_io.c create mode 100644 libtbd_io.h diff --git a/Makefile b/Makefile index 41b99c5..0f61638 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ CFLAGS ?= CFLAGS += -g CFLAGS += -Wall -Wextra -Werror -Wno-unused-result $(OPT) -SHARED_SRC := libtbd_stat.c libtbd_xattrs.c +SHARED_SRC := libtbd_stat.c libtbd_xattrs.c libtbd_io.c DEPLOY_SRC := tbdiff_deploy.c libtbd_create.c CREATE_SRC := tbdiff_create.c libtbd_apply.c @@ -20,10 +20,10 @@ CREATE_OBJ := $(patsubst %.c,%.o,$(SHARED_SRC) $(CREATE_SRC)) all: $(DEPLOY) $(CREATE) -$(DEPLOY): tbdiff_deploy.o libtbd_apply.o libtbd_stat.o libtbd_xattrs.o +$(DEPLOY): tbdiff_deploy.o libtbd_apply.o libtbd_stat.o libtbd_xattrs.o libtbd_io.o $(CC) $(LDFLAGS) -o $@ $^ -$(CREATE): tbdiff_create.o libtbd_create.o libtbd_stat.o libtbd_xattrs.o +$(CREATE): tbdiff_create.o libtbd_create.o libtbd_stat.o libtbd_xattrs.o libtbd_io.o $(CC) $(LDFLAGS) -o $@ $^ %.o: %.c @@ -46,3 +46,4 @@ test: .PHONY: clean clean: rm -f $(DEPLOY) $(CREATE) *.o *.d +check: test diff --git a/libtbd_apply.c b/libtbd_apply.c index 4bf13ca..bac1ae2 100644 --- a/libtbd_apply.c +++ b/libtbd_apply.c @@ -34,12 +34,13 @@ #include #include "libtbd_xattrs.h" +#include "libtbd_io.h" char* tbd_apply_fread_string(FILE *stream) { uint16_t dlen; - if(fread(&dlen, sizeof(uint16_t), 1, stream) != 1) + if(tbd_read_uint16_t(&dlen, stream) != 1) return NULL; char dname[dlen + 1]; if(fread(dname, 1, dlen, stream) != dlen) @@ -63,7 +64,7 @@ int tbd_apply_fread_block(FILE *stream, void **data, size_t *size) { { size_t _size; - if (fread(&_size, sizeof(_size), 1, stream) != 1) { + if (fread(&_size, 1, sizeof(_size), stream) != sizeof(_size) ) { return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); } if (_size > *size) { @@ -91,7 +92,7 @@ tbd_apply_identify(FILE *stream) if(cmd != TBD_CMD_IDENTIFY) return TBD_ERROR(TBD_ERROR_INVALID_PARAMETER); uint16_t nlen; - if(fread(&nlen, 2, 1, stream) != 1) + if(tbd_read_uint16_t(&nlen, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); if(strlen(TB_DIFF_PROTOCOL_ID) != nlen) return TBD_ERROR(TBD_ERROR_INVALID_PARAMETER); @@ -107,7 +108,7 @@ static int tbd_apply_cmd_dir_create(FILE *stream) { uint16_t dlen; - if(fread(&dlen, sizeof(uint16_t), 1, stream) != 1) + if(tbd_read_uint16_t(&dlen, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char dname[dlen + 1]; if(fread(dname, 1, dlen, stream) != dlen) @@ -118,19 +119,19 @@ tbd_apply_cmd_dir_create(FILE *stream) return TBD_ERROR(TBD_ERROR_INVALID_PARAMETER); time_t mtime; - if(fread(&mtime, sizeof(mtime), 1, stream) != 1) + if(tbd_read_time_t(&mtime, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); - uint32_t uid; - if(fread(&uid, sizeof(uint32_t), 1, stream) != 1) + uid_t uid; + if(tbd_read_uid_t(&uid, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); - uint32_t gid; - if(fread(&gid, sizeof(uint32_t), 1, stream) != 1) + gid_t gid; + if(tbd_read_gid_t(&gid, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); - uint32_t mode; - if(fread(&mode, sizeof(uint32_t), 1, stream) != 1) + mode_t mode; + if(tbd_read_mode_t(&mode, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); if(mkdir(dname, (mode_t)mode) != 0) @@ -151,7 +152,7 @@ tbd_apply_cmd_dir_enter(FILE *stream, uintptr_t *depth) { uint16_t dlen; - if(fread(&dlen, 2, 1, stream) != 1) + if(tbd_read_uint16_t(&dlen, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char dname[dlen + 1]; if(fread(dname, 1, dlen, stream) != dlen) @@ -175,7 +176,7 @@ tbd_apply_cmd_dir_leave(FILE *stream, int err = TBD_ERROR_SUCCESS; struct utimbuf time; - if (fread(&(time.modtime), sizeof(time.modtime), 1, stream) != 1) { + if (tbd_read_time_t(&(time.modtime), stream) != 1) { return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); } time.actime = time.modtime;/* not sure what the best atime to use is */ @@ -206,7 +207,7 @@ static int tbd_apply_cmd_file_create(FILE *stream) { uint16_t flen; - if(fread(&flen, 2, 1, stream) != 1) + if(tbd_read_uint16_t(&flen, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char fname[flen + 1]; if(fread(fname, 1, flen, stream) != flen) @@ -217,15 +218,15 @@ tbd_apply_cmd_file_create(FILE *stream) time_t mtime; uint32_t mode; - uint32_t uid; - uint32_t gid; + uid_t uid; + gid_t gid; uint32_t fsize; - if(fread(&mtime, sizeof(mtime), 1, stream) != 1 || - fread(&mode, sizeof(uint32_t), 1, stream) != 1 || - fread(&uid, sizeof(uint32_t), 1, stream) != 1 || - fread(&gid, sizeof(uint32_t), 1, stream) != 1 || - fread(&fsize, 4, 1, stream) != 1) + if(tbd_read_time_t(&mtime, stream) != 1 || + tbd_read_uint32_t(&mode, stream) != 1 || + tbd_read_uid_t(&uid, stream) != 1 || + tbd_read_gid_t(&gid, stream) != 1 || + tbd_read_uint32_t(&fsize, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); fprintf(stderr, "cmd_file_create %s:%"PRId32"\n", fname, fsize); @@ -269,11 +270,11 @@ tbd_apply_cmd_file_delta(FILE *stream) { uint16_t mdata_mask; time_t mtime; - uint32_t uid; - uint32_t gid; - uint32_t mode; + uid_t uid; + gid_t gid; + mode_t mode; uint16_t flen; - if(fread(&flen, 2, 1, stream) != 1) + if(tbd_read_uint16_t(&flen, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char fname[flen + 1]; if(fread(fname, 1, flen, stream) != flen) @@ -287,11 +288,11 @@ tbd_apply_cmd_file_delta(FILE *stream) return TBD_ERROR(TBD_ERROR_INVALID_PARAMETER); /* Reading metadata */ - if(fread(&mdata_mask, sizeof(uint16_t), 1, stream) != 1 || - fread(&mtime, sizeof(mtime), 1, stream) != 1 || - fread(&uid, sizeof(uint32_t), 1, stream) != 1 || - fread(&gid, sizeof(uint32_t), 1, stream) != 1 || - fread(&mode, sizeof(uint32_t), 1, stream) != 1) + if(tbd_read_uint16_t(&mdata_mask, stream) != 1 || + tbd_read_time_t(&mtime, stream) != 1 || + tbd_read_uid_t(&uid, stream) != 1 || + tbd_read_gid_t(&gid, stream) != 1 || + tbd_read_uint32_t(&mode, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); FILE *op = fopen(fname, "rb"); @@ -308,9 +309,9 @@ tbd_apply_cmd_file_delta(FILE *stream) } uint32_t dstart, dend; - if(fread(&dstart, 4, 1, stream) != 1) + if(tbd_read_uint32_t(&dstart, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); - if(fread(&dend, 4, 1, stream) != 1) + if(tbd_read_uint32_t(&dend, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); uintptr_t block; @@ -325,7 +326,7 @@ tbd_apply_cmd_file_delta(FILE *stream) } uint32_t fsize; - if(fread(&fsize, 4, 1, stream) != 1) + if(tbd_read_uint32_t(&fsize, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); for(block = 256; fsize != 0; fsize -= block) { @@ -440,7 +441,7 @@ static int tbd_apply_cmd_entity_delete(FILE *stream) { uint16_t elen; - if(fread(&elen, 2, 1, stream) != 1) + if(tbd_read_uint16_t(&elen, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char ename[elen + 1]; if(fread(ename, 1, elen, stream) != elen) @@ -459,16 +460,16 @@ tbd_apply_cmd_symlink_create(FILE *stream) { uint16_t len; time_t mtime; - uint32_t uid; - uint32_t gid; + uid_t uid; + gid_t gid; - if(fread(&mtime, sizeof(mtime), 1, stream) != 1 || - fread(&uid, sizeof(uint32_t), 1, stream) != 1 || - fread(&gid, sizeof(uint32_t), 1, stream) != 1) + if(tbd_read_time_t(&mtime, stream) != 1 || + tbd_read_uid_t(&uid, stream) != 1 || + tbd_read_gid_t(&gid, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); /* Reading link file name */ - if(fread(&len, sizeof(uint16_t), 1, stream) != 1) + if(tbd_read_uint16_t(&len, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char linkname[len + 1]; @@ -477,7 +478,7 @@ tbd_apply_cmd_symlink_create(FILE *stream) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); /* Reading target path */ - if(fread(&len, sizeof(uint16_t), 1, stream) != 1) + if(tbd_read_uint16_t(&len, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char linkpath[len+1]; linkpath[len] = '\0'; @@ -506,17 +507,17 @@ tbd_apply_cmd_special_create(FILE *stream) { char *name = tbd_apply_fread_string(stream); time_t mtime; - uint32_t mode; - uint32_t uid; - uint32_t gid; + mode_t mode; + uid_t uid; + gid_t gid; uint32_t dev; if(name == NULL || - fread(&mtime, sizeof(mtime), 1, stream) != 1 || - fread(&mode, sizeof(uint32_t), 1, stream) != 1 || - fread(&uid, sizeof(uint32_t), 1, stream) != 1 || - fread(&gid, sizeof(uint32_t), 1, stream) != 1 || - fread(&dev, sizeof(uint32_t), 1, stream) != 1) { + tbd_read_time_t(&mtime, stream) != 1 || + tbd_read_mode_t(&mode, stream) != 1 || + tbd_read_uid_t(&uid, stream) != 1 || + tbd_read_gid_t(&gid, stream) != 1 || + tbd_read_uint32_t(&dev, stream) != 1) { free(name); return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); } @@ -543,15 +544,15 @@ tbd_apply_cmd_dir_delta(FILE *stream) { uint16_t metadata_mask; time_t mtime; - uint32_t uid; - uint32_t gid; - uint32_t mode; + uid_t uid; + gid_t gid; + mode_t mode; - if(fread(&metadata_mask, sizeof(uint16_t), 1, stream) != 1 || - fread(&mtime, sizeof(mtime), 1, stream) != 1 || - fread(&uid, sizeof(uint32_t), 1, stream) != 1 || - fread(&gid, sizeof(uint32_t), 1, stream) != 1 || - fread(&mode, sizeof(uint32_t), 1, stream) != 1) + if(tbd_read_uint16_t(&metadata_mask, stream) != 1 || + tbd_read_time_t(&mtime, stream) != 1 || + tbd_read_uid_t(&uid, stream) != 1 || + tbd_read_gid_t(&gid, stream) != 1 || + tbd_read_uint32_t(&mode, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char *dname = tbd_apply_fread_string(stream); @@ -578,15 +579,15 @@ tbd_apply_cmd_file_mdata_update(FILE *stream) { uint16_t metadata_mask; time_t mtime; - uint32_t uid; - uint32_t gid; - uint32_t mode; + uid_t uid; + gid_t gid; + mode_t mode; - if(fread(&metadata_mask, sizeof(uint16_t), 1, stream) != 1 || - fread(&mtime, sizeof(mtime), 1, stream) != 1 || - fread(&uid, sizeof(uint32_t), 1, stream) != 1 || - fread(&gid, sizeof(uint32_t), 1, stream) != 1 || - fread(&mode, sizeof(uint32_t), 1, stream) != 1) + if(tbd_read_uint16_t(&metadata_mask, stream) != 1 || + tbd_read_time_t(&mtime, stream) != 1 || + tbd_read_uid_t(&uid, stream) != 1 || + tbd_read_gid_t(&gid, stream) != 1 || + tbd_read_uint32_t(&mode, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char *dname = tbd_apply_fread_string(stream); @@ -612,7 +613,7 @@ static int tbd_apply_cmd_xattrs_update(FILE *stream) { int err = TBD_ERROR_SUCCESS; char *fname; - int count; + uint32_t count; void *data = NULL; size_t dsize = 0; /* read the name of the file to operate on */ @@ -626,7 +627,7 @@ static int tbd_apply_cmd_xattrs_update(FILE *stream) } /* read how many attributes to process */ - if (fread(&count, sizeof(count), 1, stream) != 1) { + if (tbd_read_uint32_t(&count, stream) != 1) { err = TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); goto cleanup; } diff --git a/libtbd_create.c b/libtbd_create.c index 5f0bef0..f6dc16d 100644 --- a/libtbd_create.c +++ b/libtbd_create.c @@ -19,6 +19,7 @@ #include "tbdiff-private.h" #include "libtbd_xattrs.h" +#include "libtbd_io.h" #include #include @@ -46,7 +47,7 @@ tbd_create_fwrite_string(FILE *stream, const char *string) { uint16_t slen = strlen(string); - if((fwrite(&slen, 2, 1, stream) != 1) + if((tbd_write_uint16_t(slen, stream) != 1) || (fwrite(string, 1, slen, stream) != slen)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; @@ -55,11 +56,11 @@ tbd_create_fwrite_string(FILE *stream, static int tbd_create_fwrite_block(FILE *stream, void const *data, size_t size) { - if (fwrite(&size, sizeof(size), 1, stream) != 1) { + if (fwrite(&size, 1, sizeof(size), stream) != sizeof(size)) { return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); } - if (fwrite(data, size, 1, stream) != 1) { + if (fwrite(data, 1, size, stream) != size) { return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); } return TBD_ERROR_SUCCESS; @@ -69,7 +70,7 @@ static int tbd_create_fwrite_mdata_mask(FILE *stream, uint16_t mask) { - if(fwrite(&mask, sizeof(uint16_t), 1, stream) != 1) + if(tbd_write_uint16_t(mask, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } @@ -78,16 +79,16 @@ static int tbd_create_fwrite_mtime(FILE *stream, time_t mtime) { - if(fwrite(&mtime, sizeof(mtime), 1, stream) != 1) + if(tbd_write_time_t(mtime, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } static int tbd_create_fwrite_mode(FILE *stream, - uint32_t mode) + mode_t mode) { - if(fwrite(&mode, sizeof(uint32_t), 1, stream) != 1) + if(tbd_write_mode_t(mode, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } @@ -96,7 +97,7 @@ static int tbd_create_fwrite_gid(FILE *stream, gid_t gid) { - if(fwrite(&gid, sizeof(gid_t), 1, stream) != 1) + if(tbd_write_gid_t(gid, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } @@ -105,7 +106,7 @@ static int tbd_create_fwrite_uid(FILE *stream, uid_t uid) { - if(fwrite(&uid, sizeof(uid_t), 1, stream) != 1) + if(tbd_write_uid_t(uid, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } @@ -114,7 +115,7 @@ static int tbd_create_fwrite_dev(FILE *stream, uint32_t dev) { - if(fwrite(&dev, sizeof(uint32_t), 1, stream) != 1) + if(tbd_write_uint32_t(dev, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } @@ -179,7 +180,7 @@ tbd_create_cmd_fwrite_xattrs(FILE *stream, tbd_stat_t *f) } { /* write the header */ - int count; + uint32_t count; /* if failed to count or there are no xattrs */ if ((err = tbd_xattrs_names_count(&names, &count)) != TBD_ERROR_SUCCESS || count == 0) { @@ -197,7 +198,7 @@ tbd_create_cmd_fwrite_xattrs(FILE *stream, tbd_stat_t *f) goto cleanup_names; } - if (fwrite(&count, sizeof(count), 1, stream) != 1) { + if (tbd_write_uint32_t(count, stream) != 1) { err = TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); goto cleanup_names; } @@ -227,7 +228,7 @@ tbd_create_cmd_file_create(FILE *stream, return err; uint32_t size = f->size; - if(fwrite(&size, sizeof(uint32_t), 1, stream) != 1) + if(tbd_write_uint32_t(size, stream) != 1) return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); FILE *fp = tbd_stat_fopen(f, "rb"); @@ -408,9 +409,9 @@ tbd_create_cmd_file_delta(FILE *stream, fclose(fpb); return err; } - if((fwrite(&start, sizeof(uint32_t), 1, stream) != 1) || - (fwrite(&end, sizeof(uint32_t), 1, stream) != 1) || - (fwrite(&size, sizeof(uint32_t), 1, stream) != 1)) { + if((tbd_write_uint32_t(start, stream) != 1) || + (tbd_write_uint32_t(end, stream) != 1) || + (tbd_write_uint32_t(size, stream) != 1)) { fclose(fpb); return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); } diff --git a/libtbd_io.c b/libtbd_io.c new file mode 100644 index 0000000..c4d6d11 --- /dev/null +++ b/libtbd_io.c @@ -0,0 +1,149 @@ +#include +#include +#include + +#include "libtbd_stat.h" + +#if __BYTE_ORDER == __BIG_ENDIAN +//inverts the indices of an array of bytes. +static void byteswap (char* value, int size) { + char tmp; + int i; + for (i = 0; i < size/2; i++) { + tmp = value[i]; + value[i] = value[size-i-1]; + value[size-i-1] = tmp; + } +} +#endif + +size_t tbd_write_uint16_t (uint16_t value, FILE* stream) { +#if __BYTE_ORDER == __BIG_ENDIAN + byteswap((char*)&value, sizeof(value)); +#endif + return fwrite(&value, sizeof(value), 1, stream); +} + +size_t tbd_write_uint32_t (uint32_t value, FILE* stream) { +#if __BYTE_ORDER == __BIG_ENDIAN + byteswap((char*)&value, sizeof(value)); +#endif + return fwrite(&value, sizeof(value), 1, stream); +} + +size_t tbd_write_uint64_t (uint64_t value, FILE* stream) { +#if __BYTE_ORDER == __BIG_ENDIAN + byteswap((char*)&value, sizeof(value)); +#endif + return fwrite(&value, sizeof(value), 1, stream); +} + +size_t tbd_write_time_t (time_t value, FILE* stream) { + uint64_t realv = value; +#if __BYTE_ORDER == __BIG_ENDIAN + byteswap((char*)&realv, sizeof(realv)); +#endif + return fwrite(&realv, sizeof(realv), 1, stream); +} + +size_t tbd_write_mode_t (mode_t value, FILE* stream) { +#if __BYTE_ORDER == __BIG_ENDIAN + byteswap((char*)&value, sizeof(value)); +#endif + return fwrite(&value, sizeof(value), 1, stream); +} + +size_t tbd_write_uid_t (uid_t value, FILE* stream) { +#if __BYTE_ORDER == __BIG_ENDIAN + byteswap((char*)&value, sizeof(value)); +#endif + return fwrite(&value, sizeof(value), 1, stream); +} + +size_t tbd_write_gid_t (gid_t value, FILE* stream) { +#if __BYTE_ORDER == __BIG_ENDIAN + byteswap((char*)&value, sizeof(value)); +#endif + return fwrite(&value, sizeof(value), 1, stream); +} + +size_t tbd_write_size_t (size_t value, FILE* stream) { +#if __BYTE_ORDER == __BIG_ENDIAN + byteswap((char*)&value, sizeof(value)); +#endif + return fwrite(&value, sizeof(value), 1, stream); +} + +size_t tbd_read_uint16_t (uint16_t *value, FILE* stream) { + assert(value != NULL); + size_t rval = fread(value, sizeof(*value), 1, stream); +#if __BYTE_ORDER == __BIG_ENDIAN + byteswap((char*)value, sizeof(*value)); +#endif + return rval; +} + +size_t tbd_read_uint32_t (uint32_t *value, FILE* stream) { + assert(value != NULL); + size_t rval = fread(value, sizeof(*value), 1, stream); +#if __BYTE_ORDER == __BIG_ENDIAN + byteswap((char*)value, sizeof(*value)); +#endif + return rval; +} + +size_t tbd_read_uint64_t (uint64_t *value, FILE* stream) { + assert(value != NULL); + size_t rval = fread(value, sizeof(*value), 1, stream); +#if __BYTE_ORDER == __BIG_ENDIAN + byteswap((char*)value, sizeof(*value)); +#endif + return rval; +} + +size_t tbd_read_time_t (time_t *value, FILE* stream) { + assert(value != NULL); + uint64_t realv; + size_t rval = fread(&realv, sizeof(realv), 1, stream); +#if __BYTE_ORDER == __BIG_ENDIAN + byteswap((char*)&realv, sizeof(realv)); +#endif + *value = realv; + return rval; + } + +size_t tbd_read_mode_t (mode_t *value, FILE* stream) { + assert(value != NULL); + size_t rval = fread(value, sizeof(*value), 1, stream); +#if __BYTE_ORDER == __BIG_ENDIAN + byteswap((char*)value, sizeof(*value)); +#endif + return rval; + } + +size_t tbd_read_uid_t (uid_t *value, FILE* stream) { + assert(value != NULL); + size_t rval = fread(value, sizeof(*value), 1, stream); +#if __BYTE_ORDER == __BIG_ENDIAN + byteswap((char*)value, sizeof(*value)); +#endif + return rval; + } + +size_t tbd_read_gid_t (gid_t *value, FILE* stream) { + assert(value != NULL); + size_t rval = fread(value, sizeof(*value), 1, stream); +#if __BYTE_ORDER == __BIG_ENDIAN + byteswap((char*)value, sizeof(*value)); +#endif + return rval; + } + +size_t tbd_read_size_t (size_t *value, FILE* stream) { + assert(value != NULL); + size_t rval = fread(value, sizeof(*value), 1, stream); +#if __BYTE_ORDER == __BIG_ENDIAN + byteswap((char*)value, sizeof(*value)); +#endif + return rval; + } diff --git a/libtbd_io.h b/libtbd_io.h new file mode 100644 index 0000000..242dc35 --- /dev/null +++ b/libtbd_io.h @@ -0,0 +1,42 @@ +#ifndef __LIBTBD_IO_H__ +#define __LIBTBD_IO_H__ + + +#include +#include +#include +#include "libtbd_stat.h" + +size_t tbd_write_uint16_t (uint16_t value, FILE* stream); + +size_t tbd_write_uint32_t (uint32_t value, FILE* stream); + +size_t tbd_write_uint64_t (uint64_t value, FILE* stream); + +size_t tbd_write_time_t (time_t value, FILE* stream); + +size_t tbd_write_mode_t (mode_t value, FILE* stream); + +size_t tbd_write_uid_t (uid_t value, FILE* stream); + +size_t tbd_write_gid_t (gid_t value, FILE* stream); + +size_t tbd_write_size_t (size_t value, FILE* stream); + +size_t tbd_read_uint16_t (uint16_t *value, FILE* stream); + +size_t tbd_read_uint32_t (uint32_t *value, FILE* stream); + +size_t tbd_read_uint64_t (uint64_t *value, FILE* stream); + +size_t tbd_read_time_t (time_t *value, FILE* stream); + +size_t tbd_read_mode_t (mode_t *value, FILE* stream); + +size_t tbd_read_uid_t (uid_t *value, FILE* stream); + +size_t tbd_read_gid_t (gid_t *value, FILE* stream); + +size_t tbd_read_size_t (size_t *value, FILE* stream); + +#endif diff --git a/libtbd_stat.c b/libtbd_stat.c index a8e0a33..08fb639 100644 --- a/libtbd_stat.c +++ b/libtbd_stat.c @@ -88,9 +88,9 @@ tbd_stat_from_path(const char *name, } ret->rdev = (uint32_t)info.st_rdev; - ret->uid = (uint32_t)info.st_uid; - ret->gid = (uint32_t)info.st_gid; - ret->mode = (uint32_t)info.st_mode; + ret->uid = (uid_t)info.st_uid; + ret->gid = (gid_t)info.st_gid; + ret->mode = (mode_t)info.st_mode; ret->mtime = (time_t)info.st_mtime; return ret; } diff --git a/libtbd_stat.h b/libtbd_stat.h index f71337f..c43c023 100644 --- a/libtbd_stat.h +++ b/libtbd_stat.h @@ -39,9 +39,9 @@ typedef struct { tbd_stat_type_e type; time_t mtime; uint32_t size; // Count for directory. - uint32_t uid; - uint32_t gid; - uint32_t mode; + uid_t uid; + gid_t gid; + mode_t mode; uint32_t rdev; } tbd_stat_t; diff --git a/libtbd_xattrs.c b/libtbd_xattrs.c index 8f0886b..51c6168 100644 --- a/libtbd_xattrs.c +++ b/libtbd_xattrs.c @@ -99,11 +99,11 @@ int tbd_xattrs_names_each(tbd_xattrs_names_t const *names, static int names_sum(char const *name, void *ud) { if (name == NULL || ud == NULL) return TBD_ERROR(TBD_ERROR_NULL_POINTER); - (*((uint16_t*)ud))++; + (*((uint32_t*)ud))++; return TBD_ERROR_SUCCESS; } -int tbd_xattrs_names_count(tbd_xattrs_names_t const *names, int *count) { - int _count = 0; +int tbd_xattrs_names_count(tbd_xattrs_names_t const *names, uint32_t *count) { + uint32_t _count = 0; int err; if ((err = tbd_xattrs_names_each(names, &names_sum, &_count)) == TBD_ERROR_SUCCESS) { diff --git a/libtbd_xattrs.h b/libtbd_xattrs.h index 7a1399c..5f4cd5e 100644 --- a/libtbd_xattrs.h +++ b/libtbd_xattrs.h @@ -19,6 +19,7 @@ #define _LIBTBD_XATTRS_H #include +#include /* structure for names data */ typedef struct tbd_xattrs_names { @@ -38,7 +39,7 @@ extern int tbd_xattrs_names_each(tbd_xattrs_names_t const *names, void *ud); /* gets how many different attributes there are in the list */ -extern int tbd_xattrs_names_count(tbd_xattrs_names_t const *names, int *count); +extern int tbd_xattrs_names_count(tbd_xattrs_names_t const *names, uint32_t *count); /* puts the value of the attribute called name into *buf with size *bufsize * if *buf is NULL or *bufsize is 0 then memory will be allocated for it -- cgit v1.2.1