[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bug #583585 dpkg-maintscript-helper



On 2011/12/15 05:12PM, Raphael Hertzog wrote:
[...]
>> In the preinst script, "upgrade" command, I'd invoke the helper as
>> above, and it will result in something like (as per Raphael's
>> suggestions):
>> 1) # Ensure ownership of $dir by the package
>> 2) mv "$dir" "$dir".dpkg_bak
>> 
>> In postinst, command "configure", I'd have
>> 1) rm -rf "$dir".dpkg_bak
>
>Yes.
>
>> and finally, in postrm upgrade I'd have:
>> 1) mv "$dir".dpkg_bak "$dir"
>
>Here it should be "postrm failed-upgrade" I think. And it should not fail
>if $dir.dpkg_bak does not exist (the failure to upgrade might be due to
>something else and maybe that dir doesn't exist yet).
>
>BTW for consistency please use ".dpkg-bak" and not ".dpkg_bak".
>

I've tried to go on about the first case (substitute a directory with a
symlink). I've noticed there are idioms to perform some of the
operations that I had to perform, so I tried to stick to them.

I thought it was a good idea to share with you the patch for the first
case for feedback, before going on.

I've created the patch according to the guidelines at the dpkg team's
website¹. Any feedback would be much appreciated.

Cheers,

¹ http://wiki.debian.org/Teams/Dpkg/Contribute

-- 
Gianluca Ciccarelli
http://disi.unitn.it/~ciccarelli
GPG key ID: FDF429B0

From 27c6be170a5544be6e8aaf6b9f549e4aba0b53c0 Mon Sep 17 00:00:00 2001
From: Gianluca Ciccarelli (sturmer) <ciccarelli@disi.unitn.it>
Date: Wed, 28 Dec 2011 16:46:00 +0100
Subject: [PATCH] dpkg-maintscipt-helper: Case 1 for bug 583585

Case 1: We have a directory that has to be transformed into a symlink in
a later version. In preinst, we just create a backup of the directory;
in postinst, the backup is safely eliminated; in postrm failed-upgrade,
the backup directory is restored.
---
 scripts/dpkg-maintscript-helper.sh |   65 +++++++++++++++++++++++++++++++++++-
 1 files changed, 64 insertions(+), 1 deletions(-)

diff --git a/scripts/dpkg-maintscript-helper.sh b/scripts/dpkg-maintscript-helper.sh
index 92088ae..4a659ec 100755
--- a/scripts/dpkg-maintscript-helper.sh
+++ b/scripts/dpkg-maintscript-helper.sh
@@ -26,6 +26,7 @@
 ##
 ## Functions to remove an obsolete conffile during upgrade
 ##
+# Dispatcher function
 rm_conffile() {
 	local CONFFILE="$1"
 	local LASTVERSION="$2"
@@ -128,6 +129,7 @@ abort_rm_conffile() {
 ##
 ## Functions to rename a conffile during upgrade
 ##
+# Dispatcher function
 mv_conffile() {
 	local OLDCONFFILE="$1"
 	local NEWCONFFILE="$2"
@@ -216,6 +218,64 @@ abort_mv_conffile() {
 	fi
 }
 
+# (Dispatcher function)
+# Substitute a directory with a symlink
+stash_dir() {
+  local DIRECTORY=$1
+  local LAST_VERSION=$2
+
+  [ -d "$DIRECTORY" ] || error "a directory should be specified"
+  if [ "$LAST_VERSION" = "--" -o \
+    -z "$LAST_VERSION"]; then
+    error "please specify the number of the first version having a symlink"
+  fi
+
+  while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
+  [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
+    error "could not determine the launching maint script"
+
+	# In the case statement, $1 is the preinst command, $2 the version
+  case "$DPKG_MAINTSCRIPT_NAME" in
+    preinst)
+			if [ -n "$2" ] && dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
+				create_dir_backup "$DIRECTORY"
+			fi
+		;;
+    postinst)
+			if [ -n "$2" ] && dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
+				rm_dir_backup "$DIRECTORY"
+			fi
+      ;;
+    postrm)
+      if [ -n "$1" ] && [ "$1" = "failed-upgrade" ] && \
+				[ -n "$2" ] && dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
+					restore_dir_backup "$DIRECTORY"
+      fi
+      ;;
+    *)
+      # TODO substitute stash_dir with a variable
+      debug "$0 stash_dir not required in $DPKG_MAINTSCRIPT_NAME"
+      ;;
+  esac
+  shift
+}
+
+create_dir_backup() {
+  # Ensure ownership of the dir by the package
+  [ -d "$DIRECTORY" -a -O "$DIRECTORY" ] || \
+    error "the package has no ownership of the directory, check permissions"
+
+  mv "$DIRECTORY" "$DIRECTORY".dpkg-bak
+}
+
+rm_dir_backup() {
+  rm -rf "$DIRECTORY".dpkg-bak
+}
+
+restore_dir_backup() {
+  [ -d "$DIRECTORY" ] && mv "$DIRECTORY".dpkg-bak "$DIRECTORY"
+}
+
 # Common functions
 debug() {
 	if [ -n "$DPKG_DEBUG" ]; then
@@ -266,7 +326,7 @@ shift
 case "$command" in
 supports)
 	case "$1" in
-	rm_conffile|mv_conffile)
+	rm_conffile|mv_conffile|stash_dir)
 		code=0
 		;;
 	*)
@@ -289,6 +349,9 @@ rm_conffile)
 mv_conffile)
 	mv_conffile "$@"
 	;;
+stash_dir)
+  stash_dir "$@"
+  ;;
 --help|help|-?|-h)
 	usage
 	;;
-- 
1.7.2.5

Attachment: signature.asc
Description: Digital signature


Reply to: