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