@matheusbsbs / matheus-bagliano / commits / 106764e651
Fork 0

Refactor index-pack "keep $sha1" handling for reuse

There is a subtle (but important) linkage between receive-pack and index-pack that allows index-pack to create a packfile but protect it from being deleted by a concurrent `git repack -a -d` operation. The linkage works by having index-pack mark the newly created pack with a ".keep" file and then it passes the SHA-1 name of that new packfile to receive-pack along its stdout channel. The receive-pack process must unkeep the packfile by deleting the .keep file, but can it can only do so after all elgible refs have been updated in the receiving repository. This ensures that the packfile is either kept or its objects are reachable, preventing a concurrent repacker from deleting the packfile before it can determine that its objects are actually needed by the repository. The new builtin-fetch code needs to perform the same actions if it choose to run index-pack rather than unpack-objects, so I am moving this code out to its own function where both receive-pack and fetch-pack are able to invoke it when necessary. The caller is responsible for deleting the returned ".keep" and freeing the path if the returned path is not NULL. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Shawn O. Pearce committed Sep 14, 2007 at 03:31 UTC 106764e6515dd0fb9fda8bb8cab523932ae903b3
3 files changed +29 -22
pack-write.c
+26
@@ -179,3 +179,29 @@ void fixup_pack_header_footer(int pack_fd,
179 SHA1_Final(pack_file_sha1, &c);
180 write_or_die(pack_fd, pack_file_sha1, 20);
181 }
182 +
183 +char *index_pack_lockfile(int ip_out)
184 +{
185 + int len, s;
186 + char packname[46];
187 +
188 + /*
189 + * The first thing we expects from index-pack's output
190 + * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where
191 + * %40s is the newly created pack SHA1 name. In the "keep"
192 + * case, we need it to remove the corresponding .keep file
193 + * later on. If we don't get that then tough luck with it.
194 + */
195 + for (len = 0;
196 + len < 46 && (s = xread(ip_out, packname+len, 46-len)) > 0;
197 + len += s);
198 + if (len == 46 && packname[45] == '\n' &&
199 + memcmp(packname, "keep\t", 5) == 0) {
200 + char path[PATH_MAX];
201 + packname[45] = 0;
202 + snprintf(path, sizeof(path), "%s/pack/pack-%s.keep",
203 + get_object_directory(), packname + 5);
204 + return xstrdup(path);
205 + }
206 + return NULL;
207 +}
pack.h
+1
@@ -59,6 +59,7 @@ extern const char *write_idx_file(const char *index_name, struct pack_idx_entry
59
60 extern int verify_pack(struct packed_git *, int);
61 extern void fixup_pack_header_footer(int, unsigned char *, const char *, uint32_t);
62 +extern char *index_pack_lockfile(int fd);
63
64 #define PH_ERROR_EOF (-1)
65 #define PH_ERROR_PACK_SIGNATURE (-2)
receive-pack.c
+2 -22
@@ -382,9 +382,8 @@ static const char *unpack(void)
382 }
383 } else {
384 const char *keeper[6];
385 - int s, len, status;
385 + int s, status;
386 char keep_arg[256];
387 - char packname[46];
387 struct child_process ip;
388
389 s = sprintf(keep_arg, "--keep=receive-pack %i on ", getpid());
@@ -403,26 +402,7 @@ static const char *unpack(void)
402 ip.git_cmd = 1;
403 if (start_command(&ip))
404 return "index-pack fork failed";
406 -
407 - /*
408 - * The first thing we expects from index-pack's output
409 - * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where
410 - * %40s is the newly created pack SHA1 name. In the "keep"
411 - * case, we need it to remove the corresponding .keep file
412 - * later on. If we don't get that then tough luck with it.
413 - */
414 - for (len = 0;
415 - len < 46 && (s = xread(ip.out, packname+len, 46-len)) > 0;
416 - len += s);
417 - if (len == 46 && packname[45] == '\n' &&
418 - memcmp(packname, "keep\t", 5) == 0) {
419 - char path[PATH_MAX];
420 - packname[45] = 0;
421 - snprintf(path, sizeof(path), "%s/pack/pack-%s.keep",
422 - get_object_directory(), packname + 5);
423 - pack_lockfile = xstrdup(path);
424 - }
425 -
405 + pack_lockfile = index_pack_lockfile(ip.out);
406 status = finish_command(&ip);
407 if (!status) {
408 reprepare_packed_git();