3 # This is a shell library for common update/gc related functions
4 # used by various Girocco scripts.
6 # shlib.sh always sets this, it's an error to source
7 # this script without having already sourced shlib.sh
8 [ -n "$var_git_exec_path" ] ||
exit 2
11 if _result
="$(kill -0 "$1" 2>&1)"; then
12 # process exists and we have permission to signal it
15 case "$_result" in *"not permitted"*)
16 # we do not have permission to signal the process
19 # process does not exist
24 # A .lock file should only exist for much less than a second.
25 # If we see a stale lock file (> 1h old), remove it and then,
26 # just in case, wait 30 seconds for any process whose .lock
27 # we might have just removed (it's racy) to finish doing what
28 # should take much less than a second to do.
29 _stalelock
="$(find -L "$1.lock
" -maxdepth 1 -mmin +60 -print 2>/dev/null)" ||
:
30 if [ -n "$_stalelock" ]; then
35 if (set -C; >"$1.lock") 2>/dev
/null
; then
40 [ "$_try" != "p" ] ||
sleep 1
42 # cannot create lock file
46 # Create a gc.pid lockfile
47 # $1 => name of variable to store result in
49 # variable named by $1 will contain the name of the newly create lockfile (i.e. "gc.pid")
51 # variable named by $1 will contain the failure reason
53 # be compatibile with gc.pid file from newer Git releases
57 if [ "$(createlock "$_lockf")" ]; then
59 # 1) less than 12 hours old
60 # 2) contains two fields (pid hostname) NO trailing NL
61 # 3) the hostname is different OR the pid is still alive
62 # then we exit as another active process is holding the lock
63 if [ "$(find -L "$_lockf" -maxdepth 1 -mmin -720 -print 2>/dev/null)" ]; then
66 read -r _apid _ahost _ajunk
<"$_lockf" ||
:
67 if [ "$_apid" ] && [ "$_ahost" ]; then
68 if [ "$_ahost" != "$_hn" ] || pidactive
"$_apid"; then
74 eval "$1="'"unable to create $_lockf.lock file"'
77 if [ -n "$_active" ]; then
79 eval "$1="'"gc already running on machine '\''$_ahost'\'' pid '\''$_apid'\''"'
82 printf "%s %s" "$$" "$_hn" >"$_lockf.lock"
83 chmod 0664 "$_lockf.lock"
84 mv -f "$_lockf.lock" "$_lockf"
89 # duplicate the first file to the name given by the second file making sure that
90 # the second file appears atomically all-at-once after the copy has been completed
91 # and does not appear at all if the copy fails (in which case this function fails)
92 # if the second file already exists this function fails with status 1
93 # if the file names are the same this function returns immediately with success
95 [ "$1" != "$2" ] ||
return 0
96 ! [ -e "$2" ] ||
return 1
98 *?
/?
*) _tmpdir
="${2%/*}";;
101 _tmpfile
="$(mktemp "${_tmpdir:-.}/packtmp-XXXXXX
")" ||
return 1
102 cp -fp "$1" "$_tmpfile" ||
return 1
103 mv -f "$_tmpfile" "$2"
106 # rename_pack oldnamepath newnamepath
107 # note that .keep and .bndl files are left untouched and not moved at all!
109 [ $# -eq 2 ] && [ "$1" != "$2" ] ||
{
110 echo >&2 "[$proj] incorrect use of rename_pack function"
113 # Git assumes that if the destination of the rename already exists
114 # that it is, in fact, a copy of the same bytes so silently succeeds
115 # without doing anything. We duplicate that logic here.
116 # Git checks for the .idx file first before even trying to use a pack
117 # so it should be the last moved and the first removed.
118 for ext
in pack bitmap idx
; do
119 [ -f "$1.$ext" ] ||
continue
120 ln "$1.$ext" "$2.$ext" >/dev
/null
2>&1 ||
121 dupe_file
"$1.$ext" "$2.$ext" >/dev
/null
2>&1 ||
122 [ -f "$2.$ext" ] ||
{
123 echo >&2 "[$proj] unable to move $1.$ext to $2.$ext"
127 for ext
in idx pack bitmap
; do