Config-Generator
view release on metacpan or search on metacpan
lib/Config/Generator/File.pm view on Meta::CPAN
my($path) = @_;
my($what);
lstat($path) or return;
if (-f _) {
$what = sprintf("file %s", $path);
if ($NoAction) {
_printf1("would have removed %s\n", $what);
} elsif (unlink($path)) {
_printf1("removed %s\n", $what);
} else {
warnf("did not remove %s: %s", $what, $!);
}
} elsif (-d _) {
$what = sprintf("directory %s", $path);
if ($NoAction) {
_printf1("would have removed %s\n", $what);
} elsif (rmdir($path)) {
_printf1("removed %s\n", $what);
} else {
warnf("did not remove %s: %s", $what, $!);
}
} else {
warnf("skipped alien file: %s", $path);
}
}
#
# directory parent helper
#
sub _ensure_parent ($) {
my($path) = @_;
my($parent);
$parent = $path = dir_parent($path);
return if -d $parent;
while (not -d $path) {
last if $_Registered{$path};
last if $path eq $RootDir;
$_Registered{$path} = { type => "parent" };
$path = dir_parent($path);
}
log_debug("making directory %s", $parent);
dir_ensure($parent);
}
#
# (almost) atomic file write helper
#
sub _atomic_write ($$) {
my($path, $data) = @_;
my(@stat, $tmpath);
$tmpath = $path . ".tmp";
_unlink($tmpath) if -e $tmpath;
file_write($tmpath, data => $data);
@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 = (
lib/Config/Generator/File.pm view on Meta::CPAN
} else {
if ($NoAction) {
_printf1("would have updated %s\n", $what);
} else {
_unlink($rpath);
_symlink($target, $rpath);
_printf1("updated %s\n", $what);
}
}
} else {
if ($NoAction) {
_printf1("would have created %s\n", $what);
} else {
_ensure_parent($rpath);
_symlink($target, $rpath);
_printf1("created %s\n", $what);
}
}
}
#
# ensure file mode
#
my @ensure_mode_options = (
$ensure_directory_options[0],
{ type => SCALAR, regex => qr/^\d+$/ },
);
sub ensure_mode ($$) {
my($path, $mode) = validate_pos(@_, @ensure_mode_options);
my($rpath, $what, @stat);
$rpath = $RootDir . $path;
log_debug("checking mode %s", $rpath);
dief("not a registered file or directory: %s", $path)
unless $_Registered{$path}
and $_Registered{$path}{type} =~ /^(file|directory)$/;
dief("duplicate mode: %s", $path) if $_Registered{$path}{mode};
$_Registered{$path}{mode} = sprintf("%04o", $mode);
dief("invalid mode: %s", $_Registered{$path}{mode})
unless 0 < $mode and $mode <= oct(7777);
$what = sprintf("the mode of %s: %s", $rpath, $_Registered{$path}{mode});
@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_MODE] & oct(7777)) == $mode) {
_printf2("checked %s\n", $what);
} elsif ($> and $_Registered{$path}{type} eq "directory") {
# we should not change a directory mode if we are not root
# (we could shoot ourself in the foot with a read-only directory)
_printf2("would have changed %s (but we are not root)\n", $what);
} else {
if ($NoAction) {
_printf1("would have changed %s\n", $what);
} else {
_chmod($mode, $rpath);
_printf1("changed %s\n", $what);
}
}
}
#
# 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 },
( run in 1.608 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )