3
3
#include "csum-file.h"
4
4
#include "remote.h"
5
5
#include "chunk-format.h"
6
+ #include "pack-mtimes.h"
7
+ #include "oidmap.h"
8
+ #include "chunk-format.h"
9
+ #include "pack-objects.h"
6
10
7
11
void reset_pack_idx_option (struct pack_idx_option * opts )
8
12
{
@@ -277,6 +281,70 @@ const char *write_rev_file_order(const char *rev_name,
277
281
return rev_name ;
278
282
}
279
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
+
280
348
off_t write_pack_header (struct hashfile * f , uint32_t nr_entries )
281
349
{
282
350
struct pack_header hdr ;
@@ -479,6 +547,7 @@ void stage_tmp_packfiles(struct strbuf *name_buffer,
479
547
char * * idx_tmp_name )
480
548
{
481
549
const char * rev_tmp_name = NULL ;
550
+ const char * mtimes_tmp_name = NULL ;
482
551
483
552
if (adjust_shared_perm (pack_tmp_name ))
484
553
die_errno ("unable to make temporary pack file readable" );
@@ -491,9 +560,17 @@ void stage_tmp_packfiles(struct strbuf *name_buffer,
491
560
rev_tmp_name = write_rev_file (NULL , written_list , nr_written , hash ,
492
561
pack_idx_opts -> flags );
493
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
+
494
569
rename_tmp_packfile (name_buffer , pack_tmp_name , "pack" );
495
570
if (rev_tmp_name )
496
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" );
497
574
}
498
575
499
576
void write_promisor_file (const char * promisor_name , struct ref * * sought , int nr_sought )
0 commit comments