tBe a bit more resilient to errors. - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 65fb6fb74cb14afcc8b7040fcc6f253dec622fd0
 (DIR) parent ed8dfe37dbd4470bbb9ce487e78b1d6ae365fb85
 (HTM) Author: rsc <devnull@localhost>
       Date:   Thu, 24 Mar 2005 05:17:01 +0000
       
       Be a bit more resilient to errors.
       
       Diffstat:
         M src/cmd/netfiles/main.c             |       2 +-
         M src/cmd/netfiles/netfileget         |      37 ++++++++++++++++---------------
         M src/cmd/netfiles/netfilelib.rc      |      44 ++++++++++++++++++++++++-------
         M src/cmd/netfiles/netfileput         |       8 +-------
         M src/cmd/netfiles/netfilestat        |      16 ++++++++--------
       
       5 files changed, 63 insertions(+), 44 deletions(-)
       ---
 (DIR) diff --git a/src/cmd/netfiles/main.c b/src/cmd/netfiles/main.c
       t@@ -462,7 +462,7 @@ do3(Win *w, char *text)
                cleanname(name);
                cprint("b3 \t=> name=%s addr=%s\n", name, addr);
                if(strcmp(name, lastfail) == 0){
       -                cprint("b3 \t=> non-existant (cached)\n");
       +                cprint("b3 \t=> nonexistent (cached)\n");
                        free(name);
                        return -1;
                }
 (DIR) diff --git a/src/cmd/netfiles/netfileget b/src/cmd/netfiles/netfileget
       t@@ -13,33 +13,34 @@ if(! ~ $#* 2){
        
        . netfilelib.rc $1
        
       -fn getfile {
       -        rm -f $t
       -        if(! 9 echo -get $2 $t | mysftp $1 >$t.e >[2=1])
       -                exit 1
       -        egrep -v '^Fetching' $t.e >$t.e2
       -        if(test -s $t.e2){
       -                cat $t.e >[1=2]
       -                exit 1
       -        }
       -        cat $t
       -}
       -
        fn getfile9p {
                if(! 9p read $1/$2)
                        exit 1
        }
        
       -fn getdir {
       -        if(! {echo cd $2; echo ls -l} | mysftp $1 | sed '1,2d; s/sftp> //g; /^$/d' >$t)
       -                exit 1
       -        cat $t | awk '$NF == "." || $NF == ".." { next } {s = $NF; if($0 ~ /^d/) s = s "/"; print s}'
       -}
       -
        fn getdir9p {
                9p ls -l $1/$2 | awk '{s=$NF; if($0 ~ /^d/) s=s"/"; print s}'
        }
        
       +fn getfile {
       +        rm -f $t
       +        echo get $2 $t | runsftp -e $1
       +        cat $t
       +}
       +
       +fn getdir {
       +        {echo cd $2; echo ls -l} | runsftp $1
       +        awk '
       +                $NF == "." || $NF == ".." { next } 
       +                {
       +                        s = $NF
       +                        if($0 ~ /^d/)
       +                                s = s "/"
       +                        print s
       +                }
       +        ' $t.sftp
       +}
       +
        $f $1 $2
        exit 0
        
 (DIR) diff --git a/src/cmd/netfiles/netfilelib.rc b/src/cmd/netfiles/netfilelib.rc
       t@@ -3,19 +3,43 @@ ns=`{namespace}
        if(</dev/null dial 'unix!'^$ns/$1 >[2]/dev/null)
                f=$f^9p
        
       -fn mysftp {
       -        if(! </dev/null dial 'unix!'^$ns/$1.sftp >[2]/dev/null){
       -                echo 'caching sftp' $1 >[1=2]
       -                sftpcache -D $1
       +t=/tmp/netfilexxx.$pid.$USER
       +fn sigexit { rm -f $t $t.* }
       +
       +fn runsftp {
       +        eflag=no
       +        if(~ $1 -e){
       +                eflag=yes
       +                shift
                }
                if(! </dev/null dial 'unix!'^$ns/$1.sftp >[2]/dev/null){
       -                sftp -b /dev/stdin $1
       +                sftpcache -D $1 >/dev/tty
                }
       -        if not{
       -                {cat; echo DONE} | dial -e 'unix!'^$ns/$1.sftp
       +        {
       +                if(! </dev/null dial 'unix!'^$ns/$1.sftp >[2]/dev/null){
       +                        sed 's/^/-/' | sftp -b /dev/stdin $1 && echo DONE
       +                }
       +                if not{
       +                        {cat; echo DONE} | dial -e 'unix!'^$ns/$1.sftp
       +                }
       +        } >$t.sftp1 >[2=1]
       +        sed 's/^/1 /' $t.sftp1
       +        sed '/^sftp> /d
       +                /^Connecting to /d
       +                /^Fetching /d
       +                /^Uploading /d
       +                /^DONE$/d
       +        ' $t.sftp1 >$t.sftp
       +        if(! 9 grep -s '^DONE$' $t.sftp1){
       +                echo sftp did not finish: >[1=2]
       +                sed 's/^/        /g' $t.sftp1 >[1=2]
       +                exit 1
                }
       +        if(~ $eflag yes && test -s $t.sftp){
       +                echo sftp error output: >[1=2]
       +                sed '/^DONE$/d; s/^/        /g' $t.sftp1 >[1=2]
       +                exit 1
       +        }
       +        status=''
        }
        
       -t=/tmp/netfilexxx.$pid.$USER
       -fn sigexit { rm -f $t $t.e $t.e2 }
       -
 (DIR) diff --git a/src/cmd/netfiles/netfileput b/src/cmd/netfiles/netfileput
       t@@ -11,13 +11,7 @@ f=putfile
        
        fn putfile{
                cat >$t
       -        if(! 9 echo -put $t $2 | mysftp $1 >$t.e >[2=1])
       -                exit 1
       -        egrep -v '^Uploading' $t.e >$t.e2
       -        if(test -s $t.e2){
       -                cat $t.e >[1=2]
       -                exit 1
       -        }
       +        echo put $t $2 | runsftp -e $1
        }
        fn putfile9p{
                if(! 9p write $1/$2)
 (DIR) diff --git a/src/cmd/netfiles/netfilestat b/src/cmd/netfiles/netfilestat
       t@@ -10,23 +10,23 @@ f=dostat
        . netfilelib.rc $1
        
        fn dostat {
       -        {
       -                9 echo -cd $2
       -        }  | mysftp $1 >$t
       -        if(9 test -e $t -a ! -s $t){
       +        echo cd $2 | runsftp $1
       +        if(9 test -e $t.sftp -a ! -s $t.sftp){
                        echo directory
                        exit 0
                }
       -        if(9 grep -s 'Can''t change directory|is not a directory' $t){
       +        if(9 grep -s 'Can''t change directory|is not a directory' $t.sftp){
                        echo file
                        exit 0
                }
       -        if(9 grep -s 'Couldn''t stat remote file|such file' $t){
       +        if(9 grep -s 'Couldn''t stat remote file|such file' $t.sftp){
                        echo nonexistent
                        exit 0
                }
       -        cat $t >[1=2]
       -        echo unknown error
       +        {
       +                echo unknown error:
       +                sed 's/^/        /' $t.sftp 
       +        } >[1=2]
                exit 0
        }