App-Slaughter

 view release on metacpan or  search on metacpan

lib/Slaughter/API/generic.pm  view on Meta::CPAN

        }

    }
    else
    {
        $::verbose &&
          print "\tUsing contents literally; no template expansion\n";
    }


    #
    #  OK now we want to write out the content to a temporary location.
    #
    my ( $handle, $name ) = File::Temp::tempfile();
    open my $fh, ">", $name or
      return;
    print $fh $content;
    close($fh);


    #
    #  We have the file, does it differ from the live filesystem?
    #  Or is the local copy missing?
    #
    #  If so we'll move the new file into place.
    #
    my $replace = 0;

    if ( !-e $dst )
    {
        $::verbose && print "\tDestination not already present.\n";
        $replace = 1;
    }
    else
    {
        my $cur = Slaughter::Private::checksumFile($dst);
        my $new = Slaughter::Private::checksumFile($name);

        if ( $new ne $cur )
        {
            $replace = 1;

            $::verbose && print "\tContents don't match - will replace\n";
        }
        else
        {
            $::verbose &&
              print "\tCurrent file equals new one - not replacing\n";
        }
    }

    #
    #  Replace
    #
    if ($replace)
    {
        if ( -e $dst )
        {

            #
            #  If we've been given "Backup" then we backup, otherwise
            # we just remove the old file.
            #
            my $backup = $params{ 'Backup' } || "true";

            if ( $backup =~ /true/i )
            {
                $::verbose && print "\tMoving existing file out of the way.\n";
                RunCommand( Cmd => "mv $dst $dst.old" );
            }
            else
            {
                $::verbose &&
                  print "\tOverwriting existing file without creating backup\n";
            }
        }


        #
        #  Ensure the destination directory exists.
        #
        my $dir = dirname($dst);
        if ( !-d $dir )
        {
            mkpath( $dir, { verbose => 0 } );
        }


        $::verbose && print "\tReplacing $dst\n";
        RunCommand( Cmd => "mv $name $dst" );
    }

    #
    #  Change Owner/Group/Mode if we should
    #
    SetPermissions( File  => $dst,
                    Owner => $params{ 'Owner' },
                    Group => $params{ 'Group' },
                    Mode  => $params{ 'Mode' } );

    #
    #  If we didn't replace then we'll remove the temporary file
    # which would otherwise be orphaned.
    #
    if ( -e $name )
    {
        unlink($name);
    }

    return ($replace);
}




=head2 FileMatches

This allows you to test whether the contents of a given file match
either a literal line of text, or a regular expression.

=for example begin

  if ( FileMatches( File    => "/etc/sudoers",
                    Pattern => "steve" ) )
  {
     # OK "steve" is in sudoers.  Somewhere.
  }

=for example end

The following parameters are available:

=over



( run in 0.597 second using v1.01-cache-2.11-cpan-df04353d9ac )