Whamcloud - gitweb
import libsysio for b_newsysio
[fs/lustre-release.git] / libsysio / tests / test_copy.pl
1 #!/usr/bin/perl -w
2
3 #
4 # copy test: Copy a file from src to dest and verify that the new file
5 #          : is the same as the old
6 #
7
8 use IPC::Open2;
9
10 use strict;
11 use FindBin;
12 use lib "$FindBin::Bin";
13 use helper;
14
15 sub usage
16 {
17   print "Usage: ./test_copy.pl [-alpha] <src> <dest>: Copy a file from src to dest\n";
18   exit(-1);
19 }
20
21 sub process_cmd
22 {
23   my ($src, $dest, $overwrite, $is_alpha) = @_;
24   
25 # Get tests directory
26   my $testdir = $FindBin::Bin;
27
28   eval {
29                 if ($is_alpha == 0) {
30                         open2(\*OUTFILE, \*CMDFILE, "$testdir/test_driver --np");
31                 } else {
32                         open2(\*OUTFILE, \*CMDFILE, "yod -quiet -sz 1 $testdir/test_driver --np");
33                 }
34   };
35
36   if ($@) {
37     if ($@ =~ /^open2/) {
38       warn "open2 failed: $!\n$@\n";
39       return;
40     }
41     die;
42
43   }
44
45   my $outfh = \*OUTFILE;
46   my $cmdfh = \*CMDFILE;
47
48
49
50   if ($is_alpha == 0) {
51     helper::send_cmd($cmdfh, $outfh, "init", "CALL init\n");
52   }
53   
54   # Get the filesize of src
55   my $size = -s $src;
56   my $bufsize;
57
58         # If reading from stdin, just read one line
59         my $line;
60         if ($src eq "/dev/stdin") {
61                 $line = <STDIN>;
62                 $size = length($line);
63         }
64
65   if ( $size > 1024) { # Arbitrary limit
66     $bufsize = 1024;
67   } else {
68     $bufsize = $size;
69   }
70
71   my $cmdstr;
72   # Open src 
73         if ($src ne "/dev/stdin") {
74                 $cmdstr = '$src = CALL open '."$src O_RDONLY\n";
75                 helper::send_cmd($cmdfh, $outfh, "open", $cmdstr);
76                 helper::verify_cmd($cmdfh, $outfh, "open $src");
77         }
78         if ($dest ne "/dev/stdout") {
79                 # Open dest
80                 my $flags = "O_WRONLY|O_CREAT";
81                 if ($overwrite == 0) {
82                         $flags .= "|O_EXCL";
83                 }
84                 $cmdstr = '$dest = CALL open '."$dest $flags 0777\n";
85                 helper::send_cmd($cmdfh, $outfh, "open", $cmdstr);
86                 my $destfile = helper::verify_cmd($cmdfh, $outfh, "open $dest");
87         }
88
89   # Allocate buffer
90   $cmdstr = '$buf = ALLOC '."$bufsize\n";
91   helper::send_cmd($cmdfh, $outfh, "ALLOC", $cmdstr);
92
93   # Read size bytes from src and write them out to dest
94   my $bytes = $size;
95   while ($bytes > 0) {
96
97                 my $readb;
98                 my $res;
99                 if ($src eq "/dev/stdin") {
100                          # Send "delay" option to read which will give us time to 
101                         # put something in stdin (since we can't send an eof)
102                         my $cmdstr = "CALL read ".'0 $buf '."$bytes delay\n";
103                         print $cmdfh $cmdstr;
104                         # Give time to process command
105                         sleep 1;
106
107                         # Send line from stdin
108                         print $cmdfh $line;
109                         sleep 0.5;
110  
111       # Make sure read was OK
112                         $res = <$outfh>;
113                         chop($res);
114                         if ($res ne "0000 ") {
115                                 helper::print_and_exit($cmdfh, $outfh, 1, "ERROR! Read failed with code $res\n");
116                         }
117     
118                         # See how many bytes we got...
119                         $readb = helper::verify_cmd($cmdfh, $outfh, "read");
120                         $readb = oct($readb);
121                         if ($readb != $bytes) {
122                                 helper::print_and_exit($cmdfh, $outfh, 0, "Short read\n");
123                         }
124
125                         if ($dest eq "/dev/stdout") {
126                                 $cmdstr = "CALL write ".'1 $buf '."$readb\n";
127                         } else {
128                                 $cmdstr = "CALL write ".'$dest $buf '."$readb\n";
129                         }
130                         print $cmdfh $cmdstr;
131
132                         # Suck up the stdout...
133                         $res = <$outfh>;
134                         chop($res);
135   
136                         $res = <$outfh>;
137                         chop($res);
138                         $res = oct($res);
139
140                         if ($res != 0) {
141                                 helper::print_and_exit($cmdfh, $outfh, 1, "ERROR! Write failed with code $res\n");
142                                 }
143                 } else {
144                         $cmdstr = 'CALL read $src $buf '."$bufsize\n";
145                         helper::send_cmd($cmdfh, $outfh, "read", $cmdstr);
146     
147                         $res = helper::verify_cmd($cmdfh, $outfh, "read");
148                         $readb = oct($res);
149
150                         # Now write $readb back out to dest
151                         $cmdstr = 'CALL write $dest $buf '."$readb\n";
152                         helper::send_cmd($cmdfh, $outfh, "write", $cmdstr);
153     }
154
155     $res = helper::verify_cmd($cmdfh, $outfh, "write");
156
157     if ($readb != oct($res)) {
158       print STDOUT "ERROR!  Read $readb bytes but got back $res bytes\n";
159       exit 1;
160     }
161
162     $bytes -= $readb;
163   } 
164    
165   # Clean up
166         if ($src ne "/dev/stdin") {
167                 $cmdstr = 'CALL close $src'."\n";
168                 helper::send_cmd($cmdfh, $outfh, "close", $cmdstr);
169         }
170         if ($dest ne "/dev/stdout") {
171                 $cmdstr = 'CALL close $dest'."\n";
172                 helper::send_cmd($cmdfh, $outfh, "close", $cmdstr);
173         }
174         if ($src ne "/dev/stdin") {
175                 my $cmd = "cmp $src $dest " . '2>&1';
176                 my $cmpstr = qx($cmd);
177                 my $exitval = $? >> 8;
178                 if ($exitval != 0) {
179                         if ($exitval == 1) {
180                                 print STDOUT "ERROR! File $src differs from $dest\n";
181                                 print STDOUT "Comparison returned $cmpstr";
182                         } else {
183                                 print STDOUT "ERROR! File comparison failed with msg $cmpstr";
184                         }
185                         exit 1;
186                 }
187         }
188   helper::print_and_exit($cmdfh, $outfh, 0, "copy test successful\n");
189 }
190
191 my $currarg = 0;
192 my $is_alpha = 0;
193 my $overwrite = 0;
194
195 my $len = @ARGV-2;
196
197 if (@ARGV < 2) {
198   usage;
199
200
201 my $i;
202 for ($i=0; $i < $len; $i++ ) {
203   if ($ARGV[$i] eq "-alpha") {
204     $is_alpha = 1;
205   }
206         if ($ARGV[$i] eq "-o") {
207                 $overwrite = 1;
208         }
209 }
210
211 my $src = $ARGV[$i++];
212 my $dest = $ARGV[$i];
213
214
215 process_cmd($src, $dest, $overwrite, $is_alpha);
216
217
218 exit 0;