Subject: Re: adding upgrade to pkg_add
To: None <tech-pkg@netbsd.org>
From: Jeremy C. Reed <reed@reedmedia.net>
List: tech-pkg
Date: 04/09/2003 17:28:11
On Thu, 10 Apr 2003, Hubert Feyrer wrote:

> What is the exact reason for not trying to remove everything cleanly
> (maybe modulo some shared libs...), then install the new pkg cleanly?

I use pkg_add on systems where all software is installed from packages.

I don't want to pkg_delete sh-utils since other users or software may have
problems, for example.

> I have no problem with renaming pkg_add's -u switch to -r (makes sense,
> actually).

I have a beginning of a working patch; I used -o for overwrite. But I
think -r for replace and -u should be used for upgrade (without
pkg_delete).

> If you have any code to review for pkg_*, let me know.

This patch below works for upgrading from one version to another.

What needs to be done:

- make sure old package is removed if new package is installed
  successfully. This means old package is unregistered and its /var/db/pkg
  directory removed, but no DEINSTALL script ran and no files deleted.

- choose command-line switch -u for upgrade and -r to replace (currently
  is -u)

- man page updated

- make sure /usr/pkgsrc/mk/install/install has a dummy UPGRADE target or
else it will fail with "Unexpected argument".

- Only have the "Overwriting /path/to/file message ... bogus/conflicting?"
if the basename (without version) of the package is different. Is this
basename already available?

(Also, I wonder why the Overwriting ... bogus/conflicting? code is in
there twice. I will need to look closer.)

   Jeremy C. Reed

Index: pkgtools/pkg_install/files/add/extract.c
===================================================================
RCS file: /cvsroot/pkgsrc/pkgtools/pkg_install/files/add/extract.c,v
retrieving revision 1.3
diff -b -u -r1.3 extract.c
--- pkgtools/pkg_install/files/add/extract.c	2003/02/11 16:42:06	1.3
+++ pkgtools/pkg_install/files/add/extract.c	2003/04/10 00:23:19
@@ -279,6 +279,9 @@
 						printf("pkgdb_retrieve(\"%s\")=\"%s\"\n", t, s);	/* pkgdb-debug - HF */
 #endif
 						if (s)
+							if (overwrite)
+								warnx("Overwriting %s", t, s);
+							else
 							warnx("Overwriting %s - pkg %s bogus/conflicting?", t, s);
 						else {
 							rc = pkgdb_store(t, PkgName);
Index: pkgtools/pkg_install/files/add/main.c
===================================================================
RCS file: /cvsroot/pkgsrc/pkgtools/pkg_install/files/add/main.c,v
retrieving revision 1.2
diff -b -u -r1.2 main.c
--- pkgtools/pkg_install/files/add/main.c	2003/01/14 15:18:32	1.2
+++ pkgtools/pkg_install/files/add/main.c	2003/04/10 00:23:19
@@ -56,7 +56,7 @@
 #include "add.h"
 #include "verify.h"

-static char Options[] = "IMRSVfhnp:s:t:uv";
+static char Options[] = "IMRSVfhnop:s:t:uv";

 char   *Prefix = NULL;
 Boolean NoInstall = FALSE;
@@ -70,6 +70,7 @@
 char    FirstPen[FILENAME_MAX];
 add_mode_t AddMode = NORMAL;
 int	upgrade = 0;
+int	overwrite = 0;

 static void
 usage(void)
@@ -115,6 +116,11 @@
 		case 'n':
 			Fake = TRUE;
 			Verbose = TRUE;
+			break;
+
+		case 'o':
+			upgrade = 1;
+			overwrite = 1; /* do not pkg_delete */
 			break;

 		case 's':
Index: pkgtools/pkg_install/files/add/perform.c
===================================================================
RCS file: /cvsroot/pkgsrc/pkgtools/pkg_install/files/add/perform.c,v
retrieving revision 1.4
diff -b -u -r1.4 perform.c
--- pkgtools/pkg_install/files/add/perform.c	2003/03/29 18:41:56	1.4
+++ pkgtools/pkg_install/files/add/perform.c	2003/04/10 00:23:21
@@ -447,10 +447,15 @@
 						upgrading = 1;
 					}

+					if (overwrite) {
 					if (Verbose)
+							printf("Installing new package without deleting current package.\n");
+					}
+					else {
+						if (Verbose)
 						printf("pkg_delete '%s'\n", installed);
 					vsystem("%s/sbin/pkg_delete '%s'\n", PREFIX, installed);
-
+					}
 				} else {
 					warnx("other version '%s' already installed", installed);

@@ -602,7 +607,7 @@
 	}

 	/* If we're really installing, and have an installation file, run it */
-	if (!NoInstall && fexists(INSTALL_FNAME)) {
+	if (!NoInstall && !overwrite && fexists(INSTALL_FNAME)) {
 		vsystem("%s +x %s", CHMOD_CMD, INSTALL_FNAME);	/* make sure */
 		if (Verbose)
 			printf("Running install with PRE-INSTALL for %s.\n", PkgName);
@@ -634,10 +639,22 @@
 	}

 	/* Run the installation script one last time? */
-	if (!NoInstall && fexists(INSTALL_FNAME)) {
+	if (!NoInstall && !overwrite && fexists(INSTALL_FNAME)) {
 		if (Verbose)
 			printf("Running install with POST-INSTALL for %s.\n", PkgName);
 		if (!Fake && vsystem("./%s %s POST-INSTALL", INSTALL_FNAME, PkgName)) {
+			warnx("install script returned error status");
+			errc = 1;
+			goto fail;
+		}
+	}
+
+	/* Run the installation script if upgrading */
+	if (overwrite && !NoInstall && fexists(INSTALL_FNAME)) {
+		vsystem("%s +x %s", CHMOD_CMD, INSTALL_FNAME);	/* make sure */
+		if (Verbose)
+			printf("Running install with UPGRADE for %s.\n", PkgName);
+		if (!Fake && vsystem("./%s %s UPGRADE", INSTALL_FNAME, PkgName)) {
 			warnx("install script returned error status");
 			errc = 1;
 			goto fail;
Index: pkgtools/pkg_install/files/lib/lib.h.in
===================================================================
RCS file: /cvsroot/pkgsrc/pkgtools/pkg_install/files/lib/lib.h.in,v
retrieving revision 1.4
diff -b -u -r1.4 lib.h.in
--- pkgtools/pkg_install/files/lib/lib.h.in	2003/03/16 19:44:10	1.4
+++ pkgtools/pkg_install/files/lib/lib.h.in	2003/04/10 00:23:22
@@ -449,6 +449,7 @@
 extern Boolean Fake;
 extern Boolean Force;
 extern int upgrade;
+extern int overwrite;

 /* We include it at the end, because it uses TAILQ_* */
 #include "path.h"