summaryrefslogtreecommitdiff
path: root/build/cleantmpdirs
blob: 547fa070eaa10e0b7070274be232bd907f252033 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Function to do all of the temporary dir/file migration work
#
_clean_tmpdirs()
{
    #
    # Usage: _clean_tmpdirs [-v] new_dir old_dir ...
    #
    # Move temporary directories (which may be actively in use as
    # with pmdammv files) from one temporary directory to another
    # so as to transparently upgrade without loss of service.
    #
    # -v option is verbose mode for debugging
    #

    _verbose=false
    if [ $# -gt 0 -a X"$1" = "X-v" ]
    then
        _verbose=true
        shift
    fi

    if [ $# -lt 2 ]
    then
        echo >&2 "Usage: _clean_tmpdirs [-v] new_dir old_dir subdirs ..."
        return
    fi

    _new_tmp_dir="$1"
    _old_tmp_dir="$2"

    [ "$_new_tmp_dir" != "$old_tmp_dir" ] || return

    for _subdir
    do
        d="$_old_tmp_dir/$_subdir"
        test -d "$d" -a -k "$d" || continue
        cd "$d" || continue
        for f in * ; do
            [ "$f" != "*" ] || continue
            source="$d/$f"
            target="$_new_tmp_dir/$_subdir/$f"
            [ "$source" != "$target" ] || continue
            [ ! -f "$target" ] || continue
            $_verbose && echo + mv -fu "$source" "$target"
            mv -fu "$source" "$target" || true
        done
        $_verbose && echo + rmdir "$d"
        cd && rmdir "$d" 2>/dev/null || true
    done
}