source: trunk/essentials/dev-lang/perl/t/io/fs.t@ 3188

Last change on this file since 3188 was 3188, checked in by bird, 19 years ago

Applied patch from Paul (aka Creeping).

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