8 use File::Path qw(rmtree mkpath make_path remove_tree);
9 use File::Spec::Functions;
12 my $prereq = prereq();
13 plan skip_all => $prereq if defined $prereq;
18 my ( $max_uid, $max_user ) = @{ $pwent };
19 my ( $max_gid, $max_group ) = @{ $grent };
21 my $tmp_base = catdir(
23 sprintf( 'test-%x-%x-%x', time, $$, rand(99999) ),
28 catdir($tmp_base, qw(a b)),
29 catdir($tmp_base, qw(a c)),
30 catdir($tmp_base, qw(z b)),
31 catdir($tmp_base, qw(z c)),
35 my @created = mkpath([@dir]);
40 my $dir_stem = $dir = catdir($tmp_base, 'owned-by');
42 $dir = catdir($dir_stem, 'aaa');
43 @created = make_path($dir, {owner => $max_user});
44 is(scalar(@created), 2, "created a directory owned by $max_user...");
46 my $dir_uid = (stat $created[0])[4];
47 is($dir_uid, $max_uid, "... owned by $max_uid");
49 $dir = catdir($dir_stem, 'aab');
50 @created = make_path($dir, {group => $max_group});
51 is(scalar(@created), 1, "created a directory owned by group $max_group...");
53 my $dir_gid = (stat $created[0])[5];
54 is($dir_gid, $max_gid, "... owned by group $max_gid");
56 $dir = catdir($dir_stem, 'aac');
57 @created = make_path( $dir, { user => $max_user,
58 group => $max_group});
59 is(scalar(@created), 1, "created a directory owned by $max_user:$max_group...");
61 ($dir_uid, $dir_gid) = (stat $created[0])[4,5];
62 is($dir_uid, $max_uid, "... owned by $max_uid");
63 is($dir_gid, $max_gid, "... owned by group $max_gid");
66 # invent a user and group that don't exist
67 my $phony_user = get_phony_user();
68 my $phony_group = get_phony_group();
70 $dir = catdir($dir_stem, 'aad');
71 my $rv = _run_for_warning( sub {
74 { user => $phony_user, group => $phony_group }
78 qr{unable to map $phony_user to a uid, ownership not changed:}s,
79 "created a directory not owned by $phony_user:$phony_group...",
82 qr{unable to map $phony_group to a gid, group ownership not changed:}s,
83 "created a directory not owned by $phony_user:$phony_group...",
90 my $opts = { error => \$x };
91 remove_tree($tmp_base, $opts);
92 ok(! -d $tmp_base, "directory '$tmp_base' removed, as expected");
93 is(scalar(@{$x}), 0, "no error messages using remove_tree() with \$opts");
97 # find the highest uid ('nobody' or similar)
100 while (my @u = getpwent()) {
101 if ($max_uid < $u[2]) {
106 setpwent(); # in case we want to run again later
107 return [ $max_uid, $max_user ];
111 # find the highest gid ('nogroup' or similar)
113 my $max_group = undef;
114 while ( my @g = getgrent() ) {
116 if ($max_gid < $g[2]) {
121 setgrent(); # in case we want to run again later
122 return [ $max_gid, $max_group ];
126 return "getpwent() not implemented on $^O" unless $Config{d_getpwent};
127 return "getgrent() not implemented on $^O" unless $Config{d_getgrent};
128 return "not running as root" unless $< == 0;
129 return "darwin's nobody and nogroup are -1 or -2" if $^O eq 'darwin';
133 my ( $max_uid, $max_user ) = @{ $pwent };
134 my ( $max_gid, $max_group ) = @{ $grent };
136 return "getpwent() appears to be insane" unless $max_uid > 0;
137 return "getgrent() appears to be insane" unless $max_gid > 0;
142 return "getpwent() not implemented on $^O" unless $Config{d_getpwent};
143 return "not running as root" unless $< == 0;
145 while(my @a=getpwent()) {
146 $real_users{$a[0]}++;
148 my $phony_stem = 'phonyuser';
150 do { $phony = $phony_stem . int(rand(10000)); } until (! $real_users{$phony});
154 sub get_phony_group {
155 return "getgrent() not implemented on $^O" unless $Config{d_getgrent};
156 return "not running as root" unless $< == 0;
157 my %real_groups = ();
158 while(my @a=getgrent()) {
159 $real_groups{$a[0]}++;
161 my $phony_stem = 'phonygroup';
163 do { $phony = $phony_stem . int(rand(10000)); } until (! $real_groups{$phony});