Remove all void-pointer arithmetic.
[git.git] / sha1_file.c
index f77c189..3c001d7 100644 (file)
@@ -50,29 +50,6 @@ int get_sha1_hex(const char *hex, unsigned char *sha1)
        return 0;
 }
 
-int adjust_shared_perm(const char *path)
-{
-       struct stat st;
-       int mode;
-
-       if (!shared_repository)
-               return 0;
-       if (lstat(path, &st) < 0)
-               return -1;
-       mode = st.st_mode;
-       if (mode & S_IRUSR)
-               mode |= S_IRGRP;
-       if (mode & S_IWUSR)
-               mode |= S_IWGRP;
-       if (mode & S_IXUSR)
-               mode |= S_IXGRP;
-       if (S_ISDIR(mode))
-               mode |= S_ISGID;
-       if (chmod(path, mode) < 0)
-               return -2;
-       return 0;
-}
-
 int safe_create_leading_directories(char *path)
 {
        char *pos = path;
@@ -509,7 +486,8 @@ int use_packed_git(struct packed_git *p)
                 * this is cheap.
                 */
                if (memcmp((char*)(p->index_base) + p->index_size - 40,
-                          p->pack_base + p->pack_size - 20, 20)) {
+                          (char *) p->pack_base + p->pack_size - 20,
+                          20)) {
                              
                        die("packfile %s does not match index.", p->pack_name);
                }
@@ -617,6 +595,12 @@ static void prepare_packed_git_one(char *objdir, int local)
 
                /* we have .idx.  Is it a file we can map? */
                strcpy(path + len, de->d_name);
+               for (p = packed_git; p; p = p->next) {
+                       if (!memcmp(path, p->pack_name, len + namelen - 4))
+                               break;
+               }
+               if (p)
+                       continue;
                p = add_packed_git(path, len + namelen, local);
                if (!p)
                        continue;
@@ -626,12 +610,12 @@ static void prepare_packed_git_one(char *objdir, int local)
        closedir(dir);
 }
 
+static int prepare_packed_git_run_once = 0;
 void prepare_packed_git(void)
 {
-       static int run_once = 0;
        struct alternate_object_database *alt;
 
-       if (run_once)
+       if (prepare_packed_git_run_once)
                return;
        prepare_packed_git_one(get_object_directory(), 1);
        prepare_alt_odb();
@@ -640,7 +624,13 @@ void prepare_packed_git(void)
                prepare_packed_git_one(alt->base, 0);
                alt->name[-1] = '/';
        }
-       run_once = 1;
+       prepare_packed_git_run_once = 1;
+}
+
+static void reprepare_packed_git(void)
+{
+       prepare_packed_git_run_once = 0;
+       prepare_packed_git();
 }
 
 int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type)
@@ -712,7 +702,7 @@ static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size
        int bytes = strlen(buffer) + 1;
        unsigned char *buf = xmalloc(1+size);
 
-       memcpy(buf, buffer + bytes, stream->total_out - bytes);
+       memcpy(buf, (char *) buffer + bytes, stream->total_out - bytes);
        bytes = stream->total_out - bytes;
        if (bytes < size) {
                stream->next_out = buf + bytes;
@@ -864,7 +854,7 @@ static unsigned long unpack_object_header(struct packed_git *p, unsigned long of
        if (offset >= p->pack_size)
                die("object offset outside of pack file");
 
-       pack =  p->pack_base + offset;
+       pack =  (unsigned char *) p->pack_base + offset;
        c = *pack++;
        offset++;
        *type = (c >> 4) & 7;
@@ -894,7 +884,7 @@ int check_reuse_pack_delta(struct packed_git *p, unsigned long offset,
        ptr = unpack_object_header(p, ptr, kindp, sizep);
        if (*kindp != OBJ_DELTA)
                goto done;
-       memcpy(base, p->pack_base + ptr, 20);
+       memcpy(base, (char *) p->pack_base + ptr, 20);
        status = 0;
  done:
        unuse_packed_git(p);
@@ -914,7 +904,7 @@ void packed_object_info_detail(struct pack_entry *e,
        enum object_type kind;
 
        offset = unpack_object_header(p, e->offset, &kind, size);
-       pack = p->pack_base + offset;
+       pack = (unsigned char *) p->pack_base + offset;
        if (kind != OBJ_DELTA)
                *delta_chain_length = 0;
        else {
@@ -930,7 +920,7 @@ void packed_object_info_detail(struct pack_entry *e,
                        find_pack_entry_one(pack, &base_ent, p);
                        offset = unpack_object_header(p, base_ent.offset,
                                                      &kind, &junk);
-                       pack = p->pack_base + offset;
+                       pack = (unsigned char *) p->pack_base + offset;
                        chain_length++;
                } while (kind == OBJ_DELTA);
                *delta_chain_length = chain_length;
@@ -968,7 +958,7 @@ static int packed_object_info(struct pack_entry *entry,
                die("cannot map packed file");
 
        offset = unpack_object_header(p, entry->offset, &kind, &size);
-       pack = p->pack_base + offset;
+       pack = (unsigned char *) p->pack_base + offset;
        left = p->pack_size - offset;
 
        switch (kind) {
@@ -1107,7 +1097,7 @@ void *unpack_entry_gently(struct pack_entry *entry,
        void *retval;
 
        offset = unpack_object_header(p, entry->offset, &kind, &size);
-       pack = p->pack_base + offset;
+       pack = (unsigned char *) p->pack_base + offset;
        left = p->pack_size - offset;
        switch (kind) {
        case OBJ_DELTA:
@@ -1145,7 +1135,7 @@ int nth_packed_object_sha1(const struct packed_git *p, int n,
        void *index = p->index_base + 256;
        if (n < 0 || num_packed_objects(p) <= n)
                return -1;
-       memcpy(sha1, (index + 24 * n + 4), 20);
+       memcpy(sha1, (char *) index + (24 * n) + 4, 20);
        return 0;
 }
 
@@ -1159,9 +1149,9 @@ int find_pack_entry_one(const unsigned char *sha1,
 
        do {
                int mi = (lo + hi) / 2;
-               int cmp = memcmp(index + 24 * mi + 4, sha1, 20);
+               int cmp = memcmp((char *) index + (24 * mi) + 4, sha1, 20);
                if (!cmp) {
-                       e->offset = ntohl(*((unsigned int *)(index + 24 * mi)));
+                       e->offset = ntohl(*((unsigned int *) ((char *) index + (24 * mi))));
                        memcpy(e->sha1, sha1, 20);
                        e->p = p;
                        return 1;
@@ -1212,9 +1202,12 @@ int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep
        if (!map) {
                struct pack_entry e;
 
-               if (!find_pack_entry(sha1, &e))
-                       return error("unable to find %s", sha1_to_hex(sha1));
-               return packed_object_info(&e, type, sizep);
+               if (find_pack_entry(sha1, &e))
+                       return packed_object_info(&e, type, sizep);
+               reprepare_packed_git();
+               if (find_pack_entry(sha1, &e))
+                       return packed_object_info(&e, type, sizep);
+               return error("unable to find %s", sha1_to_hex(sha1));
        }
        if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
                status = error("unable to unpack %s header",
@@ -1256,6 +1249,9 @@ void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size
                munmap(map, mapsize);
                return buf;
        }
+       reprepare_packed_git();
+       if (find_pack_entry(sha1, &e))
+               return read_packed_sha1(sha1, type, size);
        return NULL;
 }
 
@@ -1295,7 +1291,7 @@ void *read_object_with_reference(const unsigned char *sha1,
                ref_length = strlen(ref_type);
 
                if (memcmp(buffer, ref_type, ref_length) ||
-                   get_sha1_hex(buffer + ref_length, actual_sha1)) {
+                   get_sha1_hex((char *) buffer + ref_length, actual_sha1)) {
                        free(buffer);
                        return NULL;
                }
@@ -1413,7 +1409,7 @@ static int write_buffer(int fd, const void *buf, size_t len)
                        return error("file write error (%s)", strerror(errno));
                }
                len -= size;
-               buf += size;
+               buf = (char *) buf + size;
        }
        return 0;
 }