| 1 | package MYDan::Util::File; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | MYDan::Util::File | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | =cut | ||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | use Carp; | ||
| 14 | use Tie::File; | ||
| 15 | use File::Temp; | ||
| 16 | use FindBin qw( $Script ); | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | my $file = MYDan::Util::File->new( '/etc/passwd' ); | ||
| 21 | |||
| 22 | $file->munge( regex => qr/^foo\b/, length => 1, lazy => 1 )->commit(); | ||
| 23 | |||
| 24 | =cut | ||
| 25 | sub new | ||
| 26 | { | ||
| 27 | my ( $class, $path, %self ) = splice @_; | ||
| 28 | |||
| 29 | local $/ = $self{RS} ||= "\n"; | ||
| 30 | |||
| 31 | confess "failed to open temp" unless my $temp = $self{temp} | ||
| 32 | = File::Temp->new( UNLINK => 0, SUFFIX => ".$Script" ); | ||
| 33 | |||
| 34 | confess "failed to cp $path $temp" if system "cp $path $temp"; | ||
| 35 | confess "failed to open file" unless tie my @list, 'Tie::File', $temp; | ||
| 36 | |||
| 37 | @self{ qw( mode uid gid ) } = ( stat $path )[2,4,5]; | ||
| 38 | @self{ qw( curr path list ) } = ( 0, $path, \@list ); | ||
| 39 | |||
| 40 | bless \%self, ref $class || $class; | ||
| 41 | } | ||
| 42 | |||
| 43 | =head1 METHODS | ||
| 44 | |||
| 45 | =head3 munge( %param ) | ||
| 46 | |||
| 47 | seek: 'set' or 'end' | ||
| 48 | offset: number of lines from seek position | ||
| 49 | length: number of lines to remove | ||
| 50 | regex: a regular expression | ||
| 51 | lazy: default to end of file if regex match failed | ||
| 52 | line: a set of lines | ||
| 53 | |||
| 54 | =cut | ||
| 55 | sub munge | ||
| 56 | { | ||
| 57 | my ( $self, %param ) = splice @_; | ||
| 58 | my $list = $self->{list}; | ||
| 59 | my $match = $param{lazy}; | ||
| 60 | my $line = $param{line} || []; | ||
| 61 | my $seek = lc( $param{seek} || 'curr' ); | ||
| 62 | my $length = $param{length} || 0; | ||
| 63 | my $curr = ( $param{offset} || 0 ) + | ||
| 64 | ( $seek eq 'set' ? 0 : $seek eq 'end' ? @$list : $self->{curr} ); | ||
| 65 | |||
| 66 | if ( my $regex = $param{regex} ) | ||
| 67 | { | ||
| 68 | $regex = eval $regex unless ref $regex; | ||
| 69 | confess 'invalid regex definition' if ref $regex ne 'Regexp'; | ||
| 70 | |||
| 71 | while ( $curr < @$list ) | ||
| 72 | { | ||
| 73 | last if $list->[$curr] =~ $regex && ( $match = 1 ); | ||
| 74 | $curr ++; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | else { $match = 1 } | ||
| 78 | |||
| 79 | if ( $match ) | ||
| 80 | { | ||
| 81 | my @list = splice @$list, $curr, $length, ref $line ? @$line : $line; | ||
| 82 | $self->{curr} = @list && $curr ? $curr - @list : $curr; | ||
| 83 | } | ||
| 84 | |||
| 85 | return $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | =head3 commit( %param ) | ||
| 89 | |||
| 90 | path: alternative path to commit | ||
| 91 | backup: back up original file | ||
| 92 | |||
| 93 | =cut | ||
| 94 | sub commit | ||
| 95 | { | ||
| 96 | my ( $self, %param ) = splice @_; | ||
| 97 | my $path = $param{path} || $self->{path}; $path = readlink if -l $path; | ||
| 98 | my $temp = $self->{temp}; | ||
| 99 | my $error = "$temp -> $path: failed to"; | ||
| 100 | |||
| 101 | confess "$error chown" unless chown @$self{ qw( uid gid ) }, $temp; | ||
| 102 | confess "$error chmod" unless chmod $self->{mode}, $temp; | ||
| 103 | |||
| 104 | system "cp $path $path.bk" if $param{backup} && -e $path; | ||
| 105 | confess "$error mv" if system "mv $temp $path"; | ||
| 106 | return $self; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub DESTROY | ||
| 110 | { | ||
| 111 | my $self = shift; | ||
| 112 | untie @{ $self->{list} }; | ||
| 113 | unlink $self->{temp} if -f $self->{temp}; | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; |