diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-20 09:28:05 -0700 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-20 09:28:05 -0700 |
commit | 706bc531a19f268bb03dda7551929dee20172548 (patch) | |
tree | 5ba25f0b0b6fb25a61426bed1de7b903d4e0f834 /sha1_file.c | |
parent | f18ca7316631914776136455c151d70318299459 (diff) | |
download | git-706bc531a19f268bb03dda7551929dee20172548.tar.gz git-706bc531a19f268bb03dda7551929dee20172548.tar.xz |
Make "write_sha1_file()" exit early if the file already exists.
Avoid the compression.
Diffstat (limited to 'sha1_file.c')
-rw-r--r-- | sha1_file.c | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/sha1_file.c b/sha1_file.c index 40c00b77d..eee3598bb 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -173,12 +173,30 @@ int write_sha1_file(char *buf, unsigned len, unsigned char *returnsha1) z_stream stream; unsigned char sha1[20]; SHA_CTX c; + char *filename; + int fd; /* Sha1.. */ SHA1_Init(&c); SHA1_Update(&c, buf, len); 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 */ memset(&stream, 0, sizeof(stream)); deflateInit(&stream, Z_BEST_COMPRESSION); @@ -195,10 +213,10 @@ int write_sha1_file(char *buf, unsigned len, unsigned char *returnsha1) deflateEnd(&stream); size = stream.total_out; - if (write_sha1_buffer(sha1, compressed, size) < 0) - return -1; - if (returnsha1) - memcpy(returnsha1, sha1, 20); + if (write(fd, compressed, size) != size) + die("unable to write file"); + close(fd); + return 0; } |