On Thu, Mar 20, 2014 at 08:51:22PM +0100, Hans Ekbrand wrote:
> You're right, copying the file is not the solution here. run-mailcap
> already provides a nice mechanism, put a randomly named symlink in
> /tmp which points back to the real file, give the viewer program
> (zathura, in our case) the symlink. This mechanism is used whenever
> the filename contains character(s) that the shell needs to be
> protected from. While the mechanism is nice, it is not used in all
> cases which introduces a complexity that has to be dealt with.
> 
> So, then, in what I think would solve the problem without reducing the
> features of zathura, I propose this entry:
> 
> application/pdf; { TEMPSYMLINK=`mktemp` \; rm $TEMPSYMLINK  \; if test -L 
> '%s' \; then cp -a '%s' $TEMPSYMLINK \; else CURRDIR=`pwd` \; FILE=`dir '%s'` 
> \; ln -s "${CURRDIR}/${FILE}" $TEMPSYMLINK \; fi \; ( zathura $TEMPSYMLINK 
> >/dev/null 2>&1 \; rm $TEMPSYMLINK ) & } ; test=test -n "$DISPLAY"; 
> description=Portable Document Format; nametemplate=%s.pdf; 
> application/pdf; { TEMPSYMLINK=`mktemp` \; rm $TEMPSYMLINK  \; if test -L 
> '%s' \; then cp -a '%s' $TEMPSYMLINK \; else CURRDIR=`pwd` \; FILE=`dir '%s'` 
> \; ln -s "${CURRDIR}/${FILE}" $TEMPSYMLINK \; fi \; ( zathura $TEMPSYMLINK 
> >/dev/null 2>&1 \; rm $TEMPSYMLINK ) & } ; test=test -n "$DISPLAY"; 
> description=Portable Document Format; nametemplate=%s.pdf; 
> application/x-pdf; { TMP=`basename '%s'` \; cp -a '%s' "/tmp/$TMP.tmp" \; 
> (zathura --fork "/tmp/$TMP.tmp" >/dev/null 2>&1 && sleep 5 && rm 
> "/tmp/$TMP.tmp" & ) } ; test=test -n "$DISPLAY"; description=Portable 
> Document Format; nametemplate=%s.pdf; 

That solution has two problems

a) it assumes that '%s' is not an absolute path
b) when used by mutt, mutt removes the PDF before zathura has opened it.

Problem a)
solved by checking for "/" as the first character in basename of '%s'

Problem b)
But, after even more hacking on this, I have a solution to both of
these problems too. That mutt relies on that the viewer program does
NOT fork is a problem with mutt rather than with zathura, but I think
I have a solution to this problem does not cause any other problems:
test if "%s" is in /tmp. If it is, then make a copy before forking.
Mutt will then delete the original, but zathura has its own copy. When
zatura is done, that copy is deleted too. The drawback is that zathura
does not remember state when viewing attached PDF:s, however that
feature is only useful on files you view a second time, and files in
/tmp are not very likely to be opened more than once.

application/pdf; { TEMPSYMLINK=`mktemp` \; rm $TEMPSYMLINK \; if test -L '%s' 
\; then cp -a '%s' $TEMPSYMLINK \; else DIR=`dirname '%s'` \; if test `echo -n 
"$DIR" | cut -b 1-4` = "/tmp" \; then cp '%s' $TEMPSYMLINK \; else 
FILE=`basename '%s'` \;         if test `echo -n $DIR | cut -b 1` = "/" \; then 
ln -s ${DIR}/${FILE} $TEMPSYMLINK \; else FULLDIR=`pwd`"/"${DIR} \; ln -s 
"${FULLDIR}"/"${FILE}" $TEMPSYMLINK \; fi \; fi \; fi \; ( zathura $TEMPSYMLINK 
>/dev/null 2>&1 \; rm $TEMPSYMLINK ) & } ; test=test -n "$DISPLAY"; 
description=Portable Document Format; nametemplate=%s.pdf; 
application/x-pdf; { TEMPSYMLINK=`mktemp` \; rm $TEMPSYMLINK \; if test -L '%s' 
\; then cp -a '%s' $TEMPSYMLINK \; else DIR=`dirname '%s'` \; if test `echo -n 
"$DIR" | cut -b 1-4` = "/tmp" \; then cp '%s' $TEMPSYMLINK \; else 
FILE=`basename '%s'` \;       if test `echo -n $DIR | cut -b 1` = "/" \; then 
ln -s ${DIR}/${FILE} $TEMPSYMLINK \; else FULLDIR=`pwd`"/"${DIR} \; ln -s 
"${FULLDIR}"/"${FILE}" $TEMPSYMLINK \; fi \; fi \; fi \; ( zathura $TEMPSYMLINK 
>/dev/null 2>&1 \; rm $TEMPSYMLINK ) & } ; test=test -n "$DISPLAY"; 
description=Portable Document Format; nametemplate=%s.pdf; 

And the full version with comments

## Get a random name
TEMPSYMLINK=`mktemp` ;
## Remove that file, we only wanted the name
rm $TEMPSYMLINK  ;
## Test if $1 is a symlink or not
if test -L "$1" ; then
    ## $1 is a symlink, we copy this one with "cp -a" to our own TEMPSYMLINK
    cp -a "$1" $TEMPSYMLINK ;
else ;
    ## $1 is not a symlink, locate it
    DIR=`dirname "$1"` ;
    ## make a special assumption about /tmp: if the file is in /tmp, then do 
not assume that the file will be left after we haved forked, so copy before we 
fork.
    if test `echo -n "$DIR" | cut -b 1-4` = "/tmp" ; then
        cp "$1" $TEMPSYMLINK ;
    else ;
        ## make a symlink from our TEMPSYMLINK to wherever $1 is
        FILE=`basename "$1"` ; 
        if test `echo -n $DIR | cut -b 1` = "/" ; then
            ## file has absolute path
            ln -s ${DIR}/${FILE} $TEMPSYMLINK ;
        else ;
            ## file has relative path
            FULLDIR=`pwd`"/"${DIR} ; 
            ln -s "${FULLDIR}"/"${FILE}" $TEMPSYMLINK ;
        fi ;
    fi ;
fi ;
## fork by starting zathura in sub shell,
## remove our TEMPSYMLINK when zathura exits
 ( zathura $TEMPSYMLINK >/dev/null 2>&1 ; rm $TEMPSYMLINK ) &


-- 
To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org

Reply via email to