-chdir $cachedir or fail "chdir $cachedir: $!";
-
-our ($service,$specpath,$spechost,$subdir);
-our ($tmpd,$gitd,$lock);
-our ($fetch,$url);
+sub lockfile ($$$) {
+ my ($fh, $fn, $flockmode) = @_;
+ my $what = $fn.(($flockmode & ~LOCK_NB) == LOCK_SH ? " (shared)" : "");
+ for (;;) {
+ close $fh;
+ open $fh, '+>', $fn or fail "open/create $fn for lock: $!";
+ logm 'debug', "lock $what: acquiring";
+ if (!flock $fh, $flockmode) {
+ if ($flockmode & LOCK_NB && $! == EWOULDBLOCK) {
+ return 0; # ok then
+ }
+ fail "lock $what: $!";
+ }
+ stat $fh or fail "stat opened $fn: $!";
+ my $fh_ino = ((stat _)[1]);
+ if (!stat $fn) {
+ $! == ENOENT or fail "stat $fn: $!";
+ next;
+ }
+ my $fn_ino = ((stat _)[1]);
+ if ($fn_ino == $fh_ino) {
+ logm 'debug', "lock $what: acquired";
+ return 1;
+ }
+ logm 'debug', "lock $what: deleted, need to loop again";
+ # oh dear
+ }
+}