+/*
+ * GIT - The information manager from hell
+ *
+ * Copyright (C) Linus Torvalds, 2005
+ */
#include "cache.h"
const char *sha1_file_directory = NULL;
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 i, 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);
ret = inflate(&stream, 0);
if (sscanf(buffer, "%10s %lu", type, size) != 2)
return NULL;
+
bytes = strlen(buffer) + 1;
buf = malloc(*size);
if (!buf)
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;
int write_sha1_buffer(unsigned char *sha1, void *buf, unsigned int size)
{
char *filename = sha1_file_name(sha1);
- int i, fd;
+ int fd;
fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd < 0)
if (fd < 0)
return (errno == ENOENT) ? 0 : error("open failed");
+ size = 0; // avoid gcc warning
map = (void *)-1;
if (!fstat(fd, &st)) {
map = NULL;