File: files.sh

package info (click to toggle)
dhelp 0.6.27
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 580 kB
  • sloc: ruby: 1,193; sh: 551; perl: 372; makefile: 77
file content (139 lines) | stat: -rw-r--r-- 4,061 bytes parent folder | download | duplicates (4)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File operation functions for Debian maintainer scripts.
# Copyright (C) 2012 Georgios M. Zarkadas <gz@member.fsf.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA.
#
# This file is intended to be sourced by maintainer scripts.


# Test executability (implies existence) of a binary in the system's path.
# ARGS:
# $1=binary_basename
#
is_exec()
{
    # First ensure we are not passed an empty argument,
    # because test -x "" returns success.

    if [ "X${1}" = "X" ]; then
        return 2
    fi

    # Then look for the binary location. If not found,
    # return error, because test -x "" returns success.

    local binary=$(which "${1}" 2>/dev/null)

    if [ "X${binary}" = "X" ]; then
        return 1
    fi

    # If the binary is found, return its executable status.

    test -x "${binary}" 2>/dev/null
}


# Ucf-interfacing of generated conffiles helpers.
# Fall-through behaviour, when ucf is not available:
# -- install our version of the conffile
# -- if old conffile exists and is a file, rename old conffile to 
#    <conffile>.ucf-old
# -- if old conffile or <conffile>.ucf-old exists and is a directory,
#    fail. 

# Copy a conffile to the filesystem and add it to the ucf and ucfr databases.
# ARGS:
# $1=package $2=source $3=destination
#
cp_ucfregister_file ()
{
    local package="${1}"
    local srcfile="${2}"
    local conffile="${3}"

    if is_exec ucf; then
        ucf --debconf-ok --three-way "${srcfile}" "${conffile}"
    else
        # If conffile is a symlink, our move-to-old-then-copy strategy
        # would make the symlink a normal file; account for this.

        if [ -h "${conffile}" ]; then

            local realconf=$(readlink --no-newline \
                --canonicalize "${conffile}" || :)

            if [ "X${realconf}" = "X" ]; then
                >&2 echo "${conffile} is a broken symlink; replacing it"
                rm --force "${conffile}"
            else
                conffile="${realconf}"
            fi
        fi

        # Now, after resolving symlinks, check for existence and being a file.

        if [ -e "${conffile}" ]; then
            if [ -d "${conffile}" ] || [ -d "${conffile}.ucf-old" ]; then
                >&2 echo "${conffile} is a directory; aborting"
                return 10
            fi
            # We do not use the backup option, because in such a case
            # we would being accumulating backups on each upgrade.

            mv --force "${conffile}" "${conffile}.ucf-old"
        fi
        cp --force --preserve "${srcfile}" "${conffile}"
    fi

    if is_exec ucfr; then
        ucfr "${package}" "${conffile}"
    fi
}

# Move a (temporary, ie dynamically created) conffile to the filesystem
# and add it to the ucf and ucfr databases.
# 
# $1=package $2=source $3=destination
#
mv_ucfregister_file ()
{
    cp_ucfregister_file "${1}" "${2}" ${3}
    rm --force "${2}"
}

# Remove a conffile from the filesystem and the ucf and ucfr databases.
# ARGS: 
# $1=package $2=conffile
#
ucf_unregister_file ()
{
    local package="${1}"
    local conffile="${2}"
    local ext

    # the '' value is to delete the file itself
    for ext in '' '~' '%' .bak .ucf-new .ucf-old .ucf-dist; do
        rm -f "${conffile}${ext}" || true
    done
    if is_exec ucf; then
        ucf --purge "${conffile}"
    fi
    if is_exec ucfr; then
        ucfr --purge "${package}" "${conffile}"
    fi
}