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