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