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