home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / dpkg / info / xserver-xorg-core.postinst < prev    next >
Encoding:
Text File  |  2009-04-08  |  28.2 KB  |  887 lines

  1. #!/bin/sh
  2.  
  3. set -e
  4.  
  5. THIS_PACKAGE=xserver-xorg-core
  6. THIS_SCRIPT=postinst
  7.  
  8. # $Id$
  9.  
  10. # This is the X Strike Force shell library for X Window System package
  11. # maintainer scripts.  It serves to define shell functions commonly used by
  12. # such packages, and performs some error checking necessary for proper operation
  13. # of those functions.  By itself, it does not "do" much; the maintainer scripts
  14. # invoke the functions defined here to accomplish package installation and
  15. # removal tasks.
  16.  
  17. # If you are reading this within a Debian package maintainer script (e.g.,
  18. # /var/lib/dpkg)info/PACKAGE.{config,preinst,postinst,prerm,postrm}), you can
  19. # skip past this library by scanning forward in this file to the string
  20. # "GOBSTOPPER".
  21.  
  22. SOURCE_VERSION=2:1.6.0-0ubuntu14
  23. OFFICIAL_BUILD=
  24.  
  25. # Use special abnormal exit codes so that problems with this library are more
  26. # easily tracked down.
  27. SHELL_LIB_INTERNAL_ERROR=86
  28. SHELL_LIB_THROWN_ERROR=74
  29. SHELL_LIB_USAGE_ERROR=99
  30.  
  31. # old -> new variable names
  32. if [ -z "$DEBUG_XORG_PACKAGE" ] && [ -n "$DEBUG_XFREE86_PACKAGE" ]; then
  33.   DEBUG_XORG_PACKAGE="$DEBUG_XFREE86_PACKAGE"
  34. fi
  35. if [ -z "$DEBUG_XORG_DEBCONF" ] && [ -n "$DEBUG_XFREE86_DEBCONF" ]; then
  36.   DEBUG_XORG_DEBCONF="$DEBUG_XFREE86_DEBCONF"
  37. fi
  38.  
  39. # initial sanity checks
  40. if [ -z "$THIS_PACKAGE" ]; then
  41.   cat >&2 <<EOF
  42. Error: package maintainer script attempted to use shell library without
  43. definining \$THIS_PACKAGE shell variable.  Please report the package name,
  44. version, and the text of this error message to the Debian Bug Tracking System.
  45. Visit <http://www.debian.org/Bugs/Reporting> on the World Wide Web for
  46. instructions, read the file /usr/share/doc/debian/bug-reporting.txt from the
  47. "doc-debian" package, or install the "reportbug" package and use the command of
  48. the same name to file a report against version $SOURCE_VERSION of this package.
  49. EOF
  50.   exit $SHELL_LIB_USAGE_ERROR
  51. fi
  52.  
  53. if [ -z "$THIS_SCRIPT" ]; then
  54.   cat >&2 <<EOF
  55. Error: package maintainer script attempted to use shell library without
  56. definining \$THIS_SCRIPT shell variable.  Please report the package name,
  57. version, and the text of this error message to the Debian Bug Tracking System.
  58. Visit <http://www.debian.org/Bugs/Reporting> on the World Wide Web for
  59. instructions, read the file /usr/share/doc/debian/bug-reporting.txt from the
  60. "doc-debian" package, or install the "reportbug" package and use the command of
  61. the same name to file a report against version $SOURCE_VERSION of the
  62. "$THIS_PACKAGE" package.
  63. EOF
  64.   exit $SHELL_LIB_USAGE_ERROR
  65. fi
  66.  
  67. ARCHITECTURE="$(dpkg --print-installation-architecture)"
  68.  
  69. if [ "$1" = "reconfigure" ] || [ -n "$DEBCONF_RECONFIGURE" ]; then
  70.   RECONFIGURE="true"
  71. else
  72.   RECONFIGURE=
  73. fi
  74.  
  75. if ([ "$1" = "install" ] || [ "$1" = "configure" ]) && [ -z "$2" ]; then
  76.   FIRSTINST="yes"
  77. fi
  78.  
  79. if [ -z "$RECONFIGURE" ] && [ -z "$FIRSTINST" ]; then
  80.   UPGRADE="yes"
  81. fi
  82.  
  83. trap "message;\
  84.       message \"Received signal.  Aborting $THIS_PACKAGE package $THIS_SCRIPT script.\";\
  85.       message;\
  86.       exit 1" HUP INT QUIT TERM
  87.  
  88. reject_nondigits () {
  89.   # syntax: reject_nondigits [ operand ... ]
  90.   #
  91.   # scan operands (typically shell variables whose values cannot be trusted) for
  92.   # characters other than decimal digits and barf if any are found
  93.   while [ -n "$1" ]; do
  94.     # does the operand contain anything but digits?
  95.     if ! expr "$1" : "[[:digit:]]\+$" > /dev/null 2>&1; then
  96.       # can't use die(), because it wraps message() which wraps this function
  97.       echo "$THIS_PACKAGE $THIS_SCRIPT error: reject_nondigits() encountered" \
  98.            "possibly malicious garbage \"$1\"" >&2
  99.       exit $SHELL_LIB_THROWN_ERROR
  100.     fi
  101.     shift
  102.   done
  103. }
  104.  
  105. reject_whitespace () {
  106.   # syntax: reject_whitespace [ operand ]
  107.   #
  108.   # scan operand (typically a shell variable whose value cannot be trusted) for
  109.   # whitespace characters and barf if any are found
  110.   if [ -n "$1" ]; then
  111.     # does the operand contain any whitespace?
  112.     if expr "$1" : "[[:space:]]" > /dev/null 2>&1; then
  113.       # can't use die(), because I want to avoid forward references
  114.       echo "$THIS_PACKAGE $THIS_SCRIPT error: reject_whitespace() encountered" \
  115.            "possibly malicious garbage \"$1\"" >&2
  116.       exit $SHELL_LIB_THROWN_ERROR
  117.     fi
  118.   fi
  119. }
  120.  
  121. reject_unlikely_path_chars () {
  122.   # syntax: reject_unlikely_path_chars [ operand ... ]
  123.   #
  124.   # scan operands (typically shell variables whose values cannot be trusted) for
  125.   # characters unlikely to be seen in a path and which the shell might
  126.   # interpret and barf if any are found
  127.   while [ -n "$1" ]; do
  128.     # does the operand contain any funny characters?
  129.     if expr "$1" : '.*[!$&()*;<>?|].*' > /dev/null 2>&1; then
  130.       # can't use die(), because I want to avoid forward references
  131.       echo "$THIS_PACKAGE $THIS_SCRIPT error: reject_unlikely_path_chars()" \
  132.            "encountered possibly malicious garbage \"$1\"" >&2
  133.       exit $SHELL_LIB_THROWN_ERROR
  134.     fi
  135.     shift
  136.   done
  137. }
  138.  
  139. # Query the terminal to establish a default number of columns to use for
  140. # displaying messages to the user.  This is used only as a fallback in the
  141. # event the COLUMNS variable is not set.  ($COLUMNS can react to SIGWINCH while
  142. # the script is running, and this cannot, only being calculated once.)
  143. DEFCOLUMNS=$(stty size 2> /dev/null | awk '{print $2}') || true
  144. if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" > /dev/null 2>&1; then
  145.   DEFCOLUMNS=80
  146. fi
  147.  
  148. message () {
  149.   # pretty-print messages of arbitrary length
  150.   reject_nondigits "$COLUMNS"
  151.   echo "$*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS} >&2
  152. }
  153.  
  154. observe () {
  155.   # syntax: observe message ...
  156.   #
  157.   # issue observational message suitable for logging someday when support for
  158.   # it exists in dpkg
  159.   if [ -n "$DEBUG_XORG_PACKAGE" ]; then
  160.     message "$THIS_PACKAGE $THIS_SCRIPT note: $*"
  161.   fi
  162. }
  163.  
  164. warn () {
  165.   # syntax: warn message ...
  166.   #
  167.   # issue warning message suitable for logging someday when support for
  168.   # it exists in dpkg; also send to standard error
  169.   message "$THIS_PACKAGE $THIS_SCRIPT warning: $*"
  170. }
  171.  
  172. die () {
  173.   # syntax: die message ...
  174.   #
  175.   # exit script with error message
  176.   message "$THIS_PACKAGE $THIS_SCRIPT error: $*"
  177.   exit $SHELL_LIB_THROWN_ERROR
  178. }
  179.  
  180. internal_error () {
  181.   # exit script with error; essentially a "THIS SHOULD NEVER HAPPEN" message
  182.   message "internal error: $*"
  183.   if [ -n "$OFFICIAL_BUILD" ]; then
  184.     message "Please report a bug in the $THIS_SCRIPT script of the" \
  185.             "$THIS_PACKAGE package, version $SOURCE_VERSION to the Debian Bug" \
  186.             "Tracking System.  Include all messages above that mention the" \
  187.             "$THIS_PACKAGE package.  Visit " \
  188.             "<http://www.debian.org/Bugs/Reporting> on the World Wide Web for" \
  189.             "instructions, read the file" \
  190.             "/usr/share/doc/debian/bug-reporting.txt from the doc-debian" \
  191.             "package, or install the reportbug package and use the command of" \
  192.             "the same name to file a report."
  193.   fi
  194.   exit $SHELL_LIB_INTERNAL_ERROR
  195. }
  196.  
  197. usage_error () {
  198.   message "usage error: $*"
  199.   message "Please report a bug in the $THIS_SCRIPT script of the" \
  200.           "$THIS_PACKAGE package, version $SOURCE_VERSION to the Debian Bug" \
  201.           "Tracking System.  Include all messages above that mention the" \
  202.           "$THIS_PACKAGE package.  Visit " \
  203.           "<http://www.debian.org/Bugs/Reporting> on the World Wide Web for" \
  204.           "instructions, read the file" \
  205.           "/usr/share/doc/debian/bug-reporting.txt from the doc-debian" \
  206.           "package, or install the reportbug package and use the command of" \
  207.           "the same name to file a report."
  208.   exit $SHELL_LIB_USAGE_ERROR
  209. }
  210.  
  211.  
  212. maplink () {
  213.   # returns what symlink should point to; i.e., what the "sane" answer is
  214.   # Keep this in sync with the debian/*.links files.
  215.   # This is only needed for symlinks to directories.
  216.   #
  217.   # XXX: Most of these look wrong in the X11R7 world and need to be fixed.
  218.   # If we've stopped using this function, fixing it might enable us to re-enable
  219.   # it again and catch more errors.
  220.   case "$1" in
  221.     /etc/X11/xkb/compiled) echo /var/lib/xkb ;;
  222.     /etc/X11/xkb/xkbcomp) echo /usr/X11R6/bin/xkbcomp ;;
  223.     /usr/X11R6/lib/X11/app-defaults) echo /etc/X11/app-defaults ;;
  224.     /usr/X11R6/lib/X11/fs) echo /etc/X11/fs ;;
  225.     /usr/X11R6/lib/X11/lbxproxy) echo /etc/X11/lbxproxy ;;
  226.     /usr/X11R6/lib/X11/proxymngr) echo /etc/X11/proxymngr ;;
  227.     /usr/X11R6/lib/X11/rstart) echo /etc/X11/rstart ;;
  228.     /usr/X11R6/lib/X11/twm) echo /etc/X11/twm ;;
  229.     /usr/X11R6/lib/X11/xdm) echo /etc/X11/xdm ;;
  230.     /usr/X11R6/lib/X11/xinit) echo /etc/X11/xinit ;;
  231.     /usr/X11R6/lib/X11/xkb) echo /etc/X11/xkb ;;
  232.     /usr/X11R6/lib/X11/xserver) echo /etc/X11/xserver ;;
  233.     /usr/X11R6/lib/X11/xsm) echo /etc/X11/xsm ;;
  234.     /usr/bin/X11) echo ../X11R6/bin ;;
  235.     /usr/bin/rstartd) echo ../X11R6/bin/rstartd ;;
  236.     /usr/include/X11) echo ../X11R6/include/X11 ;;
  237.     /usr/lib/X11) echo ../X11R6/lib/X11 ;;
  238.     *) internal_error "maplink() called with unknown path \"$1\"" ;;
  239.   esac
  240. }
  241.  
  242. analyze_path () {
  243.   # given a supplied set of pathnames, break each one up by directory and do an
  244.   # ls -dl on each component, cumulatively; i.e.
  245.   # analyze_path /usr/X11R6/bin -> ls -dl /usr /usr/X11R6 /usr/X11R6/bin
  246.   # Thanks to Randolph Chung for this clever hack.
  247.  
  248.   local f g
  249.  
  250.   while [ -n "$1" ]; do
  251.     reject_whitespace "$1"
  252.     g=
  253.     message "Analyzing $1:"
  254.     for f in $(echo "$1" | tr / \  ); do
  255.       if [ -e /$g$f ]; then
  256.         ls -dl /$g$f /$g$f.dpkg-* 2> /dev/null || true
  257.         g=$g$f/
  258.       else
  259.         message "/$g$f: nonexistent; directory contents of /$g:"
  260.         ls -l /$g
  261.         break
  262.       fi
  263.     done
  264.     shift
  265.   done
  266. }
  267.  
  268. find_culprits () {
  269.   local f p dpkg_info_dir possible_culprits smoking_guns bad_packages package \
  270.     msg
  271.  
  272.   reject_whitespace "$1"
  273.   message "Searching for overlapping packages..."
  274.   dpkg_info_dir=/var/lib/dpkg/info
  275.   if [ -d $dpkg_info_dir ]; then
  276.     if [ "$(echo $dpkg_info_dir/*.list)" != "$dpkg_info_dir/*.list" ]; then
  277.       possible_culprits=$(ls -1 $dpkg_info_dir/*.list | egrep -v \
  278.         "(xbase-clients|x11-common|xfs|xlibs)")
  279.       if [ -n "$possible_culprits" ]; then
  280.         smoking_guns=$(grep -l "$1" $possible_culprits || true)
  281.         if [ -n "$smoking_guns" ]; then
  282.           bad_packages=$(printf "\\n")
  283.           for f in $smoking_guns; do
  284.             # too bad you can't nest parameter expansion voodoo
  285.             p=${f%*.list}      # strip off the trailing ".list"
  286.             package=${p##*/}   # strip off the directories
  287.             bad_packages=$(printf "%s\n%s" "$bad_packages" "$package")
  288.           done
  289.           msg=$(cat <<EOF
  290. The following packages appear to have file overlaps with the X.Org packages;
  291. these packages are either very old, or in violation of Debian Policy.  Try
  292. upgrading each of these packages to the latest available version if possible:
  293. for example, with the command "apt-get install".  If no newer version of a
  294. package is available, you will have to remove it; for example, with the command
  295. "apt-get remove".  If even the latest available version of the package has
  296. this file overlap, please file a bug against that package with the Debian Bug
  297. Tracking System.  You may want to refer the package maintainer to section 12.8
  298. of the Debian Policy manual.
  299. EOF
  300. )
  301.           message "$msg"
  302.           message "The overlapping packages are: $bad_packages"
  303.         else
  304.           message "no overlaps found."
  305.         fi
  306.       fi
  307.     else
  308.       message "cannot search; no matches for $dpkg_info_dir/*.list."
  309.     fi
  310.   else
  311.     message "cannot search; $dpkg_info_dir does not exist."
  312.   fi
  313. }
  314.  
  315. # we require a readlink command or shell function
  316. if ! which readlink > /dev/null 2>&1; then
  317.   message "The readlink command was not found.  Please install version" \
  318.           "1.13.1 or later of the debianutils package."
  319.   readlink () {
  320.     # returns what symlink in $1 actually points to
  321.     perl -e '$l = shift; exit 1 unless -l $l; $r = readlink $l; exit 1 unless $r; print "$r\n"' "$1"
  322.   }
  323. fi
  324.  
  325. check_symlink () {
  326.   # syntax: check_symlink symlink
  327.   #
  328.   # See if specified symlink points where it is supposed to.  Return 0 if it
  329.   # does, and 1 if it does not.
  330.   #
  331.   # Primarily used by check_symlinks_and_warn() and check_symlinks_and_bomb().
  332.  
  333.   local symlink
  334.  
  335.   # validate arguments
  336.   if [ $# -ne 1 ]; then
  337.     usage_error "check_symlink() called with wrong number of arguments;" \
  338.                 "expected 1, got $#"
  339.     exit $SHELL_LIB_USAGE_ERROR
  340.   fi
  341.  
  342.   symlink="$1"
  343.  
  344.   if [ "$(maplink "$symlink")" = "$(readlink "$symlink")" ]; then
  345.     return 0
  346.   else
  347.     return 1
  348.   fi
  349. }
  350.  
  351. check_symlinks_and_warn () {
  352.   # syntax: check_symlinks_and_warn symlink ...
  353.   #
  354.   # For each argument, check for symlink sanity, and warn if it isn't sane.
  355.   #
  356.   # Call this function from a preinst script in the event $1 is "upgrade" or
  357.   # "install".
  358.  
  359.   local errmsg symlink
  360.  
  361.   # validate arguments
  362.   if [ $# -lt 1 ]; then
  363.     usage_error "check_symlinks_and_warn() called with wrong number of" \
  364.                 "arguments; expected at least 1, got $#"
  365.     exit $SHELL_LIB_USAGE_ERROR
  366.   fi
  367.  
  368.   while [ -n "$1" ]; do
  369.     symlink="$1"
  370.     if [ -L "$symlink" ]; then
  371.       if ! check_symlink "$symlink"; then
  372.         observe "$symlink symbolic link points to wrong location" \
  373.                 "$(readlink "$symlink"); removing"
  374.         rm "$symlink"
  375.       fi
  376.     elif [ -e "$symlink" ]; then
  377.       errmsg="$symlink exists and is not a symbolic link; this package cannot"
  378.       errmsg="$errmsg be installed until this"
  379.       if [ -f "$symlink" ]; then
  380.         errmsg="$errmsg file"
  381.       elif [ -d "$symlink" ]; then
  382.         errmsg="$errmsg directory"
  383.       else
  384.         errmsg="$errmsg thing"
  385.       fi
  386.       errmsg="$errmsg is removed"
  387.       die "$errmsg"
  388.     fi
  389.     shift
  390.   done
  391. }
  392.  
  393. check_symlinks_and_bomb () {
  394.   # syntax: check_symlinks_and_bomb symlink ...
  395.   #
  396.   # For each argument, check for symlink sanity, and bomb if it isn't sane.
  397.   #
  398.   # Call this function from a postinst script.
  399.  
  400.   local problem symlink
  401.  
  402.   # validate arguments
  403.   if [ $# -lt 1 ]; then
  404.     usage_error "check_symlinks_and_bomb() called with wrong number of"
  405.                 "arguments; expected at least 1, got $#"
  406.     exit $SHELL_LIB_USAGE_ERROR
  407.   fi
  408.  
  409.   while [ -n "$1" ]; do
  410.     problem=
  411.     symlink="$1"
  412.     if [ -L "$symlink" ]; then
  413.       if ! check_symlink "$symlink"; then
  414.         problem=yes
  415.         warn "$symlink symbolic link points to wrong location" \
  416.              "$(readlink "$symlink")"
  417.       fi
  418.     elif [ -e "$symlink" ]; then
  419.       problem=yes
  420.       warn "$symlink is not a symbolic link"
  421.     else
  422.       problem=yes
  423.       warn "$symlink symbolic link does not exist"
  424.     fi
  425.     if [ -n "$problem" ]; then
  426.       analyze_path "$symlink" "$(readlink "$symlink")"
  427.       find_culprits "$symlink"
  428.       die "bad symbolic links on system"
  429.     fi
  430.     shift
  431.   done
  432. }
  433.  
  434. font_update () {
  435.   # run $UPDATECMDS in $FONTDIRS
  436.  
  437.   local dir cmd shortcmd x_font_dir_prefix
  438.  
  439.   x_font_dir_prefix="/usr/share/fonts/X11"
  440.  
  441.   if [ -z "$UPDATECMDS" ]; then
  442.     usage_error "font_update() called but \$UPDATECMDS not set"
  443.   fi
  444.   if [ -z "$FONTDIRS" ]; then
  445.     usage_error "font_update() called but \$FONTDIRS not set"
  446.   fi
  447.  
  448.   reject_unlikely_path_chars "$UPDATECMDS"
  449.   reject_unlikely_path_chars "$FONTDIRS"
  450.  
  451.   for dir in $FONTDIRS; do
  452.     if [ -d "$x_font_dir_prefix/$dir" ]; then
  453.       for cmd in $UPDATECMDS; do
  454.         if which "$cmd" > /dev/null 2>&1; then
  455.           shortcmd=${cmd##*/}
  456.           observe "running $shortcmd in $dir font directory"
  457.       cmd_opts=
  458.           if [ "$shortcmd" = "update-fonts-alias" ]; then
  459.             cmd_opts=--x11r7-layout
  460.           fi
  461.           if [ "$shortcmd" = "update-fonts-dir" ]; then
  462.             cmd_opts=--x11r7-layout
  463.           fi
  464.           if [ "$shortcmd" = "update-fonts-scale" ]; then
  465.             cmd_opts=--x11r7-layout
  466.           fi
  467.           $cmd $cmd_opts $dir || warn "$cmd $cmd_opts $dir" \
  468.                               "failed; font directory data may not" \
  469.                               "be up to date"
  470.         else
  471.           warn "$cmd not found; not updating corresponding $dir font" \
  472.                "directory data"
  473.         fi
  474.       done
  475.     else
  476.       warn "$dir is not a directory; not updating font directory data"
  477.     fi
  478.   done
  479. }
  480.  
  481. remove_conffile_prepare () {
  482.   # syntax: remove_conffile_prepare filename official_md5sum ...
  483.   #
  484.   # Check a conffile "filename" against a list of canonical MD5 checksums.
  485.   # If the file's current MD5 checksum matches one of the "official_md5sum"
  486.   # operands provided, then prepare the conffile for removal from the system.
  487.   # We defer actual deletion until the package is configured so that we can
  488.   # roll this operation back if package installation fails.
  489.   #
  490.   # Call this function from a preinst script in the event $1 is "upgrade" or
  491.   # "install" and verify $2 to ensure the package is being upgraded from a
  492.   # version (or installed over a version removed-but-not-purged) prior to the
  493.   # one in which the conffile was obsoleted.
  494.  
  495.   local conffile current_checksum
  496.  
  497.   # validate arguments
  498.   if [ $# -lt 2 ]; then
  499.     usage_error "remove_conffile_prepare() called with wrong number of" \
  500.                 "arguments; expected at least 2, got $#"
  501.     exit $SHELL_LIB_USAGE_ERROR
  502.   fi
  503.  
  504.   conffile="$1"
  505.   shift
  506.  
  507.   # does the conffile even exist?
  508.   if [ -e "$conffile" ]; then
  509.     # calculate its checksum
  510.     current_checksum=$(md5sum < "$conffile" | sed 's/[[:space:]].*//')
  511.     # compare it to each supplied checksum
  512.     while [ -n "$1" ]; do
  513.       if [ "$current_checksum" = "$1" ]; then
  514.         # we found a match; move the confffile and stop looking
  515.         observe "preparing obsolete conffile $conffile for removal"
  516.         mv "$conffile" "$conffile.$THIS_PACKAGE-tmp"
  517.         break
  518.       fi
  519.       shift
  520.     done
  521.   fi
  522. }
  523.  
  524. remove_conffile_lookup () {
  525.   # syntax: remove_conffile_lookup package filename
  526.   #
  527.   # Lookup the md5sum of a conffile in dpkg's database, and prepare for removal
  528.   # if it matches the actual file's md5sum.
  529.   #
  530.   # Call this function when you would call remove_conffile_prepare but only
  531.   # want to check against dpkg's status database instead of known checksums.
  532.  
  533.   local package conffile old_md5sum
  534.  
  535.   # validate arguments
  536.   if [ $# -ne 2 ]; then
  537.     usage_error "remove_conffile_lookup() called with wrong number of" \
  538.                 "arguments; expected 1, got $#"
  539.     exit $SHELL_LIB_USAGE_ERROR
  540.   fi
  541.  
  542.   package="$1"
  543.   conffile="$2"
  544.  
  545.   if ! [ -e "$conffile" ]; then
  546.     return
  547.   fi
  548.   old_md5sum="$(dpkg-query -W -f='${Conffiles}' "$package" | \
  549.     awk '{ if (match($0, "^ '"$conffile"' ")) print $2}')"
  550.   if [ -n "$old_md5sum" ]; then
  551.     remove_conffile_prepare "$conffile" "$old_md5sum"
  552.   fi
  553. }
  554.  
  555. remove_conffile_commit () {
  556.   # syntax: remove_conffile_commit filename
  557.   #
  558.   # Complete the removal of a conffile "filename" that has become obsolete.
  559.   #
  560.   # Call this function from a postinst script after having used
  561.   # remove_conffile_prepare() in the preinst.
  562.  
  563.   local conffile
  564.  
  565.   # validate arguments
  566.   if [ $# -ne 1 ]; then
  567.     usage_error "remove_conffile_commit() called with wrong number of" \
  568.                 "arguments; expected 1, got $#"
  569.     exit $SHELL_LIB_USAGE_ERROR
  570.   fi
  571.  
  572.   conffile="$1"
  573.  
  574.   # if the temporary file created by remove_conffile_prepare() exists, remove it
  575.   if [ -e "$conffile.$THIS_PACKAGE-tmp" ]; then
  576.     observe "committing removal of obsolete conffile $conffile"
  577.     rm "$conffile.$THIS_PACKAGE-tmp"
  578.   fi
  579. }
  580.  
  581. remove_conffile_rollback () {
  582.   # syntax: remove_conffile_rollback filename
  583.   #
  584.   # Roll back the removal of a conffile "filename".
  585.   #
  586.   # Call this function from a postrm script in the event $1 is "abort-upgrade"
  587.   # or "abort-install" is  after having used remove_conffile_prepare() in the
  588.   # preinst.
  589.  
  590.   local conffile
  591.  
  592.   # validate arguments
  593.   if [ $# -ne 1 ]; then
  594.     usage_error "remove_conffile_rollback() called with wrong number of" \
  595.                 "arguments; expected 1, got $#"
  596.     exit $SHELL_LIB_USAGE_ERROR
  597.   fi
  598.  
  599.   conffile="$1"
  600.  
  601.   # if the temporary file created by remove_conffile_prepare() exists, move it
  602.   # back
  603.   if [ -e "$conffile.$THIS_PACKAGE-tmp" ]; then
  604.     observe "rolling back removal of obsolete conffile $conffile"
  605.     mv "$conffile.$THIS_PACKAGE-tmp" "$conffile"
  606.   fi
  607. }
  608.  
  609. replace_conffile_with_symlink_prepare () {
  610.   # syntax: replace_conffile_with_symlink_prepare oldfilename newfilename \
  611.   # official_md5sum ...
  612.   #
  613.   # Check a conffile "oldfilename" against a list of canonical MD5 checksums.
  614.   # If the file's current MD5 checksum matches one of the "official_md5sum"
  615.   # operands provided, then prepare the conffile for removal from the system.
  616.   # We defer actual deletion until the package is configured so that we can
  617.   # roll this operation back if package installation fails. Otherwise copy it
  618.   # to newfilename and let dpkg handle it through conffiles mechanism.
  619.   #
  620.   # Call this function from a preinst script in the event $1 is "upgrade" or
  621.   # "install" and verify $2 to ensure the package is being upgraded from a
  622.   # version (or installed over a version removed-but-not-purged) prior to the
  623.   # one in which the conffile was obsoleted.
  624.  
  625.   local conffile current_checksum
  626.  
  627.   # validate arguments
  628.   if [ $# -lt 3 ]; then
  629.     usage_error "replace_conffile_with_symlink_prepare() called with wrong" \
  630.                 " number of arguments; expected at least 3, got $#"
  631.     exit $SHELL_LIB_USAGE_ERROR
  632.   fi
  633.  
  634.   oldconffile="$1"
  635.   shift
  636.   newconffile="$1"
  637.   shift
  638.  
  639.   remove_conffile_prepare "$_oldconffile" "$@"
  640.   # If $oldconffile still exists, then md5sums didn't match.
  641.   # Copy it to new one.
  642.   if [ -f "$oldconffile" ]; then
  643.     cp "$oldconffile" "$newconffile"
  644.   fi
  645.  
  646. }
  647.  
  648. replace_conffile_with_symlink_commit () {
  649.   # syntax: replace_conffile_with_symlink_commit oldfilename
  650.   #
  651.   # Complete the removal of a conffile "oldfilename" that has been
  652.   # replaced by a symlink.
  653.   #
  654.   # Call this function from a postinst script after having used
  655.   # replace_conffile_with_symlink_prepare() in the preinst.
  656.  
  657.   local conffile
  658.  
  659.   # validate arguments
  660.   if [ $# -ne 1 ]; then
  661.     usage_error "replace_conffile_with_symlink_commit() called with wrong" \
  662.                 "number of arguments; expected 1, got $#"
  663.     exit $SHELL_LIB_USAGE_ERROR
  664.   fi
  665.  
  666.   conffile="$1"
  667.  
  668.   remove_conffile_commit "$conffile"
  669. }
  670.  
  671. replace_conffile_with_symlink_rollback () {
  672.   # syntax: replace_conffile_with_symlink_rollback oldfilename newfilename
  673.   #
  674.   # Roll back the replacing of a conffile "oldfilename" with symlink to
  675.   # "newfilename".
  676.   #
  677.   # Call this function from a postrm script in the event $1 is "abort-upgrade"
  678.   # or "abort-install" and verify $2 to ensure the package failed to upgrade
  679.   # from a version (or install over a version removed-but-not-purged) prior
  680.   # to the one in which the conffile was obsoleted.
  681.   # You should have  used replace_conffile_with_symlink_prepare() in the
  682.   # preinst.
  683.  
  684.   local conffile
  685.  
  686.   # validate arguments
  687.   if [ $# -ne 2 ]; then
  688.     usage_error "replace_conffile_with_symlink_rollback() called with wrong" \
  689.                 "number of arguments; expected 2, got $#"
  690.     exit $SHELL_LIB_USAGE_ERROR
  691.   fi
  692.  
  693.   oldconffile="$1"
  694.   newconffile="$2"
  695.  
  696.   remove_conffile_rollback "$_oldconffile"
  697.   if [ -f "$newconffile" ]; then
  698.     rm "$newconffile"
  699.   fi
  700. }
  701.  
  702. run () {
  703.   # syntax: run command [ argument ... ]
  704.   #
  705.   # Run specified command with optional arguments and report its exit status.
  706.   # Useful for commands whose exit status may be nonzero, but still acceptable,
  707.   # or commands whose failure is not fatal to us.
  708.   #
  709.   # NOTE: Do *not* use this function with db_get or db_metaget commands; in
  710.   # those cases the return value of the debconf command *must* be checked
  711.   # before the string returned by debconf is used for anything.
  712.  
  713.   local retval
  714.  
  715.   # validate arguments
  716.   if [ $# -lt 1 ]; then
  717.     usage_error "run() called with wrong number of arguments; expected at" \
  718.                 "least 1, got $#"
  719.     exit $SHELL_LIB_USAGE_ERROR
  720.   fi
  721.  
  722.   "$@" || retval=$?
  723.  
  724.   if [ ${retval:-0} -ne 0 ]; then
  725.     observe "command \"$*\" exited with status $retval"
  726.   fi
  727. }
  728.  
  729. make_symlink_sane () {
  730.   # syntax: make_symlink_sane symlink target
  731.   #
  732.   # Ensure that the symbolic link symlink exists, and points to target.
  733.   #
  734.   # If symlink does not exist, create it and point it at target.
  735.   #
  736.   # If symlink exists but is not a symbolic link, back it up.
  737.   #
  738.   # If symlink exists, is a symbolic link, but points to the wrong location, fix
  739.   # it.
  740.   #
  741.   # If symlink exists, is a symbolic link, and already points to target, do
  742.   # nothing.
  743.   #
  744.   # This function wouldn't be needed if ln had an -I, --idempotent option.
  745.  
  746.   # Validate arguments.
  747.   if [ $# -ne 2 ]; then
  748.     usage_error "make_symlink_sane() called with wrong number of arguments;" \
  749.       "expected 2, got $#"
  750.     exit $SHELL_LIB_USAGE_ERROR
  751.   fi
  752.  
  753.   # We could just use the positional parameters as-is, but that makes things
  754.   # harder to follow.
  755.   local symlink target
  756.  
  757.   symlink="$1"
  758.   target="$2"
  759.  
  760.   if [ -L "$symlink" ] && [ "$(readlink "$symlink")" = "$target" ]; then
  761.       observe "link from $symlink to $target already exists"
  762.   else
  763.     observe "creating symbolic link from $symlink to $target"
  764.     mkdir -p "${target%/*}" "${symlink%/*}"
  765.     ln -s -b -S ".dpkg-old" "$target" "$symlink"
  766.   fi
  767. }
  768.  
  769. migrate_dir_to_symlink () {
  770.   # syntax: migrate_dir_to_symlink old_location new_location
  771.   #
  772.   # Per Debian Policy section 6.5.4, "A directory will never be replaced by a
  773.   # symbolic link to a directory or vice versa; instead, the existing state
  774.   # (symlink or not) will be left alone and dpkg will follow the symlink if
  775.   # there is one."
  776.   #
  777.   # We have to do it ourselves.
  778.   #
  779.   # This function moves the contents of old_location, a directory, into
  780.   # new_location, a directory, then makes old_location a symbolic link to
  781.   # new_location.
  782.   #
  783.   # old_location need not exist, but if it does, it must be a directory (or a
  784.   # symlink to a directory).  If it is not, it is backed up.  If new_location
  785.   # exists already and is not a directory, it is backed up.
  786.   #
  787.   # This function should be called from a package's preinst so that other
  788.   # packages unpacked after this one --- but before this package's postinst runs
  789.   # --- are unpacked into new_location even if their payloads contain
  790.   # old_location filespecs.
  791.  
  792.   # Validate arguments.
  793.   if [ $# -ne 2 ]; then
  794.     usage_error "migrate_dir_to_symlink() called with wrong number of"
  795.                 "arguments; expected 2, got $#"
  796.     exit $SHELL_LIB_USAGE_ERROR
  797.   fi
  798.  
  799.   # We could just use the positional parameters as-is, but that makes things
  800.   # harder to follow.
  801.   local new old
  802.  
  803.   old="$1"
  804.   new="$2"
  805.  
  806.   # Is old location a symlink?
  807.   if [ -L "$old" ]; then
  808.     # Does it already point to new location?
  809.     if [ "$(readlink "$old")" = "$new" ]; then
  810.       # Nothing to do; migration has already been done.
  811.       observe "migration of $old to $new already done"
  812.       return 0
  813.     else
  814.       # Back it up.
  815.       warn "backing up symbolic link $old as $old.dpkg-old"
  816.       mv -b "$old" "$old.dpkg-old"
  817.     fi
  818.   fi
  819.  
  820.   # Does old location exist, but is not a directory?
  821.   if [ -e "$old" ] && ! [ -d "$old" ]; then
  822.       # Back it up.
  823.       warn "backing up non-directory $old as $old.dpkg-old"
  824.       mv -b "$old" "$old.dpkg-old"
  825.   fi
  826.  
  827.   observe "migrating $old to $new"
  828.  
  829.   # Is new location a symlink?
  830.   if [ -L "$new" ]; then
  831.     # Does it point the wrong way, i.e., back to where we're migrating from?
  832.     if [ "$(readlink "$new")" = "$old" ]; then
  833.       # Get rid of it.
  834.       observe "removing symbolic link $new which points to $old"
  835.       rm "$new"
  836.     else
  837.       # Back it up.
  838.       warn "backing up symbolic link $new as $new.dpkg-old"
  839.       mv -b "$new" "$new.dpkg-old"
  840.     fi
  841.   fi
  842.  
  843.   # Does new location exist, but is not a directory?
  844.   if [ -e "$new" ] && ! [ -d "$new" ]; then
  845.     warn "backing up non-directory $new as $new.dpkg-old"
  846.     mv -b "$new" "$new.dpkg-old"
  847.   fi
  848.  
  849.   # Create new directory if it does not yet exist.
  850.   if ! [ -e "$new" ]; then
  851.     observe "creating $new"
  852.     mkdir -p "$new"
  853.   fi
  854.  
  855.   # Copy files in old location to new location.  Back up any filenames that
  856.   # already exist in the new location with the extension ".dpkg-old".
  857.   observe "copying files from $old to $new"
  858.   if ! (cd "$old" && cp -a -b -S ".dpkg-old" . "$new"); then
  859.     die "error(s) encountered while copying files from $old to $new"
  860.   fi
  861.  
  862.   # Remove files at old location.
  863.   observe "removing $old"
  864.   rm -r "$old"
  865.  
  866.   # Create symlink from old location to new location.
  867.   make_symlink_sane "$old" "$new"
  868. }
  869.  
  870. # vim:set ai et sw=2 ts=2 tw=80:
  871.  
  872. # GOBSTOPPER: The X Strike Force shell library ends here.
  873.  
  874. case "$1" in
  875.   configure)
  876.   if dpkg --compare-versions "$2" lt-nl 2:1.5.2-1; then
  877.     remove_conffile_commit /etc/X11/xserver/SecurityPolicy
  878.     rmdir /etc/X11/xserver 2>/dev/null || true
  879.   fi
  880. esac
  881.  
  882.  
  883.  
  884. exit 0
  885.  
  886. # vim:set ai et sw=2 ts=2 tw=80:
  887.