Skip to content

Commit 85e08e5

Browse files
ttaylorrgitster
authored andcommitted
pack-mtimes: support reading .mtimes files
To store the individual mtimes of objects in a cruft pack, introduce a new `.mtimes` format that can optionally accompany a single pack in the repository. The format is defined in Documentation/technical/pack-format.txt, and stores a 4-byte network order timestamp for each object in name (index) order. This patch prepares for cruft packs by defining the `.mtimes` format, and introducing a basic API that callers can use to read out individual mtimes. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 4e88cea commit 85e08e5

File tree

7 files changed

+183
-3
lines changed

7 files changed

+183
-3
lines changed

Documentation/technical/pack-format.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,25 @@ Pack file entry: <+
294294

295295
All 4-byte numbers are in network order.
296296

297+
== pack-*.mtimes files have the format:
298+
299+
- A 4-byte magic number '0x4d544d45' ('MTME').
300+
301+
- A 4-byte version identifier (= 1).
302+
303+
- A 4-byte hash function identifier (= 1 for SHA-1, 2 for SHA-256).
304+
305+
- A table of 4-byte unsigned integers in network order. The ith
306+
value is the modification time (mtime) of the ith object in the
307+
corresponding pack by lexicographic (index) order. The mtimes
308+
count standard epoch seconds.
309+
310+
- A trailer, containing a checksum of the corresponding packfile,
311+
and a checksum of all of the above (each having length according
312+
to the specified hash function).
313+
314+
All 4-byte numbers are in network order.
315+
297316
== multi-pack-index (MIDX) files have the following format:
298317

299318
The multi-pack-index files refer to multiple pack-files and loose objects.

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,7 @@ LIB_OBJS += oidtree.o
959959
LIB_OBJS += pack-bitmap-write.o
960960
LIB_OBJS += pack-bitmap.o
961961
LIB_OBJS += pack-check.o
962+
LIB_OBJS += pack-mtimes.o
962963
LIB_OBJS += pack-objects.o
963964
LIB_OBJS += pack-revindex.o
964965
LIB_OBJS += pack-write.o

builtin/repack.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ static struct {
212212
} exts[] = {
213213
{".pack"},
214214
{".rev", 1},
215+
{".mtimes", 1},
215216
{".bitmap", 1},
216217
{".promisor", 1},
217218
{".idx"},

object-store.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,15 @@ struct packed_git {
115115
freshened:1,
116116
do_not_close:1,
117117
pack_promisor:1,
118-
multi_pack_index:1;
118+
multi_pack_index:1,
119+
is_cruft:1;
119120
unsigned char hash[GIT_MAX_RAWSZ];
120121
struct revindex_entry *revindex;
121122
const uint32_t *revindex_data;
122123
const uint32_t *revindex_map;
123124
size_t revindex_size;
125+
const uint32_t *mtimes_map;
126+
size_t mtimes_size;
124127
/* something like ".git/objects/pack/xxxxx.pack" */
125128
char pack_name[FLEX_ARRAY]; /* more */
126129
};

pack-mtimes.c

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include "pack-mtimes.h"
2+
#include "object-store.h"
3+
#include "packfile.h"
4+
5+
static char *pack_mtimes_filename(struct packed_git *p)
6+
{
7+
size_t len;
8+
if (!strip_suffix(p->pack_name, ".pack", &len))
9+
BUG("pack_name does not end in .pack");
10+
/* NEEDSWORK: this could reuse code from pack-revindex.c. */
11+
return xstrfmt("%.*s.mtimes", (int)len, p->pack_name);
12+
}
13+
14+
#define MTIMES_HEADER_SIZE (12)
15+
#define MTIMES_MIN_SIZE (MTIMES_HEADER_SIZE + (2 * the_hash_algo->rawsz))
16+
17+
struct mtimes_header {
18+
uint32_t signature;
19+
uint32_t version;
20+
uint32_t hash_id;
21+
};
22+
23+
static int load_pack_mtimes_file(char *mtimes_file,
24+
uint32_t num_objects,
25+
const uint32_t **data_p, size_t *len_p)
26+
{
27+
int fd, ret = 0;
28+
struct stat st;
29+
void *data = NULL;
30+
size_t mtimes_size;
31+
struct mtimes_header header;
32+
uint32_t *hdr;
33+
34+
fd = git_open(mtimes_file);
35+
36+
if (fd < 0) {
37+
ret = -1;
38+
goto cleanup;
39+
}
40+
if (fstat(fd, &st)) {
41+
ret = error_errno(_("failed to read %s"), mtimes_file);
42+
goto cleanup;
43+
}
44+
45+
mtimes_size = xsize_t(st.st_size);
46+
47+
if (mtimes_size < MTIMES_MIN_SIZE) {
48+
ret = error(_("mtimes file %s is too small"), mtimes_file);
49+
goto cleanup;
50+
}
51+
52+
if (mtimes_size - MTIMES_MIN_SIZE != st_mult(sizeof(uint32_t), num_objects)) {
53+
ret = error(_("mtimes file %s is corrupt"), mtimes_file);
54+
goto cleanup;
55+
}
56+
57+
data = hdr = xmmap(NULL, mtimes_size, PROT_READ, MAP_PRIVATE, fd, 0);
58+
59+
header.signature = ntohl(hdr[0]);
60+
header.version = ntohl(hdr[1]);
61+
header.hash_id = ntohl(hdr[2]);
62+
63+
if (header.signature != MTIMES_SIGNATURE) {
64+
ret = error(_("mtimes file %s has unknown signature"), mtimes_file);
65+
goto cleanup;
66+
}
67+
68+
if (header.version != 1) {
69+
ret = error(_("mtimes file %s has unsupported version %"PRIu32),
70+
mtimes_file, header.version);
71+
goto cleanup;
72+
}
73+
74+
if (!(header.hash_id == 1 || header.hash_id == 2)) {
75+
ret = error(_("mtimes file %s has unsupported hash id %"PRIu32),
76+
mtimes_file, header.hash_id);
77+
goto cleanup;
78+
}
79+
80+
cleanup:
81+
if (ret) {
82+
if (data)
83+
munmap(data, mtimes_size);
84+
} else {
85+
*len_p = mtimes_size;
86+
*data_p = (const uint32_t *)data;
87+
}
88+
89+
close(fd);
90+
return ret;
91+
}
92+
93+
int load_pack_mtimes(struct packed_git *p)
94+
{
95+
char *mtimes_name = NULL;
96+
int ret = 0;
97+
98+
if (!p->is_cruft)
99+
return ret; /* not a cruft pack */
100+
if (p->mtimes_map)
101+
return ret; /* already loaded */
102+
103+
ret = open_pack_index(p);
104+
if (ret < 0)
105+
goto cleanup;
106+
107+
mtimes_name = pack_mtimes_filename(p);
108+
ret = load_pack_mtimes_file(mtimes_name,
109+
p->num_objects,
110+
&p->mtimes_map,
111+
&p->mtimes_size);
112+
cleanup:
113+
free(mtimes_name);
114+
return ret;
115+
}
116+
117+
uint32_t nth_packed_mtime(struct packed_git *p, uint32_t pos)
118+
{
119+
if (!p->mtimes_map)
120+
BUG("pack .mtimes file not loaded for %s", p->pack_name);
121+
if (p->num_objects <= pos)
122+
BUG("pack .mtimes out-of-bounds (%"PRIu32" vs %"PRIu32")",
123+
pos, p->num_objects);
124+
125+
return get_be32(p->mtimes_map + pos + 3);
126+
}

pack-mtimes.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef PACK_MTIMES_H
2+
#define PACK_MTIMES_H
3+
4+
#include "git-compat-util.h"
5+
6+
#define MTIMES_SIGNATURE 0x4d544d45 /* "MTME" */
7+
#define MTIMES_VERSION 1
8+
9+
struct packed_git;
10+
11+
int load_pack_mtimes(struct packed_git *p);
12+
13+
uint32_t nth_packed_mtime(struct packed_git *p, uint32_t pos);
14+
15+
#endif

packfile.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,22 @@ static void close_pack_revindex(struct packed_git *p)
334334
p->revindex_data = NULL;
335335
}
336336

337+
static void close_pack_mtimes(struct packed_git *p)
338+
{
339+
if (!p->mtimes_map)
340+
return;
341+
342+
munmap((void *)p->mtimes_map, p->mtimes_size);
343+
p->mtimes_map = NULL;
344+
}
345+
337346
void close_pack(struct packed_git *p)
338347
{
339348
close_pack_windows(p);
340349
close_pack_fd(p);
341350
close_pack_index(p);
342351
close_pack_revindex(p);
352+
close_pack_mtimes(p);
343353
oidset_clear(&p->bad_objects);
344354
}
345355

@@ -363,7 +373,7 @@ void close_object_store(struct raw_object_store *o)
363373

364374
void unlink_pack_path(const char *pack_name, int force_delete)
365375
{
366-
static const char *exts[] = {".pack", ".idx", ".rev", ".keep", ".bitmap", ".promisor"};
376+
static const char *exts[] = {".pack", ".idx", ".rev", ".keep", ".bitmap", ".promisor", ".mtimes"};
367377
int i;
368378
struct strbuf buf = STRBUF_INIT;
369379
size_t plen;
@@ -718,6 +728,10 @@ struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
718728
if (!access(p->pack_name, F_OK))
719729
p->pack_promisor = 1;
720730

731+
xsnprintf(p->pack_name + path_len, alloc - path_len, ".mtimes");
732+
if (!access(p->pack_name, F_OK))
733+
p->is_cruft = 1;
734+
721735
xsnprintf(p->pack_name + path_len, alloc - path_len, ".pack");
722736
if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode)) {
723737
free(p);
@@ -869,7 +883,8 @@ static void prepare_pack(const char *full_name, size_t full_name_len,
869883
ends_with(file_name, ".pack") ||
870884
ends_with(file_name, ".bitmap") ||
871885
ends_with(file_name, ".keep") ||
872-
ends_with(file_name, ".promisor"))
886+
ends_with(file_name, ".promisor") ||
887+
ends_with(file_name, ".mtimes"))
873888
string_list_append(data->garbage, full_name);
874889
else
875890
report_garbage(PACKDIR_FILE_GARBAGE, full_name);

0 commit comments

Comments
 (0)