{"thread":{"id":"7129","subject":"[PATCH 7/9] Use off_t when we really mean a file offset.","startedAt":"2007-03-07T01:44:30Z","lastAt":"2007-03-07T01:44:30Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":9},"messages":[{"id":"36519","messageId":"20070307014430.GH26482@spearce.org","threadId":"7129","inReplyTo":null,"subject":"[PATCH 7/9] Use off_t when we really mean a file offset.","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-03-07T01:44:30Z","receivedAt":"2007-03-07T01:44:30Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Not all platforms have declared 'unsigned long' to be a 64 bit value,\nbut we want to support a 64 bit packfile (or close enough anyway)\nin the near future as some projects are getting large enough that\ntheir packed size exceeds 4 GiB.\n\nBy using off_t, the POSIX type that is declared to mean an offset\nwithin a file, we support whatever maximum file size the underlying\noperating system will handle.  For most modern systems this is up\naround 2^60 or higher.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n cache.h           |   10 ++++----\n fast-import.c     |    4 ---\n git-compat-util.h |    4 +++\n pack-check.c      |   21 +++++++++++--------\n pack-redundant.c  |    8 +++---\n sha1_file.c       |   54 ++++++++++++++++++++++++++++------------------------\n 6 files changed, 54 insertions(+), 47 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex 63a093b..ae25759 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -383,7 +383,7 @@ extern struct packed_git {\n } *packed_git;\n \n struct pack_entry {\n-\tunsigned int offset;\n+\toff_t offset;\n \tunsigned char sha1[20];\n \tstruct packed_git *p;\n };\n@@ -422,15 +422,15 @@ extern struct packed_git *find_sha1_pack(const unsigned char *sha1,\n \t\t\t\t\t struct packed_git *packs);\n \n extern void pack_report(void);\n-extern unsigned char* use_pack(struct packed_git *, struct pack_window **, unsigned long, unsigned int *);\n+extern unsigned char* use_pack(struct packed_git *, struct pack_window **, off_t, unsigned int *);\n extern void unuse_pack(struct pack_window **);\n extern struct packed_git *add_packed_git(char *, int, int);\n extern uint32_t num_packed_objects(const struct packed_git *p);\n extern int nth_packed_object_sha1(const struct packed_git *, uint32_t, unsigned char*);\n-extern unsigned long find_pack_entry_one(const unsigned char *, struct packed_git *);\n-extern void *unpack_entry(struct packed_git *, unsigned long, enum object_type *, unsigned long *);\n+extern off_t find_pack_entry_one(const unsigned char *, struct packed_git *);\n+extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsigned long *);\n extern unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);\n-extern const char *packed_object_info_detail(struct packed_git *, unsigned long, unsigned long *, unsigned long *, unsigned int *, unsigned char *);\n+extern const char *packed_object_info_detail(struct packed_git *, off_t, unsigned long *, unsigned long *, unsigned int *, unsigned char *);\n \n /* Dumb servers support */\n extern int update_server_info(int);\ndiff --git a/fast-import.c b/fast-import.c\nindex 132dd9c..a418a17 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -133,10 +133,6 @@ Format of STDIN stream:\n #define PACK_ID_BITS 16\n #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)\n \n-#ifndef PRIuMAX\n-#define PRIuMAX \"llu\"\n-#endif\n-\n struct object_entry\n {\n \tstruct object_entry *next;\ndiff --git a/git-compat-util.h b/git-compat-util.h\nindex 56212b2..33b68e4 100644\n--- a/git-compat-util.h\n+++ b/git-compat-util.h\n@@ -70,6 +70,10 @@\n #define PATH_MAX 4096\n #endif\n \n+#ifndef PRIuMAX\n+#define PRIuMAX \"llu\"\n+#endif\n+\n #ifdef __GNUC__\n #define NORETURN __attribute__((__noreturn__))\n #else\ndiff --git a/pack-check.c b/pack-check.c\nindex 7c82f67..299c514 100644\n--- a/pack-check.c\n+++ b/pack-check.c\n@@ -4,11 +4,11 @@\n static int verify_packfile(struct packed_git *p,\n \t\tstruct pack_window **w_curs)\n {\n-\tunsigned long index_size = p->index_size;\n+\toff_t index_size = p->index_size;\n \tvoid *index_base = p->index_base;\n \tSHA_CTX ctx;\n \tunsigned char sha1[20];\n-\tunsigned long offset = 0, pack_sig = p->pack_size - 20;\n+\toff_t offset = 0, pack_sig = p->pack_size - 20;\n \tuint32_t nr_objects, i;\n \tint err;\n \n@@ -24,7 +24,7 @@ static int verify_packfile(struct packed_git *p,\n \t\tunsigned char *in = use_pack(p, w_curs, offset, &remaining);\n \t\toffset += remaining;\n \t\tif (offset > pack_sig)\n-\t\t\tremaining -= offset - pack_sig;\n+\t\t\tremaining -= (unsigned int)(offset - pack_sig);\n \t\tSHA1_Update(&ctx, in, remaining);\n \t}\n \tSHA1_Final(sha1, &ctx);\n@@ -45,7 +45,8 @@ static int verify_packfile(struct packed_git *p,\n \t\tunsigned char sha1[20];\n \t\tvoid *data;\n \t\tenum object_type type;\n-\t\tunsigned long size, offset;\n+\t\tunsigned long size;\n+\t\toff_t offset;\n \n \t\tif (nth_packed_object_sha1(p, i, sha1))\n \t\t\tdie(\"internal error pack-check nth-packed-object\");\n@@ -85,7 +86,7 @@ static void show_pack_info(struct packed_git *p)\n \t\tconst char *type;\n \t\tunsigned long size;\n \t\tunsigned long store_size;\n-\t\tunsigned long offset;\n+\t\toff_t offset;\n \t\tunsigned int delta_chain_length;\n \n \t\tif (nth_packed_object_sha1(p, i, sha1))\n@@ -99,9 +100,11 @@ static void show_pack_info(struct packed_git *p)\n \t\t\t\t\t\t base_sha1);\n \t\tprintf(\"%s \", sha1_to_hex(sha1));\n \t\tif (!delta_chain_length)\n-\t\t\tprintf(\"%-6s %lu %lu\\n\", type, size, offset);\n+\t\t\tprintf(\"%-6s %lu %\"PRIuMAX\"\\n\",\n+\t\t\t       type, size, (uintmax_t)offset);\n \t\telse {\n-\t\t\tprintf(\"%-6s %lu %lu %u %s\\n\", type, size, offset,\n+\t\t\tprintf(\"%-6s %lu %\"PRIuMAX\" %u %s\\n\",\n+\t\t\t       type, size, (uintmax_t)offset,\n \t\t\t       delta_chain_length, sha1_to_hex(base_sha1));\n \t\t\tif (delta_chain_length < MAX_CHAIN)\n \t\t\t\tchain_histogram[delta_chain_length]++;\n@@ -123,7 +126,7 @@ static void show_pack_info(struct packed_git *p)\n \n int verify_pack(struct packed_git *p, int verbose)\n {\n-\tunsigned long index_size = p->index_size;\n+\toff_t index_size = p->index_size;\n \tvoid *index_base = p->index_base;\n \tSHA_CTX ctx;\n \tunsigned char sha1[20];\n@@ -132,7 +135,7 @@ int verify_pack(struct packed_git *p, int verbose)\n \tret = 0;\n \t/* Verify SHA1 sum of the index file */\n \tSHA1_Init(&ctx);\n-\tSHA1_Update(&ctx, index_base, index_size - 20);\n+\tSHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));\n \tSHA1_Final(sha1, &ctx);\n \tif (hashcmp(sha1, (unsigned char *)index_base + index_size - 20))\n \t\tret = error(\"Packfile index for %s SHA1 mismatch\",\ndiff --git a/pack-redundant.c b/pack-redundant.c\nindex edb5524..c8f7d9a 100644\n--- a/pack-redundant.c\n+++ b/pack-redundant.c\n@@ -396,9 +396,9 @@ static size_t get_pack_redundancy(struct pack_list *pl)\n \treturn ret;\n }\n \n-static inline size_t pack_set_bytecount(struct pack_list *pl)\n+static inline off_t pack_set_bytecount(struct pack_list *pl)\n {\n-\tsize_t ret = 0;\n+\toff_t ret = 0;\n \twhile (pl) {\n \t\tret += pl->pack->pack_size;\n \t\tret += pl->pack->index_size;\n@@ -413,7 +413,7 @@ static void minimize(struct pack_list **min)\n \t\t*non_unique = NULL, *min_perm = NULL;\n \tstruct pll *perm, *perm_all, *perm_ok = NULL, *new_perm;\n \tstruct llist *missing;\n-\tsize_t min_perm_size = (size_t)-1, perm_size;\n+\toff_t min_perm_size = 0, perm_size;\n \tint n;\n \n \tpl = local_packs;\n@@ -461,7 +461,7 @@ static void minimize(struct pack_list **min)\n \tperm = perm_ok;\n \twhile (perm) {\n \t\tperm_size = pack_set_bytecount(perm->pl);\n-\t\tif (min_perm_size > perm_size) {\n+\t\tif (!min_perm_size || min_perm_size > perm_size) {\n \t\t\tmin_perm_size = perm_size;\n \t\t\tmin_perm = perm->pl;\n \t\t}\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 54ffa7c..50d800e 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -629,7 +629,7 @@ static int open_packed_git(struct packed_git *p)\n \treturn -1;\n }\n \n-static int in_window(struct pack_window *win, unsigned long offset)\n+static int in_window(struct pack_window *win, off_t offset)\n {\n \t/* We must promise at least 20 bytes (one hash) after the\n \t * offset is available from this window, otherwise the offset\n@@ -644,7 +644,7 @@ static int in_window(struct pack_window *win, unsigned long offset)\n \n unsigned char* use_pack(struct packed_git *p,\n \t\tstruct pack_window **w_cursor,\n-\t\tunsigned long offset,\n+\t\toff_t offset,\n \t\tunsigned int *left)\n {\n \tstruct pack_window *win = *w_cursor;\n@@ -1049,14 +1049,14 @@ void * unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type\n \treturn unpack_sha1_rest(&stream, hdr, *size);\n }\n \n-static unsigned long get_delta_base(struct packed_git *p,\n+static off_t get_delta_base(struct packed_git *p,\n \t\t\t\t    struct pack_window **w_curs,\n-\t\t\t\t    unsigned long *curpos,\n+\t\t\t\t    off_t *curpos,\n \t\t\t\t    enum object_type type,\n-\t\t\t\t    unsigned long delta_obj_offset)\n+\t\t\t\t    off_t delta_obj_offset)\n {\n \tunsigned char *base_info = use_pack(p, w_curs, *curpos, NULL);\n-\tunsigned long base_offset;\n+\toff_t base_offset;\n \n \t/* use_pack() assured us we have [base_info, base_info + 20)\n \t * as a range that we can look at without walking off the\n@@ -1092,17 +1092,17 @@ static unsigned long get_delta_base(struct packed_git *p,\n }\n \n /* forward declaration for a mutually recursive function */\n-static int packed_object_info(struct packed_git *p, unsigned long offset,\n+static int packed_object_info(struct packed_git *p, off_t offset,\n \t\t\t      unsigned long *sizep);\n \n static int packed_delta_info(struct packed_git *p,\n \t\t\t     struct pack_window **w_curs,\n-\t\t\t     unsigned long curpos,\n+\t\t\t     off_t curpos,\n \t\t\t     enum object_type type,\n-\t\t\t     unsigned long obj_offset,\n+\t\t\t     off_t obj_offset,\n \t\t\t     unsigned long *sizep)\n {\n-\tunsigned long base_offset;\n+\toff_t base_offset;\n \n \tbase_offset = get_delta_base(p, w_curs, &curpos, type, obj_offset);\n \ttype = packed_object_info(p, base_offset, NULL);\n@@ -1152,7 +1152,7 @@ static int packed_delta_info(struct packed_git *p,\n \n static int unpack_object_header(struct packed_git *p,\n \t\t\t\tstruct pack_window **w_curs,\n-\t\t\t\tunsigned long *curpos,\n+\t\t\t\toff_t *curpos,\n \t\t\t\tunsigned long *sizep)\n {\n \tunsigned char *base;\n@@ -1176,14 +1176,15 @@ static int unpack_object_header(struct packed_git *p,\n }\n \n const char *packed_object_info_detail(struct packed_git *p,\n-\t\t\t\t      unsigned long obj_offset,\n+\t\t\t\t      off_t obj_offset,\n \t\t\t\t      unsigned long *size,\n \t\t\t\t      unsigned long *store_size,\n \t\t\t\t      unsigned int *delta_chain_length,\n \t\t\t\t      unsigned char *base_sha1)\n {\n \tstruct pack_window *w_curs = NULL;\n-\tunsigned long curpos, dummy;\n+\toff_t curpos;\n+\tunsigned long dummy;\n \tunsigned char *next_sha1;\n \tenum object_type type;\n \n@@ -1223,11 +1224,12 @@ const char *packed_object_info_detail(struct packed_git *p,\n \t}\n }\n \n-static int packed_object_info(struct packed_git *p, unsigned long obj_offset,\n+static int packed_object_info(struct packed_git *p, off_t obj_offset,\n \t\t\t      unsigned long *sizep)\n {\n \tstruct pack_window *w_curs = NULL;\n-\tunsigned long size, curpos = obj_offset;\n+\tunsigned long size;\n+\toff_t curpos = obj_offset;\n \tenum object_type type;\n \n \ttype = unpack_object_header(p, &w_curs, &curpos, &size);\n@@ -1255,7 +1257,7 @@ static int packed_object_info(struct packed_git *p, unsigned long obj_offset,\n \n static void *unpack_compressed_entry(struct packed_git *p,\n \t\t\t\t    struct pack_window **w_curs,\n-\t\t\t\t    unsigned long curpos,\n+\t\t\t\t    off_t curpos,\n \t\t\t\t    unsigned long size)\n {\n \tint st;\n@@ -1286,20 +1288,22 @@ static void *unpack_compressed_entry(struct packed_git *p,\n \n static void *unpack_delta_entry(struct packed_git *p,\n \t\t\t\tstruct pack_window **w_curs,\n-\t\t\t\tunsigned long curpos,\n+\t\t\t\toff_t curpos,\n \t\t\t\tunsigned long delta_size,\n-\t\t\t\tunsigned long obj_offset,\n+\t\t\t\toff_t obj_offset,\n \t\t\t\tenum object_type *type,\n \t\t\t\tunsigned long *sizep)\n {\n \tvoid *delta_data, *result, *base;\n-\tunsigned long base_size, base_offset;\n+\tunsigned long base_size;\n+\toff_t base_offset;\n \n \tbase_offset = get_delta_base(p, w_curs, &curpos, *type, obj_offset);\n \tbase = unpack_entry(p, base_offset, type, &base_size);\n \tif (!base)\n-\t\tdie(\"failed to read delta base object at %lu from %s\",\n-\t\t    base_offset, p->pack_name);\n+\t\tdie(\"failed to read delta base object\"\n+\t\t    \" at %\"PRIuMAX\" from %s\",\n+\t\t    (uintmax_t)base_offset, p->pack_name);\n \n \tdelta_data = unpack_compressed_entry(p, w_curs, curpos, delta_size);\n \tresult = patch_delta(base, base_size,\n@@ -1312,11 +1316,11 @@ static void *unpack_delta_entry(struct packed_git *p,\n \treturn result;\n }\n \n-void *unpack_entry(struct packed_git *p, unsigned long obj_offset,\n+void *unpack_entry(struct packed_git *p, off_t obj_offset,\n \t\t   enum object_type *type, unsigned long *sizep)\n {\n \tstruct pack_window *w_curs = NULL;\n-\tunsigned long curpos = obj_offset;\n+\toff_t curpos = obj_offset;\n \tvoid *data;\n \n \t*type = unpack_object_header(p, &w_curs, &curpos, sizep);\n@@ -1355,7 +1359,7 @@ int nth_packed_object_sha1(const struct packed_git *p, uint32_t n,\n \treturn 0;\n }\n \n-unsigned long find_pack_entry_one(const unsigned char *sha1,\n+off_t find_pack_entry_one(const unsigned char *sha1,\n \t\t\t\t  struct packed_git *p)\n {\n \tuint32_t *level1_ofs = p->index_base;\n@@ -1397,7 +1401,7 @@ static int matches_pack_name(struct packed_git *p, const char *ig)\n static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e, const char **ignore_packed)\n {\n \tstruct packed_git *p;\n-\tunsigned long offset;\n+\toff_t offset;\n \n \tprepare_packed_git();\n \n-- \n1.5.0.3.863.gf0989\n"}]}