From 5b1b9e9c6badfb57352522cb6d9d12ca109b5c1e Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Wed, 5 Oct 2011 11:21:49 +0100 Subject: tbd_error replaced with TBD_ERROR so that it is explicitly a macro TBD_ERROR should work when NDEBUG is defined. Previously return tbd_error(e); -> return return e; tbd_error is now an inline function, it feels tidier than statement expressions --- Makefile | 1 + libtbd_apply.c | 134 ++++++++++++++++++++++++++++---------------------------- libtbd_create.c | 60 ++++++++++++------------- tbdiff.h | 19 ++++---- 4 files changed, 109 insertions(+), 105 deletions(-) diff --git a/Makefile b/Makefile index 5386d93..1781b5f 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ DEPLOY=tbdiff-deploy CREATE=tbdiff-create CFLAGS ?= +CFLAGS += -std=gnu99 CFLAGS += -g CFLAGS += -Wall -Wextra -Werror -Wno-unused-result $(OPT) diff --git a/libtbd_apply.c b/libtbd_apply.c index f4d7d1b..a749952 100644 --- a/libtbd_apply.c +++ b/libtbd_apply.c @@ -51,19 +51,19 @@ tbd_apply_identify(FILE *stream) { uint8_t cmd; if(fread(&cmd, 1, 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); if(cmd != TBD_CMD_IDENTIFY) - return tbd_error(TBD_ERROR_INVALID_PARAMETER); + return TBD_ERROR(TBD_ERROR_INVALID_PARAMETER); uint16_t nlen; if(fread(&nlen, 2, 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); if(strlen(TB_DIFF_PROTOCOL_ID) != nlen) - return tbd_error(TBD_ERROR_INVALID_PARAMETER); + return TBD_ERROR(TBD_ERROR_INVALID_PARAMETER); char nstr[nlen]; if(fread(nstr, 1, nlen, stream) != nlen) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); if(strncmp(nstr, TB_DIFF_PROTOCOL_ID, nlen) != 0) - return tbd_error(TBD_ERROR_INVALID_PARAMETER); + return TBD_ERROR(TBD_ERROR_INVALID_PARAMETER); return 0; } @@ -72,33 +72,33 @@ tbd_apply_cmd_dir_create(FILE *stream) { uint16_t dlen; if(fread(&dlen, sizeof(uint16_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char dname[dlen + 1]; if(fread(dname, 1, dlen, stream) != dlen) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); dname[dlen] = '\0'; fprintf(stderr, "cmd_dir_create %s\n", dname); if(strchr(dname, '/') != NULL) - return tbd_error(TBD_ERROR_INVALID_PARAMETER); + return TBD_ERROR(TBD_ERROR_INVALID_PARAMETER); uint32_t mtime; if(fread(&mtime, sizeof(uint32_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); uint32_t uid; if(fread(&uid, sizeof(uint32_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); uint32_t gid; if(fread(&gid, sizeof(uint32_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); uint32_t mode; if(fread(&mode, sizeof(uint32_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); if(mkdir(dname, (mode_t)mode) != 0) - return tbd_error(TBD_ERROR_UNABLE_TO_CREATE_DIR); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_CREATE_DIR); // Apply metadata. struct utimbuf timebuff = { time(NULL), mtime }; @@ -116,19 +116,19 @@ tbd_apply_cmd_dir_enter(FILE *stream, { uint16_t dlen; if(fread(&dlen, 2, 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char dname[dlen + 1]; if(fread(dname, 1, dlen, stream) != dlen) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); dname[dlen] = '\0'; fprintf(stderr, "cmd_dir_enter %s\n", dname); if((strchr(dname, '/') != NULL) || (strcmp(dname, "..") == 0)) - return tbd_error(TBD_ERROR_UNABLE_TO_CHANGE_DIR); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_CHANGE_DIR); if(depth != NULL) (*depth)++; if(chdir(dname) != 0) - return tbd_error(TBD_ERROR_UNABLE_TO_CHANGE_DIR); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_CHANGE_DIR); return 0; } @@ -138,17 +138,17 @@ tbd_apply_cmd_dir_leave(FILE *stream, { uint8_t count; if(fread(&count, 1, 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); uintptr_t rcount = count + 1; fprintf(stderr, "cmd_dir_leave %"PRIuPTR"\n", rcount); if((depth != NULL) && (*depth < rcount)) - return tbd_error(TBD_ERROR_INVALID_PARAMETER); + return TBD_ERROR(TBD_ERROR_INVALID_PARAMETER); uintptr_t i; for(i = 0; i < rcount; i++) { if(chdir("..") != 0) - return tbd_error(TBD_ERROR_UNABLE_TO_CHANGE_DIR); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_CHANGE_DIR); } if(depth != NULL) @@ -161,13 +161,13 @@ tbd_apply_cmd_file_create(FILE *stream) { uint16_t flen; if(fread(&flen, 2, 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char fname[flen + 1]; if(fread(fname, 1, flen, stream) != flen) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); fname[flen] = '\0'; if((strchr(fname, '/') != NULL) || (strcmp(fname, "..") == 0)) - return tbd_error(TBD_ERROR_INVALID_PARAMETER); + return TBD_ERROR(TBD_ERROR_INVALID_PARAMETER); uint32_t mtime; uint32_t mode; @@ -180,19 +180,19 @@ tbd_apply_cmd_file_create(FILE *stream) fread(&uid, sizeof(uint32_t), 1, stream) != 1 || fread(&gid, sizeof(uint32_t), 1, stream) != 1 || fread(&fsize, 4, 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); fprintf(stderr, "cmd_file_create %s:%"PRId32"\n", fname, fsize); FILE *fp = fopen(fname, "rb"); if(fp != NULL) { fclose(fp); - return tbd_error(TBD_ERROR_FILE_ALREADY_EXISTS); + return TBD_ERROR(TBD_ERROR_FILE_ALREADY_EXISTS); } fp = fopen(fname, "wb"); if(fp == NULL) - return tbd_error(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_WRITING); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_WRITING); uintptr_t block = 256; uint8_t fbuff[block]; @@ -200,9 +200,9 @@ tbd_apply_cmd_file_create(FILE *stream) if(fsize < block) block = fsize; if(fread(fbuff, 1, block, stream) != block) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); if(fwrite(fbuff, 1, block, fp) != block) - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); } fclose(fp); @@ -228,17 +228,17 @@ tbd_apply_cmd_file_delta(FILE *stream) uint32_t mode; uint16_t flen; if(fread(&flen, 2, 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char fname[flen + 1]; if(fread(fname, 1, flen, stream) != flen) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); fname[flen] = '\0'; fprintf(stderr, "cmd_file_delta %s\n", fname); if((strchr(fname, '/') != NULL) || (strcmp(fname, "..") == 0)) - return tbd_error(TBD_ERROR_INVALID_PARAMETER); + return TBD_ERROR(TBD_ERROR_INVALID_PARAMETER); /* Reading metadata */ if(fread(&mdata_mask, sizeof(uint16_t), 1, stream) != 1 || @@ -246,26 +246,26 @@ tbd_apply_cmd_file_delta(FILE *stream) fread(&uid, sizeof(uint32_t), 1, stream) != 1 || fread(&gid, sizeof(uint32_t), 1, stream) != 1 || fread(&mode, sizeof(uint32_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); FILE *op = fopen(fname, "rb"); if(op == NULL) - return tbd_error(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_READING); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_READING); if(remove(fname) != 0) { fclose(op); - return tbd_error(TBD_ERROR_UNABLE_TO_REMOVE_FILE); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_REMOVE_FILE); } FILE *np = fopen(fname, "wb"); if(np == NULL) { fclose(op); - return tbd_error(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_WRITING); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_WRITING); } uint32_t dstart, dend; if(fread(&dstart, 4, 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); if(fread(&dend, 4, 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); uintptr_t block; uint8_t fbuff[256]; @@ -273,34 +273,34 @@ tbd_apply_cmd_file_delta(FILE *stream) if(dstart < block) block = dstart; if(fread(fbuff, 1, block, op) != block) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); if(fwrite(fbuff, 1, block, np) != block) - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); } uint32_t fsize; if(fread(&fsize, 4, 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); for(block = 256; fsize != 0; fsize -= block) { if(fsize < block) block = fsize; if(fread(fbuff, 1, block, stream) != block) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); if(fwrite(fbuff, 1, block, np) != block) - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); } if(fseek(op, dend, SEEK_SET) != 0) { fclose(np); fclose(op); - return tbd_error(TBD_ERROR_UNABLE_TO_SEEK_THROUGH_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_SEEK_THROUGH_STREAM); } for(block = 256; block != 0;) { block = fread(fbuff, 1, block, op); if(fwrite(fbuff, 1, block, np) != block) - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); } fclose(np); @@ -326,14 +326,14 @@ tbd_apply_cmd_entity_delete_for_name(const char *name) DIR *dp = opendir(name); if(dp == NULL) { if(remove(name) != 0) - return tbd_error(TBD_ERROR_UNABLE_TO_REMOVE_FILE); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_REMOVE_FILE); return 0; } if(chdir(name) != 0) { closedir(dp); - return tbd_error(TBD_ERROR_UNABLE_TO_CHANGE_DIR); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_CHANGE_DIR); } struct dirent *entry; @@ -347,7 +347,7 @@ tbd_apply_cmd_entity_delete_for_name(const char *name) closedir(dp); if(chdir("..") != 0) - return tbd_error(TBD_ERROR_UNABLE_TO_CHANGE_DIR); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_CHANGE_DIR); return err; } @@ -355,9 +355,9 @@ tbd_apply_cmd_entity_delete_for_name(const char *name) closedir(dp); if(chdir("..") != 0) - return tbd_error(TBD_ERROR_UNABLE_TO_CHANGE_DIR); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_CHANGE_DIR); if(remove(name) != 0) - return tbd_error(TBD_ERROR_UNABLE_TO_REMOVE_FILE); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_REMOVE_FILE); return 0; } @@ -366,16 +366,16 @@ tbd_apply_cmd_entity_delete(FILE *stream) { uint16_t elen; if(fread(&elen, 2, 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char ename[elen + 1]; if(fread(ename, 1, elen, stream) != elen) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); ename[elen] = '\0'; fprintf(stderr, "cmd_entity_delete %s\n", ename); if((strchr(ename, '/') != NULL) || (strcmp(ename, "..") == 0)) - return tbd_error(TBD_ERROR_INVALID_PARAMETER); + return TBD_ERROR(TBD_ERROR_INVALID_PARAMETER); return tbd_apply_cmd_entity_delete_for_name(ename); } @@ -390,25 +390,25 @@ tbd_apply_cmd_symlink_create(FILE *stream) if(fread(&mtime, sizeof(uint32_t), 1, stream) != 1 || fread(&uid, sizeof(uint32_t), 1, stream) != 1 || fread(&gid, sizeof(uint32_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); /* Reading link file name */ if(fread(&len, sizeof(uint16_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char linkname[len + 1]; linkname[len] = '\0'; if(fread(linkname, sizeof(char), len, stream) != len) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); /* Reading target path */ if(fread(&len, sizeof(uint16_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char linkpath[len+1]; linkpath[len] = '\0'; if(fread(linkpath, sizeof(char), len, stream) != len) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); fprintf(stderr, "cmd_symlink_create %s -> %s\n", linkname, linkpath); @@ -443,7 +443,7 @@ tbd_apply_cmd_special_create(FILE *stream) fread(&gid, sizeof(uint32_t), 1, stream) != 1 || fread(&dev, sizeof(uint32_t), 1, stream) != 1) { free(name); - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); } fprintf(stderr, "cmd_special_create %s\n", name); @@ -477,11 +477,11 @@ tbd_apply_cmd_dir_delta(FILE *stream) fread(&uid, sizeof(uint32_t), 1, stream) != 1 || fread(&gid, sizeof(uint32_t), 1, stream) != 1 || fread(&mode, sizeof(uint32_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char *dname = tbd_apply_fread_string(stream); if(dname == NULL) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); fprintf(stderr, "cmd_dir_delta %s\n", dname); @@ -512,11 +512,11 @@ tbd_apply_cmd_file_mdata_update(FILE *stream) fread(&uid, sizeof(uint32_t), 1, stream) != 1 || fread(&gid, sizeof(uint32_t), 1, stream) != 1 || fread(&mode, sizeof(uint32_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char *dname = tbd_apply_fread_string(stream); if(dname == NULL) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); fprintf(stderr, "cmd_metadata_update %s\n", dname); @@ -537,7 +537,7 @@ int tbd_apply(FILE *stream) { if(stream == NULL) - return tbd_error(TBD_ERROR_NULL_POINTER); + return TBD_ERROR(TBD_ERROR_NULL_POINTER); int err; if((err = tbd_apply_identify(stream)) != 0) @@ -548,7 +548,7 @@ tbd_apply(FILE *stream) while(!flush) { uint8_t cmd; if(fread(&cmd, 1, 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); switch(cmd) { case TBD_CMD_DIR_CREATE: if((err = tbd_apply_cmd_dir_create(stream)) != 0) @@ -588,7 +588,7 @@ tbd_apply(FILE *stream) break; case TBD_CMD_ENTITY_MOVE: case TBD_CMD_ENTITY_COPY: - return tbd_error(TBD_ERROR_FEATURE_NOT_IMPLEMENTED); // TODO - Implement. + return TBD_ERROR(TBD_ERROR_FEATURE_NOT_IMPLEMENTED); // TODO - Implement. case TBD_CMD_ENTITY_DELETE: if((err = tbd_apply_cmd_entity_delete(stream)) != 0) return err; @@ -598,7 +598,7 @@ tbd_apply(FILE *stream) break; default: fprintf(stderr, "Error: Invalid command 0x%02"PRIx8".\n", cmd); - return tbd_error(TBD_ERROR_INVALID_PARAMETER); + return TBD_ERROR(TBD_ERROR_INVALID_PARAMETER); } } diff --git a/libtbd_create.c b/libtbd_create.c index a85a0bd..c430b3c 100644 --- a/libtbd_create.c +++ b/libtbd_create.c @@ -36,7 +36,7 @@ tbd_create_fwrite_cmd(FILE *stream, uint8_t cmd) { if(fwrite(&cmd, 1, 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } @@ -47,7 +47,7 @@ tbd_create_fwrite_string(FILE *stream, uint16_t slen = strlen(string); if((fwrite(&slen, 2, 1, stream) != 1) || (fwrite(string, 1, slen, stream) != slen)) - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } @@ -56,7 +56,7 @@ tbd_create_fwrite_mdata_mask(FILE *stream, uint16_t mask) { if(fwrite(&mask, sizeof(uint16_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } @@ -65,7 +65,7 @@ tbd_create_fwrite_mtime(FILE *stream, uint32_t mtime) { if(fwrite(&mtime, sizeof(uint32_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } @@ -74,7 +74,7 @@ tbd_create_fwrite_mode(FILE *stream, uint32_t mode) { if(fwrite(&mode, sizeof(uint32_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } @@ -83,7 +83,7 @@ tbd_create_fwrite_gid(FILE *stream, gid_t gid) { if(fwrite(&gid, sizeof(gid_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } @@ -92,7 +92,7 @@ tbd_create_fwrite_uid(FILE *stream, uid_t uid) { if(fwrite(&uid, sizeof(uid_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } @@ -101,7 +101,7 @@ tbd_create_fwrite_dev(FILE *stream, uint32_t dev) { if(fwrite(&dev, sizeof(uint32_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } @@ -138,11 +138,11 @@ tbd_create_cmd_file_create(FILE *stream, uint32_t size = f->size; if(fwrite(&size, sizeof(uint32_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); FILE *fp = tbd_stat_fopen(f, "rb"); if(fp == NULL) - return tbd_error(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_READING); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_READING); uint8_t buff[256]; uintptr_t b = 256; @@ -150,7 +150,7 @@ tbd_create_cmd_file_create(FILE *stream, b = fread(buff, 1, b, fp); if(fwrite(buff, 1, b, stream) != b) { fclose(fp); - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); } } fclose(fp); @@ -206,11 +206,11 @@ tbd_create_cmd_file_delta(FILE *stream, { FILE *fpa = tbd_stat_fopen(a, "rb"); if(fpa == NULL) - return tbd_error(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_READING); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_READING); FILE *fpb = tbd_stat_fopen(b, "rb"); if(fpb == NULL) { fclose(fpa); - return tbd_error(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_READING); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_READING); } // Calculate start. @@ -239,7 +239,7 @@ tbd_create_cmd_file_delta(FILE *stream, if((fseek(fpa, 0, SEEK_END) != 0) || (fseek(fpb, 0, SEEK_END) != 0)) { fclose(fpa); fclose(fpb); - return tbd_error(TBD_ERROR_UNABLE_TO_SEEK_THROUGH_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_SEEK_THROUGH_STREAM); } // Find length. @@ -249,7 +249,7 @@ tbd_create_cmd_file_delta(FILE *stream, if((flena < 0) || (flenb < 0)) { fclose(fpa); fclose(fpb); - return tbd_error(TBD_ERROR_UNABLE_TO_DETECT_STREAM_POSITION); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_DETECT_STREAM_POSITION); } // Find end. @@ -265,14 +265,14 @@ tbd_create_cmd_file_delta(FILE *stream, || (fseek(fpb, flenb - (o + blks[1]), SEEK_SET) != 0)) { fclose(fpa); fclose(fpb); - return tbd_error(TBD_ERROR_UNABLE_TO_SEEK_THROUGH_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_SEEK_THROUGH_STREAM); } if((fread(buff[0], 1, blks[0], fpa) != blks[0]) || (fread(buff[1], 1, blks[1], fpb) != blks[1])) { fclose(fpa); fclose(fpb); - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); } uintptr_t i, ja, jb; @@ -321,22 +321,22 @@ tbd_create_cmd_file_delta(FILE *stream, (fwrite(&end, sizeof(uint32_t), 1, stream) != 1) || (fwrite(&size, sizeof(uint32_t), 1, stream) != 1)) { fclose(fpb); - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); } if(fseek(fpb, start, SEEK_SET) != 0) { fclose(fpb); - return tbd_error(TBD_ERROR_UNABLE_TO_SEEK_THROUGH_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_SEEK_THROUGH_STREAM); } for(o = 0; o < size; o += 256) { uintptr_t csize = ((size - o) > 256 ? 256 : (size - o)); if(fread(buff[0], 1, csize, fpb) != csize) { fclose(fpb); - return tbd_error(TBD_ERROR_UNABLE_TO_READ_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); } if(fwrite(buff[0], 1, csize, stream) != csize) { fclose(fpb); - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); } } @@ -385,13 +385,13 @@ tbd_create_cmd_dir_leave(FILE *stream, token = 255; for(; count > 256; count -= 256) { if(fwrite(&token, sizeof (uint8_t), 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); } } token = (count - 1); if(fwrite(&token, 1, 1, stream) != 1) - return tbd_error(TBD_ERROR_UNABLE_TO_WRITE_STREAM); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } @@ -548,7 +548,7 @@ tbd_create_dir(FILE *stream, tbd_stat_t *f = tbd_stat_entry(d, i); if(f == NULL) - return tbd_error(TBD_ERROR_UNABLE_TO_STAT_FILE); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_STAT_FILE); switch(f->type) { case TBD_STAT_TYPE_FILE: @@ -568,7 +568,7 @@ tbd_create_dir(FILE *stream, break; default: tbd_stat_free(f); - return tbd_error(TBD_ERROR_FEATURE_NOT_IMPLEMENTED); + return TBD_ERROR(TBD_ERROR_FEATURE_NOT_IMPLEMENTED); break; } tbd_stat_free(f); @@ -586,7 +586,7 @@ tbd_create_impl(FILE *stream, bool top) { if((a == NULL) && (b == NULL)) - return tbd_error(TBD_ERROR_NULL_POINTER); + return TBD_ERROR(TBD_ERROR_NULL_POINTER); int err; if(((b == NULL) || ((a != NULL) && (a->type != b->type)))) { @@ -613,7 +613,7 @@ tbd_create_impl(FILE *stream, fprintf(stderr, "special new %s\n", b->name); return tbd_create_cmd_special_create(stream, b); default: - return tbd_error(TBD_ERROR_FEATURE_NOT_IMPLEMENTED); + return TBD_ERROR(TBD_ERROR_FEATURE_NOT_IMPLEMENTED); break; } } @@ -649,7 +649,7 @@ tbd_create_impl(FILE *stream, for(i = 0; i < b->size; i++) { tbd_stat_t *_b = tbd_stat_entry(b, i); if(_b == NULL) - return tbd_error(TBD_ERROR_UNABLE_TO_STAT_FILE); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_STAT_FILE); tbd_stat_t *_a = tbd_stat_entry_find(a, _b->name); err = tbd_create_impl(stream, _a, _b, false); tbd_stat_free(_a); @@ -664,7 +664,7 @@ tbd_create_impl(FILE *stream, tbd_stat_t *_a = tbd_stat_entry(a, i); if(_a == NULL) - return tbd_error(TBD_ERROR_UNABLE_TO_STAT_FILE); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_STAT_FILE); tbd_stat_t *_b = tbd_stat_entry_find(b, _a->name); if (_b == NULL) @@ -689,7 +689,7 @@ tbd_create(FILE *stream, { int err; if((stream == NULL) || (a == NULL) || (b == NULL)) - return tbd_error(TBD_ERROR_NULL_POINTER); + return TBD_ERROR(TBD_ERROR_NULL_POINTER); if((err = tbd_create_cmd_ident(stream)) != 0 || (err = tbd_create_impl(stream, a, b, true)) != 0) diff --git a/tbdiff.h b/tbdiff.h index bf28022..ba14999 100644 --- a/tbdiff.h +++ b/tbdiff.h @@ -46,7 +46,7 @@ typedef enum { TBD_METADATA_MODE = 0x2, TBD_METADATA_UID = 0x4, TBD_METADATA_GID = 0x8, - TBD_METADATA_RDEV = 0x10, + TBD_METADATA_RDEV = 0x10, } tbd_metadata_type_e; typedef enum { @@ -75,14 +75,17 @@ typedef enum { } tbd_error_e; #ifdef NDEBUG -#define tbd_error(e) return e +#define TBD_ERROR(e) (e) #else -/*#define tbd_error(e) { if(e != 0) fprintf(stderr, "TBDiff error '%s' in function '%s' at line %d of file '%s'.\n", #e, __FUNCTION__, __LINE__, __FILE__); return e; }*/ -#define tbd_error(e)\ - ({if(e != 0) fprintf(stderr, \ - "TBDiff error '%s' in function '%s' at line %d of file '%s'.\n", \ - #e, __func__, __LINE__, __FILE__); \ - e;}) +#define TBD_ERROR(e) tbd_error(e, #e, __func__, __LINE__, __FILE__) +inline tbd_error_e tbd_error(tbd_error_e e, char const *s, char const *func, + int line, char const* file) +{ + if (e != TBD_ERROR_SUCCESS) + fprintf(stderr, "TBDiff error '%s' in function '%s' at line %d " + "of file '%s'.\n", s, func, line, file); + return e; +} #endif extern int tbd_apply (FILE *stream); -- cgit v1.2.1