From nobody@FreeBSD.org  Tue May 25 06:45:22 2010
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 759A51065672
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 25 May 2010 06:45:22 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 59A778FC0A
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 25 May 2010 06:45:22 +0000 (UTC)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id o4P6jM7N003342
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 25 May 2010 06:45:22 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id o4P6jMFq003341;
	Tue, 25 May 2010 06:45:22 GMT
	(envelope-from nobody)
Message-Id: <201005250645.o4P6jMFq003341@www.freebsd.org>
Date: Tue, 25 May 2010 06:45:22 GMT
From: Garrett Cooper <yaneurabeya@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: [ports] bsd.ports.mk incorrectly using pkg-deinstall for both deinstall and post-deinstall operations
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         146947
>Category:       ports
>Synopsis:       [ports] bsd.ports.mk incorrectly using pkg-deinstall for both deinstall and post-deinstall operations
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    portmgr
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue May 25 06:50:01 UTC 2010
>Closed-Date:    Sun Jun 27 04:22:48 UTC 2010
>Last-Modified:  Sun Feb 03 22:28:54 UTC 2013
>Originator:     Garrett Cooper
>Release:        9-CURRENT
>Organization:
Cisco Systems, Inc.
>Environment:
FreeBSD bayonetta.local 9.0-CURRENT FreeBSD 9.0-CURRENT #0 r206173M: Mon Apr 26 22:45:06 PDT 2010     root@bayonetta.local:/usr/obj/usr/src/sys/BAYONETTA.ata  amd64
>Description:
pkg-deinstall as specified by the ports infrastructure is being incorrectly used as both the deinstall and post-deinstall script instead of just being the deinstall script.

Example:

$ sudo make deinstall
===>  Deinstalling for devel/py-ctypes
===>   Deinstalling py26-ctypes-1.0.2
cd: can't cd to /usr/local/lib/python%%PYVER%%/
pkg_delete: deinstall script returned error status
cd: can't cd to /usr/local/lib/python%%PYVER%%/
pkg_delete: post-deinstall script returned error status
$ cat pkg-deinstall
#!/bin/sh

PYVER='%%PYVER%%'

set -e

cd "$PKG_PREFIX/lib/python${PYVER}/"

find -s -X . -name '*.py[co]' | xargs rm -f
find -s -X . -type d | xargs rmdir -p

The error was unintentional, but the outcome was interesting nonetheless.

The actual problem appears to be pkg_install though (note the Note: clause):

     -k dscript
             Set dscript to be the de-install procedure for the package.  This
             can be any executable program (or shell script).  It will be
             invoked automatically when the package is later (if ever) de-
             installed.  It will be passed the package's name as the first
             argument.

             Note: if the -K option is not given, this script will serve as
             both the de-install and the post-deinstall script for the pack-
             age, differentiating between the functionality by passing the
             keywords DEINSTALL and POST-DEINSTALL respectively, along with
             the package's name.

This behavior doesn't make sense; the deinstall script should be executed once and only once as executing these scripts twice only serves to cause confusion in the overall execution flow.

I believe that the code causing this is appears to be in delete/perform.c (in particular the second conditional block):

    /*
     * Test whether to use the old method of passing tokens to deinstallation
     * scripts, and set appropriate variables..
     */

    if (fexists(POST_DEINSTALL_FNAME)) {
        new_m = 1;
        post_script = POST_DEINSTALL_FNAME;
        pre_arg = post_arg = "";
    } else if (fexists(DEINSTALL_FNAME)) {
        post_script = DEINSTALL_FNAME;
        pre_arg = "DEINSTALL";
        post_arg = "POST-DEINSTALL";
    } else {
        post_script = pre_arg = post_arg = NULL;
    }

Whether or not this behavior should be deprecated is one thing, but I find it to be incredibly confusing.
>How-To-Repeat:
1. Create a package with pkg_create -k foo, like the following script:

cat > foo <<EOF
#!/bin/sh
echo "my name is \${0##*/}"; false
EOF

2. Add the package.
3. Delete the package.

The output from package delete will be:

"my name is POST-INSTALL"
>Fix:


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->portmgr 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Tue May 25 07:47:30 UTC 2010 
Responsible-Changed-Why:  
Make this a ports PR and assign to portmgr. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=146947 

From: Garrett Cooper <yaneurabeya@gmail.com>
To: bug-followup@FreeBSD.org, gcooper@FreeBSD.org
Cc:  
Subject: Re: ports/146947: [ports] bsd.ports.mk incorrectly using 
	pkg-deinstall for both deinstall and post-deinstall operations
Date: Fri, 25 Jun 2010 19:15:30 -0700

     This appears to be a feature of the +*DEINSTALL facility in
 pkg_install (if you don't specify a specific PRE-DEINSTALL vs
 DEINSTALL file). It feeds in the state as the first argument for the
 +DEINSTALL script to let the script know which version it's invoking,
 so you have to do:
 
 if [ "$1" = DEINSTALL ] ; then
     # do something
 elif [ "$1" = POST-DEINSTALL ] ; then
     # do something
 fi
 
     It's only really commented in one spot (from pkg_create(8)):
 
              Note: if the -K option is not given, this script will serve as
              both the de-install and the post-deinstall script for the pack-
              age, differentiating between the functionality by passing the
              keywords DEINSTALL and POST-DEINSTALL respectively, along with
              the package's name.
 
     It just isn't clear until you munge around with the bsd.*.mk code
 that that's what's actually happening.
 Thanks,
 -Garrett
 
 PS Please close this PR.
State-Changed-From-To: open->closed 
State-Changed-By: linimon 
State-Changed-When: Sun Jun 27 04:22:39 UTC 2010 
State-Changed-Why:  
Closed at submitter's request. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=146947 
>Unformatted:
