Config-Generator

 view release on metacpan or  search on metacpan

lib/Config/Generator/File.pm  view on Meta::CPAN

sub _printf2 (@) { printf(@_) if $Verbosity >= 2 }

#
# path absence helper
#

sub _ensure_absent ($) {
    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 _) {

lib/Config/Generator/File.pm  view on Meta::CPAN

# (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, $!);
}

#

lib/Config/Generator/File.pm  view on Meta::CPAN


sub ensure_symlink ($$) {
    my($path, $target) = validate_pos(@_, @ensure_symlink_options);
    my($rpath, $what, @stat, $actual);

    $rpath = $RootDir . $path;
    log_debug("checking symlink %s", $rpath);
    dief("duplicate symlink: %s", $path) if $_Registered{$path};
    $_Registered{$path} = { type => "symlink" };
    $what = sprintf("symlink %s -> %s", $rpath, $target);
    @stat = lstat($rpath);
    if (-e _) {
        dief("path exists already and is not a symlink: %s", $rpath)
            unless -l _;
        $actual = readlink($rpath);
        dief("cannot readlink(%s): %s", $rpath, $!)
            unless defined($actual);
        if ($target eq $actual) {
            _printf2("checked %s\n", $what);
        } else {
            if ($NoAction) {

lib/Config/Generator/File.pm  view on Meta::CPAN

    $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);

lib/Config/Generator/File.pm  view on Meta::CPAN

    $_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 {

lib/Config/Generator/File.pm  view on Meta::CPAN

    $_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 {



( run in 0.525 second using v1.01-cache-2.11-cpan-49f99fa48dc )