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>
Taylor Blau committed
May 20, 2022 at 19:17 UTC
5dfaf49a5a651d3e8c3552bc2e833312a3671a4f
4 files changed
+109
pack-objects.c
+6
@@ -170,6 +170,9 @@ struct object_entry *packlist_alloc(struct packing_data *pdata,
170
171
if (pdata->layer)
172
REALLOC_ARRAY(pdata->layer, pdata->nr_alloc);
173
+
174
+ if (pdata->cruft_mtime)
175
+ REALLOC_ARRAY(pdata->cruft_mtime, pdata->nr_alloc);
176
}
177
178
new_entry = pdata->objects + pdata->nr_objects++;
@@ -198,6 +201,9 @@ struct object_entry *packlist_alloc(struct packing_data *pdata,
201
if (pdata->layer)
202
pdata->layer[pdata->nr_objects - 1] = 0;
203
204
+ if (pdata->cruft_mtime)
205
+ pdata->cruft_mtime[pdata->nr_objects - 1] = 0;
206
+
207
return new_entry;
208
}
209
pack-objects.h
+25
@@ -168,6 +168,14 @@ struct packing_data {
168
/* delta islands */
169
unsigned int *tree_depth;
170
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;
179
};
180
181
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,
297
pack->layer[e - pack->objects] = layer;
298
}
299
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
+
317
#endif
pack-write.c
+77
@@ -3,6 +3,10 @@
3
#include "csum-file.h"
4
#include "remote.h"
5
#include "chunk-format.h"
6
+#include "pack-mtimes.h"
7
+#include "oidmap.h"
8
+#include "chunk-format.h"
9
+#include "pack-objects.h"
10
11
void reset_pack_idx_option(struct pack_idx_option *opts)
12
{
@@ -277,6 +281,70 @@ const char *write_rev_file_order(const char *rev_name,
281
return rev_name;
282
}
283
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
+
348
off_t write_pack_header(struct hashfile *f, uint32_t nr_entries)
349
{
350
struct pack_header hdr;
@@ -479,6 +547,7 @@ void stage_tmp_packfiles(struct strbuf *name_buffer,
547
char **idx_tmp_name)
548
{
549
const char *rev_tmp_name = NULL;
550
+ const char *mtimes_tmp_name = NULL;
551
552
if (adjust_shared_perm(pack_tmp_name))
553
die_errno("unable to make temporary pack file readable");
@@ -491,9 +560,17 @@ void stage_tmp_packfiles(struct strbuf *name_buffer,
560
rev_tmp_name = write_rev_file(NULL, written_list, nr_written, hash,
561
pack_idx_opts->flags);
562
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
+
569
rename_tmp_packfile(name_buffer, pack_tmp_name, "pack");
570
if (rev_tmp_name)
571
rename_tmp_packfile(name_buffer, rev_tmp_name, "rev");
572
+ if (mtimes_tmp_name)
573
+ rename_tmp_packfile(name_buffer, mtimes_tmp_name, "mtimes");
574
}
575
576
void write_promisor_file(const char *promisor_name, struct ref **sought, int nr_sought)
pack.h
+1
@@ -44,6 +44,7 @@ struct pack_idx_option {
44
#define WRITE_IDX_STRICT 02
45
#define WRITE_REV 04
46
#define WRITE_REV_VERIFY 010
47
+#define WRITE_MTIMES 020
48
49
uint32_t version;
50
uint32_t off32_limit;