Make "write_sha1_file()" exit early if the file already exists.
Avoid the compression.
This commit is contained in:
26
sha1_file.c
26
sha1_file.c
@ -173,12 +173,30 @@ int write_sha1_file(char *buf, unsigned len, unsigned char *returnsha1)
|
|||||||
z_stream stream;
|
z_stream stream;
|
||||||
unsigned char sha1[20];
|
unsigned char sha1[20];
|
||||||
SHA_CTX c;
|
SHA_CTX c;
|
||||||
|
char *filename;
|
||||||
|
int fd;
|
||||||
|
|
||||||
/* Sha1.. */
|
/* Sha1.. */
|
||||||
SHA1_Init(&c);
|
SHA1_Init(&c);
|
||||||
SHA1_Update(&c, buf, len);
|
SHA1_Update(&c, buf, len);
|
||||||
SHA1_Final(sha1, &c);
|
SHA1_Final(sha1, &c);
|
||||||
|
|
||||||
|
if (returnsha1)
|
||||||
|
memcpy(returnsha1, sha1, 20);
|
||||||
|
|
||||||
|
filename = sha1_file_name(sha1);
|
||||||
|
fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
|
||||||
|
if (fd < 0) {
|
||||||
|
if (errno != EEXIST)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* We might do collision checking here, but we'd need to
|
||||||
|
* uncompress the old file and check it. Later.
|
||||||
|
*/
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* Set it up */
|
/* Set it up */
|
||||||
memset(&stream, 0, sizeof(stream));
|
memset(&stream, 0, sizeof(stream));
|
||||||
deflateInit(&stream, Z_BEST_COMPRESSION);
|
deflateInit(&stream, Z_BEST_COMPRESSION);
|
||||||
@ -195,10 +213,10 @@ int write_sha1_file(char *buf, unsigned len, unsigned char *returnsha1)
|
|||||||
deflateEnd(&stream);
|
deflateEnd(&stream);
|
||||||
size = stream.total_out;
|
size = stream.total_out;
|
||||||
|
|
||||||
if (write_sha1_buffer(sha1, compressed, size) < 0)
|
if (write(fd, compressed, size) != size)
|
||||||
return -1;
|
die("unable to write file");
|
||||||
if (returnsha1)
|
close(fd);
|
||||||
memcpy(returnsha1, sha1, 20);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user