There appears to be a bug with the way the pre- and post- scripts are
executed.

In subroutine scriptrun, a string is assembled with the shell invocation
(sh -c, by default), environment variables, command, and redirection.

The problem is that the environment variables are within the shell
invocation.  For example, if you to declare:

post-server: echo "DIRVISH_SERVER is ${DIRVISH_SERVER}"; echo
"DIRVISH_CLIENT is ${DIRVISH_CLIENT}";

(Notes:
        Yes, you can try the above on any dirvish profile or vault that doesn't
already have a post-server statement.
        I've purposely used two distinct echo commands.  I just wanted to show
that my proposed solution works for multiple statements.)

You'll see in the resulting log file the following output:

DIRVISH_SERVER is
DIRVISH_CLIENT is

The problem is that the environment variables (DIRVISH_SERVER,
DIRVISH_CLIENT, et al) are put after the /bin/sh -c invocation, not
before.  The resulting string is (using only a couple of environment
variables and omitting the redirection for illustration):

/bin/sh -c 'DIRVISH_SERVER=server.example.com
DIRVISH_CLIENT=client.example.com echo "DIRVISH_SERVER is
${DIRVISH_SERVER}"; echo "DIRVISH_CLIENT is ${DIRVISH_CLIENT}";'

If you execute the above you will see it does not work as expected.  The
problem is that all the variables are expanded before execution of the
/bin/sh command.  Since the variables were not previously set, they
output as unset variables.

I believe the proper solution is to declare the variables before
invoking the shell.  So the example above would look like:

DIRVISH_SERVER=server.example.com DIRVISH_CLIENT=client.example.com
/bin/sh -c 'echo "DIRVISH_SERVER is ${DIRVISH_SERVER}"; echo
"DIRVISH_CLIENT is ${DIRVISH_CLIENT}";'

(You can also execute the above on a shell to see the results.)

Attached is the patch that I believe solves the problem; it merely
changes the placement of the $A{env} variable.

I noticed that quite some time ago, someone brought up the problem:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=559660

The proposed solution was to put the script commands in a separate file,
and execute that.  That does work as a work-around, but I believe it
would be better to fix the dirvish script.

The ordering of environment variables in front of the command is
described in the bash man page, under "Simple Commands."
--- /usr/sbin/dirvish   2015-09-25 07:40:13.496002276 -0700
+++ /usr/sbin/dirvish-debug     2015-10-11 14:54:20.319990631 -0700
@@ -929,16 +929,17 @@
        $cmd = strftime($A{cmd}, localtime($A{now}));
        if ($A{dir} =~ /^:/)
        {
-               $rcmd = sprintf ("%s 'cd %s; %s %s' >>%s",
+               $rcmd = sprintf ("%s %s 'cd %s; %s' >>%s",
+                       $A{env},
                        ("$A{shell}" || "/bin/sh -c"),
-                       $A{dir}, $A{env},
+                       $A{dir},
                        $cmd,
                        $A{log}
                );
        } else {
-               $rcmd = sprintf ("%s '%s %s' >>%s",
-                       ("$A{shell}" || "/bin/sh -c"),
+               $rcmd = sprintf ("%s %s '%s' >>%s",
                        $A{env},
+                       ("$A{shell}" || "/bin/sh -c"),
                        $cmd,
                        $A{log}
                );
_______________________________________________
Dirvish mailing list
[email protected]
http://www.dirvish.org/mailman/listinfo/dirvish

Reply via email to