Whamcloud - gitweb
import older libsysio snapshot.
[fs/lustre-release.git] / libsysio / tests / test_list.pl
1 #!/usr/bin/perl -w
2
3 #
4 # getdirentries test:  Tests the equivalent of a ls.  Note that this is not
5 #                      the most robust test in the world; it simply verifies
6 #                      that libsysio returns all the entries in the directory
7 #
8 #
9
10 use IPC::Open2;
11
12 use strict;
13
14 use FindBin;
15 use lib "$FindBin::Bin";
16 use helper;
17
18 sub usage
19 {
20   print "Usage: ./test_list.pl [-p|-alpha] <dir> \n";
21   print "       ./test_list.pl -m [-p|-alpha] fstype:mdir dir\n";
22   print "      In the first form, will attempt to verify libsysio's\n";
23   print "      getdirentries.  If no dir is given, will use the \n";
24   print "      current working directory\n";
25   print "      In the second form, will mount the given mdir (of type fstype) in dir.\n";
26   print "      It will then verify the output of libsysio's getdirentries. It will \n";
27   print "      then umount the directory and verify that the umount worked\n";
28   print "      The -p option will print the directory listing\n";
29   print "      The -alpha option is for alpha architecture \n";
30   exit(-1);
31 }
32
33
34 sub write_print
35 {
36   my ($offset, $outfh, $cmdfh, $do_print, $is_alpha) = @_;
37   my $bytes = 0;
38   
39   my $intsize = 8;
40   my $intcmd = "INT";
41   if ($is_alpha == 1) {
42       $intsize = 16;
43       $intcmd = "LONG"
44   }
45   my $shortoffset = $offset+$intsize;
46   my $charoffset = $shortoffset+2;
47   my $stroffset = $charoffset+1;
48   my $cmdstr = 'PRINT $buf '. 
49     "$offset $intsize $intcmd $shortoffset 2 SHORT $charoffset 1 CHAR $stroffset 1 STR\n";
50   helper::send_cmd($cmdfh, $outfh, "PRINT", $cmdstr);
51
52   my $res = <$outfh>;
53   chop($res);
54   my ($inode, $foffset, $size, $type, @names) = split(' ',$res);
55   $size = oct($size);
56   if ($size == 0) {
57     return -1;
58   }
59   my $name = join(' ', @names);
60
61   if ($do_print == 1) {
62     printf(STDOUT "%-35s %-14s %-14s %-6s %-4s\n", $name, $inode, $foffset, $size, $type);
63   }
64
65   return $size;
66 }
67
68 sub do_print_cmds
69 {
70   my ($numbytes, $outfh, $cmdfh, $start, $do_print, $is_alpha) = @_;
71
72   my $offset = 0;
73   my $bytes = 0;
74   my $numfiles = 0;
75   my $i = $start;
76   
77   if ($numbytes == 0) {
78     $numbytes = 8192;
79   }
80   while ($bytes < $numbytes) {
81     my $len = write_print($offset, $outfh, $cmdfh, $do_print, $is_alpha);
82     if ($len <= 0) {
83       # write_print saw a 0 length record, indicating end of dir
84       return $numfiles;
85     }
86     $numfiles++;
87     if ($is_alpha == 0) {
88         $len += $len%4;
89     } else {
90         $len += $len%8;
91     }
92     $offset += $len;
93     $bytes += $len;
94     $i++;
95   }
96   return $numfiles;
97 }
98
99 sub print_dir_cmd
100 {
101
102   my ($outfh, $cmdfh, $start, $mdir, $do_print, $is_alpha) = @_;
103
104   my $cmdstr = "CALL getdirentries ( ".'$fd = CALL open '."$mdir O_RDONLY ) ( ";
105   $cmdstr .= '$buf = ALLOC 8192 ) 8192 $basep'."\n";
106   helper::send_cmd($cmdfh, $outfh, "getdirentries", $cmdstr);
107   
108   # Verify that the sysio call succeeded
109   my $res = helper::verify_cmd($cmdfh, $outfh, "getdirentries");
110   my $numbytes = oct($res);
111   
112   while ($numbytes > 0) {
113
114     do_print_cmds($numbytes, $outfh, $cmdfh, $start, $do_print, $is_alpha);
115
116     $cmdstr = "CALL getdirentries ".'$fd $buf 8192 $basep'."\n";
117     helper::send_cmd($cmdfh, $outfh, "getdirentries", $cmdstr);
118
119     # Verify that the sysio call succeeded
120     my $res = helper::verify_cmd($cmdfh, $outfh, "getdirentries");
121     $numbytes = oct($res);
122   }
123 }
124
125 sub process_cmd
126 {
127   my ($mdir, $tdir, $do_mount, $is_alpha, $do_print) = @_;
128   my $size = 8192;
129   my $done_files = 0;
130
131   # Get tests directory
132   my $testdir = $FindBin::Bin;
133  
134   eval {
135       if ($is_alpha == 1) {
136           open2(\*OUTFILE, \*CMDFILE, "yod -quiet -sz 1 $testdir/test_driver --np");
137       } else {
138           open2(\*OUTFILE, \*CMDFILE, "$testdir/test_driver --np");
139       }
140   };
141
142   if ($@) {
143     if ($@ =~ /^open2/) {
144       warn "open2 failed: $!\n$@\n";
145       return;
146     }
147     die;
148
149   }
150
151   my $outfh = \*OUTFILE;
152   my $cmdfh = \*CMDFILE;
153
154   if ($is_alpha == 0) {
155     helper::send_cmd($cmdfh, $outfh, "init", "CALL init\n");
156   }
157
158   my $start = 0;
159
160   if ($do_mount == 1) {
161     helper::send_cmd($cmdfh, $outfh, "mount", "CALL mount $mdir $tdir\n");
162     print_dir_cmd($outfh, $cmdfh, $start, $tdir, $do_print, $is_alpha);
163   } else {
164     print_dir_cmd($outfh, $cmdfh, $start, $mdir, $do_print, $is_alpha);
165   }  
166
167   # Attempt to unmount and verify the contents
168   if ($do_mount == 1) {
169
170     # Close the dir before we umount
171     my $cmdstr = 'CALL close $fd'."\n";
172     helper::send_cmd($cmdfh, $outfh, "close", $cmdstr);
173    
174     # umount dir
175     helper::send_cmd($cmdfh, $outfh, "umount", "CALL umount $tdir\n");
176
177    
178     # Verify it is umounted
179     $cmdstr = "CALL getdirentries ( ".'$fd2 = CALL open '."$tdir O_RDONLY ) ";
180     $cmdstr .= '$buf 8192 $newp'."\n";
181     helper::send_cmd($cmdfh, $outfh, "getdirentries", $cmdstr);
182     my $res = helper::verify_cmd($cmdfh, $outfh, "getdirentries");
183
184     my $numbytes = oct($res);
185     # The only entries should be . and .., so should return 32
186     if ($numbytes != 32) {
187       helper::print_and_exit($cmdfh, $outfh, 1, "ERROR! Read in $numbytes bytes\n");
188     }
189     # Clean up
190     $cmdstr = 'CALL close $fd2'."\n";
191     helper::send_cmd($cmdfh, $outfh, "close", $cmdstr);
192
193   } else {
194     my $cmdstr = 'CALL close $fd'."\n";
195     helper::send_cmd($cmdfh, $outfh, "getdirentries", $cmdstr);
196   }
197
198   helper::print_and_exit($cmdfh, $outfh, 0, "list test successful\n");
199 }
200
201
202 # Default dir is cwd
203 my @mydir;
204 $mydir[0] = "./";
205 my $do_mount = 0;
206 my $is_alpha = 0;
207 my $do_print = 0;
208 my $dircnt = 0;
209 for (my $i = 0; $i < @ARGV; $i++) 
210 {
211   if ($ARGV[$i] eq "-p") {
212     $do_print = 1;
213   } elsif ($ARGV[$i] eq "-m") {
214     $do_mount = 1;
215   } elsif ($ARGV[$i] eq "-alpha") {
216     $is_alpha = 1;
217   } else {
218     $mydir[$dircnt] = $ARGV[$i];
219     $dircnt++;
220   }
221 }
222
223 if (  ($dircnt == 0) || ($dircnt > 2) ||
224       (($do_mount==1) && ($dircnt < 2)) ||
225       (($do_mount == 0) && ($dircnt > 1)) ) {
226   usage();
227 }
228
229 my $dir = $mydir[0];
230 if ($do_mount == 1) {
231   my $fstype;
232   ($fstype, $dir) = split(/:/, $mydir[0]);
233 }
234
235 process_cmd($mydir[0], $mydir[1], $do_mount, $is_alpha, $do_print);
236
237 exit 0;