File Coverage

blib/lib/Armadito/Agent/Tools/File.pm
Criterion Covered Total %
statement 27 45 60.0
branch 0 6 0.0
condition n/a
subroutine 9 12 75.0
pod 3 3 100.0
total 39 66 59.0


line stmt bran cond sub pod time code
1             package Armadito::Agent::Tools::File;
2              
3 23     23   2262868 use strict;
  23         30  
  23         574  
4 23     23   75 use warnings;
  23         33  
  23         574  
5 23     23   71 use base 'Exporter';
  23         67  
  23         1492  
6              
7 23     23   99 use UNIVERSAL::require();
  23         28  
  23         295  
8 23     23   869 use Encode;
  23         12197  
  23         1360  
9 23     23   84 use English qw(-no_match_vars);
  23         28  
  23         123  
10 23     23   20204 use Memoize;
  23         42149  
  23         1181  
11 23     23   11282 use File::Which;
  23         16316  
  23         1079  
12 23     23   8015 use Armadito::Agent::Tools qw (getNoWhere);
  23         41  
  23         6637  
13              
14             our @EXPORT_OK = qw(
15             readFile
16             writeFile
17             canRun
18             rmFile
19             );
20              
21             if ( $OSNAME ne 'MSWin32' ) {
22             memoize('canRun');
23             }
24              
25             sub rmFile {
26 0     0 1   my (%params) = @_;
27              
28 0 0         if ( -f $params{filepath} ) {
29 0           unlink( $params{filepath} );
30             }
31             }
32              
33             sub readFile {
34 0     0 1   my (%params) = @_;
35 0           my $fh;
36              
37 0 0         if ( !open( $fh, "<", $params{filepath} ) ) {
38 0           die "Error io $params{filepath} : $!";
39             }
40              
41 0           return do { local $/; <$fh> };
  0            
  0            
42             }
43              
44             sub writeFile {
45 0     0 1   my (%params) = @_;
46 0           my $fh;
47              
48 0 0         if ( !open( $fh, $params{mode}, $params{filepath} ) ) {
49 0           die "Error io $params{filepath} : $!";
50             }
51              
52 0           binmode $fh;
53 0           print $fh $params{content};
54 0           close $fh;
55 0           return 1;
56             }
57              
58             sub canRun {
59             my ($binary) = @_;
60              
61             return $binary =~ m{^/} ? -x $binary : # full path
62             scalar( which($binary) ); # executable name
63             }
64              
65             1;
66             __END__
67              
68             =head1 NAME
69              
70             Armadito::Agent::Tools::File - Basic I/O functions used in Armadito Agent.
71              
72             =head1 DESCRIPTION
73              
74             This module provides some high level I/O functions for easy use.
75              
76             =head1 FUNCTIONS
77              
78             =head2 readFile(%params)
79              
80             Read file and return its content in a scalar.
81              
82             =over
83              
84             =item I<filepath>
85              
86             Path of the file to be read.
87              
88             =back
89              
90             =head2 writeFile(%params)
91              
92             Write a scalar to a file in binary mode.
93              
94             =over
95              
96             =item I<content>
97              
98             Content to write (scalar).
99              
100             =item I<filepath>
101              
102             Path of the file where content will be written to.
103              
104             =item I<mode>
105              
106             File opening mode.
107              
108             =back
109              
110             =head2 canRun($binary)
111              
112             Returns true if given binary can be executed.
113              
114             =head2 rmFile(%params)
115              
116             Remove a file if it exists.
117              
118             =over
119              
120             =item I<filepath>
121              
122             Path of the file to remove.
123