1 #!nfsh
   2 #
   3 # CDDL HEADER START
   4 #
   5 # The contents of this file are subject to the terms of the
   6 # Common Development and Distribution License (the "License").
   7 # You may not use this file except in compliance with the License.
   8 #
   9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10 # or http://www.opensolaris.org/os/licensing.
  11 # See the License for the specific language governing permissions
  12 # and limitations under the License.
  13 #
  14 # When distributing Covered Code, include this CDDL HEADER in each
  15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16 # If applicable, add the following below this CDDL HEADER, with the
  17 # fields enclosed by brackets "[]" replaced with your own identifying
  18 # information: Portions Copyright [yyyy] [name of copyright owner]
  19 #
  20 # CDDL HEADER END
  21 #
  22 
  23 #
  24 # Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  25 # Use is subject to license terms.
  26 #
  27 
  28 #
  29 # Test script for NFS TCL to do local-to-remote or
  30 # remote-to-local (small) file copy.
  31 #
  32 
  33 if { $argc != 2 } {
  34     puts "Usage: $argv0 <readfile> <writefile>"
  35     puts "       note: <writefile> must exist and writable"
  36     puts "       note: if <readfile> or <writefile> is an NFS file,"
  37     puts "             must be in format of <hostname:/dir/filename>."
  38     exit 1
  39 }
  40 
  41 set rfile [ lindex $argv 0 ]
  42 set wfile [ lindex $argv 1 ]
  43 
  44 set rfid "Not opened"
  45 set wfid "Not opened"
  46 
  47 proc cleanup { rfid wfid exitcode } {
  48     if { $rfid != "Not opened" } {
  49         close $rfid
  50     }
  51     if { $wfid != "Not opened" } {
  52         close $wfid
  53     }
  54     exit $exitcode
  55 }
  56 
  57 # First check if the <file> is local or remote:
  58 set rremote 0
  59 set rl [ split $rfile : ]
  60 set l [ llength $rl ]
  61 if { $l == 2 } {        # remote file
  62     set rremote 1
  63     set rhost [ lindex $rl 0 ]
  64     set rfile [ split [ lindex $rl 1 ] / ]
  65     set rfile [ lrange $rfile 1 [expr [llength $rfile] - 1] ]
  66 } else {                # Open the local file for readding:
  67     set rfile $rl
  68     if [ catch { open $rfile r } rfid ] {
  69         puts stderr "Cannot open local file \[$rfile\] for readding."
  70         cleanup $rfid $wfid 1
  71     }
  72     puts "Opened local file \[$rfile\] for reading."
  73     set rdata [ read $rfid ]
  74 }
  75 
  76 if { $rremote == 1 } {          # open the remote file for reading
  77     #connect -t udp -p 9999 $host
  78     connect -t tcp -p 2049 $rhost
  79     puts "Connected to \[$rhost\] for reading."
  80 
  81     puts "rfile=<$rfile>"
  82     set res [compound { Putrootfh; foreach i $rfile {Lookup $i};
  83         Getfh; Getattr type; Access r}]
  84     if {$status != "OK"} {
  85         puts "compound{Lookup $rfile} return status=$status"
  86         puts "res=$res"
  87         cleanup $rfid $wfid 2
  88     }
  89     set c 0
  90     foreach i $rfile {incr c}
  91     set fh  [lindex [lindex $res [expr $c + 1]] 2]
  92     set rft [lindex [lindex [lindex [lindex $res [expr $c + 2]] 2] 0] 1]
  93     set rfa [lindex [lindex [lindex [lindex $res [expr $c + 3]] 2] 1] 1]
  94 
  95     puts "res=$res"
  96     puts "<$fh> <$rft> <$rfa>"
  97     if {$rft != "reg" && $rfa != "READ"} {
  98         puts "compound{type/access} don't have correct values."
  99         cleanup $rfid $wfid 2
 100     }
 101 
 102     set sid 0
 103     set res2 [compound { Putfh $fh; Read {0 0} 0 9999}]
 104     if {$status != "OK"} {
 105         puts "compound{Read} return status=$status"
 106         cleanup $rfid $wfid 2
 107     }
 108     set rcnt  [lindex [lindex [lindex [ lindex $res2 1] 2] 1] 1]
 109     set rdata [lindex [lindex [ lindex $res2 1] 2] 2]
 110     #puts "res=$res2"
 111     #puts "<$rcnt> <$rdata>"
 112     puts "Total of \[$rcnt\] bytes have been read from \[[lindex $argv 0]\]."
 113     disconnect
 114 }
 115 
 116 # Now the write file
 117 set wremote 0
 118 set wl [ split $wfile : ]
 119 set w [ llength $wl ]
 120 if { $w == 2 } {        # remote file
 121     set wremote 1
 122     set whost [ lindex $wl 0 ]
 123     set wfile [ split [ lindex $wl 1 ] / ]
 124     set wfile [ lrange $wfile 1 [expr [llength $wfile] -1] ]
 125 } else {
 126     set wfile $wl
 127     if [ catch { open $wfile w+ } wfid ] {
 128         puts stderr "Cannot open local file \[$wfile\] for writting."
 129         cleanup $rfid $wfid 1
 130     }
 131     puts "Opened local file \[$wfile\] for writing."
 132     puts -nonewline $wfid $rdata
 133     flush $wfid
 134     puts "Following data has been written to \[$wfile\]:"
 135     seek $wfid 0
 136     set rd [ read $wfid ]
 137     puts $rd
 138 }
 139 
 140 if { $wremote == 1 } {          # Open the remote file for writing.
 141     #connect -t udp -p 9999 $host
 142     connect -t tcp -p 2049 $whost
 143     puts "Connected remote host \[$whost\] for writing."
 144     puts "wfile=<$wfile>"
 145 
 146     set res [compound { Putrootfh; foreach i $wfile {Lookup $i};
 147         Getfh; Getattr type; Access m}]
 148     if {$status != "OK"} {
 149         puts "compound{Lookup $wfile} return status=$status"
 150         puts "res=$res"
 151         cleanup $rfid $wfid 2
 152     }
 153     set c 0
 154     foreach i $wfile {incr c}
 155     set fh  [lindex [lindex $res [expr $c + 1]] 2]
 156     set wft [lindex [lindex [lindex [lindex $res [expr $c + 2]] 2] 0] 1]
 157     set wfa [lindex [lindex [lindex [lindex $res [expr $c + 3]] 2] 1] 1]
 158 
 159     puts "res=$res"
 160     puts "<$fh> <$wft> <$wfa>"
 161     if {$wft != "reg" && $wfa != "MODIFY"} {
 162         puts "compound{type/access} don't have correct values."
 163         cleanup $rfid $wfid 2
 164     }
 165 
 166     set sid 0
 167     set res2 [compound { Putfh $fh; Write {0 0} 0 f a $rdata }]
 168     if {$status != "OK"} {
 169         puts "compound{Write rdata} return status=$status"
 170         cleanup $rfid $wfid 2
 171     }
 172 
 173     #puts "res=$res2"
 174     set wcnt [lindex [lindex [ lindex $res2 1] 2] 0]
 175     set wf [ lindex $argv 1 ]
 176     #puts "<$wcnt> <$wf>"
 177     puts "Total of \[$wcnt\] bytes have been written to \[$wf\]."
 178     set r3 [readv4_fh $fh {0 0} 0 9999]
 179     puts $r3
 180     disconnect
 181 }
 182 
 183 cleanup $rfid $wfid 0