6 use File::Path qw(rmtree mkpath make_path remove_tree);
7 use File::Spec::Functions;
10 my $prereq = prereq();
11 plan skip_all => $prereq if defined $prereq;
16 my ( $max_uid, $max_user ) = @{ $pwent };
17 my ( $max_gid, $max_group ) = @{ $grent };
19 my $tmp_base = catdir(
21 sprintf( 'test-%x-%x-%x', time, $$, rand(99999) ),
26 catdir($tmp_base, qw(a b)),
27 catdir($tmp_base, qw(a c)),
28 catdir($tmp_base, qw(z b)),
29 catdir($tmp_base, qw(z c)),
33 my @created = mkpath([@dir]);
38 my $dir_stem = $dir = catdir($tmp_base, 'owned-by');
40 $dir = catdir($dir_stem, 'aaa');
41 @created = make_path($dir, {owner => $max_user});
42 is(scalar(@created), 2, "created a directory owned by $max_user...");
44 my $dir_uid = (stat $created[0])[4];
45 is($dir_uid, $max_uid, "... owned by $max_uid");
47 $dir = catdir($dir_stem, 'aab');
48 @created = make_path($dir, {group => $max_group});
49 is(scalar(@created), 1, "created a directory owned by group $max_group...");
51 my $dir_gid = (stat $created[0])[5];
52 is($dir_gid, $max_gid, "... owned by group $max_gid");
54 $dir = catdir($dir_stem, 'aac');
55 @created = make_path( $dir, { user => $max_user,
56 group => $max_group});
57 is(scalar(@created), 1, "created a directory owned by $max_user:$max_group...");
59 ($dir_uid, $dir_gid) = (stat $created[0])[4,5];
60 is($dir_uid, $max_uid, "... owned by $max_uid");
61 is($dir_gid, $max_gid, "... owned by group $max_gid");
64 skip('Skip until RT 85878 is fixed', 1);
65 # invent a user and group that don't exist
66 do { ++$max_user } while ( getpwnam( $max_user ) );
67 do { ++$max_group } while ( getgrnam( $max_group ) );
69 $dir = catdir($dir_stem, 'aad');
70 my $rv = _run_for_warning( sub { make_path( $dir,
72 group => $max_group } ) } );
74 qr{\Aunable to map $max_user to a uid, ownership not changed: .* at \S+ line \d+
75 unable to map $max_group to a gid, group ownership not changed: .* at \S+ line \d+\b},
76 "created a directory not owned by $max_user:$max_group..."
81 # find the highest uid ('nobody' or similar)
84 while (my @u = getpwent()) {
85 if ($max_uid < $u[2]) {
90 setpwent(); # in case we want to run again later
91 return [ $max_uid, $max_user ];
95 # find the highest gid ('nogroup' or similar)
97 my $max_group = undef;
98 while ( my @g = getgrent() ) {
100 if ($max_gid < $g[2]) {
105 setgrent(); # in case we want to run again later
106 return [ $max_gid, $max_group ];
110 return "getpwent() not implemented on $^O" unless $Config{d_getpwent};
111 return "getgrent() not implemented on $^O" unless $Config{d_getgrent};
112 return "not running as root" unless $< == 0;
113 return "darwin's nobody and nogroup are -1 or -2" if $^O eq 'darwin';
117 my ( $max_uid, $max_user ) = @{ $pwent };
118 my ( $max_gid, $max_group ) = @{ $grent };
120 return "getpwent() appears to be insane" unless $max_uid > 0;
121 return "getgrent() appears to be insane" unless $max_gid > 0;