| 1 | #!/bin/bash |
| 2 | |
| 3 | UCF="ucf --three-way --debconf-ok" |
| 4 | |
| 5 | # rename ucf-conffile. This was mostly stolen from cacti.postinst after |
| 6 | # a short discussion on debian-mentors, see |
| 7 | # http://lists.debian.org/debian-mentors/2013/07/msg00027.html |
| 8 | # and the following thread. Thanks to Paul Gevers |
| 9 | rename_ucf_file() { |
| 10 | local oldname |
| 11 | local newname |
| 12 | |
| 13 | # override UCF_FORCE_CONFFNEW with an empty local variable |
| 14 | local UCF_FORCE_CONFFNEW |
| 15 | |
| 16 | oldname="$1" |
| 17 | newname="$2" |
| 18 | if [ ! -e "$newname" ] ; then |
| 19 | if [ -e "$oldname" ]; then |
| 20 | mv "$oldname" "$newname" |
| 21 | fi |
| 22 | # ucf doesn't offer a documented way to do this, so we need to |
| 23 | # peddle around with undocumented ucf internals. |
| 24 | sed -i "s|$oldname|$newname|" /var/lib/ucf/hashfile |
| 25 | ucfr --purge "$PKGNAME" "$oldname" |
| 26 | ucfr "$PKGNAME" "$newname" |
| 27 | # else: Don't do anything, leave old file in place |
| 28 | fi |
| 29 | ucfr "$PKGNAME" "$newname" |
| 30 | } |
| 31 | |
| 32 | generate_directory_structure() { |
| 33 | local pkgdir |
| 34 | local locdir |
| 35 | pkgdir="$1" |
| 36 | locdir="$2" |
| 37 | |
| 38 | # generate empty directory structure |
| 39 | |
| 40 | (cd "$pkgdir" && find . -type d -print0 ) | \ |
| 41 | (cd "$locdir" && xargs -0 mkdir -p --) |
| 42 | } |
| 43 | |
| 44 | # handle a single ucf_conffile by first checking whether the file might be |
| 45 | # accociated with a different package. If so, we keep our hands off the file |
| 46 | # so that a different package can safely hijack our conffiles. |
| 47 | # to hijack a file, simply ucfr it to a package before the ucf processing |
| 48 | # code. |
| 49 | # If the file is either unassociated or already associated with us, call ucf |
| 50 | # proper and register the file as ours. |
| 51 | handle_single_ucf_file() |
| 52 | { |
| 53 | local pkgfile |
| 54 | local locfile |
| 55 | if [ -n "${UCF_HELPER_FUNCTIONS_DEBUG:-}" ]; then |
| 56 | set -x |
| 57 | fi |
| 58 | |
| 59 | pkgfile="$1" |
| 60 | locfile="$2" |
| 61 | export DEBIAN_FRONTEND |
| 62 | |
| 63 | PKG="$(ucfq --with-colons "$locdir/$file" | head -n 1 | cut --delimiter=: --fields=2 )" |
| 64 | # skip conffile if it is associated with a different package. |
| 65 | # This allows other packages to safely hijack our conffiles. |
| 66 | if [ -z "$PKG" ] || [ "$PKG" = "$PKGNAME" ]; then |
| 67 | $UCF "$pkgfile" "$locdir/$file" |
| 68 | ucfr "$PKGNAME" "$locdir/$file" |
| 69 | fi |
| 70 | set +x |
| 71 | } |
| 72 | |
| 73 | # checks whether a file was deleted in the package and handle it on the local |
| 74 | # system appropriately: If the local file differs from what we had previously, |
| 75 | # we just unregister it and leave it on the system (preserving local changes), |
| 76 | # otherwise we remove it. |
| 77 | # this also removes conffiles that are zero-size after the |
| 78 | # ucf run, which might happen if the local admin has |
| 79 | # deleted a conffile that has changed in the package. |
| 80 | handle_deleted_ucf_file() |
| 81 | { |
| 82 | local locfile |
| 83 | local locdir |
| 84 | local pkgdir |
| 85 | if [ -n "${UCF_HELPER_FUNCTIONS_DEBUG:-}" ]; then |
| 86 | set -x |
| 87 | fi |
| 88 | locfile="$1" |
| 89 | pkgdir="$2" |
| 90 | locdir="$3" |
| 91 | |
| 92 | # compute the name of the reference file in $pkgdir |
| 93 | reffile="$(echo "$locfile" | sed "s|$locdir|$pkgdir|")" |
| 94 | if ! [ -e "$reffile" ]; then |
| 95 | # if the reference file does not exist, then it was removed in the package |
| 96 | # do as if the file was replaced with an empty file |
| 97 | $UCF /dev/null "$locfile" |
| 98 | if [ -s "$locfile" ]; then |
| 99 | # the file has non-zero size after the ucf run. local admin must |
| 100 | # have decided to keep the file with contents. Done here. |
| 101 | : |
| 102 | else |
| 103 | # the file has zero size and can be removed |
| 104 | # remove the file itself ('') and all possible backup/reference extensions |
| 105 | for ext in '' '~' '%' .bak .dpkg-tmp .dpkg-new .dpkg-old .dpkg-dist .ucf-new .ucf-old .ucf-dist; do |
| 106 | rm -f "${locfile}$ext" |
| 107 | done |
| 108 | fi |
| 109 | # unregister the file anyhow since the package doesn't know about it any more |
| 110 | ucf --purge "${locfile}" |
| 111 | ucfr --purge "$PKGNAME" "${locfile}" |
| 112 | fi |
| 113 | set +x |
| 114 | } |
| 115 | |
| 116 | handle_all_ucf_files() { |
| 117 | local pkgdir |
| 118 | local locdir |
| 119 | pkgdir="$1" |
| 120 | locdir="$2" |
| 121 | |
| 122 | generate_directory_structure "$pkgdir" "$locdir" |
| 123 | |
| 124 | # handle regular ucf-conffiles by iterating through all conffiles |
| 125 | # that come with the package |
| 126 | for file in $(find "$pkgdir" -type f -printf '%P\n' ); do |
| 127 | handle_single_ucf_file "$pkgdir/$file" "$locdir/$file" |
| 128 | done |
| 129 | |
| 130 | # handle ucf-conffiles that were deleted in our package by iterating |
| 131 | # through all ucf-conffiles that are registered for the package |
| 132 | for locfile in $(ucfq --with-colons "$PKGNAME" | cut --delimiter=: --fields=1); do |
| 133 | handle_deleted_ucf_file "$locfile" "$pkgdir" "$locdir" |
| 134 | done |
| 135 | } |
| 136 | |
| 137 | # vim:sw=4:sts=4:et: |
| 138 |