Config-Generator
view release on metacpan or search on metacpan
lib/Config/Generator/File.pm view on Meta::CPAN
@stat = stat($path);
if (@stat) {
_chmod($stat[ST_MODE] & oct(7777), $tmpath);
_chown($stat[ST_UID], $stat[ST_GID], $tmpath);
_unlink($path);
}
rename($tmpath, $path)
or dief("cannot rename(%s, %s): %s", $tmpath, $path, $!);
}
#
# file contents helper
#
sub _ensure_contents ($$) {
my($path, $contents) = @_;
my($what);
log_debug("checking file %s", $path);
$what = sprintf("file %s", $path);
if (-e $path) {
dief("path exists already and is not a file: %s", $path)
unless -f _;
if (file_read($path) eq $contents) {
_printf2("checked %s\n", $what);
} else {
proc_run(
command => [ "diff", "-u", $path, "-" ],
stdin => \$contents,
) if $Verbosity;
if ($NoAction) {
_printf1("would have updated %s\n", $what);
} else {
_atomic_write($path, \$contents);
_printf1("updated %s\n", $what);
}
}
} else {
if ($NoAction) {
_printf1("would have created %s\n", $what);
} else {
_ensure_parent($path);
_atomic_write($path, \$contents);
_printf1("created %s\n", $what);
}
}
}
#
# fatal helpers
#
sub _chmod ($$) {
my($mode, $path) = @_;
chmod($mode, $path)
or dief("cannot chmod(%04o, %s): %s", $mode, $path, $!);
}
sub _chown ($$$) {
my($uid, $gid, $path) = @_;
chown($uid, $gid, $path)
or dief("cannot chown(%d, %d, %s): %s", $uid, $gid, $path, $!);
}
sub _symlink ($$) {
my($target, $path) = @_;
symlink($target, $path)
or dief("cannot symlink(%s, %s): %s", $target, $path, $!);
}
sub _unlink ($) {
my($path) = @_;
unlink($path)
or dief("cannot unlink(%s): %s", $path, $!);
}
#
# ensure directory existence
#
my @ensure_directory_options = (
{ type => SCALAR, regex => qr/^\// },
);
sub ensure_directory ($) {
my($path) = validate_pos(@_, @ensure_directory_options);
my($rpath, $what);
$rpath = $RootDir . $path;
log_debug("checking directory %s", $rpath);
dief("duplicate directory: %s", $path) if $_Registered{$path};
$_Registered{$path} = { type => "directory" };
$what = sprintf("directory %s", $rpath);
if (-d $rpath) {
_printf2("checked %s\n", $what);
} else {
dief("path exists already and is not a directory: %s", $rpath)
if -e _;
if ($NoAction) {
_printf1("would have created %s\n", $what);
} else {
_ensure_parent($rpath);
dir_ensure($rpath);
_printf1("created %s\n", $what);
}
}
}
#
# ensure file contents
#
my @ensure_file_options = (
$ensure_directory_options[0],
{ type => SCALAR },
);
sub ensure_file ($$) {
my($path, $contents) = validate_pos(@_, @ensure_file_options);
lib/Config/Generator/File.pm view on Meta::CPAN
#
# ensure file user
#
my @ensure_user_options = (
$ensure_directory_options[0],
{ type => SCALAR },
);
sub ensure_user ($$) {
my($path, $user) = validate_pos(@_, @ensure_user_options);
my($rpath, $what, $uid, @stat);
$rpath = $RootDir . $path;
log_debug("checking user %s", $rpath);
dief("not a registered file or directory: %s", $path)
unless $_Registered{$path}
and $_Registered{$path}{type} =~ /^(file|directory)$/;
dief("duplicate user: %s", $path) if $_Registered{$path}{user};
$_Registered{$path}{user} = $user;
$what = sprintf("the user of %s: %s", $rpath, $_Registered{$path}{user});
if ($>) {
# we do not change the user if we are not root
# (and we stop immediately since the user may not even exist)
_printf2("would have checked %s (but we are not root)\n", $what);
return;
}
$uid = getpwnam($user);
dief("unknown user: %s", $user) unless defined($uid);
@stat = stat($rpath);
unless (@stat) {
dief("cannot stat(%s): %s", $rpath, $!)
unless $NoAction and $! == ENOENT;
_printf2("would have checked %s (but it does no exist yet)\n", $what);
return;
}
if ($stat[ST_UID] == $uid) {
_printf2("checked %s\n", $what);
} else {
if ($NoAction) {
_printf1("would have changed %s\n", $what);
} else {
_chown($uid, $stat[ST_GID], $rpath);
_printf1("changed %s\n", $what);
}
}
}
#
# ensure file group
#
my @ensure_group_options = (
$ensure_directory_options[0],
{ type => SCALAR },
);
sub ensure_group ($$) {
my($path, $group) = validate_pos(@_, @ensure_group_options);
my($rpath, $what, $gid, @stat);
$rpath = $RootDir . $path;
log_debug("checking group %s", $rpath);
dief("not a registered file or directory: %s", $path)
unless $_Registered{$path}
and $_Registered{$path}{type} =~ /^(file|directory)$/;
dief("duplicate group: %s", $path) if $_Registered{$path}{group};
$_Registered{$path}{group} = $group;
$what = sprintf("the group of %s: %s", $rpath, $_Registered{$path}{group});
if ($>) {
# we do not change the group if we are not root
# (and we stop immediately since the group may not even exist)
_printf2("would have checked %s (but we are not root)\n", $what);
return;
}
$gid = getgrnam($group);
dief("unknown group: %s", $group) unless defined($gid);
@stat = stat($rpath);
unless (@stat) {
dief("cannot stat(%s): %s", $rpath, $!)
unless $NoAction and $! == ENOENT;
_printf2("would have checked %s (but it does no exist yet)\n", $what);
return;
}
if ($stat[ST_GID] == $gid) {
_printf2("checked %s\n", $what);
} else {
if ($NoAction) {
_printf1("would have changed %s\n", $what);
} else {
_chown($stat[ST_UID], $gid, $rpath);
_printf1("changed %s\n", $what);
}
}
}
#
# return the list of registered files (paths only)
#
sub files_manifest () {
my(@lines);
@lines = sort(keys(%_Registered));
return(@lines) if wantarray();
return(join("", map("$_\n", @lines)));
}
#
# return the list of registered files (rpm spec %files format)
#
# note: this assumes an initial "%defattr(-,root,root,-)" line
#
sub files_spec () {
my(%defattr, @lines, @list, $mode, $user, $group);
%defattr = (
mode => "-",
user => "root",
group => "root",
);
foreach my $path (sort(keys(%_Registered))) {
@list = ();
$mode = $_Registered{$path}{mode} || $defattr{mode};
$user = $_Registered{$path}{user} || $defattr{user};
$group = $_Registered{$path}{group} || $defattr{group};
push(@list, "%attr($mode, $user, $group)")
if $mode ne $defattr{mode}
or $user ne $defattr{user}
or $group ne $defattr{group};
push(@list, "%dir") if $_Registered{$path}{type} eq "directory";
push(@list, $path);
push(@lines, "@list");
}
return(@lines) if wantarray();
return(join("", map("$_\n", @lines)));
}
#
# handle a manifest file
#
my @handle_manifest_options = (
{ type => SCALAR },
{ type => BOOLEAN },
);
sub handle_manifest ($$) {
my($manifest, $clean) = validate_pos(@_, @handle_manifest_options);
my($keep, %tokeep, %toclean, @list);
( run in 2.865 seconds using v1.01-cache-2.11-cpan-5735350b133 )