cat2type in tests for VMS
[p5sagit/p5-mst-13.2.git] / t / io / fs.t
1 #!./perl
2
3 # $RCSfile: fs.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:28 $
4
5 BEGIN {
6     chdir 't' if -d 't';
7     @INC = '../lib';
8 }
9
10 use Config;
11
12 my $Is_VMSish = ($^O eq 'VMS');
13 $Is_Dosish = ($^O eq 'MSWin32' or $^O eq 'NetWare' or $^O eq 'dos' or
14               $^O eq 'os2' or $^O eq 'mint' or $^O eq 'cygwin' or
15               $^O eq 'mpeix');
16
17 if (defined &Win32::IsWinNT && Win32::IsWinNT()) {
18     $Is_Dosish = '' if Win32::FsType() eq 'NTFS';
19 }
20
21 print "1..29\n";
22
23 $wd = ((($^O eq 'MSWin32') || ($^O eq 'NetWare')) ? `cd` : ($Is_VMSish) ? `show default` : `pwd`);
24 chop($wd);
25
26 if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
27     `rmdir /s /q tmp 2>nul`;
28     `mkdir tmp`;
29 }
30 elsif ($Is_VMSish) {
31     `if f\$search("[.tmp]*.*") .nes. "" then delete/nolog/noconfirm [.tmp]*.*.*`;
32     `if f\$search("tmp.dir") .nes. "" then delete/nolog/noconfirm tmp.dir;`;
33     `create/directory [.tmp]`;
34 }
35 else {
36     `rm -f tmp 2>/dev/null; mkdir tmp 2>/dev/null`;
37 }
38 chdir './tmp';
39 `/bin/rm -rf a b c x` if -x '/bin/rm';
40
41 umask(022);
42
43 if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) { print "ok 1 # skipped: bogus umask()\n"; }
44 elsif ((umask(0)&0777) == 022) {print "ok 1\n";} else {print "not ok 1\n";}
45 open(fh,'>x') || die "Can't create x";
46 close(fh);
47 open(fh,'>a') || die "Can't create a";
48 close(fh);
49
50 if ($Is_Dosish || $Is_VMSish) {print "ok 2 # skipped: no link\n";} 
51 elsif (eval {link('a','b')}) {print "ok 2\n";} 
52 else {print "not ok 2\n";}
53
54 if ($Is_Dosish || $Is_VMSish) {print "ok 3 # skipped: no link\n";} 
55 elsif (eval {link('b','c')}) {print "ok 3\n";} 
56 else {print "not ok 3\n";}
57
58 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
59     $blksize,$blocks) = stat('c');
60
61 if ($Config{dont_use_nlink} || $Is_Dosish || $Is_VMSish)
62     {print "ok 4 # skipped: no link\n";} 
63 elsif ($nlink == 3)
64     {print "ok 4\n";} 
65 else {print "not ok 4\n";}
66
67 if ($^O eq 'amigaos' || $Is_Dosish || $Is_VMSish)
68     {print "ok 5 # skipped: no link\n";} 
69 elsif (($mode & 0777) == 0666)
70     {print "ok 5\n";} 
71 else {print "not ok 5\n";}
72
73 $newmode = (($^O eq 'MSWin32') || ($^O eq 'NetWare')) ? 0444 : 0777;
74 if ((chmod $newmode,'a') == 1) {print "ok 6\n";} else {print "not ok 6\n";}
75
76 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
77     $blksize,$blocks) = stat('c');
78 if ($Is_Dosish || $Is_VMSish) {print "ok 7 # skipped: no link\n";} 
79 elsif (($mode & 0777) == $newmode) {print "ok 7\n";} 
80 else {print "not ok 7\n";}
81
82 $newmode = 0700;
83 if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
84     chmod 0444, 'x';
85     $newmode = 0666;
86 }
87
88 if ($Is_Dosish || $Is_VMSish) {print "ok 8 # skipped: no link\n";} 
89 elsif ((chmod $newmode,'c','x') == 2) {print "ok 8\n";} 
90 else {print "not ok 8\n";}
91
92 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
93     $blksize,$blocks) = stat('c');
94 if ($Is_Dosish || $Is_VMSish) {print "ok 9 # skipped: no link\n";} 
95 elsif (($mode & 0777) == $newmode) {print "ok 9\n";} 
96 else {print "not ok 9\n";}
97
98 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
99     $blksize,$blocks) = stat('x');
100 if ($Is_Dosish || $Is_VMSish) {print "ok 10 # skipped: no link\n";} 
101 elsif (($mode & 0777) == $newmode) {print "ok 10\n";} 
102 else {print "not ok 10\n";}
103
104 if ($Is_Dosish || $Is_VMSish) {print "ok 11 # skipped: no link\n"; unlink 'b','x'; } 
105 elsif ((unlink 'b','x') == 2) {print "ok 11\n";} 
106 else {print "not ok 11\n";}
107 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
108     $blksize,$blocks) = stat('b');
109 if ($ino == 0) {print "ok 12\n";} else {print "not ok 12\n";}
110 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
111     $blksize,$blocks) = stat('x');
112 if ($ino == 0) {print "ok 13\n";} else {print "not ok 13\n";}
113
114 if (rename('a','b')) {print "ok 14\n";} else {print "not ok 14\n";}
115 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
116     $blksize,$blocks) = stat('a');
117 if ($ino == 0) {print "ok 15\n";} else {print "not ok 15\n";}
118 $delta = $Is_Dosish ? 2 : 1;    # Granularity of time on the filesystem
119 chmod 0777, 'b';
120 $foo = (utime 500000000,500000000 + $delta,'b');
121 if ($foo == 1) {print "ok 16\n";} else {print "not ok 16 $foo\n";}
122 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
123     $blksize,$blocks) = stat('b');
124 if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) { print "ok 17 # skipped: bogus (stat)[1]\n"; }
125 elsif ($ino) {print "ok 17\n";} else {print "not ok 17\n";}
126 if ($wd =~ m#$Config{'afsroot'}/# || $^O eq 'amigaos' || $^O eq 'dos' || $^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'cygwin')
127     {print "ok 18 # skipped: granularity of the filetime\n";}
128 elsif ($atime == 500000000 && $mtime == 500000000 + $delta)
129     {print "ok 18\n";}
130 elsif ($^O =~ /\blinux\b/i) {
131     # Maybe stat() cannot get the correct atime, as happens via NFS on linux?
132     $foo = (utime 400000000,500000000 + 2*$delta,'b');
133     my ($new_atime, $new_mtime) = (stat('b'))[8,9];
134     if ($new_atime == $atime && $new_mtime - $mtime == $delta)
135         {print "ok 18 # accounted for possible NFS/glibc2.2 bug on linux\n";}
136     else
137         {print "not ok 18 $atime/$new_atime $mtime/$new_mtime\n";}
138 }
139 elsif ($Is_VMSish) {
140     if ($atime == 500000001 && $mtime == 500000000 + $delta)
141         {print "ok 18\n";}
142     else
143         {print "not ok 18 $atime $mtime\n";}
144 } else
145     {print "not ok 18 $atime $mtime\n";}
146
147 if ((unlink 'b') == 1) {print "ok 19\n";} else {print "not ok 19\n";}
148 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
149     $blksize,$blocks) = stat('b');
150 if ($ino == 0) {print "ok 20\n";} else {print "not ok 20\n";}
151 unlink 'c';
152
153 chdir $wd || die "Can't cd back to $wd";
154
155 unlink 'c';
156 # Yet another way to look for links (perhaps those that cannot be created by perl?).
157 # Hopefully there is an ls utility in your %PATH%. N.B. that $^O is 'cygwin' on Cygwin.
158 if ((($^O eq 'MSWin32') || ($^O eq 'NetWare')) and `ls -l perl 2>nul` =~ /^l.*->/) {
159     # we have symbolic links
160     system("cp TEST TEST$$");
161     # we have to copy because e.g. GNU grep gets huffy if we have
162     # a symlink forest to another disk (it complains about too many
163     # levels of symbolic links, even if we have only two)
164     if (symlink("TEST$$","c")) {print "ok 21\n";} else {print "not ok 21\n";}
165     $foo = `grep perl c 2>&1`;
166     if ($foo) {print "ok 22\n";} else {print "not ok 22\n";}
167     unlink 'c';
168     unlink("TEST$$");
169 }
170 else {
171     if ( ($^O eq 'MSWin32') || ($^O eq 'NetWare') ) {
172         print "ok 21 # skipped: no link\nok 22 # skipped: no link\n";
173     }
174     else {
175         print "ok 21 # skipped: '$^O' is neither 'MSWin32' nor 'NetWare'\nok 22 # skipped: $^O is neither 'MSWin32' nor 'NetWare'\n";
176     }
177 }
178
179 # truncate (may not be implemented everywhere)
180 unlink "Iofs.tmp";
181 if ($Is_VMSish) {
182     open IOFSCOM, ">Iofs.tmp" or die "Could not write IOfs.tmp: $!";
183     print IOFSCOM 'helloworld';
184     close(IOFSCOM);
185 }
186 else {
187     `echo helloworld > Iofs.tmp`;
188 }
189 #
190 # Perhaps the eval would be better written with a construct such as?:
191 #if (defined($Config{d_truncate}) && $Config{d_truncate} eq 'define') {
192 #
193 eval { truncate "Iofs.tmp", 5; };
194 if ($@) {
195   if ($@ =~ /not implemented/) {
196     print "# truncate not implemented -- skipping tests 23 through 26\n";
197     for (23 .. 26) {
198       print "ok $_ # Skip: no truncate\n";
199     }
200   } else {
201     warn "io/fs before test 23: truncate dies with \$\@[$@]";
202   }
203 }
204 else {
205   if (-s "Iofs.tmp" == 5) {
206     print "ok 23\n";
207   } else {
208     my $s = -s "Iofs.tmp";
209     printf "# -s Iofs.tmp: %s\n", defined($s) ? $s : "UNDEFINED";
210     print "not ok 23\n";
211   }
212   truncate "Iofs.tmp", 0;
213   if (-z "Iofs.tmp") {print "ok 24\n"} else {print "not ok 24\n"}
214   open(FH, ">Iofs.tmp") or die "Can't create Iofs.tmp";
215   binmode FH;
216   { select FH; $| = 1; select STDOUT }
217   {
218     use strict;
219     print FH "x\n" x 200;
220     truncate(FH, 200) or die "Can't truncate FH: $!";
221   }
222   if ($^O eq 'dos'
223         # Not needed on HPFS, but needed on HPFS386 ?!
224       or $^O eq 'os2')
225   {
226       close (FH); open (FH, ">>Iofs.tmp") or die "Can't reopen Iofs.tmp";
227   }
228   if (-s "Iofs.tmp" == 200) {
229       print "ok 25\n"
230   }
231   else {
232     my $s = -s "Iofs.tmp";
233     printf "# -s Iofs.tmp: %s\n", defined($s) ? $s : "UNDEFINED";
234     print "not ok 25\n";
235   }
236   truncate FH, 0;
237   if ($^O eq 'dos'
238         # Not needed on HPFS, but needed on HPFS386 ?!
239       or $^O eq 'os2')
240   {
241       close (FH); open (FH, ">>Iofs.tmp") or die "Can't reopen Iofs.tmp";
242   }
243   if (-z "Iofs.tmp") {print "ok 26\n"} else {print "not ok 26\n"}
244   close FH;
245 }
246
247 # check if rename() can be used to just change case of filename
248 if ($^O eq 'cygwin') {
249   print "ok 27 # skipped: works only if check_case is set to relaxed.\n";
250 } else { 
251   chdir './tmp';
252   open(fh,'>x') || die "Can't create x";
253   close(fh);
254   rename('x', 'X');
255   
256   # this works on win32 only, because fs isn't casesensitive
257   print 'not ' unless -e 'X'; 
258   
259   print "ok 27\n";
260   unlink 'X';
261   chdir $wd || die "Can't cd back to $wd";
262 }
263
264 # check if rename() works on directories
265 if ($Is_VMSish) {
266     # must have delete access to rename a directory
267     `set file tmp.dir/protection=o:d`;
268     rename 'tmp.dir', 'tmp1.dir' or print "not ";
269 }
270 else {
271     rename 'tmp', 'tmp1' or print "not ";
272 }
273 print "ok 28\n";
274 -d 'tmp1' or print "not ";
275 print "ok 29\n";
276
277 # need to remove 'tmp' if rename() in test 28 failed!
278 END { rmdir 'tmp1'; rmdir 'tmp'; unlink "Iofs.tmp"; }