Skip to content

Commit

Permalink
add valid_length to fix handling of !zs->filelen on 0 byte files
Browse files Browse the repository at this point in the history
  • Loading branch information
eam authored and gianm committed Jan 17, 2012
1 parent ed3906a commit 59e7f00
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
13 changes: 13 additions & 0 deletions c/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,19 @@ int main(int argc, char **argv) {
strcpy(temp_file, filename);
strcat(temp_file, ".part");

/* Bail out for 0 length files */
if (! zsync_filelen(zs)) {
FILE *f = fopen(filename, "w");
if (!f) {
perror("open");
fprintf(stderr, "Could not open %s for writing zero byte file\n", filename);
exit(1);
}
mtime = zsync_mtime(zs);
if (mtime != -1) set_mtime(filename, mtime);
return 0;
}

{ /* STEP 2: read available local data and fill in what we know in the
*target file */
int i;
Expand Down
20 changes: 18 additions & 2 deletions c/libzsync/zsync.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
#include <ctype.h>
#include <time.h>

#include <errno.h>

#include <arpa/inet.h>

#ifdef WITH_DMALLOC
Expand Down Expand Up @@ -139,6 +141,7 @@ static char **append_ptrlist(int *n, char **p, char *a) {

/* Constructor */
struct zsync_state *zsync_begin(FILE * f) {
int valid_length=0;
/* Defaults for the checksum bytes and sequential matches properties of the
* rcksum_state. These are the defaults from versions of zsync before these
* were variable. */
Expand Down Expand Up @@ -194,7 +197,11 @@ struct zsync_state *zsync_begin(FILE * f) {
}
}
else if (!strcmp(buf, "Length")) {
zs->filelen = atol(p);
errno = 0;
zs->filelen = strtol(p, 0, 10);
if (errno == 0) {
valid_length = 1;
}
}
else if (!strcmp(buf, "Filename")) {
zs->filename = strdup(p);
Expand Down Expand Up @@ -303,7 +310,12 @@ struct zsync_state *zsync_begin(FILE * f) {
return NULL;
}
}
if (!zs->filelen || !zs->blocksize) {
/* zero length file, skip blocksums */
if (valid_length && !zs->filelen) {
return zs;
}
/* cannot test !zs->filelen or we will fail on valid 0 length files */
if (!valid_length || !zs->blocksize) {
fprintf(stderr, "Not a zsync file (looked for Blocksize and Length lines)\n");
free(zs);
return NULL;
Expand Down Expand Up @@ -391,6 +403,10 @@ char *zsync_filename(const struct zsync_state *zs) {
return strdup(zs->gzhead && zs->zfilename ? zs->zfilename : zs->filename);
}

off_t zsync_filelen(const struct zsync_state *zs) {
return zs->filelen;
}

/* time_t = zsync_mtime(self)
* Returns the mtime on the original copy of the target; for the client program
* to set the mtime of the local file to match, if it so chooses.
Expand Down

0 comments on commit 59e7f00

Please sign in to comment.