This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: futimes [PATCH]
[perl5.git] / t / io / fs.t
CommitLineData
8d063cd8
LW
1#!./perl
2
ea368a7c
CS
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
0c5d4ba3 6 require "./test.pl";
ea368a7c
CS
7}
8
9use Config;
dc459aad 10use File::Spec::Functions;
ea368a7c 11
dc459aad 12my $Is_MacOS = ($^O eq 'MacOS');
6d738113 13my $Is_VMSish = ($^O eq 'VMS');
0c5d4ba3 14
20dd405c
MS
15if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
16 $wd = `cd`;
fc8d54b0
GA
17}
18elsif ($^O eq 'VMS') {
20dd405c 19 $wd = `show default`;
fc8d54b0
GA
20}
21else {
20dd405c
MS
22 $wd = `pwd`;
23}
24chomp($wd);
25
0c5d4ba3
JH
26my $has_link = $Config{d_link};
27my $accurate_timestamps =
28 !($^O eq 'MSWin32' || $^O eq 'NetWare' ||
29 $^O eq 'dos' || $^O eq 'os2' ||
20dd405c 30 $^O eq 'mint' || $^O eq 'cygwin' ||
dc459aad
JH
31 $^O eq 'amigaos' || $wd =~ m#$Config{afsroot}/# ||
32 $Is_MacOS
20dd405c 33 );
39e571d4 34
6b980173 35if (defined &Win32::IsWinNT && Win32::IsWinNT()) {
0c5d4ba3 36 if (Win32::FsType() eq 'NTFS') {
20dd405c
MS
37 $has_link = 1;
38 $accurate_timestamps = 1;
0c5d4ba3 39 }
6b980173
JD
40}
41
0c5d4ba3
JH
42my $needs_fh_reopen =
43 $^O eq 'dos'
44 # Not needed on HPFS, but needed on HPFS386 ?!
45 || $^O eq 'os2';
46
7a2cf369
NK
47$needs_fh_reopen = 1 if (defined &Win32::IsWin95 && Win32::IsWin95());
48
4e51f8e4
SR
49my $skip_mode_checks =
50 $^O eq 'cygwin' && $ENV{CYGWIN} !~ /ntsec/;
51
e96b369d 52plan tests => 49;
8d063cd8 53
378cc40b 54
6d738113
PP
55if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
56 `rmdir /s /q tmp 2>nul`;
57 `mkdir tmp`;
dc459aad
JH
58}
59elsif ($^O eq 'VMS') {
6d738113
PP
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}
dc459aad
JH
64elsif ($Is_MacOS) {
65 rmdir "tmp"; mkdir "tmp";
66}
6d738113
PP
67else {
68 `rm -f tmp 2>/dev/null; mkdir tmp 2>/dev/null`;
69}
0c5d4ba3 70
dc459aad 71chdir catdir(curdir(), 'tmp');
0c5d4ba3 72
b8440792 73`/bin/rm -rf a b c x` if -x '/bin/rm';
8d063cd8
LW
74
75umask(022);
76
20dd405c 77SKIP: {
dc459aad 78 skip "bogus umask", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare') || ($^O eq 'epoc') || $Is_MacOS;
20dd405c
MS
79
80 is((umask(0)&0777), 022, 'umask'),
0c5d4ba3
JH
81}
82
d5fc3e70
SP
83open(FH,'>x') || die "Can't create x";
84close(FH);
85open(FH,'>a') || die "Can't create a";
86close(FH);
8d063cd8 87
8268670f
JH
88my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
89 $blksize,$blocks);
90
3ed9f8f7 91SKIP: {
0c5d4ba3 92 skip("no link", 4) unless $has_link;
8d063cd8 93
0c5d4ba3
JH
94 ok(link('a','b'), "link a b");
95 ok(link('b','c'), "link b c");
8d063cd8 96
8268670f
JH
97 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
98 $blksize,$blocks) = stat('c');
8d063cd8 99
20dd405c
MS
100 SKIP: {
101 skip "no nlink", 1 if $Config{dont_use_nlink};
102
103 is($nlink, 3, "link count of triply-linked file");
0c5d4ba3 104 }
ea368a7c 105
20dd405c
MS
106 SKIP: {
107 skip "hard links not that hard in $^O", 1 if $^O eq 'amigaos';
4e51f8e4 108 skip "no mode checks", 1 if $skip_mode_checks;
20dd405c 109
bbf171ae
GH
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 {
4e51f8e4 113 is($mode & 0777, 0666, "mode of triply-linked file");
bbf171ae 114# }
0c5d4ba3
JH
115 }
116}
8d063cd8 117
2986a63f 118$newmode = (($^O eq 'MSWin32') || ($^O eq 'NetWare')) ? 0444 : 0777;
8d063cd8 119
0c5d4ba3 120is(chmod($newmode,'a'), 1, "chmod succeeding");
8d063cd8 121
0c5d4ba3 122SKIP: {
2f3b333f 123 skip("no link", 7) unless $has_link;
0c5d4ba3 124
8268670f
JH
125 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
126 $blksize,$blocks) = stat('c');
0c5d4ba3 127
4e51f8e4
SR
128 SKIP: {
129 skip "no mode checks", 1 if $skip_mode_checks;
130
131 is($mode & 0777, $newmode, "chmod going through");
132 }
0c5d4ba3
JH
133
134 $newmode = 0700;
6b980173
JD
135 chmod 0444, 'x';
136 $newmode = 0666;
6b980173 137
0c5d4ba3 138 is(chmod($newmode,'c','x'), 2, "chmod two files");
3ed9f8f7 139
0c5d4ba3
JH
140 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
141 $blksize,$blocks) = stat('c');
8d063cd8 142
4e51f8e4
SR
143 SKIP: {
144 skip "no mode checks", 1 if $skip_mode_checks;
145
146 is($mode & 0777, $newmode, "chmod going through to c");
147 }
a245ea2d 148
0c5d4ba3
JH
149 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
150 $blksize,$blocks) = stat('x');
8d063cd8 151
4e51f8e4
SR
152 SKIP: {
153 skip "no mode checks", 1 if $skip_mode_checks;
154
155 is($mode & 0777, $newmode, "chmod going through to x");
156 }
0c5d4ba3
JH
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");
8268670f 169}
0c5d4ba3 170
c4aca7d0
GA
171SKIP: {
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
182SKIP: {
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
188SKIP: {
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
195SKIP: {
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
8268670f 202is(rename('a','b'), 1, "rename a b");
0c5d4ba3 203
8268670f
JH
204($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
205 $blksize,$blocks) = stat('a');
0c5d4ba3 206
8268670f 207is($ino, undef, "ino of renamed file a should be undef");
0c5d4ba3
JH
208
209$delta = $accurate_timestamps ? 1 : 2; # Granularity of time on the filesystem
1d825fcc 210chmod 0777, 'b';
0c5d4ba3 211
e96b369d 212$foo = (utime 500000000,500000000 + $delta,'b');
0c5d4ba3 213is($foo, 1, "utime");
e96b369d
GA
214check_utime_result();
215
216utime undef, undef, 'b';
217($atime,$mtime) = (stat 'b')[8,9];
218print "# utime undef, undef --> $atime, $mtime\n";
219isnt($atime, 500000000, 'atime');
220isnt($mtime, 500000000 + $delta, 'mtime');
221
222SKIP: {
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}
0c5d4ba3 229
e96b369d
GA
230
231sub check_utime_result {
fc8d54b0
GA
232 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
233 $blksize,$blocks) = stat('b');
0c5d4ba3 234
fc8d54b0
GA
235 SKIP: {
236 skip "bogus inode num", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare');
20dd405c 237
fc8d54b0
GA
238 ok($ino, 'non-zero inode num');
239 }
0c5d4ba3 240
fc8d54b0
GA
241 SKIP: {
242 skip "filesystem atime/mtime granularity too low", 2
243 unless $accurate_timestamps;
20dd405c 244
fc8d54b0
GA
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 }
0c5d4ba3 282 }
6d738113 283}
e96b369d
GA
284
285SKIP: {
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}
0c5d4ba3
JH
291
292is(unlink('b'), 1, "unlink b");
293
8d063cd8
LW
294($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
295 $blksize,$blocks) = stat('b');
0c5d4ba3 296is($ino, undef, "ino of unlinked file b should be undef");
378cc40b
LW
297unlink 'c';
298
299chdir $wd || die "Can't cd back to $wd";
300
0c5d4ba3
JH
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
20dd405c
MS
305SKIP: {
306 skip "Win32/Netware specific test", 2
307 unless ($^O eq 'MSWin32') || ($^O eq 'NetWare');
3ed9f8f7 308 skip "No symbolic links found to test with", 2
20dd405c
MS
309 unless `ls -l perl 2>nul` =~ /^l.*->/;
310
4ba7095c
JH
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)
0c5d4ba3 315 is(symlink("TEST$$","c"), 1, "symlink");
4ba7095c 316 $foo = `grep perl c 2>&1`;
0c5d4ba3 317 ok($foo, "found perl in c");
44a8e56a 318 unlink 'c';
4ba7095c 319 unlink("TEST$$");
378cc40b 320}
f783569b 321
f783569b 322unlink "Iofs.tmp";
0c5d4ba3
JH
323open IOFSCOM, ">Iofs.tmp" or die "Could not write IOfs.tmp: $!";
324print IOFSCOM 'helloworld';
325close(IOFSCOM);
326
327# TODO: pp_truncate needs to be taught about F_CHSIZE and F_FREESP,
328# as per UNIX FAQ.
329
330SKIP: {
63720136 331# Check truncating a closed file.
0c5d4ba3 332 eval { truncate "Iofs.tmp", 5; };
90ddc76f 333
090bf15b 334 skip("no truncate - $@", 8) if $@;
0c5d4ba3
JH
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
7a2cf369
NK
342#these steps are necessary to check if file is really truncated
343#On Win95, FH is updated, but file properties aren't
0c5d4ba3 344 open(FH, ">Iofs.tmp") or die "Can't create Iofs.tmp";
7a2cf369
NK
345 print FH "x\n" x 200;
346 close FH;
347
63720136
PG
348# Check truncating an open file.
349 open(FH, ">>Iofs.tmp") or die "Can't open Iofs.tmp for appending";
0c5d4ba3
JH
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");
62b86938 360 }
0c5d4ba3
JH
361
362 if ($needs_fh_reopen) {
363 close (FH); open (FH, ">>Iofs.tmp") or die "Can't reopen Iofs.tmp";
364 }
90ddc76f 365
090bf15b
SR
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 }
0c5d4ba3 370
090bf15b 371 is(-s "Iofs.tmp", 200, "fh resize to 200 working (filename check)");
63720136 372
090bf15b 373 ok(truncate(FH, 0), "fh resize to zero");
0c5d4ba3 374
090bf15b
SR
375 if ($needs_fh_reopen) {
376 close (FH); open (FH, ">>Iofs.tmp") or die "Can't reopen Iofs.tmp";
377 }
0c5d4ba3 378
090bf15b 379 ok(-z "Iofs.tmp", "fh resize to zero working (filename check)");
7a2cf369 380
090bf15b
SR
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 }
f783569b 404}
80252599 405
65cb15a1 406# check if rename() can be used to just change case of filename
20dd405c
MS
407SKIP: {
408 skip "Works in Cygwin only if check_case is set to relaxed", 1
409 if $^O eq 'cygwin';
410
0c5d4ba3 411 chdir './tmp';
d5fc3e70
SP
412 open(FH,'>x') || die "Can't create x";
413 close(FH);
0c5d4ba3 414 rename('x', 'X');
3ed9f8f7 415
0c5d4ba3
JH
416 # this works on win32 only, because fs isn't casesensitive
417 ok(-e 'X', "rename working");
8268670f 418
20dd405c 419 1 while unlink 'X';
0c5d4ba3 420 chdir $wd || die "Can't cd back to $wd";
73077d53 421}
65cb15a1 422
80252599 423# check if rename() works on directories
0c5d4ba3 424if ($^O eq 'VMS') {
9df548ee
CB
425 # must have delete access to rename a directory
426 `set file tmp.dir/protection=o:d`;
20dd405c
MS
427 ok(rename('tmp.dir', 'tmp1.dir'), "rename on directories") ||
428 print "# errno: $!\n";
fc8d54b0
GA
429}
430else {
0c5d4ba3 431 ok(rename('tmp', 'tmp1'), "rename on directories");
6d738113 432}
0c5d4ba3
JH
433
434ok(-d 'tmp1', "rename on directories working");
80252599 435
73077d53 436# need to remove 'tmp' if rename() in test 28 failed!
4e7ee149 437END { rmdir 'tmp1'; rmdir 'tmp'; 1 while unlink "Iofs.tmp"; }