Skip to content

Commit 1c5daa1

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 <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent ae371bd commit 1c5daa1

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
{
@@ -276,6 +280,70 @@ const char *write_rev_file_order(const char *rev_name,
276280
return rev_name;
277281
}
278282

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

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

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

498575
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)