line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# (c) Jan Gehring |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
###### DEPRECATED |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package Rex::Sudo::File; |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
14
|
use v5.12.5; |
|
1
|
|
|
|
|
5
|
|
10
|
1
|
|
|
1
|
|
17
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
43
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '1.14.3'; # VERSION |
13
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
|
9
|
use Rex; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
20
|
|
15
|
1
|
|
|
1
|
|
7
|
use Rex::Commands; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
8
|
|
16
|
1
|
|
|
1
|
|
10
|
use Rex::Helper::Run; |
|
1
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
61
|
|
17
|
1
|
|
|
1
|
|
7
|
use Rex::Commands::Fs; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
8
|
|
18
|
1
|
|
|
1
|
|
12
|
use Rex::Helper::Path; |
|
1
|
|
|
|
|
18
|
|
|
1
|
|
|
|
|
73
|
|
19
|
1
|
|
|
1
|
|
12
|
use IO::File; |
|
1
|
|
|
|
|
1455
|
|
|
1
|
|
|
|
|
722
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub open { |
22
|
0
|
|
|
0
|
0
|
|
my $that = shift; |
23
|
0
|
|
0
|
|
|
|
my $proto = ref($that) || $that; |
24
|
0
|
|
|
|
|
|
my $self = {}; |
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
|
$self->{mode} = shift; |
27
|
0
|
|
|
|
|
|
$self->{file} = shift; |
28
|
0
|
|
|
|
|
|
$self->{rndfile} = get_tmp_file; |
29
|
|
|
|
|
|
|
|
30
|
0
|
0
|
|
|
|
|
if ( my $sftp = Rex::get_sftp() ) { |
31
|
0
|
0
|
|
|
|
|
if ( $self->{mode} eq ">" ) { |
|
|
0
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$self->{fh} = |
33
|
0
|
|
|
|
|
|
$sftp->open( $self->{rndfile}, O_WRONLY | O_CREAT | O_TRUNC ); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
elsif ( $self->{mode} eq ">>" ) { |
36
|
0
|
|
|
|
|
|
cp( $self->{file}, $self->{rndfile} ); |
37
|
0
|
|
|
|
|
|
chmod( 666, $self->{rndfile} ); |
38
|
0
|
|
|
|
|
|
$self->{fh} = $sftp->open( $self->{rndfile}, O_WRONLY | O_APPEND ); |
39
|
0
|
|
|
|
|
|
my %stat = stat $self->{rndfile}; |
40
|
0
|
|
|
|
|
|
$self->{fh}->seek( $stat{size} ); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
else { |
43
|
0
|
|
|
|
|
|
cp( $self->{file}, $self->{rndfile} ); |
44
|
0
|
|
|
|
|
|
chmod( 666, $self->{rndfile} ); |
45
|
0
|
|
|
|
|
|
$self->{fh} = $sftp->open( $self->{rndfile}, O_RDONLY ); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
else { |
49
|
0
|
|
|
|
|
|
$self->{fh} = IO::File->new; |
50
|
0
|
|
|
|
|
|
$self->{fh}->open( $self->{mode} . " " . $self->{rndfile} ); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
bless( $self, $proto ); |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
return $self; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub write { |
59
|
0
|
|
|
0
|
0
|
|
my ( $self, $content ) = @_; |
60
|
|
|
|
|
|
|
|
61
|
0
|
0
|
|
|
|
|
if ( ref( $self->{fh} ) eq "Net::SSH2::File" ) { |
62
|
0
|
|
|
|
|
|
$self->{fh}->write($content); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
else { |
65
|
0
|
|
|
|
|
|
$self->{fh}->print($content); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub seek { |
70
|
0
|
|
|
0
|
0
|
|
my ( $self, $offset ) = @_; |
71
|
|
|
|
|
|
|
|
72
|
0
|
0
|
|
|
|
|
if ( ref( $self->{fh} ) eq "Net::SSH2::File" ) { |
73
|
0
|
|
|
|
|
|
$self->{fh}->seek($offset); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
else { |
76
|
0
|
|
|
|
|
|
$self->{fh}->seek( $offset, 0 ); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub read { |
81
|
0
|
|
|
0
|
0
|
|
my ( $self, $len ) = @_; |
82
|
0
|
|
0
|
|
|
|
$len ||= 64; |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
my $buf; |
85
|
0
|
|
|
|
|
|
$self->{fh}->read( $buf, $len ); |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
return $buf; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub close { |
91
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
92
|
|
|
|
|
|
|
|
93
|
0
|
0
|
|
|
|
|
return unless $self->{fh}; |
94
|
|
|
|
|
|
|
|
95
|
0
|
0
|
|
|
|
|
if ( ref( $self->{fh} ) eq "Net::SSH2::File" ) { |
96
|
0
|
|
|
|
|
|
$self->{fh} = undef; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
else { |
99
|
0
|
|
|
|
|
|
$self->{fh}->close; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# use cat to not overwrite attributes/owner/group |
103
|
0
|
0
|
0
|
|
|
|
if ( $self->{mode} eq ">" || $self->{mode} eq ">>" ) { |
104
|
0
|
|
|
|
|
|
i_run "cat " . $self->{rndfile} . " >" . $self->{file}; |
105
|
0
|
|
|
|
|
|
rm( $self->{rndfile} ); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub DESTROY { |
110
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
111
|
0
|
|
|
|
|
|
$self->close; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
1; |