git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Expose more sha1_file.c interfaces
@ 2005-06-03 15:08 Jason McMullan
  0 siblings, 0 replies; only message in thread
From: Jason McMullan @ 2005-06-03 15:08 UTC (permalink / raw)
  To: git

Expose more SHA1 file interfaces:

/* Unpack the rest of a SHA1 file, after using unpack_sha1_header()
 */
void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size);

/* Copy SHA1 object file between FDs, and verify that the SHA1 is as
 * expected. Useful for pulling from a socket into a temporary file
 * before you run object reference dependency checks. (Hint Hint people
 * maintaining pull.c!)
 */
int write_sha1_from_fd_to_fd(const unsigned char *sha1, int fd, int fd_local);

Signed-off-by: Jason McMullan <jason.mcmullan@timesys.com>

diff --git a/cache.h b/cache.h
--- a/cache.h
+++ b/cache.h
@@ -153,6 +153,7 @@ extern char *sha1_file_name(const unsign
 extern void * map_sha1_file(const unsigned char *sha1, unsigned long *size);
 extern int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size);
 extern int parse_sha1_header(char *hdr, char *type, unsigned long *sizep);
+extern void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size);
 extern void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size);
 extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size);
 extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
@@ -162,6 +163,7 @@ extern int check_sha1_signature(const un
 /* Read a tree into the cache */
 extern int read_tree(void *buffer, unsigned long size, int stage);
 
+extern int write_sha1_from_fd_to_fd(const unsigned char *sha1, int fd, int fd_local);
 extern int write_sha1_from_fd(const unsigned char *sha1, int fd);
 
 extern int has_sha1_file(const unsigned char *sha1);
diff --git a/sha1_file.c b/sha1_file.c
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -579,23 +579,15 @@ int write_sha1_file(void *buf, unsigned 
 	return 0;
 }
 
-int write_sha1_from_fd(const unsigned char *sha1, int fd)
+int write_sha1_from_fd_to_fd(const unsigned char *sha1, int fd, int local)
 {
-	char *filename = sha1_file_name(sha1);
-
-	int local;
 	z_stream stream;
 	unsigned char real_sha1[20];
 	unsigned char buf[4096];
 	unsigned char discard[4096];
-	int ret;
+	int ret, compressed_size=0;
 	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);
@@ -606,13 +598,12 @@ int write_sha1_from_fd(const unsigned ch
 		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;
 		}
+		compressed_size += size;
 		write(local, buf, size);
 		stream.avail_in = size;
 		stream.next_in = buf;
@@ -627,18 +618,35 @@ int write_sha1_from_fd(const unsigned ch
 	} 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;
+	return compressed_size;
+}
+
+int write_sha1_from_fd(const unsigned char *sha1, int fd)
+{
+	char *filename = sha1_file_name(sha1);
+
+	int local;
+	int ret;
+
+	local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
+
+	if (local < 0)
+		return error("Couldn't open %s\n", filename);
+
+	ret = write_sha1_from_fd_to_fd(sha1, fd, local);
+	close(local);
+	if (ret < 0)
+		unlink(filename);
+
+	return ret;
 }
 
 int has_sha1_file(const unsigned char *sha1)
======== end ========

-- 
Jason McMullan <jason.mcmullan@timesys.com>
TimeSys Corporation

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2005-06-03 15:05 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-06-03 15:08 [PATCH] Expose more sha1_file.c interfaces Jason McMullan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).