diff options
author | Ben Brewer <ben.brewer@codethink.co.uk> | 2014-06-04 16:57:00 +0100 |
---|---|---|
committer | Ben Brewer <ben.brewer@codethink.co.uk> | 2014-06-04 16:57:00 +0100 |
commit | 0b9a35045c6fb7baa7a2b29408c2df9017111432 (patch) | |
tree | 899dc8dee7ef09aa38b9341a1a2a5421102f31ac | |
parent | 20d5fb340c6ad1e7b16c6dc17e3f6c364a5a5515 (diff) | |
parent | 1de58f93e1b19442d40badde4b68fc34bb08f386 (diff) | |
download | tbdiff-0b9a35045c6fb7baa7a2b29408c2df9017111432.tar.gz |
Merge branch 'baserock/benbrewer/post-overhaul-v2'
Reviewed-by: Michael Drake
Reviewed-by: Richard Maw
-rw-r--r-- | tbdiff-create/main.c | 20 | ||||
-rw-r--r-- | tbdiff-deploy/main.c | 22 | ||||
-rw-r--r-- | tbdiff/tbdiff-apply.c | 224 | ||||
-rw-r--r-- | tbdiff/tbdiff-common.h | 22 | ||||
-rw-r--r-- | tbdiff/tbdiff-create.c | 252 | ||||
-rw-r--r-- | tbdiff/tbdiff-io.c | 88 | ||||
-rw-r--r-- | tbdiff/tbdiff-io.h | 29 | ||||
-rw-r--r-- | tbdiff/tbdiff-stat.c | 73 | ||||
-rw-r--r-- | tbdiff/tbdiff-stat.h | 42 | ||||
-rw-r--r-- | tbdiff/tbdiff-xattrs.c | 30 | ||||
-rw-r--r-- | tbdiff/tbdiff-xattrs.h | 23 |
11 files changed, 418 insertions, 407 deletions
diff --git a/tbdiff-create/main.c b/tbdiff-create/main.c index d20feba..c8df2bf 100644 --- a/tbdiff-create/main.c +++ b/tbdiff-create/main.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011-2012 Codethink Ltd. + * Copyright (C) 2011-2014 Codethink Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License Version 2 as @@ -18,7 +18,9 @@ #include <stdlib.h> #include <stdio.h> #include <stdint.h> +#include <errno.h> +#include <fcntl.h> #include <unistd.h> #include <tbdiff/tbdiff.h> @@ -38,7 +40,7 @@ main(int argc, if(getcwd(cwd_buff, cwd_size) == NULL) return EXIT_FAILURE; - tbd_stat_t *tstat[2]; + struct tbd_stat *tstat[2]; tstat[0] = tbd_stat(argv[2]); if(tstat[0] == NULL) { @@ -62,15 +64,17 @@ main(int argc, return EXIT_FAILURE; } - FILE *fp = fopen(argv[1], "wb"); - if(fp == NULL) { - fprintf(stderr, "ERROR: Unable to open patch for writing.\n"); + int fd = open(argv[1], + O_WRONLY | O_CREAT | O_TRUNC, + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + if(fd < 0) { + fprintf(stderr, "Error(%d): Unable to open patch for writing.\n", errno); return EXIT_FAILURE; } int err; - if((err = tbd_create(fp, tstat[0], tstat[1])) != 0) { - fclose(fp); + if((err = tbd_create(fd, tstat[0], tstat[1])) != 0) { + close(fd); tbd_stat_free(tstat[0]); tbd_stat_free(tstat[1]); @@ -89,7 +93,7 @@ main(int argc, return EXIT_FAILURE; } - fclose(fp); + close(fd); tbd_stat_free(tstat[0]); tbd_stat_free(tstat[1]); diff --git a/tbdiff-deploy/main.c b/tbdiff-deploy/main.c index b5b7ce9..228a753 100644 --- a/tbdiff-deploy/main.c +++ b/tbdiff-deploy/main.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011-2012 Codethink Ltd. + * Copyright (C) 2011-2014 Codethink Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License Version 2 as @@ -19,33 +19,43 @@ #include <stdio.h> #include <stdint.h> #include <stdbool.h> +#include <fcntl.h> #include <unistd.h> #include <errno.h> #include <tbdiff/tbdiff.h> +static void +print_usage(const char *name) +{ + printf("Usage: %s <patch-stream>\n", name); +} + int main(int argc, char **argv) { if(argc < 2) { fprintf(stderr, "Error: No patch stream specified.\n"); + print_usage(argv[0]); return EXIT_FAILURE; } - FILE* patch = fopen(argv[1], "rb"); - if(patch == NULL) { - fprintf(stderr, "Error: Can't open patch stream for reading.\n"); + int patch = open(argv[1], O_RDONLY); + if(patch < 0) { + fprintf(stderr, + "Error: Can't open patch stream for reading (err=%d).\n", + errno); return EXIT_FAILURE; } int err; if((err = tbd_apply(patch)) != 0) { - fclose(patch); + close(patch); fprintf(stderr, "Error: Error applying patch stream (err=%d).\n", err); return EXIT_FAILURE; } - fclose(patch); + close(patch); return EXIT_SUCCESS; } diff --git a/tbdiff/tbdiff-apply.c b/tbdiff/tbdiff-apply.c index 666d876..615032f 100644 --- a/tbdiff/tbdiff-apply.c +++ b/tbdiff/tbdiff-apply.c @@ -16,7 +16,6 @@ */ #include <stdlib.h> -#include <stdio.h> #include <string.h> #include <stdint.h> #include <stdbool.h> @@ -26,9 +25,12 @@ #include <sys/stat.h> #include <sys/time.h> #include <dirent.h> +#include <fcntl.h> #include <unistd.h> #include <utime.h> +#include <errno.h> + #include "config.h" #if HAVE_ATTR_XATTR_H @@ -37,19 +39,19 @@ #include <sys/xattr.h> #endif -#include <tbdiff/tbdiff-common.h> -#include <tbdiff/tbdiff-io.h> -#include <tbdiff/tbdiff-private.h> -#include <tbdiff/tbdiff-xattrs.h> +#include "tbdiff-common.h" +#include "tbdiff-io.h" +#include "tbdiff-private.h" +#include "tbdiff-xattrs.h" char* -tbd_apply_read_string(FILE *stream) +tbd_apply_read_string(int stream) { uint16_t dlen; - if(tbd_read_uint16(&dlen, stream) != 1) + if(!tbd_read_uint16(&dlen, stream)) return NULL; char dname[dlen + 1]; - if(fread(dname, 1, dlen, stream) != dlen) + if(read(stream, dname, dlen) != dlen) return NULL; dname[dlen] = '\0'; @@ -67,11 +69,11 @@ tbd_apply_read_string(FILE *stream) * - or your allocator does nothing when asked to free non-allocated memory */ int -tbd_apply_read_block(FILE *stream, void **data, size_t *size) +tbd_apply_read_block(int stream, void **data, size_t *size) { { size_t _size; - if (fread(&_size, 1, sizeof(_size), stream) != sizeof(_size) ) { + if (read(stream, &_size, sizeof(_size)) != sizeof(_size) ) { return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); } if (_size > *size) { @@ -84,27 +86,27 @@ tbd_apply_read_block(FILE *stream, void **data, size_t *size) } } - if (fread(*data, 1, *size, stream) != *size) { + if (read(stream, *data, *size) != *size) { return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); } return TBD_ERROR_SUCCESS; } static int -tbd_apply_identify(FILE *stream) +tbd_apply_identify(int stream) { - tbd_cmd_t cmd; - if(fread(&cmd, sizeof(tbd_cmd_t), 1, stream) != 1) + tbd_cmd_type cmd; + if(read(stream, &cmd, sizeof(tbd_cmd_type)) != sizeof(tbd_cmd_type)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); if(cmd != TBD_CMD_IDENTIFY) return TBD_ERROR(TBD_ERROR_INVALID_PARAMETER); uint16_t nlen; - if(tbd_read_uint16(&nlen, stream) != 1) + if(!tbd_read_uint16(&nlen, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); if(strlen(TB_DIFF_PROTOCOL_ID) != nlen) return TBD_ERROR(TBD_ERROR_INVALID_PARAMETER); char nstr[nlen]; - if(fread(nstr, 1, nlen, stream) != nlen) + if(read(stream, nstr, nlen) != nlen) 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); @@ -112,13 +114,13 @@ tbd_apply_identify(FILE *stream) } static int -tbd_apply_cmd_dir_create(FILE *stream) +tbd_apply_cmd_dir_create(int stream) { uint16_t dlen; - if(tbd_read_uint16(&dlen, stream) != 1) + if(!tbd_read_uint16(&dlen, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char dname[dlen + 1]; - if(fread(dname, 1, dlen, stream) != dlen) + if(read(stream, dname, dlen) != dlen) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); dname[dlen] = '\0'; TBD_DEBUGF("cmd_dir_create %s\n", dname); @@ -126,19 +128,19 @@ tbd_apply_cmd_dir_create(FILE *stream) return TBD_ERROR(TBD_ERROR_INVALID_PARAMETER); time_t mtime; - if(tbd_read_time(&mtime, stream) != 1) + if(!tbd_read_time(&mtime, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); uid_t uid; - if(tbd_read_uid(&uid, stream) != 1) + if(!tbd_read_uid(&uid, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); gid_t gid; - if(tbd_read_gid(&gid, stream) != 1) + if(!tbd_read_gid(&gid, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); mode_t mode; - if(tbd_read_mode(&mode, stream) != 1) + if(!tbd_read_mode(&mode, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); if(mkdir(dname, (mode_t)mode) != 0) @@ -156,14 +158,14 @@ tbd_apply_cmd_dir_create(FILE *stream) } static int -tbd_apply_cmd_dir_enter(FILE *stream, +tbd_apply_cmd_dir_enter(int stream, uintptr_t *depth) { uint16_t dlen; - if(tbd_read_uint16(&dlen, stream) != 1) + if(!tbd_read_uint16(&dlen, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char dname[dlen + 1]; - if(fread(dname, 1, dlen, stream) != dlen) + if(read(stream, dname, dlen) != dlen) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); dname[dlen] = '\0'; TBD_DEBUGF("cmd_dir_enter %s\n", dname); @@ -178,13 +180,13 @@ tbd_apply_cmd_dir_enter(FILE *stream, } static int -tbd_apply_cmd_dir_leave(FILE *stream, +tbd_apply_cmd_dir_leave(int stream, uintptr_t *depth) { int err = TBD_ERROR_SUCCESS; struct utimbuf time; - if (tbd_read_time(&(time.modtime), stream) != 1) { + if (!tbd_read_time(&(time.modtime), stream)) { return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); } time.actime = time.modtime;/* not sure what the best atime to use is */ @@ -212,13 +214,13 @@ tbd_apply_cmd_dir_leave(FILE *stream, } static int -tbd_apply_cmd_file_create(FILE *stream) +tbd_apply_cmd_file_create(int stream) { uint16_t flen; - if(tbd_read_uint16(&flen, stream) != 1) + if(!tbd_read_uint16(&flen, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char fname[flen + 1]; - if(fread(fname, 1, flen, stream) != flen) + if(read(stream, fname, flen) != flen) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); fname[flen] = '\0'; if((strchr(fname, '/') != NULL) || (strcmp(fname, "..") == 0)) @@ -230,40 +232,37 @@ tbd_apply_cmd_file_create(FILE *stream) gid_t gid; uint32_t fsize; - if(tbd_read_time (&mtime, stream) != 1 || - tbd_read_uint32(&mode , stream) != 1 || - tbd_read_uid (&uid , stream) != 1 || - tbd_read_gid (&gid , stream) != 1 || - tbd_read_uint32(&fsize, stream) != 1) + if(!tbd_read_time (&mtime, stream) || + !tbd_read_uint32(&mode , stream) || + !tbd_read_uid (&uid , stream) || + !tbd_read_gid (&gid , stream) || + !tbd_read_uint32(&fsize, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); TBD_DEBUGF("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); - } - - fp = fopen(fname, "wb"); - if(fp == NULL) + int fd = open(fname, O_WRONLY | O_CREAT | O_EXCL, mode); + if(fd < 0) { + if (errno == EEXIST) + return TBD_ERROR(TBD_ERROR_FILE_ALREADY_EXISTS); return TBD_ERROR(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_WRITING); + } uintptr_t block = 256; uint8_t fbuff[block]; for(; fsize != 0; fsize -= block) { if(fsize < block) block = fsize; - if(fread(fbuff, 1, block, stream) != block) { - fclose(fp); + if(read(stream, fbuff, block) != block) { + close(fd); return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); } - if(fwrite(fbuff, 1, block, fp) != block) { - fclose(fp); + if(write(fd, fbuff, block) != block) { + close(fd); return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); } } - fclose(fp); + close(fd); /* Apply metadata. */ struct utimbuf timebuff = { time(NULL), mtime }; @@ -279,7 +278,7 @@ tbd_apply_cmd_file_create(FILE *stream) } static int -tbd_apply_cmd_file_delta(FILE *stream) +tbd_apply_cmd_file_delta(int stream) { uint16_t mdata_mask; time_t mtime; @@ -289,10 +288,10 @@ tbd_apply_cmd_file_delta(FILE *stream) uint16_t flen; int error; - if(tbd_read_uint16(&flen, stream) != 1) + if(!tbd_read_uint16(&flen, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char fname[flen + 1]; - if(fread(fname, 1, flen, stream) != flen) + if(read(stream, fname, flen) != flen) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); fname[flen] = '\0'; @@ -303,32 +302,32 @@ tbd_apply_cmd_file_delta(FILE *stream) return TBD_ERROR(TBD_ERROR_INVALID_PARAMETER); /* Reading metadata */ - if(tbd_read_uint16(&mdata_mask, stream) != 1 || - tbd_read_time (&mtime , stream) != 1 || - tbd_read_uid (&uid , stream) != 1 || - tbd_read_gid (&gid , stream) != 1 || - tbd_read_uint32(&mode , stream) != 1) + if(!tbd_read_uint16(&mdata_mask, stream) || + !tbd_read_time (&mtime , stream) || + !tbd_read_uid (&uid , stream) || + !tbd_read_gid (&gid , stream) || + !tbd_read_uint32(&mode , stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); - FILE *op = fopen(fname, "rb"); - if(op == NULL) + int od = open(fname, O_RDONLY); + if(od < 0) return TBD_ERROR(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_READING); if(remove(fname) != 0) { - fclose(op); + close(od); return TBD_ERROR(TBD_ERROR_UNABLE_TO_REMOVE_FILE); } - FILE *np = fopen(fname, "wb"); - if(np == NULL) { - fclose(op); + int nd = open(fname, O_WRONLY | O_CREAT, mode); + if(nd < 0) { + close(od); return TBD_ERROR(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_WRITING); } uint32_t dstart, dend; - if(tbd_read_uint32(&dstart, stream) != 1) { + if(!tbd_read_uint32(&dstart, stream)) { error = TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); goto tbd_apply_cmd_file_delta_error; } - if(tbd_read_uint32(&dend, stream) != 1) { + if(!tbd_read_uint32(&dend, stream)) { error = TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); goto tbd_apply_cmd_file_delta_error; } @@ -338,18 +337,18 @@ tbd_apply_cmd_file_delta(FILE *stream) for(block = 256; dstart != 0; dstart -= block) { if(dstart < block) block = dstart; - if(fread(fbuff, 1, block, op) != block) { + if(read(od, fbuff, block) != block) { error = TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); goto tbd_apply_cmd_file_delta_error; } - if(fwrite(fbuff, 1, block, np) != block) { + if(write(nd, fbuff, block) != block) { error = TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); goto tbd_apply_cmd_file_delta_error; } } uint32_t fsize; - if(tbd_read_uint32(&fsize, stream) != 1) { + if(!tbd_read_uint32(&fsize, stream)) { error = TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); goto tbd_apply_cmd_file_delta_error; } @@ -357,31 +356,31 @@ tbd_apply_cmd_file_delta(FILE *stream) for(block = 256; fsize != 0; fsize -= block) { if(fsize < block) block = fsize; - if(fread(fbuff, 1, block, stream) != block) { + if(read(stream, fbuff, block) != block) { error = TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); goto tbd_apply_cmd_file_delta_error; } - if(fwrite(fbuff, 1, block, np) != block) { + if(write(nd, fbuff, block) != block) { error = TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); goto tbd_apply_cmd_file_delta_error; } } - if(fseek(op, dend, SEEK_SET) != 0) { + if(lseek(od, dend, SEEK_SET) < 0) { error = TBD_ERROR(TBD_ERROR_UNABLE_TO_SEEK_THROUGH_STREAM); goto tbd_apply_cmd_file_delta_error; } for(block = 256; block != 0;) { - block = fread(fbuff, 1, block, op); - if(fwrite(fbuff, 1, block, np) != block) { + block = read(od, fbuff, block); + if(write(nd, fbuff, block) != block) { error = TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); goto tbd_apply_cmd_file_delta_error; } } - fclose(np); - fclose(op); + close(nd); + close(od); /* Apply metadata. */ /* file was removed so old permissions were lost @@ -410,8 +409,8 @@ tbd_apply_cmd_file_delta(FILE *stream) return 0; tbd_apply_cmd_file_delta_error: - fclose(np); - fclose(op); + close(nd); + close(od); return error; } @@ -477,13 +476,13 @@ tbd_apply_cmd_entity_delete_for_name(const char *name) } static int -tbd_apply_cmd_entity_delete(FILE *stream) +tbd_apply_cmd_entity_delete(int stream) { uint16_t elen; - if(tbd_read_uint16(&elen, stream) != 1) + if(!tbd_read_uint16(&elen, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char ename[elen + 1]; - if(fread(ename, 1, elen, stream) != elen) + if(read(stream, ename, elen) != elen) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); ename[elen] = '\0'; @@ -495,34 +494,34 @@ tbd_apply_cmd_entity_delete(FILE *stream) } static int -tbd_apply_cmd_symlink_create(FILE *stream) +tbd_apply_cmd_symlink_create(int stream) { uint16_t len; time_t mtime; uid_t uid; gid_t gid; - if(tbd_read_time(&mtime, stream) != 1 || - tbd_read_uid(&uid , stream) != 1 || - tbd_read_gid(&gid , stream) != 1) + if(!tbd_read_time(&mtime, stream) || + !tbd_read_uid (&uid , stream) || + !tbd_read_gid (&gid , stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); /* Reading link file name */ - if(tbd_read_uint16(&len, stream) != 1) + if(!tbd_read_uint16(&len, 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) + if(read(stream, linkname, len) != len) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); /* Reading target path */ - if(tbd_read_uint16(&len, stream) != 1) + if(!tbd_read_uint16(&len, 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) + if(read(stream, linkpath, len) != len) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); TBD_DEBUGF("cmd_symlink_create %s -> %s\n", linkname, linkpath); @@ -543,7 +542,7 @@ tbd_apply_cmd_symlink_create(FILE *stream) } static int -tbd_apply_cmd_special_create(FILE *stream) +tbd_apply_cmd_special_create(int stream) { char *name = tbd_apply_read_string(stream); time_t mtime; @@ -553,11 +552,11 @@ tbd_apply_cmd_special_create(FILE *stream) uint32_t dev; if(name == NULL || - tbd_read_time (&mtime, stream) != 1 || - tbd_read_mode (&mode , stream) != 1 || - tbd_read_uid (&uid , stream) != 1 || - tbd_read_gid (&gid , stream) != 1 || - tbd_read_uint32(&dev , stream) != 1) { + !tbd_read_time (&mtime, stream) || + !tbd_read_mode (&mode , stream) || + !tbd_read_uid (&uid , stream) || + !tbd_read_gid (&gid , stream) || + !tbd_read_uint32(&dev , stream)) { free(name); return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); } @@ -581,7 +580,7 @@ tbd_apply_cmd_special_create(FILE *stream) } static int -tbd_apply_cmd_dir_delta(FILE *stream) +tbd_apply_cmd_dir_delta(int stream) { uint16_t metadata_mask; time_t mtime; @@ -589,11 +588,11 @@ tbd_apply_cmd_dir_delta(FILE *stream) gid_t gid; mode_t mode; - if(tbd_read_uint16(&metadata_mask, stream) != 1 || - tbd_read_time (&mtime , stream) != 1 || - tbd_read_uid (&uid , stream) != 1 || - tbd_read_gid (&gid , stream) != 1 || - tbd_read_uint32(&mode , stream) != 1) + if(!tbd_read_uint16(&metadata_mask, stream) || + !tbd_read_time (&mtime , stream) || + !tbd_read_uid (&uid , stream) || + !tbd_read_gid (&gid , stream) || + !tbd_read_uint32(&mode , stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char *dname = tbd_apply_read_string(stream); @@ -618,7 +617,7 @@ tbd_apply_cmd_dir_delta(FILE *stream) } static int -tbd_apply_cmd_file_mdata_update(FILE *stream) +tbd_apply_cmd_file_mdata_update(int stream) { uint16_t metadata_mask; time_t mtime; @@ -626,11 +625,11 @@ tbd_apply_cmd_file_mdata_update(FILE *stream) gid_t gid; mode_t mode; - if(tbd_read_uint16(&metadata_mask, stream) != 1 || - tbd_read_time (&mtime , stream) != 1 || - tbd_read_uid (&uid , stream) != 1 || - tbd_read_gid (&gid , stream) != 1 || - tbd_read_uint32(&mode , stream) != 1) + if(!tbd_read_uint16(&metadata_mask, stream) || + !tbd_read_time (&mtime , stream) || + !tbd_read_uid (&uid , stream) || + !tbd_read_gid (&gid , stream) || + !tbd_read_uint32(&mode , stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); char *dname = tbd_apply_read_string(stream); @@ -656,7 +655,7 @@ tbd_apply_cmd_file_mdata_update(FILE *stream) } static int -tbd_apply_cmd_xattrs_update(FILE *stream) +tbd_apply_cmd_xattrs_update(int stream) { int err = TBD_ERROR_SUCCESS; char *fname; @@ -674,7 +673,7 @@ tbd_apply_cmd_xattrs_update(FILE *stream) } /* read how many attributes to process */ - if (tbd_read_uint32(&count, stream) != 1) { + if (!tbd_read_uint32(&count, stream)) { err = TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); goto cleanup; } @@ -709,9 +708,9 @@ cleanup: } int -tbd_apply(FILE *stream) +tbd_apply(int stream) { - if(stream == NULL) + if(stream < 0) return TBD_ERROR(TBD_ERROR_NULL_POINTER); int err; @@ -721,8 +720,9 @@ tbd_apply(FILE *stream) uintptr_t depth = 0; bool flush = false; while(!flush) { - tbd_cmd_t cmd; - if(fread(&cmd, sizeof(tbd_cmd_t), 1, stream) != 1) + tbd_cmd_type cmd; + if(read(stream, &cmd, sizeof(tbd_cmd_type)) != + sizeof(tbd_cmd_type)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); switch(cmd) { case TBD_CMD_DIR_CREATE: diff --git a/tbdiff/tbdiff-common.h b/tbdiff/tbdiff-common.h index cbb5c0d..8a5c889 100644 --- a/tbdiff/tbdiff-common.h +++ b/tbdiff/tbdiff-common.h @@ -28,7 +28,7 @@ #include <tbdiff/tbdiff-stat.h> -typedef enum { +enum tbd_cmd { TBD_CMD_IDENTIFY = 0x00, TBD_CMD_UPDATE = 0x01, TBD_CMD_DIR_CREATE = 0x10, @@ -44,20 +44,20 @@ typedef enum { TBD_CMD_SYMLINK_CREATE = 0x40, TBD_CMD_SPECIAL_CREATE = 0x50, TBD_CMD_XATTRS_UPDATE = 0x60, -} tbd_cmd_e; +}; -typedef uint8_t tbd_cmd_t; +typedef uint8_t tbd_cmd_type; -typedef enum { +enum tbd_metadata_type { TBD_METADATA_NONE = 0x0, TBD_METADATA_MTIME = 0x1, TBD_METADATA_MODE = 0x2, TBD_METADATA_UID = 0x4, TBD_METADATA_GID = 0x8, TBD_METADATA_RDEV = 0x10, -} tbd_metadata_type_e; +}; -typedef enum { +enum tbd_error { TBD_ERROR_SUCCESS = 0, TBD_ERROR_FAILURE = -1, TBD_ERROR_OUT_OF_MEMORY = -2, @@ -83,7 +83,7 @@ typedef enum { TBD_ERROR_UNABLE_TO_CREATE_SOCKET_FILE = -22, TBD_ERROR_XATTRS_NOT_SUPPORTED = -23, TBD_ERROR_XATTRS_MISSING_ATTR = -24, -} tbd_error_e; +}; #ifdef NDEBUG #define TBD_DEBUG(d) @@ -116,8 +116,8 @@ tbd_log(char const *t, char const *s, char const *func, int line, } #define TBD_ERROR(e) tbd_error(e, #e, __func__, __LINE__, __FILE__) -static inline tbd_error_e -tbd_error(tbd_error_e e, char const *s, char const *func, int line, +static inline enum tbd_error +tbd_error(enum tbd_error e, char const *s, char const *func, int line, char const* file) { if (e != TBD_ERROR_SUCCESS) @@ -126,7 +126,7 @@ tbd_error(tbd_error_e e, char const *s, char const *func, int line, } #endif -extern int tbd_apply (FILE *stream); -extern int tbd_create(FILE *stream, tbd_stat_t *a, tbd_stat_t *b); +int tbd_apply (int stream); +int tbd_create(int stream, struct tbd_stat *a, struct tbd_stat *b); #endif /* !__TBDIFF_COMMON_H__ */ diff --git a/tbdiff/tbdiff-create.c b/tbdiff/tbdiff-create.c index edfb985..0c5823d 100644 --- a/tbdiff/tbdiff-create.c +++ b/tbdiff/tbdiff-create.c @@ -16,113 +16,113 @@ */ #include <stdlib.h> -#include <stdio.h> #include <string.h> #include <stdint.h> #include <stdbool.h> #include <sys/stat.h> #include <dirent.h> +#include <fcntl.h> #include <unistd.h> -#include <tbdiff/tbdiff-common.h> -#include <tbdiff/tbdiff-io.h> -#include <tbdiff/tbdiff-private.h> -#include <tbdiff/tbdiff-xattrs.h> +#include "tbdiff-common.h" +#include "tbdiff-io.h" +#include "tbdiff-private.h" +#include "tbdiff-xattrs.h" #define PATH_BUFFER_LENGTH 4096 static int -tbd_create_write_cmd(FILE *stream, - tbd_cmd_t cmd) +tbd_create_write_cmd(int stream, + tbd_cmd_type cmd) { - if(fwrite(&cmd, sizeof(tbd_cmd_t), 1, stream) != 1) + if(write(stream, &cmd, sizeof(tbd_cmd_type)) != sizeof(tbd_cmd_type)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } static int -tbd_create_write_string(FILE *stream, +tbd_create_write_string(int stream, const char *string) { uint16_t slen = strlen(string); - if((tbd_write_uint16(slen, stream) != 1) - || (fwrite(string, 1, slen, stream) != slen)) + if((!tbd_write_uint16(slen, stream)) + || (write(stream, string, slen) != slen)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } static int -tbd_create_write_block(FILE *stream, +tbd_create_write_block(int stream, void const *data, size_t size) { - if (fwrite(&size, 1, sizeof(size), stream) != sizeof(size)) { + if (write(stream, &size, sizeof(size)) != sizeof(size)) { return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); } - if (fwrite(data, 1, size, stream) != size) { + if (write(stream, data, size) != size) { return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); } return TBD_ERROR_SUCCESS; } static int -tbd_create_write_mdata_mask(FILE *stream, +tbd_create_write_mdata_mask(int stream, uint16_t mask) { - if(tbd_write_uint16(mask, stream) != 1) + if(!tbd_write_uint16(mask, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } static int -tbd_create_write_mtime(FILE *stream, +tbd_create_write_mtime(int stream, time_t mtime) { - if(tbd_write_time(mtime, stream) != 1) + if(!tbd_write_time(mtime, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } static int -tbd_create_write_mode(FILE *stream, +tbd_create_write_mode(int stream, mode_t mode) { - if(tbd_write_mode(mode, stream) != 1) + if(!tbd_write_mode(mode, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } static int -tbd_create_write_gid(FILE *stream, +tbd_create_write_gid(int stream, gid_t gid) { - if(tbd_write_gid(gid, stream) != 1) + if(!tbd_write_gid(gid, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } static int -tbd_create_write_uid(FILE *stream, +tbd_create_write_uid(int stream, uid_t uid) { - if(tbd_write_uid(uid, stream) != 1) + if(!tbd_write_uid(uid, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } static int -tbd_create_write_dev(FILE *stream, +tbd_create_write_dev(int stream, uint32_t dev) { - if(tbd_write_uint32(dev, stream) != 1) + if(!tbd_write_uint32(dev, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); return 0; } static int -tbd_create_cmd_ident(FILE *stream) +tbd_create_cmd_ident(int stream) { int err; @@ -134,7 +134,7 @@ tbd_create_cmd_ident(FILE *stream) } static int -tbd_create_cmd_update(FILE *stream) +tbd_create_cmd_update(int stream) { return tbd_create_write_cmd(stream, TBD_CMD_UPDATE); } @@ -146,9 +146,10 @@ static int tbd_create_cmd_write_xattr_pair(char const *name, void const *data, size_t size, - void *stream) + void *ud) { int err; + int stream = *((int *)ud); if ((err = tbd_create_write_string(stream, name)) != TBD_ERROR_SUCCESS) @@ -162,10 +163,10 @@ tbd_create_cmd_write_xattr_pair(char const *name, } static int -tbd_create_cmd_write_xattrs(FILE *stream, tbd_stat_t *f) +tbd_create_cmd_write_xattrs(int stream, struct tbd_stat *f) { int err = TBD_ERROR_SUCCESS; - tbd_xattrs_names_t names; + struct tbd_xattrs_names names; char *path = tbd_stat_path(f); if (path == NULL) { return TBD_ERROR(TBD_ERROR_OUT_OF_MEMORY); @@ -199,7 +200,7 @@ tbd_create_cmd_write_xattrs(FILE *stream, tbd_stat_t *f) goto cleanup_names; } - if (tbd_write_uint32(count, stream) != 1) { + if (!tbd_write_uint32(count, stream)) { err = TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); goto cleanup_names; } @@ -209,7 +210,7 @@ tbd_create_cmd_write_xattrs(FILE *stream, tbd_stat_t *f) err = tbd_xattrs_pairs(&names, path, tbd_create_cmd_write_xattr_pair, - stream); + &stream); cleanup_names: tbd_xattrs_names_free(&names); @@ -219,8 +220,8 @@ cleanup_path: } static int -tbd_create_cmd_file_create(FILE *stream, - tbd_stat_t *f) +tbd_create_cmd_file_create(int stream, + struct tbd_stat *f) { int err; if((err = tbd_create_write_cmd(stream, TBD_CMD_FILE_CREATE)) != 0 || @@ -232,30 +233,30 @@ tbd_create_cmd_file_create(FILE *stream, return err; uint32_t size = f->size; - if(tbd_write_uint32(size, stream) != 1) + if(!tbd_write_uint32(size, stream)) return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); - FILE *fp = tbd_stat_fopen(f, "rb"); - if(fp == NULL) + int fd = tbd_stat_open(f, O_RDONLY); + if(fd < 0) return TBD_ERROR(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_READING); uint8_t buff[256]; uintptr_t b = 256; for(b = 256; b == 256; ) { - b = fread(buff, 1, b, fp); - if(fwrite(buff, 1, b, stream) != b) { - fclose(fp); + b = read(fd, buff, b); + if(write(stream, buff, b) != b) { + close(fd); return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); } } - fclose(fp); + close(fd); return tbd_create_cmd_write_xattrs(stream, f); } static uint16_t -tbd_metadata_mask(tbd_stat_t *a, - tbd_stat_t *b) +tbd_metadata_mask(struct tbd_stat *a, + struct tbd_stat *b) { uint16_t metadata_mask = TBD_METADATA_NONE; @@ -273,9 +274,9 @@ tbd_metadata_mask(tbd_stat_t *a, } static int -tbd_create_cmd_file_metadata_update(FILE *stream, - tbd_stat_t *a, - tbd_stat_t *b) +tbd_create_cmd_file_metadata_update(int stream, + struct tbd_stat *a, + struct tbd_stat *b) { int err; uint16_t metadata_mask = tbd_metadata_mask(a, b); @@ -295,16 +296,16 @@ tbd_create_cmd_file_metadata_update(FILE *stream, } static int -tbd_create_cmd_file_delta(FILE *stream, - tbd_stat_t *a, - tbd_stat_t *b) +tbd_create_cmd_file_delta(int stream, + struct tbd_stat *a, + struct tbd_stat *b) { - FILE *fpa = tbd_stat_fopen(a, "rb"); - if(fpa == NULL) + int fda = tbd_stat_open(a, O_RDONLY); + if(fda < 0) return TBD_ERROR(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_READING); - FILE *fpb = tbd_stat_fopen(b, "rb"); - if(fpb == NULL) { - fclose(fpa); + int fdb = tbd_stat_open(b, O_RDONLY); + if(fdb < 0) { + close(fda); return TBD_ERROR(TBD_ERROR_UNABLE_TO_OPEN_FILE_FOR_READING); } @@ -314,8 +315,8 @@ tbd_create_cmd_file_delta(FILE *stream, uintptr_t o; for(o = 0; (blks[1] == blks[0]) && (blks[0] != 0); o += blks[1]) { - blks[0] = fread(buff[0], 1, blks[0], fpa); - blks[1] = fread(buff[1], 1, blks[0], fpb); + blks[0] = read(fda, buff[0], blks[0]); + blks[1] = read(fdb, buff[1], blks[0]); if((blks[0] == 0) || (blks[1] == 0)) break; @@ -331,20 +332,14 @@ tbd_create_cmd_file_delta(FILE *stream, } uint32_t start = o; - 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); - } - /* Find length. */ - long flena = ftell(fpa); - long flenb = ftell(fpb); + off_t flena = lseek(fda, 0, SEEK_END); + off_t flenb = lseek(fdb, 0, SEEK_END); if((flena < 0) || (flenb < 0)) { - fclose(fpa); - fclose(fpb); - return TBD_ERROR(TBD_ERROR_UNABLE_TO_DETECT_STREAM_POSITION); + close(fda); + close(fdb); + return TBD_ERROR(TBD_ERROR_UNABLE_TO_SEEK_THROUGH_STREAM); } /* Find end. */ @@ -356,17 +351,17 @@ tbd_create_cmd_file_delta(FILE *stream, if((blks[0] == 0) || (blks[1] == 0)) break; - if((fseek(fpa, flena - (o + blks[0]), SEEK_SET) != 0) - || (fseek(fpb, flenb - (o + blks[1]), SEEK_SET) != 0)) { - fclose(fpa); - fclose(fpb); + if((lseek(fda, flena - (o + blks[0]), SEEK_SET) < 0) + || (lseek(fdb, flenb - (o + blks[1]), SEEK_SET) < 0)) { + close(fda); + close(fdb); 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); + if((read(fda, buff[0], blks[0]) != blks[0]) + || (read(fdb, buff[1], blks[1]) != blks[1])) { + close(fda); + close(fdb); return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); } @@ -380,7 +375,7 @@ tbd_create_cmd_file_delta(FILE *stream, if(i < blks[1]) break; } - fclose(fpa); + close(fda); /* Ensure that the start and end don't overlap for the new file. */ if((flenb - o) < start) @@ -395,7 +390,7 @@ tbd_create_cmd_file_delta(FILE *stream, /* Data is identical, only alter metadata */ if((end == start) && (size == 0)) { tbd_create_cmd_file_metadata_update(stream, a, b); - fclose(fpb); + close(fdb); return tbd_create_cmd_write_xattrs(stream, b); } @@ -410,39 +405,40 @@ tbd_create_cmd_file_delta(FILE *stream, ((err = tbd_create_write_uid (stream, b->uid)) != 0) || ((err = tbd_create_write_gid (stream, b->gid)) != 0) || ((err = tbd_create_write_mode (stream, b->mode)) != 0)) { - fclose(fpb); + close(fdb); return err; } - if((tbd_write_uint32(start, stream) != 1) || - (tbd_write_uint32(end, stream) != 1) || - (tbd_write_uint32(size, stream) != 1)) { - fclose(fpb); + + if((!tbd_write_uint32(start, stream)) || + (!tbd_write_uint32(end , stream)) || + (!tbd_write_uint32(size , stream))) { + close(fdb); return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); } - if(fseek(fpb, start, SEEK_SET) != 0) { - fclose(fpb); + if(lseek(fdb, start, SEEK_SET) < 0) { + close(fdb); 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); + if(read(fdb, buff[0], csize) != csize) { + close(fdb); return TBD_ERROR(TBD_ERROR_UNABLE_TO_READ_STREAM); } - if(fwrite(buff[0], 1, csize, stream) != csize) { - fclose(fpb); + if(write(stream, buff[0], csize) != csize) { + close(fdb); return TBD_ERROR(TBD_ERROR_UNABLE_TO_WRITE_STREAM); } } - fclose(fpb); + close(fdb); return tbd_create_cmd_write_xattrs(stream, b); } static int -tbd_create_cmd_dir_create(FILE *stream, - tbd_stat_t *d) +tbd_create_cmd_dir_create(int stream, + struct tbd_stat *d) { int err; @@ -457,7 +453,7 @@ tbd_create_cmd_dir_create(FILE *stream, } static int -tbd_create_cmd_dir_enter(FILE *stream, +tbd_create_cmd_dir_enter(int stream, const char *name) { int err; @@ -467,8 +463,8 @@ tbd_create_cmd_dir_enter(FILE *stream, } static int -tbd_create_cmd_dir_leave(FILE *stream, - tbd_stat_t *dir) +tbd_create_cmd_dir_leave(int stream, + struct tbd_stat *dir) { int err; if ((err = tbd_create_write_cmd(stream, TBD_CMD_DIR_LEAVE)) != @@ -480,7 +476,7 @@ tbd_create_cmd_dir_leave(FILE *stream, } static int -tbd_create_cmd_entity_delete(FILE *stream, +tbd_create_cmd_entity_delete(int stream, const char *name) { int err; @@ -490,9 +486,9 @@ tbd_create_cmd_entity_delete(FILE *stream, } static int -tbd_create_cmd_dir_delta(FILE *stream, - tbd_stat_t *a, - tbd_stat_t *b) +tbd_create_cmd_dir_delta(int stream, + struct tbd_stat *a, + struct tbd_stat *b) { int err; uint16_t metadata_mask = tbd_metadata_mask(a, b); @@ -512,8 +508,8 @@ tbd_create_cmd_dir_delta(FILE *stream, } static int -tbd_create_cmd_symlink_create(FILE *stream, - tbd_stat_t *symlink) +tbd_create_cmd_symlink_create(int stream, + struct tbd_stat *symlink) { int err; char path[PATH_BUFFER_LENGTH]; @@ -536,9 +532,9 @@ tbd_create_cmd_symlink_create(FILE *stream, } static int -tbd_create_cmd_symlink_delta(FILE *stream, - tbd_stat_t *a, - tbd_stat_t *b) +tbd_create_cmd_symlink_delta(int stream, + struct tbd_stat *a, + struct tbd_stat *b) { int err; char path_a[PATH_BUFFER_LENGTH]; @@ -574,8 +570,8 @@ tbd_create_cmd_symlink_delta(FILE *stream, } static int -tbd_create_cmd_special_create(FILE *stream, - tbd_stat_t *nod) +tbd_create_cmd_special_create(int stream, + struct tbd_stat *nod) { int err; @@ -590,9 +586,9 @@ tbd_create_cmd_special_create(FILE *stream, } static int -tbd_create_cmd_special_delta(FILE *stream, - tbd_stat_t *a, - tbd_stat_t *b) +tbd_create_cmd_special_delta(int stream, + struct tbd_stat *a, + struct tbd_stat *b) { uint16_t metadata_mask = tbd_metadata_mask(a, b); if(a->rdev != b->rdev) @@ -610,8 +606,8 @@ tbd_create_cmd_special_delta(FILE *stream, } static int -tbd_create_cmd_socket_create(FILE *stream, - tbd_stat_t *nod) +tbd_create_cmd_socket_create(int stream, + struct tbd_stat *nod) { (void)stream; (void)nod; @@ -619,9 +615,9 @@ tbd_create_cmd_socket_create(FILE *stream, } static int -tbd_create_cmd_socket_delta(FILE *stream, - tbd_stat_t *a, - tbd_stat_t *b) +tbd_create_cmd_socket_delta(int stream, + struct tbd_stat *a, + struct tbd_stat *b) { (void)stream; (void)a; @@ -630,8 +626,8 @@ tbd_create_cmd_socket_delta(FILE *stream, } static int -tbd_create_dir(FILE *stream, - tbd_stat_t *d) +tbd_create_dir(int stream, + struct tbd_stat *d) { int err; if(((err =tbd_create_cmd_dir_create(stream, d)) != 0) || @@ -640,7 +636,7 @@ tbd_create_dir(FILE *stream, uintptr_t i; for(i = 0; i < d->size; i++) { - tbd_stat_t *f = tbd_stat_entry(d, i); + struct tbd_stat *f = tbd_stat_entry(d, i); if(f == NULL) return TBD_ERROR(TBD_ERROR_UNABLE_TO_STAT_FILE); @@ -675,9 +671,9 @@ tbd_create_dir(FILE *stream, } static int -tbd_create_impl(FILE *stream, - tbd_stat_t *a, - tbd_stat_t *b, +tbd_create_impl(int stream, + struct tbd_stat *a, + struct tbd_stat *b, bool top) { if((a == NULL) && (b == NULL)) @@ -748,10 +744,10 @@ tbd_create_impl(FILE *stream, /* Handle changes/additions. */ uintptr_t i; for(i = 0; i < b->size; i++) { - tbd_stat_t *_b = tbd_stat_entry(b, i); + struct tbd_stat *_b = tbd_stat_entry(b, i); if(_b == NULL) return TBD_ERROR(TBD_ERROR_UNABLE_TO_STAT_FILE); - tbd_stat_t *_a = tbd_stat_entry_find(a, _b->name); + struct tbd_stat *_a = tbd_stat_entry_find(a, _b->name); err = tbd_create_impl(stream, _a, _b, false); tbd_stat_free(_a); tbd_stat_free(_b); @@ -763,10 +759,10 @@ tbd_create_impl(FILE *stream, for(i = 0; i < a->size; i++) { err = 0; - tbd_stat_t *_a = tbd_stat_entry(a, i); + struct tbd_stat *_a = tbd_stat_entry(a, i); if(_a == NULL) return TBD_ERROR(TBD_ERROR_UNABLE_TO_STAT_FILE); - tbd_stat_t *_b = tbd_stat_entry_find(b, _a->name); + struct tbd_stat *_b = tbd_stat_entry_find(b, _a->name); if (_b == NULL) err = tbd_create_cmd_entity_delete(stream, _a->name); @@ -786,12 +782,12 @@ tbd_create_impl(FILE *stream, } int -tbd_create(FILE *stream, - tbd_stat_t *a, - tbd_stat_t *b) +tbd_create(int stream, + struct tbd_stat *a, + struct tbd_stat *b) { int err; - if((stream == NULL) || (a == NULL) || (b == NULL)) + if((stream < 0) || (a == NULL) || (b == NULL)) return TBD_ERROR(TBD_ERROR_NULL_POINTER); if((err = tbd_create_cmd_ident(stream)) != 0 || diff --git a/tbdiff/tbdiff-io.c b/tbdiff/tbdiff-io.c index c33364b..8ad0235 100644 --- a/tbdiff/tbdiff-io.c +++ b/tbdiff/tbdiff-io.c @@ -15,11 +15,12 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include <stdbool.h> #include <endian.h> #include <unistd.h> #include <assert.h> -#include <tbdiff/tbdiff-stat.h> +#include "tbdiff-stat.h" #if __STDC_VERSION__ >= 199901L #define RESTRICT restrict @@ -52,89 +53,104 @@ static inline void endianswap(void* value, size_t size) #define ENDIANSWAP(v) #endif -size_t tbd_write_uint16(uint16_t value, FILE *stream) { +bool tbd_write_uint16(uint16_t value, int stream) { ENDIANSWAP(&value); - return fwrite(&value, sizeof(value), 1, stream); + return (write(stream, &value, sizeof(value)) == sizeof(value)); } -size_t tbd_write_uint32(uint32_t value, FILE *stream) { +bool tbd_write_uint32(uint32_t value, int stream) { ENDIANSWAP(&value); - return fwrite(&value, sizeof(value), 1, stream); + return (write(stream, &value, sizeof(value)) == sizeof(value)); } -size_t tbd_write_uint64(uint64_t value, FILE *stream) { +bool tbd_write_uint64(uint64_t value, int stream) { ENDIANSWAP(&value); - return fwrite(&value, sizeof(value), 1, stream); + return (write(stream, &value, sizeof(value)) == sizeof(value)); } -size_t tbd_write_time(time_t value, FILE *stream) { +bool tbd_write_time(time_t value, int stream) { uint64_t realv = value; ENDIANSWAP(&realv); - return fwrite(&realv, sizeof(realv), 1, stream); + return (write(stream, &realv, sizeof(realv)) == sizeof(value)); } -size_t tbd_write_mode(mode_t value, FILE *stream) { +bool tbd_write_mode(mode_t value, int stream) { ENDIANSWAP(&value); - return fwrite(&value, sizeof(value), 1, stream); + return (write(stream, &value, sizeof(value)) == sizeof(value)); } -size_t tbd_write_uid(uid_t value, FILE *stream) { +bool tbd_write_uid(uid_t value, int stream) { ENDIANSWAP(&value); - return fwrite(&value, sizeof(value), 1, stream); + return (write(stream, &value, sizeof(value)) == sizeof(value)); } -size_t tbd_write_gid(gid_t value, FILE *stream) { +bool tbd_write_gid(gid_t value, int stream) { ENDIANSWAP(&value); - return fwrite(&value, sizeof(value), 1, stream); + return (write(stream, &value, sizeof(value)) == sizeof(value)); } -size_t tbd_read_uint16(uint16_t *value, FILE *stream) { + +bool tbd_read_uint16(uint16_t *value, int stream) { assert(value != NULL); - size_t rval = fread(value, sizeof(*value), 1, stream); + size_t rval = read(stream, value, sizeof(*value)); + if (rval != sizeof(*value)) + return false; ENDIANSWAP(value); - return rval; + return true; } -size_t tbd_read_uint32(uint32_t *value, FILE *stream) { +bool tbd_read_uint32(uint32_t *value, int stream) { assert(value != NULL); - size_t rval = fread(value, sizeof(*value), 1, stream); + size_t rval = read(stream, value, sizeof(*value)); + if (rval != sizeof(*value)) + return false; ENDIANSWAP(value); - return rval; + return true; } -size_t tbd_read_uint64(uint64_t *value, FILE *stream) { +bool tbd_read_uint64(uint64_t *value, int stream) { assert(value != NULL); - size_t rval = fread(value, sizeof(*value), 1, stream); + size_t rval = read(stream, value, sizeof(*value)); + if (rval != sizeof(*value)) + return false; ENDIANSWAP(value); - return rval; + return true; } -size_t tbd_read_time(time_t *value, FILE *stream) { +bool tbd_read_time(time_t *value, int stream) { assert(value != NULL); uint64_t realv; - size_t rval = fread(&realv, sizeof(realv), 1, stream); + size_t rval = read(stream, &realv, sizeof(realv)); + if (rval != sizeof(*value)) + return false; ENDIANSWAP(&realv); *value = realv; - return rval; + return true; } -size_t tbd_read_mode(mode_t *value, FILE *stream) { +bool tbd_read_mode(mode_t *value, int stream) { assert(value != NULL); - size_t rval = fread(value, sizeof(*value), 1, stream); + ssize_t rval = read(stream, value, sizeof(*value)); + if (rval != sizeof(*value)) + return false; ENDIANSWAP(value); - return rval; + return true; } -size_t tbd_read_uid(uid_t *value, FILE *stream) { +bool tbd_read_uid(uid_t *value, int stream) { assert(value != NULL); - size_t rval = fread(value, sizeof(*value), 1, stream); + size_t rval = read(stream, value, sizeof(*value)); + if (rval != sizeof(*value)) + return false; ENDIANSWAP(value); - return rval; + return true; } -size_t tbd_read_gid(gid_t *value, FILE *stream) { +bool tbd_read_gid(gid_t *value, int stream) { assert(value != NULL); - size_t rval = fread(value, sizeof(*value), 1, stream); + size_t rval = read(stream, value, sizeof(*value)); + if (rval != sizeof(*value)) + return false; ENDIANSWAP(value); - return rval; + return true; } diff --git a/tbdiff/tbdiff-io.h b/tbdiff/tbdiff-io.h index 585f670..f5bb5e5 100644 --- a/tbdiff/tbdiff-io.h +++ b/tbdiff/tbdiff-io.h @@ -22,26 +22,27 @@ #ifndef __TBDIFF_IO_H__ #define __TBDIFF_IO_H__ +#include <stdbool.h> #include <endian.h> #include <unistd.h> #include <assert.h> #include <tbdiff/tbdiff-stat.h> -extern size_t tbd_write_uint16(uint16_t value, FILE* stream); -extern size_t tbd_write_uint32(uint32_t value, FILE* stream); -extern size_t tbd_write_uint64(uint64_t value, FILE* stream); -extern size_t tbd_write_time(time_t value, FILE* stream); -extern size_t tbd_write_mode(mode_t value, FILE* stream); -extern size_t tbd_write_uid(uid_t value, FILE* stream); -extern size_t tbd_write_gid(gid_t value, FILE* stream); +bool tbd_write_uint16(uint16_t value, int stream); +bool tbd_write_uint32(uint32_t value, int stream); +bool tbd_write_uint64(uint64_t value, int stream); +bool tbd_write_time (time_t value, int stream); +bool tbd_write_mode (mode_t value, int stream); +bool tbd_write_uid (uid_t value, int stream); +bool tbd_write_gid (gid_t value, int stream); -extern size_t tbd_read_uint16(uint16_t *value, FILE* stream); -extern size_t tbd_read_uint32(uint32_t *value, FILE* stream); -extern size_t tbd_read_uint64(uint64_t *value, FILE* stream); -extern size_t tbd_read_time(time_t *value, FILE* stream); -extern size_t tbd_read_mode(mode_t *value, FILE* stream); -extern size_t tbd_read_uid(uid_t *value, FILE* stream); -extern size_t tbd_read_gid(gid_t *value, FILE* stream); +bool tbd_read_uint16(uint16_t *value, int stream); +bool tbd_read_uint32(uint32_t *value, int stream); +bool tbd_read_uint64(uint64_t *value, int stream); +bool tbd_read_time (time_t *value, int stream); +bool tbd_read_mode (mode_t *value, int stream); +bool tbd_read_uid (uid_t *value, int stream); +bool tbd_read_gid (gid_t *value, int stream); #endif /* !__TBDIFF_IO_H__ */ diff --git a/tbdiff/tbdiff-stat.c b/tbdiff/tbdiff-stat.c index 66e2caf..4d20481 100644 --- a/tbdiff/tbdiff-stat.c +++ b/tbdiff/tbdiff-stat.c @@ -27,9 +27,9 @@ #include <errno.h> #include <fcntl.h> -#include <tbdiff/tbdiff-stat.h> +#include "tbdiff-stat.h" -static tbd_stat_t* +static struct tbd_stat* tbd_stat_from_path(const char *name, const char *path) { @@ -39,13 +39,15 @@ tbd_stat_from_path(const char *name, return NULL; size_t nlen = strlen(name); - tbd_stat_t *ret = (tbd_stat_t*)malloc(sizeof(tbd_stat_t) + (nlen + 1)); + struct tbd_stat *ret = (struct tbd_stat*)malloc( + sizeof(struct tbd_stat) + (nlen + 1)); if(ret == NULL) return NULL; ret->parent = NULL; ret->size = 0; - ret->name = (char*)((uintptr_t)ret + sizeof(tbd_stat_t)); + ret->name = (char*)((uintptr_t)ret + + sizeof(struct tbd_stat)); memcpy(ret->name, name, (nlen + 1)); if(S_ISREG(info.st_mode)) { @@ -93,27 +95,21 @@ tbd_stat_from_path(const char *name, return ret; } -tbd_stat_t* +struct tbd_stat* tbd_stat(const char *path) { - tbd_stat_t *ret = tbd_stat_from_path(path, path); + struct tbd_stat *ret = tbd_stat_from_path(path, path); return ret; } void -tbd_stat_free(tbd_stat_t *file) +tbd_stat_free(struct tbd_stat *file) { free(file); } -void -tbd_stat_print(tbd_stat_t *file) -{ - (void)file; -} - -tbd_stat_t* -tbd_stat_entry(tbd_stat_t *file, uint32_t entry) +struct tbd_stat* +tbd_stat_entry(struct tbd_stat *file, uint32_t entry) { if((file == NULL) || (file->type != TBD_STAT_TYPE_DIR) @@ -140,16 +136,17 @@ tbd_stat_entry(tbd_stat_t *file, uint32_t entry) (strcmp(ds->d_name, "..") == 0)) i--; } - char *name = strndup(ds->d_name, ds->d_reclen-offsetof(struct dirent, d_name)); + char *name = strndup(ds->d_name, + ds->d_reclen-offsetof(struct dirent, d_name)); closedir (dp); - char *spath = tbd_stat_subpath(file, name); + char *spath = tbd_statubpath(file, name); if(spath == NULL) { free(name); return NULL; } - tbd_stat_t *ret = tbd_stat_from_path(name, (const char*)spath); + struct tbd_stat *ret = tbd_stat_from_path(name, (const char*)spath); free(name); free(spath); @@ -161,9 +158,9 @@ tbd_stat_entry(tbd_stat_t *file, uint32_t entry) return ret; } -tbd_stat_t* -tbd_stat_entry_find(tbd_stat_t *file, - const char *name) +struct tbd_stat* +tbd_stat_entry_find(struct tbd_stat *file, + const char *name) { if((file == NULL) || (file->type != TBD_STAT_TYPE_DIR)) @@ -179,14 +176,15 @@ tbd_stat_entry_find(tbd_stat_t *file, struct dirent *ds; for(ds = readdir(dp); ds != NULL; ds = readdir(dp)) { if(strcmp(ds->d_name, name) == 0) { - char *spath = tbd_stat_subpath(file, ds->d_name); + char *spath = tbd_statubpath(file, ds->d_name); if(spath == NULL) { closedir (dp); return NULL; } - tbd_stat_t *ret = tbd_stat_from_path(ds->d_name, (const char*)spath); + struct tbd_stat *ret = tbd_stat_from_path(ds->d_name, + (const char*)spath); free(spath); ret->parent = file; @@ -200,8 +198,8 @@ tbd_stat_entry_find(tbd_stat_t *file, } char* -tbd_stat_subpath(tbd_stat_t *file, - const char *entry) +tbd_statubpath(struct tbd_stat *file, + const char *entry) { if(file == NULL) return NULL; @@ -209,10 +207,11 @@ tbd_stat_subpath(tbd_stat_t *file, size_t elen = ((entry == NULL) ? 0 : (strlen(entry) + 1)); size_t plen; - tbd_stat_t *root; + struct tbd_stat *root; for(root = file, plen = 0; root != NULL; - plen += (strlen(root->name) + 1), root = (tbd_stat_t*)root->parent); + plen += (strlen(root->name) + 1), + root = (struct tbd_stat*)root->parent); plen += elen; @@ -226,7 +225,7 @@ tbd_stat_subpath(tbd_stat_t *file, memcpy(ptr, entry, elen); } - for(root = file; root != NULL; root = (tbd_stat_t*)root->parent) { + for(root = file; root != NULL; root = (struct tbd_stat*)root->parent) { size_t rlen = strlen(root->name) + 1; ptr = (char*)((uintptr_t)ptr - rlen); memcpy(ptr, root->name, rlen); @@ -238,13 +237,13 @@ tbd_stat_subpath(tbd_stat_t *file, } char* -tbd_stat_path(tbd_stat_t *file) +tbd_stat_path(struct tbd_stat *file) { - return tbd_stat_subpath(file, NULL); + return tbd_statubpath(file, NULL); } int -tbd_stat_open(tbd_stat_t *file, int flags) +tbd_stat_open(struct tbd_stat *file, int flags) { char *path = tbd_stat_path(file); if(path == NULL) @@ -253,15 +252,3 @@ tbd_stat_open(tbd_stat_t *file, int flags) free(path); return fd; } - -FILE* -tbd_stat_fopen(tbd_stat_t *file, - const char *mode) -{ - char *path = tbd_stat_path(file); - if(path == NULL) - return NULL; - FILE *fp = fopen(path, mode); - free(path); - return fp; -} diff --git a/tbdiff/tbdiff-stat.h b/tbdiff/tbdiff-stat.h index 22da466..5f9af83 100644 --- a/tbdiff/tbdiff-stat.h +++ b/tbdiff/tbdiff-stat.h @@ -27,7 +27,7 @@ #include <stdbool.h> #include <sys/types.h> -typedef enum { +enum tbd_stat_type { TBD_STAT_TYPE_FILE = 'f', TBD_STAT_TYPE_DIR = 'd', TBD_STAT_TYPE_SYMLINK = 'l', @@ -35,30 +35,26 @@ typedef enum { TBD_STAT_TYPE_BLKDEV = 'b', TBD_STAT_TYPE_FIFO = 'p', TBD_STAT_TYPE_SOCKET = 's' -} tbd_stat_type_e; - -typedef struct tbd_stat_s tbd_stat_t; +}; -struct tbd_stat_s { - tbd_stat_t* parent; - char* name; - tbd_stat_type_e type; - time_t mtime; - uint32_t size; /* Count for directory. */ - uid_t uid; - gid_t gid; - mode_t mode; - uint32_t rdev; +struct tbd_stat { + struct tbd_stat* parent; + char* name; + enum tbd_stat_type type; + time_t mtime; + uint32_t size; /* Count for directory. */ + uid_t uid; + gid_t gid; + mode_t mode; + uint32_t rdev; }; -extern tbd_stat_t* tbd_stat(const char *path); -extern void tbd_stat_free(tbd_stat_t *file); -extern void tbd_stat_print(tbd_stat_t *file); -extern tbd_stat_t* tbd_stat_entry(tbd_stat_t *file, uint32_t entry); -extern tbd_stat_t* tbd_stat_entry_find(tbd_stat_t *file, const char *name); -extern char* tbd_stat_subpath(tbd_stat_t *file, const char *entry); -extern char* tbd_stat_path(tbd_stat_t *file); -extern int tbd_stat_open(tbd_stat_t *file, int flags); -extern FILE* tbd_stat_fopen(tbd_stat_t *file, const char *mode); +struct tbd_stat* tbd_stat(const char *path); +void tbd_stat_free(struct tbd_stat *file); +struct tbd_stat* tbd_stat_entry(struct tbd_stat *file, uint32_t entry); +struct tbd_stat* tbd_stat_entry_find(struct tbd_stat *file, const char *name); +char* tbd_statubpath(struct tbd_stat *file, const char *entry); +char* tbd_stat_path(struct tbd_stat *file); +int tbd_stat_open(struct tbd_stat *file, int flags); #endif /* !__TBDIFF_STAT_H__ */ diff --git a/tbdiff/tbdiff-xattrs.c b/tbdiff/tbdiff-xattrs.c index c020ee5..5150199 100644 --- a/tbdiff/tbdiff-xattrs.c +++ b/tbdiff/tbdiff-xattrs.c @@ -28,10 +28,10 @@ #include <errno.h> -#include <tbdiff/tbdiff-common.h> -#include <tbdiff/tbdiff-xattrs.h> +#include "tbdiff-common.h" +#include "tbdiff-xattrs.h" -int tbd_xattrs_names(char const *path, tbd_xattrs_names_t *names) +int tbd_xattrs_names(char const *path, struct tbd_xattrs_names *names) { char *attrnames = NULL; /* get size of names list */ @@ -85,12 +85,12 @@ int tbd_xattrs_names(char const *path, tbd_xattrs_names_t *names) return TBD_ERROR_SUCCESS; } -void tbd_xattrs_names_free(tbd_xattrs_names_t *names) +void tbd_xattrs_names_free(struct tbd_xattrs_names *names) { free((void *)names->begin); } -int tbd_xattrs_names_each(tbd_xattrs_names_t const *names, +int tbd_xattrs_names_each(struct tbd_xattrs_names const *names, int (*f)(char const *name, void *ud), void *ud) { char const *name; @@ -110,7 +110,8 @@ static int names_sum(char const *name, void *ud) { (*((uint32_t*)ud))++; return TBD_ERROR_SUCCESS; } -int tbd_xattrs_names_count(tbd_xattrs_names_t const *names, uint32_t *count) { +int tbd_xattrs_names_count(struct tbd_xattrs_names const *names, + uint32_t *count) { uint32_t _count = 0; int err; if ((err = tbd_xattrs_names_each(names, &names_sum, &_count)) == @@ -137,7 +138,7 @@ static int name_remove(char const *name, void *ud) { int tbd_xattrs_removeall(char const *path) { int err = TBD_ERROR_SUCCESS; - tbd_xattrs_names_t list; + struct tbd_xattrs_names list; /* get the list of attributes */ if ((err = tbd_xattrs_names(path, &list)) != TBD_ERROR_SUCCESS) { @@ -202,16 +203,16 @@ int tbd_xattrs_get(char const *path, char const* name, void **buf, return TBD_ERROR_SUCCESS; } -typedef struct { +struct tbd_xattrs_pairs_params { char const *path; - tbd_xattrs_pairs_callback_t f; + int (*f)(char const *, void const *, size_t, void *); void *pairs_ud; void *data; size_t data_size; -} tbd_xattrs_pairs_params_t; +}; static int call_with_data(char const *name, void *ud) { - tbd_xattrs_pairs_params_t *params; + struct tbd_xattrs_pairs_params *params; params = ud; size_t value_size; int err; @@ -222,10 +223,11 @@ static int call_with_data(char const *name, void *ud) } return params->f(name, params->data, value_size, params->pairs_ud); } -int tbd_xattrs_pairs(tbd_xattrs_names_t const *names, char const *path, - tbd_xattrs_pairs_callback_t f, void *ud) +int tbd_xattrs_pairs(struct tbd_xattrs_names const *names, char const *path, + int (*f)(char const *, void const *, size_t, void *), + void *ud) { - tbd_xattrs_pairs_params_t params = { + struct tbd_xattrs_pairs_params params = { path, f, ud, NULL, 0, }; int err = tbd_xattrs_names_each(names, &call_with_data, ¶ms); diff --git a/tbdiff/tbdiff-xattrs.h b/tbdiff/tbdiff-xattrs.h index ba4a79e..c263e18 100644 --- a/tbdiff/tbdiff-xattrs.h +++ b/tbdiff/tbdiff-xattrs.h @@ -26,39 +26,38 @@ #include <stdint.h> /* structure for names data */ -typedef struct tbd_xattrs_names { +struct tbd_xattrs_names { char const *begin; char const *end; -} tbd_xattrs_names_t; +}; /* gets a list of the names of the file referenced by path */ -extern int tbd_xattrs_names(char const *path, tbd_xattrs_names_t *names_out); +int tbd_xattrs_names(char const *path, struct tbd_xattrs_names *names_out); /* frees up the INTERNAL resources of the list, doesn't free the list itself */ -extern void tbd_xattrs_names_free(tbd_xattrs_names_t *names); +void tbd_xattrs_names_free(struct tbd_xattrs_names *names); /* calls f with every name in the list */ -extern int tbd_xattrs_names_each(tbd_xattrs_names_t const *names, +int tbd_xattrs_names_each(struct tbd_xattrs_names const *names, int (*f)(char const *name, void *ud), void *ud); /* gets how many different attributes there are in the list */ -extern int tbd_xattrs_names_count(tbd_xattrs_names_t const *names, uint32_t *count); +int tbd_xattrs_names_count(struct tbd_xattrs_names 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 * if *buf was too small it will be reallocated * if it is successful, *buf will contain *valsize bytes of data */ -extern int tbd_xattrs_get(char const *path, char const* name, void **buf, +int tbd_xattrs_get(char const *path, char const* name, void **buf, size_t *bufsize, size_t *valsize); /* removes all attributes of the file referenced by path */ -extern int tbd_xattrs_removeall(char const *path); +int tbd_xattrs_removeall(char const *path); /* calls f for every attribute:value pair in the list */ -typedef int (*tbd_xattrs_pairs_callback_t)(char const *name, void const *data, - size_t size, void *ud); -extern int tbd_xattrs_pairs(tbd_xattrs_names_t const *names, char const *path, - tbd_xattrs_pairs_callback_t f, void *ud); +int tbd_xattrs_pairs(struct tbd_xattrs_names const *names, char const *path, + int (*f)(char const *, void const *, size_t, void *), + void *ud); #endif /* !__TBDIFF_XATTRS_H__ */ |