diff options
author | Alberto Ruiz <alberto.ruiz@codethink.co.uk> | 2011-09-28 13:39:02 +0100 |
---|---|---|
committer | Alberto Ruiz <alberto.ruiz@codethink.co.uk> | 2011-09-28 13:39:02 +0100 |
commit | 29d1911984f7b1b024173ead1fd3b1dfb10c80d0 (patch) | |
tree | 732cf1d2a96dd1535a5f7f1d142eef152d02424c | |
parent | e026d3decb04844e341bd4f94b80f2e77d8ea383 (diff) | |
download | tbdiff-29d1911984f7b1b024173ead1fd3b1dfb10c80d0.tar.gz |
Taking metadata into account in file deltas. Code cleanups
-rw-r--r-- | otap.h | 27 | ||||
-rw-r--r-- | otap_apply.c | 30 | ||||
-rw-r--r-- | otap_create.c | 128 | ||||
-rw-r--r-- | stat.c | 10 |
4 files changed, 103 insertions, 92 deletions
@@ -8,19 +8,20 @@ typedef enum { - OTAP_CMD_IDENTIFY = 0x00, - OTAP_CMD_UPDATE = 0x01, - OTAP_CMD_DIR_CREATE = 0x10, - OTAP_CMD_DIR_ENTER = 0x11, - OTAP_CMD_DIR_LEAVE = 0x12, - OTAP_CMD_DIR_DELTA = 0x13, - OTAP_CMD_FILE_CREATE = 0x20, - OTAP_CMD_FILE_DELTA = 0x21, - OTAP_CMD_ENTITY_MOVE = 0x30, - OTAP_CMD_ENTITY_COPY = 0x31, - OTAP_CMD_ENTITY_DELETE = 0x32, - OTAP_CMD_SYMLINK_CREATE = 0x40, - OTAP_CMD_SPECIAL_CREATE = 0x50 + OTAP_CMD_IDENTIFY = 0x00, + OTAP_CMD_UPDATE = 0x01, + OTAP_CMD_DIR_CREATE = 0x10, + OTAP_CMD_DIR_ENTER = 0x11, + OTAP_CMD_DIR_LEAVE = 0x12, + OTAP_CMD_DIR_DELTA = 0x13, + OTAP_CMD_FILE_CREATE = 0x20, + OTAP_CMD_FILE_DELTA = 0x21, + OTAP_CMD_FILE_METADATA_UPDATE = 0x22, + OTAP_CMD_ENTITY_MOVE = 0x30, + OTAP_CMD_ENTITY_COPY = 0x31, + OTAP_CMD_ENTITY_DELETE = 0x32, + OTAP_CMD_SYMLINK_CREATE = 0x40, + OTAP_CMD_SPECIAL_CREATE = 0x50 } otap_cmd_e; typedef enum diff --git a/otap_apply.c b/otap_apply.c index b47526e..8d1fd67 100644 --- a/otap_apply.c +++ b/otap_apply.c @@ -414,30 +414,12 @@ _otap_apply_cmd_special_create(FILE *stream) uint32_t gid; uint32_t dev; - if(name == NULL) - return OTAP_ERROR_UNABLE_TO_READ_STREAM; - - if(fread(&mtime, sizeof(uint32_t), 1, stream) != 1) - { - free(name); - otap_error(OTAP_ERROR_UNABLE_TO_READ_STREAM); - } - if(fread(&mode, sizeof(uint32_t), 1, stream) != 1) - { - free(name); - otap_error(OTAP_ERROR_UNABLE_TO_READ_STREAM); - } - if(fread(&uid, sizeof(uint32_t), 1, stream) != 1) - { - free(name); - otap_error(OTAP_ERROR_UNABLE_TO_READ_STREAM); - } - if(fread(&gid, sizeof(uint32_t), 1, stream) != 1) - { - free(name); - otap_error(OTAP_ERROR_UNABLE_TO_READ_STREAM); - } - if(fread(&dev, sizeof(uint32_t), 1, stream) != 1) + if(name == NULL || + fread(&mtime, sizeof(uint32_t), 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) { free(name); otap_error(OTAP_ERROR_UNABLE_TO_READ_STREAM); diff --git a/otap_create.c b/otap_create.c index 29ac777..6562096 100644 --- a/otap_create.c +++ b/otap_create.c @@ -34,6 +34,15 @@ _otap_create_fwrite_string(FILE *stream, } static int +_otap_create_fwrite_mdata_mask (FILE *stream, + uint16_t mask) +{ + if(fwrite(&mask, sizeof(uint16_t), 1, stream) != 1) + otap_error(OTAP_ERROR_UNABLE_TO_WRITE_STREAM); + return 0; +} + +static int _otap_create_fwrite_mtime (FILE *stream, uint32_t mtime) { @@ -100,17 +109,12 @@ static int _otap_create_cmd_file_create (FILE* stream, otap_stat_t* f) { int err; - if((err = _otap_create_fwrite_cmd(stream, OTAP_CMD_FILE_CREATE)) != 0) - return err; - if((err = _otap_create_fwrite_string(stream, f->name)) != 0) - return err; - if((err = _otap_create_fwrite_mtime(stream, f->mtime)) != 0) - return err; - if((err = _otap_create_fwrite_mode(stream, f->mode)) != 0) - return err; - if((err = _otap_create_fwrite_uid(stream, f->uid)) != 0) - return err; - if((err = _otap_create_fwrite_gid(stream, f->gid)) != 0) + if((err = _otap_create_fwrite_cmd(stream, OTAP_CMD_FILE_CREATE)) != 0 || + (err = _otap_create_fwrite_string(stream, f->name)) != 0 || + (err = _otap_create_fwrite_mtime (stream, f->mtime)) != 0 || + (err = _otap_create_fwrite_mode (stream, f->mode)) != 0 || + (err = _otap_create_fwrite_uid (stream, f->uid)) != 0 || + (err = _otap_create_fwrite_gid (stream, f->gid)) != 0) return err; uint32_t size = f->size; @@ -137,6 +141,47 @@ _otap_create_cmd_file_create (FILE* stream, otap_stat_t* f) return 0; } +static uint16_t +_otap_metadata_mask (otap_stat_t *a, + otap_stat_t *b) +{ + uint16_t metadata_mask = OTAP_METADATA_NONE; + + /* If nothing changes we issue no command */ + if (a->mtime == b->mtime) + metadata_mask |= OTAP_METADATA_MTIME; + if (a->uid == b->uid) + metadata_mask |= OTAP_METADATA_UID; + if (a->gid == b->gid) + metadata_mask |= OTAP_METADATA_GID; + if (a->mode == b->mode) + metadata_mask |= OTAP_METADATA_MODE; + + return metadata_mask; +} + +static int +_otap_create_cmd_file_metadata_update (FILE *stream, + otap_stat_t *a, + otap_stat_t *b) +{ + int err; + uint16_t metadata_mask = _otap_metadata_mask (a, b); + + if (metadata_mask == OTAP_METADATA_NONE) + return 0; + /* TODO: Optimize protocol by only sending useful metadata */ + if ((err = _otap_create_fwrite_cmd(stream, OTAP_CMD_FILE_METADATA_UPDATE)) != 0 || + (err = _otap_create_fwrite_mdata_mask (stream, metadata_mask)) != 0 || + (err = _otap_create_fwrite_mtime (stream, b->mtime)) != 0 || + (err = _otap_create_fwrite_uid (stream, b->uid)) != 0 || + (err = _otap_create_fwrite_gid (stream, b->gid)) != 0 || + (err = _otap_create_fwrite_mode (stream, b->mode)) != 0) + return err; + + return _otap_create_fwrite_string(stream, b->name); +} + static int _otap_create_cmd_file_delta(FILE *stream, otap_stat_t *a, @@ -245,24 +290,32 @@ _otap_create_cmd_file_delta(FILE *stream, end = start; uint32_t size = flenb - ((flena - end) + start); //(flenb - (o + start)); - + if((end == start) && (size == 0)) { + _otap_create_cmd_file_metadata_update (stream, a, b); fclose(fpb); return 0; } + uint16_t metadata_mask = _otap_metadata_mask (a, b); + + /* TODO: Optimize protocol by only sending useful metadata */ int err; - if(((err = _otap_create_fwrite_cmd(stream, OTAP_CMD_FILE_DELTA)) != 0) - || ((err = _otap_create_fwrite_string(stream, b->name)) != 0) - || ((err = _otap_create_fwrite_mtime(stream, b->mtime)) != 0)) + if(((err = _otap_create_fwrite_cmd(stream, OTAP_CMD_FILE_DELTA)) != 0) || + ((err = _otap_create_fwrite_string(stream, b->name)) != 0) || + ((err = _otap_create_fwrite_mdata_mask(stream, metadata_mask)) != 0) || + ((err = _otap_create_fwrite_mtime (stream, b->mtime)) != 0) || + ((err = _otap_create_fwrite_uid (stream, b->uid)) != 0) || + ((err = _otap_create_fwrite_gid (stream, b->gid)) != 0) || + ((err = _otap_create_fwrite_mode (stream, b->mode)) != 0)) { fclose(fpb); return err; } - if((fwrite(&start, 4, 1, stream) != 1) - || (fwrite(&end, 4, 1, stream) != 1) - || (fwrite(&size, 4, 1, stream) != 1)) + 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)) { fclose(fpb); otap_error(OTAP_ERROR_UNABLE_TO_WRITE_STREAM); @@ -412,42 +465,17 @@ _otap_create_cmd_dir_delta (FILE *stream, otap_stat_t *b) { int err; - uint16_t metadata_mask = OTAP_METADATA_NONE; + uint16_t metadata_mask = _otap_metadata_mask (a, b); - /* If nothing changes we issue no command */ - if (a->mtime == b->mtime) - metadata_mask |= OTAP_METADATA_MTIME; - if (a->uid == b->uid) - metadata_mask |= OTAP_METADATA_UID; - if (a->gid == b->gid) - metadata_mask |= OTAP_METADATA_GID; - if (a->mode == b->mode) - metadata_mask |= OTAP_METADATA_MODE; - if (metadata_mask == OTAP_METADATA_NONE) return 0; - err = _otap_create_fwrite_cmd(stream, OTAP_CMD_DIR_DELTA); - if (err != 0) - return err; - - if(fwrite(&metadata_mask, sizeof (uint16_t), 1, stream) != 1) - otap_error(OTAP_ERROR_UNABLE_TO_WRITE_STREAM); - - err = _otap_create_fwrite_mtime (stream, b->mtime); - if (err != 0) - return err; - - err = _otap_create_fwrite_uid (stream, b->uid); - if (err != 0) - return err; - - err = _otap_create_fwrite_gid (stream, b->gid); - if (err != 0) - return err; - - err = _otap_create_fwrite_mode (stream, b->mode); - if (err != 0) + if ((err = _otap_create_fwrite_cmd(stream, OTAP_CMD_DIR_DELTA)) != 0 || + (err = _otap_create_fwrite_mdata_mask (stream, metadata_mask)) != 0 || + (err = _otap_create_fwrite_mtime (stream, b->mtime)) != 0 || + (err = _otap_create_fwrite_uid (stream, b->uid)) != 0 || + (err = _otap_create_fwrite_gid (stream, b->gid)) != 0 || + (err = _otap_create_fwrite_mode (stream, b->mode)) != 0) return err; return _otap_create_fwrite_string(stream, b->name); @@ -14,8 +14,8 @@ static otap_stat_t* -__otap_stat_fd(const char *name, - const char *path) +__otap_stat(const char *name, + const char *path) { struct stat info; @@ -102,7 +102,7 @@ __otap_stat_fd(const char *name, otap_stat_t* otap_stat(const char* path) { - otap_stat_t* ret = __otap_stat_fd(path, path); + otap_stat_t* ret = __otap_stat(path, path); return ret; } @@ -150,7 +150,7 @@ otap_stat_entry(otap_stat_t* file, uint32_t entry) if(spath == NULL) return NULL; - otap_stat_t* ret = __otap_stat_fd(ds->d_name, (const char*)spath); + otap_stat_t* ret = __otap_stat(ds->d_name, (const char*)spath); free(spath); @@ -184,7 +184,7 @@ otap_stat_entry_find(otap_stat_t* file, const char* name) if(spath == NULL) return NULL; - otap_stat_t* ret = __otap_stat_fd(ds->d_name, (const char *)spath); + otap_stat_t* ret = __otap_stat(ds->d_name, (const char *)spath); free(spath); ret->parent = file; |