make sure packs to be replaced are closed beforehand
Especially on Windows where an opened file cannot be replaced, make sure pack-objects always close packs it is about to replace. Even on non Windows systems, this could save potential bad results if ever objects were to be read from the new pack file using offset from the old index. This should fix t5303 on Windows. Signed-off-by: Nicolas Pitre <nico@cam.org> Tested-by: Johannes Sixt <j6t@kdbg.org> (MinGW) Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nicolas Pitre committed
Dec 9, 2008 at 14:26 UTC
c74faea19e39ca933492f697596310397175c329
3 files changed
+33
builtin-pack-objects.c
+1
@@ -520,6 +520,7 @@ static void write_pack_file(void)
520
521
snprintf(tmpname, sizeof(tmpname), "%s-%s.pack",
522
base_name, sha1_to_hex(sha1));
523
+ free_pack_by_name(tmpname);
524
if (adjust_perm(pack_tmp_name, mode))
525
die("unable to make temporary pack file readable: %s",
526
strerror(errno));
cache.h
+1
@@ -738,6 +738,7 @@ extern int open_pack_index(struct packed_git *);
738
extern unsigned char* use_pack(struct packed_git *, struct pack_window **, off_t, unsigned int *);
739
extern void close_pack_windows(struct packed_git *);
740
extern void unuse_pack(struct pack_window **);
741
+extern void free_pack_by_name(const char *);
742
extern struct packed_git *add_packed_git(const char *, int, int);
743
extern const unsigned char *nth_packed_object_sha1(struct packed_git *, uint32_t);
744
extern off_t nth_packed_object_offset(const struct packed_git *, uint32_t);
sha1_file.c
+31
@@ -659,6 +659,37 @@ void unuse_pack(struct pack_window **w_cursor)
659
}
660
}
661
662
+/*
663
+ * This is used by git-repack in case a newly created pack happens to
664
+ * contain the same set of objects as an existing one. In that case
665
+ * the resulting file might be different even if its name would be the
666
+ * same. It is best to close any reference to the old pack before it is
667
+ * replaced on disk. Of course no index pointers nor windows for given pack
668
+ * must subsist at this point. If ever objects from this pack are requested
669
+ * again, the new version of the pack will be reinitialized through
670
+ * reprepare_packed_git().
671
+ */
672
+void free_pack_by_name(const char *pack_name)
673
+{
674
+ struct packed_git *p, **pp = &packed_git;
675
+
676
+ while (*pp) {
677
+ p = *pp;
678
+ if (strcmp(pack_name, p->pack_name) == 0) {
679
+ close_pack_windows(p);
680
+ if (p->pack_fd != -1)
681
+ close(p->pack_fd);
682
+ if (p->index_data)
683
+ munmap((void *)p->index_data, p->index_size);
684
+ free(p->bad_object_sha1);
685
+ *pp = p->next;
686
+ free(p);
687
+ return;
688
+ }
689
+ pp = &p->next;
690
+ }
691
+}
692
+
693
/*
694
* Do not call this directly as this leaks p->pack_fd on error return;
695
* call open_packed_git() instead.