@matheusbsbs / matheus-bagliano / commits / 576162a45f
Fork 0

remove .keep pack lock files when done with refs update

This makes both git-fetch and git-push (fetch-pack and receive-pack) safe against a possible race with aparallel git-repack -a -d that could prune the new pack while it is not yet referenced, and remove the .keep file after refs have been updated. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Nov 1, 2006 at 17:06 UTC 576162a45f35e157427300066b0ff566ff698a0f
4 files changed +116 -39
Documentation/git-index-pack.txt
+11
@@ -69,6 +69,17 @@ OPTIONS
69 locate any which have outlived their usefulness.
70
71
72 +Note
73 +----
74 +
75 +Once the index has been created, the list of object names is sorted
76 +and the SHA1 hash of that list is printed to stdout. If --stdin was
77 +also used then this is prefixed by either "pack\t", or "keep\t" if a
78 +new .keep file was successfully created. This is useful to remove a
79 +.keep file used as a lock to prevent the race with gitlink:git-repack[1]
80 +mentioned above.
81 +
82 +
83 Author
84 ------
85 Written by Sergey Vlasov <vsu@altlinux.ru>
git-fetch.sh
+9 -1
@@ -51,7 +51,7 @@ do
51 verbose=Yes
52 ;;
53 -k|--k|--ke|--kee|--keep)
54 - keep=--keep
54 + keep='-k -k'
55 ;;
56 --reflog-action=*)
57 rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`
@@ -368,6 +368,7 @@ fetch_main () {
368 ;; # we are already done.
369 *)
370 ( : subshell because we muck with IFS
371 + pack_lockfile=
372 IFS=" $LF"
373 (
374 git-fetch-pack --thin $exec $keep "$remote" $rref || echo failed "$remote"
@@ -378,6 +379,12 @@ fetch_main () {
379 failed)
380 echo >&2 "Fetch failure: $remote"
381 exit 1 ;;
382 + # special line coming from index-pack with the pack name
383 + pack)
384 + continue ;;
385 + keep)
386 + pack_lockfile="$GIT_OBJECT_DIRECTORY/pack/pack-$remote_name.keep"
387 + continue ;;
388 esac
389 found=
390 single_force=
@@ -408,6 +415,7 @@ fetch_main () {
415 append_fetch_head "$sha1" "$remote" \
416 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
417 done
418 + if [ "$pack_lockfile" ]; then rm -f "$pack_lockfile"; fi
419 ) || exit ;;
420 esac
421
index-pack.c
+23 -15
@@ -757,6 +757,7 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
757 const char *keep_name, const char *keep_msg,
758 unsigned char *sha1)
759 {
760 + char *report = "pack";
761 char name[PATH_MAX];
762 int err;
763
@@ -767,18 +768,6 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
768 if (err)
769 die("error while closing pack file: %s", strerror(errno));
770 chmod(curr_pack_name, 0444);
770 -
771 - /*
772 - * Let's just mimic git-unpack-objects here and write
773 - * the last part of the buffer to stdout.
774 - */
775 - while (input_len) {
776 - err = xwrite(1, input_buffer + input_offset, input_len);
777 - if (err <= 0)
778 - break;
779 - input_len -= err;
780 - input_offset += err;
781 - }
771 }
772
773 if (keep_msg) {
@@ -798,6 +787,7 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
787 write_or_die(keep_fd, "\n", 1);
788 }
789 close(keep_fd);
790 + report = "keep";
791 }
792 }
793
@@ -821,6 +811,27 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
811 if (move_temp_to_file(curr_index_name, final_index_name))
812 die("cannot store index file");
813 }
814 +
815 + if (!from_stdin) {
816 + printf("%s\n", sha1_to_hex(sha1));
817 + } else {
818 + char buf[48];
819 + int len = snprintf(buf, sizeof(buf), "%s\t%s\n",
820 + report, sha1_to_hex(sha1));
821 + xwrite(1, buf, len);
822 +
823 + /*
824 + * Let's just mimic git-unpack-objects here and write
825 + * the last part of the input buffer to stdout.
826 + */
827 + while (input_len) {
828 + err = xwrite(1, input_buffer + input_offset, input_len);
829 + if (err <= 0)
830 + break;
831 + input_len -= err;
832 + input_offset += err;
833 + }
834 + }
835 }
836
837 int main(int argc, char **argv)
@@ -937,8 +948,5 @@ int main(int argc, char **argv)
948 free(index_name_buf);
949 free(keep_name_buf);
950
940 - if (!from_stdin)
941 - printf("%s\n", sha1_to_hex(sha1));
942 -
951 return 0;
952 }
receive-pack.c
+73 -23
@@ -3,8 +3,10 @@
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "run-command.h"
6 +#include "exec_cmd.h"
7 #include "commit.h"
8 #include "object.h"
9 +#include <sys/wait.h>
10
11 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
12
@@ -251,12 +253,13 @@ static const char *parse_pack_header(struct pack_header *hdr)
253 return NULL;
254 }
255
256 +static const char *pack_lockfile;
257 +
258 static const char *unpack(void)
259 {
260 struct pack_header hdr;
261 const char *hdr_err;
262 char hdr_arg[38];
259 - int code;
263
264 hdr_err = parse_pack_header(&hdr);
265 if (hdr_err)
@@ -265,33 +268,13 @@ static const char *unpack(void)
268 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
269
270 if (ntohl(hdr.hdr_entries) < unpack_limit) {
271 + int code;
272 const char *unpacker[3];
273 unpacker[0] = "unpack-objects";
274 unpacker[1] = hdr_arg;
275 unpacker[2] = NULL;
276 code = run_command_v_opt(1, unpacker, RUN_GIT_CMD);
273 - } else {
274 - const char *keeper[6];
275 - char my_host[255], keep_arg[128 + 255];
276 -
277 - if (gethostname(my_host, sizeof(my_host)))
278 - strcpy(my_host, "localhost");
279 - snprintf(keep_arg, sizeof(keep_arg),
280 - "--keep=receive-pack %i on %s",
281 - getpid(), my_host);
282 -
283 - keeper[0] = "index-pack";
284 - keeper[1] = "--stdin";
285 - keeper[2] = "--fix-thin";
286 - keeper[3] = hdr_arg;
287 - keeper[4] = keep_arg;
288 - keeper[5] = NULL;
289 - code = run_command_v_opt(1, keeper, RUN_GIT_CMD);
290 - if (!code)
291 - reprepare_packed_git();
292 - }
293 -
294 - switch (code) {
277 + switch (code) {
278 case 0:
279 return NULL;
280 case -ERR_RUN_COMMAND_FORK:
@@ -308,6 +291,71 @@ static const char *unpack(void)
291 return "unpacker died strangely";
292 default:
293 return "unpacker exited with error code";
294 + }
295 + } else {
296 + const char *keeper[6];
297 + int fd[2], s, len, status;
298 + pid_t pid;
299 + char keep_arg[256];
300 + char packname[46];
301 +
302 + s = sprintf(keep_arg, "--keep=receive-pack %i on ", getpid());
303 + if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
304 + strcpy(keep_arg + s, "localhost");
305 +
306 + keeper[0] = "index-pack";
307 + keeper[1] = "--stdin";
308 + keeper[2] = "--fix-thin";
309 + keeper[3] = hdr_arg;
310 + keeper[4] = keep_arg;
311 + keeper[5] = NULL;
312 +
313 + if (pipe(fd) < 0)
314 + return "index-pack pipe failed";
315 + pid = fork();
316 + if (pid < 0)
317 + return "index-pack fork failed";
318 + if (!pid) {
319 + dup2(fd[1], 1);
320 + close(fd[1]);
321 + close(fd[0]);
322 + execv_git_cmd(keeper);
323 + die("execv of index-pack failed");
324 + }
325 + close(fd[1]);
326 +
327 + /*
328 + * The first thing we expects from index-pack's output
329 + * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where
330 + * %40s is the newly created pack SHA1 name. In the "keep"
331 + * case, we need it to remove the corresponding .keep file
332 + * later on. If we don't get that then tough luck with it.
333 + */
334 + for (len = 0;
335 + len < 46 && (s = xread(fd[0], packname+len, 46-len)) > 0;
336 + len += s);
337 + close(fd[0]);
338 + if (len == 46 && packname[45] == '\n' &&
339 + memcmp(packname, "keep\t", 5) == 0) {
340 + char path[PATH_MAX];
341 + packname[45] = 0;
342 + snprintf(path, sizeof(path), "%s/pack/pack-%s.keep",
343 + get_object_directory(), packname + 5);
344 + pack_lockfile = xstrdup(path);
345 + }
346 +
347 + /* Then wrap our index-pack process. */
348 + while (waitpid(pid, &status, 0) < 0)
349 + if (errno != EINTR)
350 + return "waitpid failed";
351 + if (WIFEXITED(status)) {
352 + int code = WEXITSTATUS(status);
353 + if (code)
354 + return "index-pack exited with error code";
355 + reprepare_packed_git();
356 + return NULL;
357 + }
358 + return "index-pack abnormal exit";
359 }
360 }
361
@@ -363,6 +411,8 @@ int main(int argc, char **argv)
411 const char *unpack_status = unpack();
412 if (!unpack_status)
413 execute_commands();
414 + if (pack_lockfile)
415 + unlink(pack_lockfile);
416 if (report_status)
417 report(unpack_status);
418 }