Skip to content

Commit c4001d9

Browse files
spearceJunio C Hamano
authored andcommitted
Use off_t when we really mean a file offset.
Not all platforms have declared 'unsigned long' to be a 64 bit value, but we want to support a 64 bit packfile (or close enough anyway) in the near future as some projects are getting large enough that their packed size exceeds 4 GiB. By using off_t, the POSIX type that is declared to mean an offset within a file, we support whatever maximum file size the underlying operating system will handle. For most modern systems this is up around 2^60 or higher. Signed-off-by: Shawn O. Pearce <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7cadf49 commit c4001d9

File tree

6 files changed

+54
-47
lines changed

6 files changed

+54
-47
lines changed

cache.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ extern struct packed_git {
383383
} *packed_git;
384384

385385
struct pack_entry {
386-
unsigned int offset;
386+
off_t offset;
387387
unsigned char sha1[20];
388388
struct packed_git *p;
389389
};
@@ -422,15 +422,15 @@ extern struct packed_git *find_sha1_pack(const unsigned char *sha1,
422422
struct packed_git *packs);
423423

424424
extern void pack_report(void);
425-
extern unsigned char* use_pack(struct packed_git *, struct pack_window **, unsigned long, unsigned int *);
425+
extern unsigned char* use_pack(struct packed_git *, struct pack_window **, off_t, unsigned int *);
426426
extern void unuse_pack(struct pack_window **);
427427
extern struct packed_git *add_packed_git(char *, int, int);
428428
extern uint32_t num_packed_objects(const struct packed_git *p);
429429
extern int nth_packed_object_sha1(const struct packed_git *, uint32_t, unsigned char*);
430-
extern unsigned long find_pack_entry_one(const unsigned char *, struct packed_git *);
431-
extern void *unpack_entry(struct packed_git *, unsigned long, enum object_type *, unsigned long *);
430+
extern off_t find_pack_entry_one(const unsigned char *, struct packed_git *);
431+
extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsigned long *);
432432
extern unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
433-
extern const char *packed_object_info_detail(struct packed_git *, unsigned long, unsigned long *, unsigned long *, unsigned int *, unsigned char *);
433+
extern const char *packed_object_info_detail(struct packed_git *, off_t, unsigned long *, unsigned long *, unsigned int *, unsigned char *);
434434

435435
/* Dumb servers support */
436436
extern int update_server_info(int);

fast-import.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,6 @@ Format of STDIN stream:
133133
#define PACK_ID_BITS 16
134134
#define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
135135

136-
#ifndef PRIuMAX
137-
#define PRIuMAX "llu"
138-
#endif
139-
140136
struct object_entry
141137
{
142138
struct object_entry *next;

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
#define PATH_MAX 4096
7171
#endif
7272

73+
#ifndef PRIuMAX
74+
#define PRIuMAX "llu"
75+
#endif
76+
7377
#ifdef __GNUC__
7478
#define NORETURN __attribute__((__noreturn__))
7579
#else

pack-check.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
static int verify_packfile(struct packed_git *p,
55
struct pack_window **w_curs)
66
{
7-
unsigned long index_size = p->index_size;
7+
off_t index_size = p->index_size;
88
void *index_base = p->index_base;
99
SHA_CTX ctx;
1010
unsigned char sha1[20];
11-
unsigned long offset = 0, pack_sig = p->pack_size - 20;
11+
off_t offset = 0, pack_sig = p->pack_size - 20;
1212
uint32_t nr_objects, i;
1313
int err;
1414

@@ -24,7 +24,7 @@ static int verify_packfile(struct packed_git *p,
2424
unsigned char *in = use_pack(p, w_curs, offset, &remaining);
2525
offset += remaining;
2626
if (offset > pack_sig)
27-
remaining -= offset - pack_sig;
27+
remaining -= (unsigned int)(offset - pack_sig);
2828
SHA1_Update(&ctx, in, remaining);
2929
}
3030
SHA1_Final(sha1, &ctx);
@@ -45,7 +45,8 @@ static int verify_packfile(struct packed_git *p,
4545
unsigned char sha1[20];
4646
void *data;
4747
enum object_type type;
48-
unsigned long size, offset;
48+
unsigned long size;
49+
off_t offset;
4950

5051
if (nth_packed_object_sha1(p, i, sha1))
5152
die("internal error pack-check nth-packed-object");
@@ -85,7 +86,7 @@ static void show_pack_info(struct packed_git *p)
8586
const char *type;
8687
unsigned long size;
8788
unsigned long store_size;
88-
unsigned long offset;
89+
off_t offset;
8990
unsigned int delta_chain_length;
9091

9192
if (nth_packed_object_sha1(p, i, sha1))
@@ -99,9 +100,11 @@ static void show_pack_info(struct packed_git *p)
99100
base_sha1);
100101
printf("%s ", sha1_to_hex(sha1));
101102
if (!delta_chain_length)
102-
printf("%-6s %lu %lu\n", type, size, offset);
103+
printf("%-6s %lu %"PRIuMAX"\n",
104+
type, size, (uintmax_t)offset);
103105
else {
104-
printf("%-6s %lu %lu %u %s\n", type, size, offset,
106+
printf("%-6s %lu %"PRIuMAX" %u %s\n",
107+
type, size, (uintmax_t)offset,
105108
delta_chain_length, sha1_to_hex(base_sha1));
106109
if (delta_chain_length < MAX_CHAIN)
107110
chain_histogram[delta_chain_length]++;
@@ -123,7 +126,7 @@ static void show_pack_info(struct packed_git *p)
123126

124127
int verify_pack(struct packed_git *p, int verbose)
125128
{
126-
unsigned long index_size = p->index_size;
129+
off_t index_size = p->index_size;
127130
void *index_base = p->index_base;
128131
SHA_CTX ctx;
129132
unsigned char sha1[20];
@@ -132,7 +135,7 @@ int verify_pack(struct packed_git *p, int verbose)
132135
ret = 0;
133136
/* Verify SHA1 sum of the index file */
134137
SHA1_Init(&ctx);
135-
SHA1_Update(&ctx, index_base, index_size - 20);
138+
SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));
136139
SHA1_Final(sha1, &ctx);
137140
if (hashcmp(sha1, (unsigned char *)index_base + index_size - 20))
138141
ret = error("Packfile index for %s SHA1 mismatch",

pack-redundant.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,9 @@ static size_t get_pack_redundancy(struct pack_list *pl)
396396
return ret;
397397
}
398398

399-
static inline size_t pack_set_bytecount(struct pack_list *pl)
399+
static inline off_t pack_set_bytecount(struct pack_list *pl)
400400
{
401-
size_t ret = 0;
401+
off_t ret = 0;
402402
while (pl) {
403403
ret += pl->pack->pack_size;
404404
ret += pl->pack->index_size;
@@ -413,7 +413,7 @@ static void minimize(struct pack_list **min)
413413
*non_unique = NULL, *min_perm = NULL;
414414
struct pll *perm, *perm_all, *perm_ok = NULL, *new_perm;
415415
struct llist *missing;
416-
size_t min_perm_size = (size_t)-1, perm_size;
416+
off_t min_perm_size = 0, perm_size;
417417
int n;
418418

419419
pl = local_packs;
@@ -461,7 +461,7 @@ static void minimize(struct pack_list **min)
461461
perm = perm_ok;
462462
while (perm) {
463463
perm_size = pack_set_bytecount(perm->pl);
464-
if (min_perm_size > perm_size) {
464+
if (!min_perm_size || min_perm_size > perm_size) {
465465
min_perm_size = perm_size;
466466
min_perm = perm->pl;
467467
}

sha1_file.c

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ static int open_packed_git(struct packed_git *p)
629629
return -1;
630630
}
631631

632-
static int in_window(struct pack_window *win, unsigned long offset)
632+
static int in_window(struct pack_window *win, off_t offset)
633633
{
634634
/* We must promise at least 20 bytes (one hash) after the
635635
* offset is available from this window, otherwise the offset
@@ -644,7 +644,7 @@ static int in_window(struct pack_window *win, unsigned long offset)
644644

645645
unsigned char* use_pack(struct packed_git *p,
646646
struct pack_window **w_cursor,
647-
unsigned long offset,
647+
off_t offset,
648648
unsigned int *left)
649649
{
650650
struct pack_window *win = *w_cursor;
@@ -1049,14 +1049,14 @@ void * unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type
10491049
return unpack_sha1_rest(&stream, hdr, *size);
10501050
}
10511051

1052-
static unsigned long get_delta_base(struct packed_git *p,
1052+
static off_t get_delta_base(struct packed_git *p,
10531053
struct pack_window **w_curs,
1054-
unsigned long *curpos,
1054+
off_t *curpos,
10551055
enum object_type type,
1056-
unsigned long delta_obj_offset)
1056+
off_t delta_obj_offset)
10571057
{
10581058
unsigned char *base_info = use_pack(p, w_curs, *curpos, NULL);
1059-
unsigned long base_offset;
1059+
off_t base_offset;
10601060

10611061
/* use_pack() assured us we have [base_info, base_info + 20)
10621062
* as a range that we can look at without walking off the
@@ -1092,17 +1092,17 @@ static unsigned long get_delta_base(struct packed_git *p,
10921092
}
10931093

10941094
/* forward declaration for a mutually recursive function */
1095-
static int packed_object_info(struct packed_git *p, unsigned long offset,
1095+
static int packed_object_info(struct packed_git *p, off_t offset,
10961096
unsigned long *sizep);
10971097

10981098
static int packed_delta_info(struct packed_git *p,
10991099
struct pack_window **w_curs,
1100-
unsigned long curpos,
1100+
off_t curpos,
11011101
enum object_type type,
1102-
unsigned long obj_offset,
1102+
off_t obj_offset,
11031103
unsigned long *sizep)
11041104
{
1105-
unsigned long base_offset;
1105+
off_t base_offset;
11061106

11071107
base_offset = get_delta_base(p, w_curs, &curpos, type, obj_offset);
11081108
type = packed_object_info(p, base_offset, NULL);
@@ -1152,7 +1152,7 @@ static int packed_delta_info(struct packed_git *p,
11521152

11531153
static int unpack_object_header(struct packed_git *p,
11541154
struct pack_window **w_curs,
1155-
unsigned long *curpos,
1155+
off_t *curpos,
11561156
unsigned long *sizep)
11571157
{
11581158
unsigned char *base;
@@ -1176,14 +1176,15 @@ static int unpack_object_header(struct packed_git *p,
11761176
}
11771177

11781178
const char *packed_object_info_detail(struct packed_git *p,
1179-
unsigned long obj_offset,
1179+
off_t obj_offset,
11801180
unsigned long *size,
11811181
unsigned long *store_size,
11821182
unsigned int *delta_chain_length,
11831183
unsigned char *base_sha1)
11841184
{
11851185
struct pack_window *w_curs = NULL;
1186-
unsigned long curpos, dummy;
1186+
off_t curpos;
1187+
unsigned long dummy;
11871188
unsigned char *next_sha1;
11881189
enum object_type type;
11891190

@@ -1223,11 +1224,12 @@ const char *packed_object_info_detail(struct packed_git *p,
12231224
}
12241225
}
12251226

1226-
static int packed_object_info(struct packed_git *p, unsigned long obj_offset,
1227+
static int packed_object_info(struct packed_git *p, off_t obj_offset,
12271228
unsigned long *sizep)
12281229
{
12291230
struct pack_window *w_curs = NULL;
1230-
unsigned long size, curpos = obj_offset;
1231+
unsigned long size;
1232+
off_t curpos = obj_offset;
12311233
enum object_type type;
12321234

12331235
type = unpack_object_header(p, &w_curs, &curpos, &size);
@@ -1255,7 +1257,7 @@ static int packed_object_info(struct packed_git *p, unsigned long obj_offset,
12551257

12561258
static void *unpack_compressed_entry(struct packed_git *p,
12571259
struct pack_window **w_curs,
1258-
unsigned long curpos,
1260+
off_t curpos,
12591261
unsigned long size)
12601262
{
12611263
int st;
@@ -1286,20 +1288,22 @@ static void *unpack_compressed_entry(struct packed_git *p,
12861288

12871289
static void *unpack_delta_entry(struct packed_git *p,
12881290
struct pack_window **w_curs,
1289-
unsigned long curpos,
1291+
off_t curpos,
12901292
unsigned long delta_size,
1291-
unsigned long obj_offset,
1293+
off_t obj_offset,
12921294
enum object_type *type,
12931295
unsigned long *sizep)
12941296
{
12951297
void *delta_data, *result, *base;
1296-
unsigned long base_size, base_offset;
1298+
unsigned long base_size;
1299+
off_t base_offset;
12971300

12981301
base_offset = get_delta_base(p, w_curs, &curpos, *type, obj_offset);
12991302
base = unpack_entry(p, base_offset, type, &base_size);
13001303
if (!base)
1301-
die("failed to read delta base object at %lu from %s",
1302-
base_offset, p->pack_name);
1304+
die("failed to read delta base object"
1305+
" at %"PRIuMAX" from %s",
1306+
(uintmax_t)base_offset, p->pack_name);
13031307

13041308
delta_data = unpack_compressed_entry(p, w_curs, curpos, delta_size);
13051309
result = patch_delta(base, base_size,
@@ -1312,11 +1316,11 @@ static void *unpack_delta_entry(struct packed_git *p,
13121316
return result;
13131317
}
13141318

1315-
void *unpack_entry(struct packed_git *p, unsigned long obj_offset,
1319+
void *unpack_entry(struct packed_git *p, off_t obj_offset,
13161320
enum object_type *type, unsigned long *sizep)
13171321
{
13181322
struct pack_window *w_curs = NULL;
1319-
unsigned long curpos = obj_offset;
1323+
off_t curpos = obj_offset;
13201324
void *data;
13211325

13221326
*type = unpack_object_header(p, &w_curs, &curpos, sizep);
@@ -1355,7 +1359,7 @@ int nth_packed_object_sha1(const struct packed_git *p, uint32_t n,
13551359
return 0;
13561360
}
13571361

1358-
unsigned long find_pack_entry_one(const unsigned char *sha1,
1362+
off_t find_pack_entry_one(const unsigned char *sha1,
13591363
struct packed_git *p)
13601364
{
13611365
uint32_t *level1_ofs = p->index_base;
@@ -1397,7 +1401,7 @@ static int matches_pack_name(struct packed_git *p, const char *ig)
13971401
static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e, const char **ignore_packed)
13981402
{
13991403
struct packed_git *p;
1400-
unsigned long offset;
1404+
off_t offset;
14011405

14021406
prepare_packed_git();
14031407

0 commit comments

Comments
 (0)