Whamcloud - gitweb
c68fdc8e46220480b3be66354decbcd744b88080
[fs/lustre-release.git] / libsysio / tests / test_path.pl
1 #!/usr/bin/perl -w 
2
3 #
4 # path test: reads paths from stdin and prints out the path along with its 
5 #          : type
6 #
7
8 use IPC::Open2;
9
10 use strict;
11 use FindBin;
12 use lib "$FindBin::Bin";
13 use helper;
14 use POSIX;
15 use Fcntl ':mode';
16
17 sub usage
18 {
19   print "Usage ./test_path.pl [path1 path2...] : Print each path listed and its type\n";
20   print "                                      : If no paths are given, stdin is read\n";
21   exit(-1);
22 }
23
24 sub get_type
25 {
26   my $mode = $_[0];
27   my $t = '?';
28
29   if (S_ISDIR($mode)) {
30     $t = 'd';
31   } elsif (S_ISCHR($mode)) {
32     $t = 'c';
33   } elsif (S_ISBLK($mode)) {
34     $t = 'b';
35   } elsif (S_ISREG($mode)) {
36     $t = 'f';
37   } elsif (S_ISFIFO($mode)) {
38     $t = 'p';
39   } elsif (S_ISLNK($mode)) {
40     $t = 'S';
41   } elsif (S_ISSOCK($mode)) {
42     $t = 's';
43   }
44
45   return $t;
46 }
47
48 sub print_path
49 {
50   my ($mode, $path) = @_;
51   
52   my $typechar = get_type($mode);
53   print STDOUT "$path: $typechar\n";
54 }
55
56 sub process_path
57 {
58   my ($cmdfh, $outfh, $bits, $path) = @_;
59
60   # Issue the stat command
61   my $cmdstr = 'CALL stat "';
62   $cmdstr = sprintf("%s%s%s\n", $cmdstr, $path, '" $buf');
63
64   helper::send_cmd($cmdfh, $outfh, "stat", $cmdstr);  
65   helper::verify_cmd($cmdfh, $outfh, "stat");
66
67   # Print out the stat buffer
68   if ($bits == 32) {
69       $cmdstr = 'PRINT $buf 0 8 LONG 12 24 INT 44 8 LONG 52 8 INT 64 24 LONG';
70   } else {
71       $cmdstr = 'PRINT $buf 0 24 LONG 24 16 INT 48 32 LONG 88 8 LONG 104 8 LONG';
72   }
73   $cmdstr .= "\n";
74   helper::send_cmd($cmdfh, $outfh, "PRINT", $cmdstr);
75   
76   my $res = <$outfh>;
77   chop($res);
78   my ( $iodev, $ioino, $iomode, $ionlink, $iouid, $iogid, $iordev, 
79        $iosize, $ioblksize, $ioblks, $ioatime, $iomtime, $ioctime ) 
80       = split(' ', $res);
81    if ($bits == 64) {
82       ( $iodev, $ioino, $ionlink, $iomode, $iouid, $iogid, $iordev, 
83            $iosize, $ioblksize, $ioblks, $ioatime, $iomtime, $ioctime ) 
84           = split(' ', $res);
85   }
86   $iomode = oct($iomode);
87
88   # Print out the path
89   print_path($iomode, $path);
90 }
91   
92 sub process_cmd
93 {
94   my ($usestdin, $isalpha, @paths) = @_;
95
96   my $path;
97   
98   # Get tests directory
99   my $testdir = $FindBin::Bin;
100  
101   eval {
102     if ($isalpha == 0) {
103       open2(\*OUTFILE, \*CMDFILE, "$testdir/test_driver --np");
104     } else {
105       open2(\*OUTFILE, \*CMDFILE, 
106             "yod -batch -quiet -sz 1 $testdir/test_driver --np");
107     }
108   };
109
110   if ($@) {
111     if ($@ =~ /^open2/) {
112       warn "open2 failed: $!\n$@\n";
113       return;
114     }
115     die;
116
117   }
118   my $outfh = \*OUTFILE;
119   my $cmdfh = \*CMDFILE;
120
121   if ($isalpha == 0) {
122     helper::send_cmd($cmdfh, $outfh, "init", "CALL init\n");
123   }
124
125   # Allocate the stat buffer
126   my $cmdstr = '$buf = ALLOC ( $size = CALL sizeof stat )';
127   $cmdstr .= "\n";
128   helper::send_cmd($cmdfh, $outfh, "alloc", $cmdstr);  
129
130    # Attempt to determine type
131   $cmdstr = 'PRINT $size'."\n";
132   helper::send_cmd($cmdfh, $outfh, "print", $cmdstr);  
133   my $statsize = <$outfh>;
134   chop($statsize);
135   $statsize = oct($statsize);
136   my $bits = 32;
137   if ($statsize == 144) {
138       $bits = 64;
139   }
140
141   my $i=0;
142   if ($usestdin) {
143     $path = <STDIN>;
144     if (defined($path)) {
145       chop($path);
146     }
147   } else {
148     $path = $paths[$i++];
149   }
150
151   # Enter a loop, reading a path argument and processing it with each 
152   # phase of loop.  
153   while (defined($path)) {
154
155     process_path($cmdfh, $outfh, $bits, $path);
156     if ($usestdin) {
157       $path = <STDIN>;
158
159       if (defined($path)) {
160         chop($path);
161       }
162       if ($path eq "quit") {
163         helper::print_and_exit($cmdfh, $outfh, 0, "path test successful\n");
164       }
165     } else {
166       $path = $paths[$i++];
167     }
168   }
169   helper::print_and_exit($cmdfh, $outfh, 0, "path test successful\n");
170 }
171
172
173 my $usestdin = 0;
174 my $isalpha = 0;
175
176 # The -alpha arg must be before the paths
177 # (if they exist)
178 if ( (@ARGV > 0) && ($ARGV[0] eq "-alpha")) {
179   $isalpha = 1;
180   shift(@ARGV);
181 }
182
183 if (@ARGV == 0) {
184   $usestdin = 1;
185
186
187 process_cmd($usestdin, $isalpha, @ARGV);
188