aboutsummaryrefslogtreecommitdiff
path: root/sha1_file.c
diff options
context:
space:
mode:
authorDaniel Barkalow <barkalow@iabervon.org>2005-04-23 18:47:23 -0700
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-23 18:47:23 -0700
commit8237b1854c64492ab5772994b4ac4efd7f1c2fc0 (patch)
tree562b4adb2f435756bed81fc129116f449d507e53 /sha1_file.c
parent08692164e0937352fb2c8aab5a7318facbd07daa (diff)
downloadgit-8237b1854c64492ab5772994b4ac4efd7f1c2fc0.tar.gz
git-8237b1854c64492ab5772994b4ac4efd7f1c2fc0.tar.xz
[PATCH] Additional functions for the objects database
This adds two functions: one to check if an object is present in the local database, and one to add an object to the local database by reading it from a file descriptor and checking its hash. Signed-Off-By: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'sha1_file.c')
-rw-r--r--sha1_file.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/sha1_file.c b/sha1_file.c
index 66308ede8..97a515a07 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -328,3 +328,75 @@ int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
close(fd);
return 0;
}
+
+int write_sha1_from_fd(const unsigned char *sha1, int fd)
+{
+ char *filename = sha1_file_name(sha1);
+
+ int local;
+ z_stream stream;
+ unsigned char real_sha1[20];
+ char buf[4096];
+ char discard[4096];
+ int ret;
+ SHA_CTX c;
+
+ local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
+
+ if (local < 0)
+ return error("Couldn't open %s\n", filename);
+
+ memset(&stream, 0, sizeof(stream));
+
+ inflateInit(&stream);
+
+ SHA1_Init(&c);
+
+ do {
+ ssize_t size;
+ size = read(fd, buf, 4096);
+ if (size <= 0) {
+ close(local);
+ unlink(filename);
+ if (!size)
+ return error("Connection closed?");
+ perror("Reading from connection");
+ return -1;
+ }
+ write(local, buf, size);
+ stream.avail_in = size;
+ stream.next_in = buf;
+ do {
+ stream.next_out = discard;
+ stream.avail_out = sizeof(discard);
+ ret = inflate(&stream, Z_SYNC_FLUSH);
+ SHA1_Update(&c, discard, sizeof(discard) -
+ stream.avail_out);
+ } while (stream.avail_in && ret == Z_OK);
+
+ } while (ret == Z_OK);
+ inflateEnd(&stream);
+
+ close(local);
+ SHA1_Final(real_sha1, &c);
+ if (ret != Z_STREAM_END) {
+ unlink(filename);
+ return error("File %s corrupted", sha1_to_hex(sha1));
+ }
+ if (memcmp(sha1, real_sha1, 20)) {
+ unlink(filename);
+ return error("File %s has bad hash\n", sha1_to_hex(sha1));
+ }
+
+ return 0;
+}
+
+int has_sha1_file(const unsigned char *sha1)
+{
+ char *filename = sha1_file_name(sha1);
+ struct stat st;
+
+ if (!stat(filename, &st))
+ return 1;
+ return 0;
+}