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