summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Linus Torvalds <torvalds@ppc970.osdl.org>2005-04-08 14:42:29 -0700
committerLibravatar Linus Torvalds <torvalds@ppc970.osdl.org>2005-04-08 14:42:29 -0700
commit24778e335a6450e34257a311d0bf4a12bdb3006c (patch)
treedb8e952afe1ad62c4bc5989ef3d2292ef4aa4d4f
parentUse "-Wall -O2" for the compiler to get more warnings. (diff)
downloadtgif-24778e335a6450e34257a311d0bf4a12bdb3006c.tar.xz
Factor out "read_sha1_file" into mapping/inflating/unmapping.
This allows us to also actually check the sha1 hash using these routines. Needed for the "fsck" thing.
-rw-r--r--cache.h2
-rw-r--r--read-cache.c40
2 files changed, 33 insertions, 9 deletions
diff --git a/cache.h b/cache.h
index b1313c503c..1732859765 100644
--- a/cache.h
+++ b/cache.h
@@ -81,8 +81,10 @@ extern char *sha1_file_name(unsigned char *sha1);
extern int write_sha1_buffer(unsigned char *sha1, void *buf, unsigned int size);
/* Read and unpack a sha1 file into memory, write memory to a sha1 file */
+extern void * map_sha1_file(unsigned char *sha1, unsigned long *size);
extern void * read_sha1_file(unsigned char *sha1, char *type, unsigned long *size);
extern int write_sha1_file(char *buf, unsigned len);
+extern int check_sha1_signature(unsigned char *sha1, void *buf, unsigned long size);
/* Convert to/from hex/sha1 representation */
extern int get_sha1_hex(char *hex, unsigned char *sha1);
diff --git a/read-cache.c b/read-cache.c
index 60a0b5b2e7..4d750506e9 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -84,33 +84,40 @@ char *sha1_file_name(unsigned char *sha1)
return base;
}
-void * read_sha1_file(unsigned char *sha1, char *type, unsigned long *size)
+void *map_sha1_file(unsigned char *sha1, unsigned long *size)
{
- z_stream stream;
- char buffer[8192];
- struct stat st;
- int fd, ret, bytes;
- void *map, *buf;
char *filename = sha1_file_name(sha1);
+ int fd = open(filename, O_RDONLY);
+ struct stat st;
+ void *map;
- fd = open(filename, O_RDONLY);
if (fd < 0) {
perror(filename);
return NULL;
}
if (fstat(fd, &st) < 0) {
- close(fd);
+ close(fd);
return NULL;
}
map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if (-1 == (int)(long)map)
return NULL;
+ *size = st.st_size;
+ return map;
+}
+
+void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
+{
+ int ret, bytes;
+ z_stream stream;
+ char buffer[8192];
+ char *buf;
/* Get the data stream */
memset(&stream, 0, sizeof(stream));
stream.next_in = map;
- stream.avail_in = st.st_size;
+ stream.avail_in = mapsize;
stream.next_out = buffer;
stream.avail_out = sizeof(buffer);
@@ -118,6 +125,7 @@ void * read_sha1_file(unsigned char *sha1, char *type, unsigned long *size)
ret = inflate(&stream, 0);
if (sscanf(buffer, "%10s %lu", type, size) != 2)
return NULL;
+
bytes = strlen(buffer) + 1;
buf = malloc(*size);
if (!buf)
@@ -135,6 +143,20 @@ void * read_sha1_file(unsigned char *sha1, char *type, unsigned long *size)
return buf;
}
+void * read_sha1_file(unsigned char *sha1, char *type, unsigned long *size)
+{
+ unsigned long mapsize;
+ void *map, *buf;
+
+ map = map_sha1_file(sha1, &mapsize);
+ if (map) {
+ buf = unpack_sha1_file(map, mapsize, type, size);
+ munmap(map, mapsize);
+ return buf;
+ }
+ return NULL;
+}
+
int write_sha1_file(char *buf, unsigned len)
{
int size;