This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Workaround on Cwd bootstrap problem.
[perl5.git] / t / io / fs.t
CommitLineData
8d063cd8
LW
1#!./perl
2
79072805 3# $RCSfile: fs.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:28 $
8d063cd8 4
ea368a7c
CS
5BEGIN {
6 chdir 't' if -d 't';
20822f61 7 @INC = '../lib';
ea368a7c
CS
8}
9
10use Config;
11
2986a63f 12$Is_Dosish = ($^O eq 'MSWin32' or $^O eq 'NetWare' or $^O eq 'dos' or
c9ff6e92
JH
13 $^O eq 'os2' or $^O eq 'mint' or $^O eq 'cygwin' or
14 $^O eq 'mpeix');
39e571d4 15
6b980173
JD
16if (defined &Win32::IsWinNT && Win32::IsWinNT()) {
17 $Is_Dosish = '' if Win32::FsType() eq 'NTFS';
18}
19
65cb15a1 20print "1..29\n";
8d063cd8 21
2986a63f 22$wd = ((($^O eq 'MSWin32') || ($^O eq 'NetWare')) ? `cd` : `pwd`);
378cc40b
LW
23chop($wd);
24
2986a63f 25if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) { `rmdir /s /q tmp 2>nul`; `mkdir tmp`; }
68dc0745 26else { `rm -f tmp 2>/dev/null; mkdir tmp 2>/dev/null`; }
378cc40b 27chdir './tmp';
b8440792 28`/bin/rm -rf a b c x` if -x '/bin/rm';
8d063cd8
LW
29
30umask(022);
31
2986a63f 32if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) { print "ok 1 # skipped: bogus umask()\n"; }
80252599 33elsif ((umask(0)&0777) == 022) {print "ok 1\n";} else {print "not ok 1\n";}
8d063cd8
LW
34open(fh,'>x') || die "Can't create x";
35close(fh);
36open(fh,'>a') || die "Can't create a";
37close(fh);
38
a245ea2d
IZ
39if ($Is_Dosish) {print "ok 2 # skipped: no link\n";}
40elsif (eval {link('a','b')}) {print "ok 2\n";}
41else {print "not ok 2\n";}
8d063cd8 42
a245ea2d
IZ
43if ($Is_Dosish) {print "ok 3 # skipped: no link\n";}
44elsif (eval {link('b','c')}) {print "ok 3\n";}
45else {print "not ok 3\n";}
8d063cd8
LW
46
47($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
48 $blksize,$blocks) = stat('c');
49
a245ea2d
IZ
50if ($Config{dont_use_nlink} || $Is_Dosish)
51 {print "ok 4 # skipped: no link\n";}
52elsif ($nlink == 3)
53 {print "ok 4\n";}
54else {print "not ok 4\n";}
ea368a7c 55
a245ea2d
IZ
56if ($^O eq 'amigaos' || $Is_Dosish)
57 {print "ok 5 # skipped: no link\n";}
58elsif (($mode & 0777) == 0666)
59 {print "ok 5\n";}
60else {print "not ok 5\n";}
8d063cd8 61
2986a63f 62$newmode = (($^O eq 'MSWin32') || ($^O eq 'NetWare')) ? 0444 : 0777;
6b980173 63if ((chmod $newmode,'a') == 1) {print "ok 6\n";} else {print "not ok 6\n";}
8d063cd8
LW
64
65($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
66 $blksize,$blocks) = stat('c');
a245ea2d 67if ($Is_Dosish) {print "ok 7 # skipped: no link\n";}
6b980173 68elsif (($mode & 0777) == $newmode) {print "ok 7\n";}
a245ea2d 69else {print "not ok 7\n";}
8d063cd8 70
6b980173 71$newmode = 0700;
2986a63f 72if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
6b980173
JD
73 chmod 0444, 'x';
74 $newmode = 0666;
75}
76
a245ea2d 77if ($Is_Dosish) {print "ok 8 # skipped: no link\n";}
6b980173 78elsif ((chmod $newmode,'c','x') == 2) {print "ok 8\n";}
a245ea2d 79else {print "not ok 8\n";}
8d063cd8
LW
80
81($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
82 $blksize,$blocks) = stat('c');
a245ea2d 83if ($Is_Dosish) {print "ok 9 # skipped: no link\n";}
6b980173 84elsif (($mode & 0777) == $newmode) {print "ok 9\n";}
a245ea2d
IZ
85else {print "not ok 9\n";}
86
8d063cd8
LW
87($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
88 $blksize,$blocks) = stat('x');
a245ea2d 89if ($Is_Dosish) {print "ok 10 # skipped: no link\n";}
6b980173 90elsif (($mode & 0777) == $newmode) {print "ok 10\n";}
a245ea2d 91else {print "not ok 10\n";}
8d063cd8 92
a245ea2d
IZ
93if ($Is_Dosish) {print "ok 11 # skipped: no link\n"; unlink 'b','x'; }
94elsif ((unlink 'b','x') == 2) {print "ok 11\n";}
95else {print "not ok 11\n";}
8d063cd8
LW
96($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
97 $blksize,$blocks) = stat('b');
98if ($ino == 0) {print "ok 12\n";} else {print "not ok 12\n";}
99($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
100 $blksize,$blocks) = stat('x');
101if ($ino == 0) {print "ok 13\n";} else {print "not ok 13\n";}
102
103if (rename('a','b')) {print "ok 14\n";} else {print "not ok 14\n";}
104($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
105 $blksize,$blocks) = stat('a');
106if ($ino == 0) {print "ok 15\n";} else {print "not ok 15\n";}
a245ea2d 107$delta = $Is_Dosish ? 2 : 1; # Granularity of time on the filesystem
1d825fcc 108chmod 0777, 'b';
a245ea2d 109$foo = (utime 500000000,500000000 + $delta,'b');
378cc40b 110if ($foo == 1) {print "ok 16\n";} else {print "not ok 16 $foo\n";}
8d063cd8
LW
111($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
112 $blksize,$blocks) = stat('b');
2986a63f 113if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) { print "ok 17 # skipped: bogus (stat)[1]\n"; }
80252599 114elsif ($ino) {print "ok 17\n";} else {print "not ok 17\n";}
897bbb29 115if ($wd =~ m#$Config{'afsroot'}/# || $^O eq 'amigaos' || $^O eq 'dos' || $^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'cygwin')
a245ea2d
IZ
116 {print "ok 18 # skipped: granularity of the filetime\n";}
117elsif ($atime == 500000000 && $mtime == 500000000 + $delta)
6eb13c3b 118 {print "ok 18\n";}
da93fe49
RM
119elsif ($^O =~ /\blinux\b/i) {
120 # Maybe stat() cannot get the correct atime, as happens via NFS on linux?
121 $foo = (utime 400000000,500000000 + 2*$delta,'b');
122 my ($new_atime, $new_mtime) = (stat('b'))[8,9];
123 if ($new_atime == $atime && $new_mtime - $mtime == $delta)
124 {print "ok 18 # accounted for possible NFS/glibc2.2 bug on linux\n";}
125 else
126 {print "not ok 18 $atime/$new_atime $mtime/$new_mtime\n";}
127} else
6eb13c3b 128 {print "not ok 18 $atime $mtime\n";}
8d063cd8 129
378cc40b 130if ((unlink 'b') == 1) {print "ok 19\n";} else {print "not ok 19\n";}
8d063cd8
LW
131($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
132 $blksize,$blocks) = stat('b');
378cc40b
LW
133if ($ino == 0) {print "ok 20\n";} else {print "not ok 20\n";}
134unlink 'c';
135
136chdir $wd || die "Can't cd back to $wd";
137
8d063cd8 138unlink 'c';
7054c437
PK
139# Yet another way to look for links (perhaps those that cannot be created by perl?).
140# Hopefully there is an ls utility in your %PATH%. N.B. that $^O is 'cygwin' on Cygwin.
141if ((($^O eq 'MSWin32') || ($^O eq 'NetWare')) and `ls -l perl 2>nul` =~ /^l.*->/) {
68dc0745 142 # we have symbolic links
4ba7095c
JH
143 system("cp TEST TEST$$");
144 # we have to copy because e.g. GNU grep gets huffy if we have
145 # a symlink forest to another disk (it complains about too many
146 # levels of symbolic links, even if we have only two)
147 if (symlink("TEST$$","c")) {print "ok 21\n";} else {print "not ok 21\n";}
148 $foo = `grep perl c 2>&1`;
378cc40b 149 if ($foo) {print "ok 22\n";} else {print "not ok 22\n";}
44a8e56a 150 unlink 'c';
4ba7095c 151 unlink("TEST$$");
378cc40b
LW
152}
153else {
7054c437
PK
154 if ( ($^O eq 'MSWin32') || ($^O eq 'NetWare') ) {
155 print "ok 21 # skipped: no link\nok 22 # skipped: no link\n";
156 }
157 else {
158 print "ok 21 # skipped: $^O is neither 'MSWin32' nor 'NetWare'\nok 22 # skipped: $^O is neither 'MSWin32' nor 'NetWare'\n";
159 }
378cc40b 160}
f783569b
PP
161
162# truncate (may not be implemented everywhere)
163unlink "Iofs.tmp";
164`echo helloworld > Iofs.tmp`;
165eval { truncate "Iofs.tmp", 5; };
62b86938
A
166if ($@) {
167 if ($@ =~ /not implemented/) {
168 print "# truncate not implemented -- skipping tests 23 through 26\n";
169 for (23 .. 26) {
170 print "ok $_ # Skip: no truncate\n";
171 }
172 } else {
173 warn "io/fs before test 23: truncate dies with \$\@[$@]";
f783569b
PP
174 }
175}
176else {
62b86938
A
177 if (-s "Iofs.tmp" == 5) {
178 print "ok 23\n";
179 } else {
180 my $s = -s "Iofs.tmp";
181 printf "# -s Iofs.tmp: %s\n", defined($s) ? $s : "UNDEFINED";
182 print "not ok 23\n";
183 }
f783569b
PP
184 truncate "Iofs.tmp", 0;
185 if (-z "Iofs.tmp") {print "ok 24\n"} else {print "not ok 24\n"}
f783569b 186 open(FH, ">Iofs.tmp") or die "Can't create Iofs.tmp";
2f3b6ae4 187 binmode FH;
1bcfde30 188 { select FH; $| = 1; select STDOUT }
bb53490d
GS
189 {
190 use strict;
2f3b6ae4
GS
191 print FH "x\n" x 200;
192 truncate(FH, 200) or die "Can't truncate FH: $!";
bb53490d 193 }
ed344e4f
IZ
194 if ($^O eq 'dos'
195 # Not needed on HPFS, but needed on HPFS386 ?!
196 or $^O eq 'os2')
197 {
39e571d4
ML
198 close (FH); open (FH, ">>Iofs.tmp") or die "Can't reopen Iofs.tmp";
199 }
2f3b6ae4 200 if (-s "Iofs.tmp" == 200) {print "ok 25\n"} else {print "not ok 25\n"}
f783569b 201 truncate FH, 0;
ed344e4f
IZ
202 if ($^O eq 'dos'
203 # Not needed on HPFS, but needed on HPFS386 ?!
204 or $^O eq 'os2')
205 {
39e571d4
ML
206 close (FH); open (FH, ">>Iofs.tmp") or die "Can't reopen Iofs.tmp";
207 }
f783569b
PP
208 if (-z "Iofs.tmp") {print "ok 26\n"} else {print "not ok 26\n"}
209 close FH;
210}
80252599 211
65cb15a1
GS
212# check if rename() can be used to just change case of filename
213chdir './tmp';
214open(fh,'>x') || die "Can't create x";
215close(fh);
216rename('x', 'X');
217print 'not ' unless -e 'X';
218print "ok 27\n";
219unlink 'X';
220chdir $wd || die "Can't cd back to $wd";
221
80252599
GS
222# check if rename() works on directories
223rename 'tmp', 'tmp1' or print "not ";
80252599 224print "ok 28\n";
65cb15a1
GS
225-d 'tmp1' or print "not ";
226print "ok 29\n";
80252599
GS
227
228END { rmdir 'tmp1'; unlink "Iofs.tmp"; }