Integrate mainline (for ndbm fixes etc.)
[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
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 plan tests => 32;
43
44
45 if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
46     `rmdir /s /q tmp 2>nul`;
47     `mkdir tmp`;
48 } elsif ($^O eq 'VMS') {
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 }
53 else {
54     `rm -f tmp 2>/dev/null; mkdir tmp 2>/dev/null`;
55 }
56
57 chdir './tmp';
58
59 `/bin/rm -rf a b c x` if -x '/bin/rm';
60
61 umask(022);
62
63 SKIP: {
64     skip "bogus umask", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare');
65
66     is((umask(0)&0777), 022, 'umask'),
67 }
68
69 open(fh,'>x') || die "Can't create x";
70 close(fh);
71 open(fh,'>a') || die "Can't create a";
72 close(fh);
73
74 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
75     $blksize,$blocks);
76
77 SKIP: { 
78     skip("no link", 4) unless $has_link;
79
80     ok(link('a','b'), "link a b");
81     ok(link('b','c'), "link b c");
82
83     ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
84      $blksize,$blocks) = stat('c');
85
86     SKIP: {
87         skip "no nlink", 1 if $Config{dont_use_nlink};
88
89         is($nlink, 3, "link count of triply-linked file");
90     }
91
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");
96     }
97 }
98
99 $newmode = (($^O eq 'MSWin32') || ($^O eq 'NetWare')) ? 0444 : 0777;
100
101 is(chmod($newmode,'a'), 1, "chmod succeeding");
102
103 SKIP: {
104     skip("no link", 7) unless $has_link;
105
106     ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
107      $blksize,$blocks) = stat('c');
108
109     is($mode & 0777, $newmode, "chmod going through");
110
111     $newmode = 0700;
112     chmod 0444, 'x';
113     $newmode = 0666;
114
115     is(chmod($newmode,'c','x'), 2, "chmod two files");
116     
117     ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
118      $blksize,$blocks) = stat('c');
119
120     is($mode & 0777, $newmode, "chmod going through to c");
121
122     ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
123      $blksize,$blocks) = stat('x');
124
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");
138 }
139
140 is(rename('a','b'), 1, "rename a b");
141
142 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
143  $blksize,$blocks) = stat('a');
144
145 is($ino, undef, "ino of renamed file a should be undef");
146
147 $delta = $accurate_timestamps ? 1 : 2;  # Granularity of time on the filesystem
148 chmod 0777, 'b';
149 $foo = (utime 500000000,500000000 + $delta,'b');
150
151 is($foo, 1, "utime");
152
153 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
154     $blksize,$blocks) = stat('b');
155
156 SKIP: {
157     skip "bogus inode num", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare');
158
159     ok($ino,    'non-zero inode num');
160 }
161
162 SKIP: {
163     skip "filesystem atime/mtime granularity too low", 2 
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');
170     }
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) {
179                 pass("atime/mtime - accounted for possible NFS/glibc2.2 bug on linux");
180             } 
181             else {
182                 fail("atime mtime - $atime/$new_atime $mtime/$new_mtime");
183             }
184         } 
185         elsif ($^O eq 'VMS') {
186             # why is this 1 second off?
187             is( $atime, 500000001,          'atime' );
188             is( $mtime, 500000000 + $delta, 'mtime' );
189         } 
190         elsif ($^O eq 'beos') {
191             SKIP: { skip "atime not updated", 1; }
192             is($mtime, 500000001, 'mtime');
193         } 
194         else {
195             fail("atime");
196             fail("mtime");
197         }
198     }
199 }
200
201 is(unlink('b'), 1, "unlink b");
202
203 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
204     $blksize,$blocks) = stat('b');
205 is($ino, undef, "ino of unlinked file b should be undef");
206 unlink 'c';
207
208 chdir $wd || die "Can't cd back to $wd";
209
210 # Yet another way to look for links (perhaps those that cannot be
211 # created by perl?).  Hopefully there is an ls utility in your
212 # %PATH%. N.B. that $^O is 'cygwin' on Cygwin.
213
214 SKIP: {
215     skip "Win32/Netware specific test", 2
216       unless ($^O eq 'MSWin32') || ($^O eq 'NetWare');
217     skip "No symbolic links found to test with", 2 
218       unless  `ls -l perl 2>nul` =~ /^l.*->/;
219
220     system("cp TEST TEST$$");
221     # we have to copy because e.g. GNU grep gets huffy if we have
222     # a symlink forest to another disk (it complains about too many
223     # levels of symbolic links, even if we have only two)
224     is(symlink("TEST$$","c"), 1, "symlink");
225     $foo = `grep perl c 2>&1`;
226     ok($foo, "found perl in c");
227     unlink 'c';
228     unlink("TEST$$");
229 }
230
231 unlink "Iofs.tmp";
232 open IOFSCOM, ">Iofs.tmp" or die "Could not write IOfs.tmp: $!";
233 print IOFSCOM 'helloworld';
234 close(IOFSCOM);
235
236 # TODO: pp_truncate needs to be taught about F_CHSIZE and F_FREESP,
237 # as per UNIX FAQ.
238
239 SKIP: {
240     eval { truncate "Iofs.tmp", 5; };
241
242     skip("no truncate - $@", 6) if $@;
243
244     is(-s "Iofs.tmp", 5, "truncation to five bytes");
245
246     truncate "Iofs.tmp", 0;
247
248     ok(-z "Iofs.tmp",    "truncation to zero bytes");
249
250     open(FH, ">Iofs.tmp") or die "Can't create Iofs.tmp";
251
252     binmode FH;
253     select FH;
254     $| = 1;
255     select STDOUT;
256
257     {
258         use strict;
259         print FH "x\n" x 200;
260         ok(truncate(FH, 200), "fh resize to 200");
261     }
262
263     if ($needs_fh_reopen) {
264         close (FH); open (FH, ">>Iofs.tmp") or die "Can't reopen Iofs.tmp";
265     }
266
267     is(-s "Iofs.tmp", 200, "fh resize to 200 working");
268
269     ok(truncate(FH, 0), "fh resize to zero");
270
271     if ($needs_fh_reopen) {
272         close (FH); open (FH, ">>Iofs.tmp") or die "Can't reopen Iofs.tmp";
273     }
274
275     ok(-z "Iofs.tmp", "fh resize to zero working");
276
277     close FH;
278 }
279
280 # check if rename() can be used to just change case of filename
281 SKIP: {
282     skip "Works in Cygwin only if check_case is set to relaxed", 1
283       if $^O eq 'cygwin';
284
285     chdir './tmp';
286     open(fh,'>x') || die "Can't create x";
287     close(fh);
288     rename('x', 'X');
289     
290     # this works on win32 only, because fs isn't casesensitive
291     ok(-e 'X', "rename working");
292
293     1 while unlink 'X';
294     chdir $wd || die "Can't cd back to $wd";
295 }
296
297 # check if rename() works on directories
298 if ($^O eq 'VMS') {
299     # must have delete access to rename a directory
300     `set file tmp.dir/protection=o:d`;
301     ok(rename('tmp.dir', 'tmp1.dir'), "rename on directories") ||
302       print "# errno: $!\n";
303 } else {
304     ok(rename('tmp', 'tmp1'), "rename on directories");
305 }
306
307 ok(-d 'tmp1', "rename on directories working");
308
309 # need to remove 'tmp' if rename() in test 28 failed!
310 END { rmdir 'tmp1'; rmdir 'tmp'; unlink "Iofs.tmp"; }