]> git.ipfire.org Git - thirdparty/git.git/commitdiff
pack: move open_pack_index(), parse_pack_index()
authorJonathan Tan <jonathantanmy@google.com>
Fri, 18 Aug 2017 22:20:19 +0000 (15:20 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 23 Aug 2017 22:12:06 +0000 (15:12 -0700)
alloc_packed_git() in packfile.c is duplicated from sha1_file.c. In a
subsequent commit, alloc_packed_git() will be removed from sha1_file.c.

Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/count-objects.c
builtin/fsck.c
builtin/pack-objects.c
cache.h
pack-bitmap.c
pack-check.c
packfile.c
packfile.h
sha1_file.c
sha1_name.c

index 1d82e61f2a6391bd93a6c3dd91a2515189ff79cc..33343818c830bf64452640b14b2ce7b876221022 100644 (file)
@@ -10,6 +10,7 @@
 #include "builtin.h"
 #include "parse-options.h"
 #include "quote.h"
+#include "packfile.h"
 
 static unsigned long garbage;
 static off_t size_garbage;
index b0964a8d3499a6a288985f92ade5568356bd6a56..338f3ce20b5cd1516be8268dd642f1bb13e04be8 100644 (file)
@@ -15,6 +15,7 @@
 #include "progress.h"
 #include "streaming.h"
 #include "decorate.h"
+#include "packfile.h"
 
 #define REACHABLE 0x0001
 #define SEEN      0x0002
index c753e9237a8d5981a17e872db33d5326bd8d7eab..82ad6e0c813375b480129c22035afc4d6674992a 100644 (file)
@@ -25,6 +25,7 @@
 #include "sha1-array.h"
 #include "argv-array.h"
 #include "mru.h"
+#include "packfile.h"
 
 static const char *pack_usage[] = {
        N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"),
diff --git a/cache.h b/cache.h
index aaf825fb7bf8e5bdc7772395ac4a42122d7d0264..9fd61e9ea0039e656c617110d0a7c4d558e27ceb 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -1605,8 +1605,6 @@ struct pack_entry {
        struct packed_git *p;
 };
 
-extern struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path);
-
 /* A hook to report invalid files in pack directory */
 #define PACKDIR_FILE_PACK 1
 #define PACKDIR_FILE_IDX 2
@@ -1641,12 +1639,6 @@ extern int odb_mkstemp(struct strbuf *template, const char *pattern);
  */
 extern int odb_pack_keep(const char *name);
 
-/*
- * mmap the index file for the specified packfile (if it is not
- * already mmapped).  Return 0 on success.
- */
-extern int open_pack_index(struct packed_git *);
-
 /*
  * munmap the index file for the specified packfile (if it is
  * currently mmapped).
index 327634cd71b5c8fdd5ab531493c2de020bee869d..cb3d14ba45a94dab5fee3b2ebc52eb379506313e 100644 (file)
@@ -9,6 +9,7 @@
 #include "pack-bitmap.h"
 #include "pack-revindex.h"
 #include "pack-objects.h"
+#include "packfile.h"
 
 /*
  * An entry on the bitmap index, representing the bitmap for a given
index e1fcb228fa126e34528e9d5a38b81a6b2eae2f2a..073c1fbd46dfeeb215c209aa0659768e72b81c3b 100644 (file)
@@ -2,6 +2,7 @@
 #include "pack.h"
 #include "pack-revindex.h"
 #include "progress.h"
+#include "packfile.h"
 
 struct idx_entry {
        off_t                offset;
index 60d9fc3b036a7d7ab62c6adb01953f743ddceb6a..6edc432288b40d8f378e2bab5b9f0182b4c4bea9 100644 (file)
@@ -1,5 +1,6 @@
 #include "cache.h"
 #include "mru.h"
+#include "pack.h"
 
 char *odb_pack_name(struct strbuf *buf,
                    const unsigned char *sha1,
@@ -59,3 +60,151 @@ void pack_report(void)
                pack_open_windows, peak_pack_open_windows,
                sz_fmt(pack_mapped), sz_fmt(peak_pack_mapped));
 }
+
+/*
+ * Open and mmap the index file at path, perform a couple of
+ * consistency checks, then record its information to p.  Return 0 on
+ * success.
+ */
+static int check_packed_git_idx(const char *path, struct packed_git *p)
+{
+       void *idx_map;
+       struct pack_idx_header *hdr;
+       size_t idx_size;
+       uint32_t version, nr, i, *index;
+       int fd = git_open(path);
+       struct stat st;
+
+       if (fd < 0)
+               return -1;
+       if (fstat(fd, &st)) {
+               close(fd);
+               return -1;
+       }
+       idx_size = xsize_t(st.st_size);
+       if (idx_size < 4 * 256 + 20 + 20) {
+               close(fd);
+               return error("index file %s is too small", path);
+       }
+       idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
+       close(fd);
+
+       hdr = idx_map;
+       if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) {
+               version = ntohl(hdr->idx_version);
+               if (version < 2 || version > 2) {
+                       munmap(idx_map, idx_size);
+                       return error("index file %s is version %"PRIu32
+                                    " and is not supported by this binary"
+                                    " (try upgrading GIT to a newer version)",
+                                    path, version);
+               }
+       } else
+               version = 1;
+
+       nr = 0;
+       index = idx_map;
+       if (version > 1)
+               index += 2;  /* skip index header */
+       for (i = 0; i < 256; i++) {
+               uint32_t n = ntohl(index[i]);
+               if (n < nr) {
+                       munmap(idx_map, idx_size);
+                       return error("non-monotonic index %s", path);
+               }
+               nr = n;
+       }
+
+       if (version == 1) {
+               /*
+                * Total size:
+                *  - 256 index entries 4 bytes each
+                *  - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
+                *  - 20-byte SHA1 of the packfile
+                *  - 20-byte SHA1 file checksum
+                */
+               if (idx_size != 4*256 + nr * 24 + 20 + 20) {
+                       munmap(idx_map, idx_size);
+                       return error("wrong index v1 file size in %s", path);
+               }
+       } else if (version == 2) {
+               /*
+                * Minimum size:
+                *  - 8 bytes of header
+                *  - 256 index entries 4 bytes each
+                *  - 20-byte sha1 entry * nr
+                *  - 4-byte crc entry * nr
+                *  - 4-byte offset entry * nr
+                *  - 20-byte SHA1 of the packfile
+                *  - 20-byte SHA1 file checksum
+                * And after the 4-byte offset table might be a
+                * variable sized table containing 8-byte entries
+                * for offsets larger than 2^31.
+                */
+               unsigned long min_size = 8 + 4*256 + nr*(20 + 4 + 4) + 20 + 20;
+               unsigned long max_size = min_size;
+               if (nr)
+                       max_size += (nr - 1)*8;
+               if (idx_size < min_size || idx_size > max_size) {
+                       munmap(idx_map, idx_size);
+                       return error("wrong index v2 file size in %s", path);
+               }
+               if (idx_size != min_size &&
+                   /*
+                    * make sure we can deal with large pack offsets.
+                    * 31-bit signed offset won't be enough, neither
+                    * 32-bit unsigned one will be.
+                    */
+                   (sizeof(off_t) <= 4)) {
+                       munmap(idx_map, idx_size);
+                       return error("pack too large for current definition of off_t in %s", path);
+               }
+       }
+
+       p->index_version = version;
+       p->index_data = idx_map;
+       p->index_size = idx_size;
+       p->num_objects = nr;
+       return 0;
+}
+
+int open_pack_index(struct packed_git *p)
+{
+       char *idx_name;
+       size_t len;
+       int ret;
+
+       if (p->index_data)
+               return 0;
+
+       if (!strip_suffix(p->pack_name, ".pack", &len))
+               die("BUG: pack_name does not end in .pack");
+       idx_name = xstrfmt("%.*s.idx", (int)len, p->pack_name);
+       ret = check_packed_git_idx(idx_name, p);
+       free(idx_name);
+       return ret;
+}
+
+static struct packed_git *alloc_packed_git(int extra)
+{
+       struct packed_git *p = xmalloc(st_add(sizeof(*p), extra));
+       memset(p, 0, sizeof(*p));
+       p->pack_fd = -1;
+       return p;
+}
+
+struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path)
+{
+       const char *path = sha1_pack_name(sha1);
+       size_t alloc = st_add(strlen(path), 1);
+       struct packed_git *p = alloc_packed_git(alloc);
+
+       memcpy(p->pack_name, path, alloc); /* includes NUL */
+       hashcpy(p->sha1, sha1);
+       if (check_packed_git_idx(idx_path, p)) {
+               free(p);
+               return NULL;
+       }
+
+       return p;
+}
index bfa94c8fee275a262908a08d0cfc16f72acb1b03..703887d41ba8315d45feb301de4e16d1f4d5ac0d 100644 (file)
@@ -33,6 +33,14 @@ extern unsigned int pack_max_fds;
 extern size_t peak_pack_mapped;
 extern size_t pack_mapped;
 
+extern struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path);
+
 extern void pack_report(void);
 
+/*
+ * mmap the index file for the specified packfile (if it is not
+ * already mmapped).  Return 0 on success.
+ */
+extern int open_pack_index(struct packed_git *);
+
 #endif
index 4895f45b51ca013054eec324fb0c14abe5781aa8..084fe291d382169031c9506861c7cabf5b279836 100644 (file)
@@ -681,130 +681,6 @@ static int has_loose_object(const unsigned char *sha1)
        return check_and_freshen(sha1, 0);
 }
 
-/*
- * Open and mmap the index file at path, perform a couple of
- * consistency checks, then record its information to p.  Return 0 on
- * success.
- */
-static int check_packed_git_idx(const char *path, struct packed_git *p)
-{
-       void *idx_map;
-       struct pack_idx_header *hdr;
-       size_t idx_size;
-       uint32_t version, nr, i, *index;
-       int fd = git_open(path);
-       struct stat st;
-
-       if (fd < 0)
-               return -1;
-       if (fstat(fd, &st)) {
-               close(fd);
-               return -1;
-       }
-       idx_size = xsize_t(st.st_size);
-       if (idx_size < 4 * 256 + 20 + 20) {
-               close(fd);
-               return error("index file %s is too small", path);
-       }
-       idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
-       close(fd);
-
-       hdr = idx_map;
-       if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) {
-               version = ntohl(hdr->idx_version);
-               if (version < 2 || version > 2) {
-                       munmap(idx_map, idx_size);
-                       return error("index file %s is version %"PRIu32
-                                    " and is not supported by this binary"
-                                    " (try upgrading GIT to a newer version)",
-                                    path, version);
-               }
-       } else
-               version = 1;
-
-       nr = 0;
-       index = idx_map;
-       if (version > 1)
-               index += 2;  /* skip index header */
-       for (i = 0; i < 256; i++) {
-               uint32_t n = ntohl(index[i]);
-               if (n < nr) {
-                       munmap(idx_map, idx_size);
-                       return error("non-monotonic index %s", path);
-               }
-               nr = n;
-       }
-
-       if (version == 1) {
-               /*
-                * Total size:
-                *  - 256 index entries 4 bytes each
-                *  - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
-                *  - 20-byte SHA1 of the packfile
-                *  - 20-byte SHA1 file checksum
-                */
-               if (idx_size != 4*256 + nr * 24 + 20 + 20) {
-                       munmap(idx_map, idx_size);
-                       return error("wrong index v1 file size in %s", path);
-               }
-       } else if (version == 2) {
-               /*
-                * Minimum size:
-                *  - 8 bytes of header
-                *  - 256 index entries 4 bytes each
-                *  - 20-byte sha1 entry * nr
-                *  - 4-byte crc entry * nr
-                *  - 4-byte offset entry * nr
-                *  - 20-byte SHA1 of the packfile
-                *  - 20-byte SHA1 file checksum
-                * And after the 4-byte offset table might be a
-                * variable sized table containing 8-byte entries
-                * for offsets larger than 2^31.
-                */
-               unsigned long min_size = 8 + 4*256 + nr*(20 + 4 + 4) + 20 + 20;
-               unsigned long max_size = min_size;
-               if (nr)
-                       max_size += (nr - 1)*8;
-               if (idx_size < min_size || idx_size > max_size) {
-                       munmap(idx_map, idx_size);
-                       return error("wrong index v2 file size in %s", path);
-               }
-               if (idx_size != min_size &&
-                   /*
-                    * make sure we can deal with large pack offsets.
-                    * 31-bit signed offset won't be enough, neither
-                    * 32-bit unsigned one will be.
-                    */
-                   (sizeof(off_t) <= 4)) {
-                       munmap(idx_map, idx_size);
-                       return error("pack too large for current definition of off_t in %s", path);
-               }
-       }
-
-       p->index_version = version;
-       p->index_data = idx_map;
-       p->index_size = idx_size;
-       p->num_objects = nr;
-       return 0;
-}
-
-int open_pack_index(struct packed_git *p)
-{
-       char *idx_name;
-       size_t len;
-       int ret;
-
-       if (p->index_data)
-               return 0;
-
-       if (!strip_suffix(p->pack_name, ".pack", &len))
-               die("BUG: pack_name does not end in .pack");
-       idx_name = xstrfmt("%.*s.idx", (int)len, p->pack_name);
-       ret = check_packed_git_idx(idx_name, p);
-       free(idx_name);
-       return ret;
-}
-
 static void scan_windows(struct packed_git *p,
        struct packed_git **lru_p,
        struct pack_window **lru_w,
@@ -1302,22 +1178,6 @@ struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
        return p;
 }
 
-struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path)
-{
-       const char *path = sha1_pack_name(sha1);
-       size_t alloc = st_add(strlen(path), 1);
-       struct packed_git *p = alloc_packed_git(alloc);
-
-       memcpy(p->pack_name, path, alloc); /* includes NUL */
-       hashcpy(p->sha1, sha1);
-       if (check_packed_git_idx(idx_path, p)) {
-               free(p);
-               return NULL;
-       }
-
-       return p;
-}
-
 void install_packed_git(struct packed_git *pack)
 {
        if (pack->pack_fd != -1)
index 862b6f1308e7031317ab6eb6d181652a6fc644ad..134ac9742f9eed3dc6e587875bdb9aa77b7a8307 100644 (file)
@@ -9,6 +9,7 @@
 #include "remote.h"
 #include "dir.h"
 #include "sha1-array.h"
+#include "packfile.h"
 
 static int get_oid_oneline(const char *, struct object_id *, struct commit_list *);