Skip to content

Commit 5dfaf49

Browse files
ttaylorrgitster
authored andcommitted
pack-mtimes: support writing pack .mtimes files
Now that the `.mtimes` format is defined, supplement the pack-write API to be able to conditionally write an `.mtimes` file along with a pack by setting an additional flag and passing an oidmap that contains the timestamps corresponding to each object in the pack. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d9fef9d commit 5dfaf49

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

pack-objects.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ struct object_entry *packlist_alloc(struct packing_data *pdata,
170170

171171
if (pdata->layer)
172172
REALLOC_ARRAY(pdata->layer, pdata->nr_alloc);
173+
174+
if (pdata->cruft_mtime)
175+
REALLOC_ARRAY(pdata->cruft_mtime, pdata->nr_alloc);
173176
}
174177

175178
new_entry = pdata->objects + pdata->nr_objects++;
@@ -198,6 +201,9 @@ struct object_entry *packlist_alloc(struct packing_data *pdata,
198201
if (pdata->layer)
199202
pdata->layer[pdata->nr_objects - 1] = 0;
200203

204+
if (pdata->cruft_mtime)
205+
pdata->cruft_mtime[pdata->nr_objects - 1] = 0;
206+
201207
return new_entry;
202208
}
203209

pack-objects.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ struct packing_data {
168168
/* delta islands */
169169
unsigned int *tree_depth;
170170
unsigned char *layer;
171+
172+
/*
173+
* Used when writing cruft packs.
174+
*
175+
* Object mtimes are stored in pack order when writing, but
176+
* written out in lexicographic (index) order.
177+
*/
178+
uint32_t *cruft_mtime;
171179
};
172180

173181
void prepare_packing_data(struct repository *r, struct packing_data *pdata);
@@ -289,4 +297,21 @@ static inline void oe_set_layer(struct packing_data *pack,
289297
pack->layer[e - pack->objects] = layer;
290298
}
291299

300+
static inline uint32_t oe_cruft_mtime(struct packing_data *pack,
301+
struct object_entry *e)
302+
{
303+
if (!pack->cruft_mtime)
304+
return 0;
305+
return pack->cruft_mtime[e - pack->objects];
306+
}
307+
308+
static inline void oe_set_cruft_mtime(struct packing_data *pack,
309+
struct object_entry *e,
310+
uint32_t mtime)
311+
{
312+
if (!pack->cruft_mtime)
313+
CALLOC_ARRAY(pack->cruft_mtime, pack->nr_alloc);
314+
pack->cruft_mtime[e - pack->objects] = mtime;
315+
}
316+
292317
#endif

pack-write.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
#include "csum-file.h"
44
#include "remote.h"
55
#include "chunk-format.h"
6+
#include "pack-mtimes.h"
7+
#include "oidmap.h"
8+
#include "chunk-format.h"
9+
#include "pack-objects.h"
610

711
void reset_pack_idx_option(struct pack_idx_option *opts)
812
{
@@ -277,6 +281,70 @@ const char *write_rev_file_order(const char *rev_name,
277281
return rev_name;
278282
}
279283

284+
static void write_mtimes_header(struct hashfile *f)
285+
{
286+
hashwrite_be32(f, MTIMES_SIGNATURE);
287+
hashwrite_be32(f, MTIMES_VERSION);
288+
hashwrite_be32(f, oid_version(the_hash_algo));
289+
}
290+
291+
/*
292+
* Writes the object mtimes of "objects" for use in a .mtimes file.
293+
* Note that objects must be in lexicographic (index) order, which is
294+
* the expected ordering of these values in the .mtimes file.
295+
*/
296+
static void write_mtimes_objects(struct hashfile *f,
297+
struct packing_data *to_pack,
298+
struct pack_idx_entry **objects,
299+
uint32_t nr_objects)
300+
{
301+
uint32_t i;
302+
for (i = 0; i < nr_objects; i++) {
303+
struct object_entry *e = (struct object_entry*)objects[i];
304+
hashwrite_be32(f, oe_cruft_mtime(to_pack, e));
305+
}
306+
}
307+
308+
static void write_mtimes_trailer(struct hashfile *f, const unsigned char *hash)
309+
{
310+
hashwrite(f, hash, the_hash_algo->rawsz);
311+
}
312+
313+
static const char *write_mtimes_file(const char *mtimes_name,
314+
struct packing_data *to_pack,
315+
struct pack_idx_entry **objects,
316+
uint32_t nr_objects,
317+
const unsigned char *hash)
318+
{
319+
struct hashfile *f;
320+
int fd;
321+
322+
if (!to_pack)
323+
BUG("cannot call write_mtimes_file with NULL packing_data");
324+
325+
if (!mtimes_name) {
326+
struct strbuf tmp_file = STRBUF_INIT;
327+
fd = odb_mkstemp(&tmp_file, "pack/tmp_mtimes_XXXXXX");
328+
mtimes_name = strbuf_detach(&tmp_file, NULL);
329+
} else {
330+
unlink(mtimes_name);
331+
fd = xopen(mtimes_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
332+
}
333+
f = hashfd(fd, mtimes_name);
334+
335+
write_mtimes_header(f);
336+
write_mtimes_objects(f, to_pack, objects, nr_objects);
337+
write_mtimes_trailer(f, hash);
338+
339+
if (adjust_shared_perm(mtimes_name) < 0)
340+
die(_("failed to make %s readable"), mtimes_name);
341+
342+
finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA,
343+
CSUM_HASH_IN_STREAM | CSUM_CLOSE | CSUM_FSYNC);
344+
345+
return mtimes_name;
346+
}
347+
280348
off_t write_pack_header(struct hashfile *f, uint32_t nr_entries)
281349
{
282350
struct pack_header hdr;
@@ -479,6 +547,7 @@ void stage_tmp_packfiles(struct strbuf *name_buffer,
479547
char **idx_tmp_name)
480548
{
481549
const char *rev_tmp_name = NULL;
550+
const char *mtimes_tmp_name = NULL;
482551

483552
if (adjust_shared_perm(pack_tmp_name))
484553
die_errno("unable to make temporary pack file readable");
@@ -491,9 +560,17 @@ void stage_tmp_packfiles(struct strbuf *name_buffer,
491560
rev_tmp_name = write_rev_file(NULL, written_list, nr_written, hash,
492561
pack_idx_opts->flags);
493562

563+
if (pack_idx_opts->flags & WRITE_MTIMES) {
564+
mtimes_tmp_name = write_mtimes_file(NULL, to_pack, written_list,
565+
nr_written,
566+
hash);
567+
}
568+
494569
rename_tmp_packfile(name_buffer, pack_tmp_name, "pack");
495570
if (rev_tmp_name)
496571
rename_tmp_packfile(name_buffer, rev_tmp_name, "rev");
572+
if (mtimes_tmp_name)
573+
rename_tmp_packfile(name_buffer, mtimes_tmp_name, "mtimes");
497574
}
498575

499576
void write_promisor_file(const char *promisor_name, struct ref **sought, int nr_sought)

pack.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct pack_idx_option {
4444
#define WRITE_IDX_STRICT 02
4545
#define WRITE_REV 04
4646
#define WRITE_REV_VERIFY 010
47+
#define WRITE_MTIMES 020
4748

4849
uint32_t version;
4950
uint32_t off32_limit;

0 commit comments

Comments
 (0)