Debian Codegraph @

t/04_at.t

raw
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use sigtrap qw(die normal-signals error-signals);
6
7 use Fcntl qw(:DEFAULT :mode);
8 use File::Path 'rmtree';
9 use File::Spec;
10 use File::Temp 'mktemp';
11 use Scalar::Util qw(blessed looks_like_number);
12
13 use Test::More tests => 27;
14 use POSIX::2008 ':at';
15
16 my $HAVE_WEIRD_DIR_MODE = $^O =~ /^(?:MSWin32|cygwin)$/ || do {
17 if (open my $fh, '<', '/proc/version') {
18 <$fh> =~ /microsoft/i;
19 }
20 else {
21 0;
22 }
23 };
24
25 my $rv;
26 my $tmpname = mktemp('tmpXXXXX');
27 rmtree($tmpname);
28
29 SKIP: {
30 if (! defined &openat) {
31 skip 'openat() UNAVAILABLE', 27;
32 }
33
34 umask 0;
35 opendir my $dot, File::Spec->curdir() or die "Could not opendir(.): $!";
36
37 $rv = openat(undef, $tmpname, O_RDWR|O_CREAT|O_TRUNC);
38 is($rv, undef, 'openat with undef fd');
39
40 $rv = eval { openat('xyz', $tmpname, O_RDWR|O_CREAT|O_TRUNC) };
41 is($rv, undef, 'openat with invalid fd');
42
43 # Redirect STDERR to /dev/null temporarily so that the "embedded null
44 # character" warning from openat() doesn't clutter up the test output.
45 my $olderr;
46 my $restore_stderr = 0;
47 if (! open $olderr, '>&', *STDERR) {
48 warn "Can't save STDERR";
49 }
50 elsif (! open STDERR, '>', File::Spec->devnull()) {
51 warn "Can't redirect STDERR to devnull";
52 }
53 else {
54 $restore_stderr = 1;
55 }
56 $rv = openat($dot, "\0", O_RDWR|O_CREAT|O_TRUNC);
57 open STDERR, '>&', $olderr if $restore_stderr;
58 is($rv, undef, 'openat with invalid path');
59
60 # Use AT_FDCWD() as a function instead of a bareword
61 # to avoid compile errors if it's not present.
62 $rv = openat(AT_FDCWD(), $tmpname, O_RDWR|O_CREAT|O_TRUNC);
63 ok(looks_like_number($rv),
64 "openat(AT_FDCWD=${\AT_FDCWD()}, $tmpname) returns file descriptor");
65 ok(-e $tmpname, "$tmpname exists");
66
67 $rv = openat(\AT_FDCWD(), $tmpname, O_RDWR|O_CREAT|O_TRUNC);
68 like(blessed($rv), qr/^IO::File/,
69 "openat(\\AT_FDCWD=\\${\AT_FDCWD()}, $tmpname) returns file handle");
70
71 # Perl < 5.22 doesn't support fileno for directory handles
72 if (defined(my $fndot = fileno $dot)) {
73 $rv = openat($fndot, $tmpname, O_RDWR|O_CREAT|O_TRUNC);
74 ok(looks_like_number($rv), "openat(fd=$fndot, $tmpname) returns file descriptor");
75 }
76 else {
77 pass("This Perl doesn't support fileno for directory handles");
78 }
79
80 $rv = openat($dot, $tmpname, O_RDWR|O_CREAT|O_TRUNC);
81 like(blessed($rv), qr/^IO::File/, "openat(fh, $tmpname) returns file handle");
82 ok(-f $rv, 'handle references a regular file');
83 ok(unlinkat($dot, $tmpname), "unlinkat(fh, $tmpname)");
84 ok(! -e $tmpname, "$tmpname is gone");
85
86 ok(symlinkat('abc', $dot, $tmpname), "symlinkat: $tmpname \-> abc");
87 is(readlinkat($dot, $tmpname), 'abc', 'readlinkat');
88 ok(unlinkat($dot, $tmpname), "unlinkat: $tmpname (symlink)");
89
90 ok(mkdirat($dot, $tmpname, 0700), "mkdirat: $tmpname");
91
92 my @stat = fstatat($dot, $tmpname);
93 ok(scalar(@stat), 'fstatat ok after mkdirat');
94 ok(S_ISDIR($stat[2]), 'fstatat S_ISDIR after mkdirat');
95 cmp_ok(
96 ($HAVE_WEIRD_DIR_MODE ? 0700 : $stat[2] & 0777), '==', 0700,
97 'fstatat mode after mkdirat'
98 );
99
100 ok(fchmodat($dot, $tmpname, 0755), 'fchmodat');
101
102 @stat = fstatat($dot, $tmpname);
103 ok(scalar(@stat), 'fstatat ok after fchmodat');
104 ok(S_ISDIR($stat[2]), 'fstatat S_ISDIR after fchmodat');
105 cmp_ok(
106 ($HAVE_WEIRD_DIR_MODE ? 0755 : $stat[2] & 0777), '==', 0755,
107 'fstatat mode after fchmodat'
108 );
109
110 # Don't use O_DIRECTORY here because it's not always available. We don't need
111 # it anyway because we know we've just created a directory.
112 $rv = openat($dot, $tmpname, O_RDONLY);
113 like(blessed($rv), qr/^IO::Dir/, 'openat returns directory handle');
114
115 ok(openat($rv, $tmpname, O_RDWR|O_CREAT|O_TRUNC), 'openat() in subdir');
116 ok(readdir $rv, 'readdir() on handle from openat()');
117 ok(unlinkat($rv, $tmpname), 'unlinkat() in subdir');
118
119 ok(unlinkat($dot, $tmpname, AT_REMOVEDIR()), "unlinkat: directory $tmpname");
120 }
121
122 END {
123 rmtree($tmpname);
124 }