gc-util-functions.sh: extract common gc-related functions
[girocco.git] / jobd / gc-util-functions.sh
blob7040434a62e96c85e890089b9ff8546bb69806ec
1 #!/bin/sh
3 # This is a shell library for common 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
10 # make sure combine-packs uses the correct Git executable
11 run_combine_packs() {
12 PATH="$var_git_exec_path:$cfg_basedir/bin:$PATH" @basedir@/jobd/combine-packs.sh "$@"
15 # duplicate the first file to the name given by the second file making sure that
16 # the second file appears atomically all-at-once after the copy has been completed
17 # and does not appear at all if the copy fails (in which case this function fails)
18 # if the second file already exists this function fails with status 1
19 # if the file names are the same this function returns immediately with success
20 dupe_file() {
21 [ "$1" != "$2" ] || return 0
22 ! [ -e "$2" ] || return 1
23 case "$2" in
24 *?/?*) _tmpdir="${2%/*}";;
25 *) _tmpdir=".";;
26 esac
27 _tmpfile="$(mktemp "${_tmpdir:-.}/packtmp-XXXXXX")" || return 1
28 cp -fp "$1" "$_tmpfile" || return 1
29 mv -f "$_tmpfile" "$2"
32 # rename_pack oldnamepath newnamepath
33 # note that .keep and .bndl files are left untouched and not moved at all!
34 rename_pack() {
35 [ $# -eq 2 ] && [ "$1" != "$2" ] || {
36 echo >&2 "[$proj] incorrect use of rename_pack function"
37 exit 1
39 # Git assumes that if the destination of the rename already exists
40 # that it is, in fact, a copy of the same bytes so silently succeeds
41 # without doing anything. We duplicate that logic here.
42 # Git checks for the .idx file first before even trying to use a pack
43 # so it should be the last moved and the first removed.
44 for ext in pack bitmap idx; do
45 [ -f "$1.$ext" ] || continue
46 ln "$1.$ext" "$2.$ext" >/dev/null 2>&1 ||
47 dupe_file "$1.$ext" "$2.$ext" >/dev/null 2>&1 ||
48 [ -f "$2.$ext" ] || {
49 echo >&2 "[$proj] unable to move $1.$ext to $2.$ext"
50 exit 1
52 done
53 for ext in idx pack bitmap; do
54 rm -f "$1.$ext"
55 done
56 return 0
59 # current directory must already be set to the $GIT_DIR
60 # see if there are "lotsa" loose objects
61 # "lotsa" is defined as the 17, 68, 71 and 86 object directories existing
62 # and there being at least 5 total objects between them which corresponds
63 # to an approximate average of 320 loose objects before this function starts
64 # returning true and triggering a "mini" gc to pack up loose objects
65 lotsa_loose_objects() {
66 [ -d objects/17 ] && [ -d objects/68 ] && [ -d objects/71 ] && [ -d objects/86 ] || return 1
67 _objs=$(( $(find -L objects/17 objects/68 objects/71 objects/86 -maxdepth 1 -name "$octet19*" -type f -print 2>/dev/null | LC_ALL=C wc -l) ))
68 [ ${_objs:-0} -ge 5 ]