lib/File/Access/Driver.pm

filter: Statementstmt 54.1% 255 / 471 Branchbran 32.6% 98 / 300 Conditioncond 38.4% 5 / 13 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 71.7% 33 / 46 Podpod 44.4% 16 / 36 totaltotal 46.7% 408 / 873 CC 157 SCAR 39.8
CC
157
Cov
46%
_openaFile
240.0
appendLine
240.0
changeFileName
132.0
CRAP 53.4
↑ Summary
1 #
2 # @author Bodo (Hugo) Barwich
3 # @version 2026-03-21
4 # @package File Access Driver
5 # @subpackage lib/File/Access/Driver.pm
6  
7 # This module defines the a class to interact with files
8 #
9 #---------------------------------
10 # Requirements:
11 #
12 #---------------------------------
13 # Extensions:
14 #
15 #---------------------------------
16 # Configurations:
17 #
18 #---------------------------------
19 # Features:
20  
21 #==============================================================================
22 # The File::Access::Driver Package
23  
24 =head1 NAME
25  
26 File::Access::Driver - Convenient File Access with "Batteries included"
27  
28 =cut
29  
30 package File::Access::Driver;
31  
32 2 our $VERSION = '1.0.3';
33  
34 #----------------------------------------------------------------------------
35 #Dependencies
36  
37 2 ▶use Fcntl ':flock'; # import LOCK_* constants
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 =head1 DESCRIPTION
41  
42 C<File::Access::Driver> is a class for convenient file access designed to reduce
43 the code needed to interact with files.
44  
45 It has grown to a "I<Batteries included>" solution covering the most file access
46 use cases.
47  
48 It will not produce exceptions but instead report the errors over the C<getErrorCode()>
49 and the C<getErrorString()> methods.
50  
51 =head1 SYNOPSIS
52  
53 The C<File::Access::Driver> can be used as seen in the "I<File Write>" test:
54  
55 use File::Access::Driver;
56  
57 my $driver = File::Access::Driver->new( 'filepath' => $spath . 'files/out/testfile_out.txt' );
58  
59 # Make sure the file does not exist
60 is( $driver->Delete(), 1, "File Delete: Delete operation 1 correct" );
61 is( $driver->Exists(), 0, "File Exist: File does not exist anymore" );
62  
63 $driver->writeContent(q(This is the multi line content for the test file.
64  
65 It will be written into the test file.
66 The file should only contain this text.
67 Also the file should be created.
68 ));
69  
70 printf(
71 "Test File Exists - File '%s': Write finished with [%d]\n",
72 $driver->getFileName(),
73 $driver->getErrorCode()
74 );
75 printf(
76 "Test File Exists - File '%s': Write Report:\n'%s'\n",
77 $driver->getFileName(),
78 ${ $driver->getReportString() }
79 );
80 printf(
81 "Test File Exists - File '%s': Write Error:\n'%s'\n",
82 $driver->getFileName(),
83 ${ $driver->getErrorString() }
84 );
85  
86 is( $driver->getErrorCode(), 0, "Write Error Code: No errors have occurred" );
87 is( ${ $driver->getErrorString() }, '', "Write Error Message: No errors are reported" );
88  
89 is( $driver->Exists(), 1, "File Exist: File does exist now" );
90 isnt( $driver->getFileSize(), 0, "File Size: File is not empty anymore" );
91  
92 =cut
93  
94 #----------------------------------------------------------------------------
95 #Constructors
96  
97 =head1 METHODS
98  
99 =head2 Constructor
100  
101 =head3 new ( [ CONFIGURATIONS ] )
102  
103 This is the constructor for a new C<File::Access::Driver> object.
104  
105 B<Parameters:>
106  
107 =over 4
108  
109 =item C<CONFIGURATIONS>
110  
111 Key and value pairs containing the configurations.
112  
113 B<Recognized fields:>
114  
115 C<filedirectory> - The directory where the file is located.
116  
117 C<filename> - The base name of the file.
118  
119 C<filepath> - The complete path with directory and file base name.
120  
121 =back
122  
123 See L<Method C<setFileDirectory()>|/"setFileDirectory ( DIRECTORY )">
124  
125 See L<Method C<setFileName()>|/"setFileName ( NAME )">
126  
127 =cut
128  
129 6 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
130  
131 #Take the Method Parameters
132 6 my ( $invocant, %hshprms ) = @_;
133 6 ▶ my $class = ref($invocant) || $invocant;
Condition33%1/3
ref $invocant || $invocant
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
134 6 my $self = undef;
135  
136 # Set the Default Attributes and assign the initial Values
137 6 $self = {
138 '_file' => undef,
139 '_directory_name' => '',
140 '_file_name' => '',
141  
142 # A Reference to the Content Text
143 '_file_content' => undef,
144 '_file_content_lines' => undef,
145 '_file_time' => -1,
146 '_file_access_time' => -1,
147 '_file_size' => -1,
148 '_package_size' => 32768,
149 '_buffered' => 0,
150 '_persistent' => 0,
151 '_locked' => 0,
152 '_writable' => 0,
153 '_appendable' => 0,
154 '_report' => '',
155 '_error_message' => '',
156 '_error_code' => 0
157 };
158  
159 6 bless $self, $class;
160  
161 6 ▶ if ( scalar( keys %hshprms ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %hshprms > 0) 5 1 6
162 5 ▶ $self->setFileDirectory( $hshprms{'filedirectory'} ) if ( defined $hshprms{'filedirectory'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $hshprms{"filedirectory"} 1 4 5
163 5 ▶ $self->setFileName( $hshprms{'filename'} ) if ( defined $hshprms{'filename'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $hshprms{"filename"} 1 4 5
164 5 ▶ $self->setFilePath( $hshprms{'filepath'} ) if ( defined $hshprms{'filepath'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $hshprms{"filepath"} 4 1 5
165 } #if(scalar(keys %hshprms) > 0)
166  
167 #Give the Object back
168 6 return $self;
169 }
170  
171 6 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
172 6 my $self = $_[0];
173  
174 # Free the System Resources
175 6 $self->freeResources;
176 }
177  
178 #----------------------------------------------------------------------------
179 #Administration Methods
180  
181 =head3 setFileDirectory ( DIRECTORY )
182  
183 This method sets the directory where the file is located.
184  
185 B<Parameters:>
186  
187 =over 4
188  
189 =item C<DIRECTORY>
190  
191 The directory where the file is located.
192  
193 If the directory does not end on a slash C< / > it will be appended.
194  
195 =back
196  
197 =cut
198  
199 5 ▶sub setFileDirectory {
Subroutine setFileDirectory: called CC=5 SCAR=16.8
Pod documented
200 5 my $self = $_[0];
201  
202 5 ▶ if ( scalar(@_) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) { } 5 0 5
203 5 $self->{'_directory_name'} = $_[1];
204 }
205 else #No Parameter given
206 {
207 0 $self->{'_directory_name'} = '';
208 }
209  
210 5 ▶ $self->{'_directory_name'} = '' unless ( defined $self->{'_directory_name'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_directory_name"} 0 5 5
211  
212 5 ▶ if ( $self->{'_directory_name'} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_directory_name"} ne "") 5 0 5
213 5 ▶ $self->{'_directory_name'} .= '/' unless ( $self->{'_directory_name'} =~ qr#/$# );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_directory_name"} =~ qr"/$" 1 4 5
214 }
215  
216 #Clear the File Object
217 5 $self->Clear;
218 }
219  
220 =head3 setFileName ( NAME )
221  
222 This method sets the base name of the file.
223  
224 This will also close open file handles and free in-memory cache.
225  
226 B<Parameters:>
227  
228 =over 4
229  
230 =item C<NAME>
231  
232 The base name of the file.
233  
234 =back
235  
236 See L<Method C<Clear()>|/"Clear ()">
237  
238 =cut
239  
240 5 ▶sub setFileName {
Subroutine setFileName: called CC=3 SCAR=11.8
Pod documented
241 5 my $self = $_[0];
242  
243 5 ▶ if ( scalar(@_) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) { } 5 0 5
244 5 $self->{'_file_name'} = $_[1];
245 }
246 else #No Parameter given
247 {
248 0 $self->{'_file_name'} = '';
249 }
250  
251 5 ▶ $self->{'_file_name'} = '' unless ( defined $self->{'_file_name'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_file_name"} 0 5 5
252  
253 # Clear the File Object
254 5 $self->Clear;
255 }
256  
257 =head3 setFilePath ( PATH )
258  
259 This method sets the complete path of the file.
260  
261 B<Parameters:>
262  
263 =over 4
264  
265 =item C<PATH>
266  
267 The complete path of the file.
268  
269 This will split the C<PATH> and call C<setFileDirectory()> with the directory
270 and C<setFileName()> with the file base name.
271  
272 =back
273  
274 See L<Method C<setFileDirectory()>|/"setFileDirectory ( DIRECTORY )">
275  
276 See L<Method C<setFileName()>|/"setFileName ( NAME )">
277  
278 =cut
279  
280 4 ▶sub setFilePath {
Subroutine setFilePath: called CC=5 SCAR=17.4
Pod documented
281 4 my $self = $_[0];
282 4 ▶ my $sdirnm = $_[1] || '';
Condition50%1/2
$_[1] || ''
l!l
40
MC/DC0%0/1
$_[1] || ''
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
283 4 my $sflnm = '';
284  
285 4 ▶ if ( $sdirnm ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sdirnm ne "") 4 0 4
286 4 ▶ if ( index( $sdirnm, '/' ) > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($sdirnm, '/') != -1) { } 4 0 4
287 4 ▶ if ( $sdirnm =~ qr#(.*/)([^/]+)$# ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sdirnm =~ qr"(.*/)([^/]+)$") 4 0 4
288 4 $sdirnm = $1;
289 4 $sflnm = $2;
290 }
291 }
292 else #The Path does not include a Slash Sign
293 {
294 0 $sflnm = $sdirnm;
295 0 $sdirnm = '';
296 }
297 }
298  
299 #Set the Parsed Values
300 4 $self->setFileDirectory($sdirnm);
301 4 $self->setFileName($sflnm);
302 }
303  
304 0 ▶sub changeFileName {
Subroutine changeFileName: not called CC=11 SCAR=48.8
Pod undocumented
305 0 my $self = $_[0];
306 0 my $sdirnm = '';
307 0 ▶ my $sflnm = $_[1] || '';
Condition0%0/2
$_[1] || ''
l!l
00
MC/DC0%0/1
$_[1] || ''
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
308 0 my $irs = 0;
309  
310 0 ▶ if ( $sflnm ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sflnm ne '') { } 0 0 0
311  
312 #If File Name contains Directory Information
313 0 ▶ if ( index( $sflnm, '/' ) > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($sflnm, "/") != -1) 0 0 0
314 0 ▶ if ( $sflnm =~ qr#(.*/)([^/]+)$# ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sflnm =~ qr"(.*/)([^/]+)$") 0 0 0
315 0 $sdirnm = $1;
316 0 $sflnm = $2;
317 }
318 }
319  
320 #Assume the same Directory
321 0 ▶ $sdirnm = $self->{'_directory_name'}
Branch: 1 branch
BranchTrueFalseTotal
if $sdirnm eq "" 0 0 0
322 if ( $sdirnm eq '' );
323  
324 0 ▶ if ( $self->Exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Exists) { } 0 0 0
325 0 $irs = rename $self->{'_directory_name'} . $self->{'_file_name'}, $sdirnm . $sflnm;
326  
327 0 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) { } 0 0 0
328  
329 #Update the File Access Object accordingly but keep the Report History
330  
331 0 $self->{'_directory_name'} = $sdirnm;
332 0 $self->{'_file_name'} = $sflnm;
333 }
334 else #Change File Name failed
335 {
336 0 $irs = 0 + $!;
337  
338 $self->{'_error_message'} .=
339 "File '"
340 . $self->{'_directory_name'}
341 0 . $self->{'_file_name'}
342 . "': Change File Name failed with [$irs]!"
343 . "Message: '$!'\n";
344 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
345  
346 0 $irs = 0;
347 }
348 }
349 else #File does not exist
350 {
351 0 $self->{'_error_message'} .= "File does not exist!\n";
352 0 ▶ $self->{'_error_code'} = 3 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
353 }
354 }
355 else #New File Name was not given
356 {
357 0 $self->{'_error_message'} .= "New File Name is not set!\n";
358 0 ▶ $self->{'_error_code'} = 3 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
359 }
360  
361 0 return $irs;
362 }
363  
364 3 ▶sub setContent {
Subroutine setContent: called CC=5 SCAR=17.1
Pod undocumented
365 3 my $self = $_[0];
366  
367 3 $self->{'_file_content'} = undef;
368 3 $self->{'_file_content_lines'} = undef;
369 3 ▶ $self->{'_buffered'} ||= 1;
Condition100%2/2
$self->{"_buffered"} ||= 1
l!l
12
MC/DC100%1/1
$self->{"_buffered"} ||= 1
$self->{"_buffered"}
Truth table100%2/2
A
A= $self->{"_buffered"}
Aresult
11
00
370  
371 3 ▶ if ( scalar(@_) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) 3 0 3
372 3 ▶ if ( ref( $_[1] ) eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq '') { } 3 0 3
373 3 $self->{'_file_content'} = \$_[1];
374 }
375 else {
376 0 $self->{'_file_content'} = $_[1];
377 }
378 }
379  
380 0 ${ $self->{'_file_content'} } = ''
381 3 ▶ unless ( defined $self->{'_file_content'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_file_content"} 0 3 3
382  
383 }
384  
385 1 ▶sub setContentArray {
Subroutine setContentArray: called CC=4 SCAR=14.8
Pod undocumented
386 1 my $self = $_[0];
387  
388 1 $self->{'_file_content'} = \'';
389 1 $self->{'_file_content_lines'} = undef;
390 1 ▶ $self->{'_buffered'} ||= 1;
Condition50%1/2
$self->{"_buffered"} ||= 1
l!l
10
MC/DC0%0/1
$self->{"_buffered"} ||= 1
$self->{"_buffered"}
Truth table50%1/2
A
A= $self->{"_buffered"}
Aresult
11
00
391  
392 1 ▶ if ( scalar(@_) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) 1 0 1
393 1 ▶ if ( ref( $_[1] ) eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq '') { } 0 1 1
394 0 $self->{'_file_content_lines'} = \$_[1];
395 }
396 else {
397 1 $self->{'_file_content_lines'} = $_[1];
398 }
399 }
400 }
401  
402 0 ▶sub setFileTime {
Subroutine setFileTime: not called CC=6 SCAR=37.4
Pod undocumented
403 0 my $self = $_[0];
404 0 ▶ my $itmfl = $_[1] || time;
Condition0%0/2
$_[1] || time
l!l
00
MC/DC0%0/1
$_[1] || time
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
405 0 my $irs = 0;
406  
407 0 ▶ if ( $self->Exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Exists) { } 0 0 0
408 0 $self->getFileTime;
409  
410 0 $irs = utime $self->{'_file_access_time'}, $itmfl, $self->{'_directory_name'} . $self->{'_file_name'};
411  
412 0 ▶ if ( $irs < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs < 1) 0 0 0
413 0 $irs = 0 + $!;
414  
415 $self->{'_error_message'} .=
416 "File '"
417 . $self->{'_directory_name'}
418 0 . $self->{'_file_name'}
419 . "': Set File Time failed with [$irs]!"
420 . "Message: '$!'\n";
421 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
422  
423 0 $irs = 0;
424 }
425 }
426 else #File does not exist
427 {
428 0 $self->{'_error_message'} .= "File does not exist!\n";
429 0 ▶ $self->{'_error_code'} = 3 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
430 }
431  
432 0 return $irs;
433 }
434  
435 =head3 setBuffered ( BUFFERED )
436  
437 This method enables or disables the in-memory buffer.
438  
439 B<Parameters:>
440  
441 =over 4
442  
443 =item C<BUFFERED>
444  
445 If the in-memory buffer should be enabled or disabled. (Possible values: C< 0 > | C< 1 >)
446  
447 =back
448  
449 =cut
450  
451 0 ▶sub setBuffered {
Subroutine setBuffered: not called CC=4 SCAR=30.0
Pod documented
452 0 my $self = $_[0];
453  
454 0 ▶ if ( scalar(@_) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) { } 0 0 0
455 0 ▶ if ( $_[1] =~ qr/^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] =~ qr/^\d+$/) { } 0 0 0
456  
457 # The parameter is an unsigned whole number
458  
459 0 ▶ if ( $_[1] != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] != 0) { } 0 0 0
460 0 $self->{'_buffered'} = 1;
461 }
462 else {
463 0 $self->{'_buffered'} = 0;
464 }
465 }
466 else #The Parameter is not a Number
467 {
468 0 $self->{'_buffered'} = 0;
469 }
470 }
471 else #No Parameter was given
472 {
473 0 $self->{'_buffered'} = 1;
474 }
475 }
476  
477 =head3 setPersistent ( PERSISTENT )
478  
479 This method enables or disables the persistent mode.
480  
481 If the persistent mode is enabled the file is kept open.
482  
483 B<Parameters:>
484  
485 =over 4
486  
487 =item C<PERSISTENT>
488  
489 If the filehandle should be kept open. (Possible values: C< 0 > | C< 1 >)
490  
491 =back
492  
493 =cut
494  
495 0 ▶sub setPersistent {
Subroutine setPersistent: not called CC=4 SCAR=30.0
Pod documented
496 0 my $self = $_[0];
497  
498 0 ▶ if ( scalar(@_) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) { } 0 0 0
499 0 ▶ if ( $_[1] =~ qr/^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] =~ qr/^\d+$/) { } 0 0 0
500  
501 # The parameter is an unsigned whole number
502  
503 0 ▶ if ( $_[1] != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] != 0) { } 0 0 0
504 0 $self->{'_persistent'} = 1;
505 }
506 else {
507 0 $self->{'_persistent'} = 0;
508 }
509 }
510 else #The Parameter is not a Number
511 {
512 0 $self->{'_persistent'} = 0;
513 }
514 }
515 else #No Parameter was given
516 {
517 0 $self->{'_persistent'} = 1;
518 }
519 }
520  
521 =head3 Create ()
522  
523 This method will create an empty file.
524  
525 If the file already exists it will be truncated.
526  
527 =cut
528  
529 1 ▶sub Create {
Subroutine Create: called CC=1 SCAR=0.0
Pod documented
530 1 my $self = $_[0];
531  
532 1 return $self->writeContent('');
533 }
534  
535 1 ▶sub _openrFile {
Subroutine _openrFile: called CC=9 SCAR=30.0
536 1 my $self = $_[0];
537 1 my $irs = 0;
538  
539 1 ▶ if ( $self->_isOpen() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isOpen) 0 1 1
540  
541 #Reopen the File in shared Reading Mode
542 0 ▶ $self->_closeFile() if ( $self->_isWritable() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isWritable 0 0 0
543 } #if($self->_isOpen())
544  
545 1 ▶ unless ( $self->_isOpen() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isOpen) { } 0 1 1
546 1 ▶ if ( $self->Exists() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Exists) { } 1 0 1
547  
548 #Open the File
549 1 $irs = open $self->{'_file'}, '<', $self->{'_directory_name'} . $self->{'_file_name'};
550  
551 1 ▶ if ( defined $irs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $irs) { } 1 0 1
552 1 $irs = flock( $self->{'_file'}, LOCK_SH );
553  
554 1 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 1 0 1
555 1 $self->{'_locked'} = 1;
556  
557 1 $irs = 1;
558 } #if($irs)
559 }
560 else #The File could not be opened
561 {
562 $self->{'_error_message'} .=
563 "File '"
564 . $self->{'_directory_name'}
565 0 . $self->{'_file_name'} . "': "
566 . "Open Read failed!\n"
567 . "Message: '$!'\n";
568 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
569  
570 0 $irs = 0;
571 } #unless(defined $irs)
572 }
573 else #The File does not exist
574 {
575 $self->{'_error_message'} .=
576 "File '"
577 . $self->{'_directory_name'}
578 0 . $self->{'_file_name'} . "': "
579 . "Open Read failed!\n"
580 . "Message: 'File does not exist.'\n";
581 0 ▶ $self->{'_error_code'} = 3 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
582 }
583 }
584 else #The File is open and in Reading Mode
585 {
586 0 $irs = 1;
587 }
588  
589 1 return $irs;
590 }
591  
592 4 ▶sub _openwFile {
Subroutine _openwFile: called CC=14 SCAR=33.7
593 4 my $self = $_[0];
594 4 my $irs = 0;
595  
596 4 ▶ unless ( $self->_isWritable() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isWritable) { } 0 4 4
597 4 ▶ $self->_closeFile() if ( $self->_isOpen() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isOpen 0 4 4
598  
599 4 ▶ if ( $self->{'_directory_name'} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_directory_name'} ne '') { } 4 0 4
600 4 ▶ unless ( -d $self->{'_directory_name'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $self->{'_directory_name'}) { } 3 1 4
601 1 my $idircnt = -1;
602  
603 $self->{"_report"} .=
604 "Directory '"
605 1 . $self->{'_directory_name'} . "': "
606 . "Directory does not exist. Directory creating ...\n";
607  
608 #Clear any previous Error Messages
609 1 $@ = '';
610  
611 1 eval {
612 #Create the Directory
613 1 $idircnt = make_path( $self->{'_directory_name'}, { mode => 0775 } );
614 };
615  
616 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
617 $self->{'_error_message'} .=
618 "Directory '"
619 0 . $self->{'_directory_name'} . "': "
620 . "Create Directory failed.\n"
621 . "Message (Code '"
622 . ( $! + 0 )
623 . "'): '$@'\n";
624  
625 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
626  
627 0 $idircnt = -1;
628 }
629  
630 1 ▶ $irs = 1 if ( $idircnt != -1 );
Branch: 1 branch
BranchTrueFalseTotal
if $idircnt != -1 1 0 1
631 }
632 else # Directory does exist
633 {
634 3 $irs = 1;
635 }
636 }
637 else #The File Directory is not set
638 {
639 0 $irs = 1;
640 }
641  
642 4 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 4 0 4
643 4 ▶ if ( $self->{'_file_name'} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_file_name'} ne '') { } 4 0 4
644  
645 #Open the File
646 4 $irs = open $self->{'_file'}, ">", $self->{'_directory_name'} . $self->{'_file_name'};
647  
648 4 ▶ if ( defined $irs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $irs) { } 4 0 4
649 4 $self->{'_writable'} = 1;
650  
651 4 $irs = flock( $self->{'_file'}, LOCK_EX );
652  
653 4 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 4 0 4
654 4 $self->{'_locked'} = 1;
655  
656 4 $irs = 1;
657 }
658 }
659 else #The File could not be opened
660 {
661 $self->{'_error_message'} .=
662 "File '"
663 . $self->{'_directory_name'}
664 0 . $self->{'_file_name'}
665 . "': Open Write failed!\n"
666 . "Message: '$!'\n";
667 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
668  
669 0 $irs = 0;
670 }
671 }
672 else #The File Name isnt set
673 {
674 0 $self->{'_error_message'} .= "File Name isn't set!\n";
675 0 ▶ $self->{'_error_code'} = 3 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
676  
677 0 $irs = 0;
678 }
679 }
680 }
681 else #The File is already open in Appending Mode
682 {
683 0 $irs = 1;
684 } #unless($self->_isWritable())
685  
686 4 return $irs;
687 }
688  
689 0 ▶sub _openaFile {
Subroutine _openaFile: not called CC=15 SCAR=54.8
690 0 my $self = $_[0];
691 0 my $irs = 0;
692  
693 0 ▶ unless ( $self->_isAppendable() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isAppendable) { } 0 0 0
694 0 ▶ $self->_closeFile() if ( $self->_isOpen() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isOpen 0 0 0
695  
696 0 ▶ if ( $self->{'_directory_name'} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_directory_name'} ne '') { } 0 0 0
697 0 ▶ unless ( -d $self->{'_directory_name'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $self->{'_directory_name'}) { } 0 0 0
698 0 my $idircnt = -1;
699  
700 #Clear any previous Error Messages
701 0 $@ = '';
702  
703 0 eval {
704 #Create the Directory
705 0 $idircnt = make_path( $self->{'_directory_name'}, { mode => 0775 } );
706 };
707  
708 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
709 $self->{'_error_message'} .=
710 "Directory '"
711 0 . $self->{'_directory_name'} . "': "
712 . "Create Directory failed.\n"
713 . "Message (Code '"
714 . ( 0 + $! )
715 . "'): '$@'\n";
716  
717 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
718  
719 0 $idircnt = -1;
720 }
721  
722 0 ▶ $irs = 1 if ( $idircnt != -1 );
Branch: 1 branch
BranchTrueFalseTotal
if $idircnt != -1 0 0 0
723 }
724 else # Directory does exist
725 {
726 0 $irs = 1;
727 }
728 }
729 else #The File Directory is not set
730 {
731 0 $irs = 1;
732 }
733  
734 0 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 0 0 0
735 0 ▶ if ( $self->{'_file_name'} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_file_name'} ne '') { } 0 0 0
736  
737 #Open the File
738 0 $irs = open $self->{'_file'}, ">>", $self->{'_directory_name'} . $self->{'_file_name'};
739  
740 0 ▶ if ( defined $irs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $irs) { } 0 0 0
741 0 $self->{'_writable'} = 1;
742 0 $self->{'_appendable'} = 1;
743  
744 0 $irs = flock( $self->{'_file'}, LOCK_EX );
745  
746 0 ▶ $irs = seek( $self->{'_file'}, 0, SEEK_END ) if ($irs);
Branch: 1 branch
BranchTrueFalseTotal
if $irs 0 0 0
747  
748 0 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 0 0 0
749 0 $self->{'_locked'} = 1;
750  
751 0 $irs = 1;
752 }
753 }
754 else #The File could not be opened
755 {
756 $self->{'_error_message'} .=
757 "File '"
758 . $self->{'_directory_name'}
759 0 . $self->{'_file_name'}
760 . "': Open Append failed!\n"
761 . "Message: '$!'\n";
762 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
763  
764 0 $irs = 0;
765 }
766 }
767 else #The File Name isnt set
768 {
769 0 $self->{'_error_message'} .= "File Name isn't set!\n";
770 0 ▶ $self->{'_error_code'} = 3 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
771 }
772 }
773 }
774 else #The File is already open in Appending Mode
775 {
776 0 $irs = 1;
777 } #unless($self->_isAppendable())
778  
779 0 return $irs;
780 }
781  
782 1 ▶sub Read {
Subroutine Read: called CC=14 SCAR=30.2
Pod undocumented
783 1 my $self = $_[0];
784 1 my $irs = 0;
785  
786 1 ${ $self->{'_file_content'} } = '';
787 1 $self->{'_file_content_lines'} = undef;
788 1 ▶ $self->{'_buffered'} = 1 unless ( $self->{'_buffered'} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_buffered"} 1 0 1
789 1 $self->{'_file_time'} = -1;
790 1 $self->{'_file_size'} = -1;
791  
792 1 ▶ $self->_openrFile() if ( $self->_isWritable() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isWritable 0 1 1
793  
794 1 ▶ $self->_openrFile() unless ( $self->_isOpen() );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_isOpen 1 0 1
795  
796 1 ▶ if ( $self->_isOpen() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isOpen) 1 0 1
797 1 my $scntntln = '';
798 1 my $irdcnt = -1;
799  
800 do #while($irdcnt);
801 1 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$irdcnt and do { do { $irdcnt = sysread($self->{"_file"}, $scntntln, $self->{"_package_size"}); if (defined $irdcnt) { ${$$self{"_file_content"};} .= $scntntln if $irdcnt > 0; } else { $self->{"_error_message"} .= q[File '] . $self->{"_directory_name"} . $self->{"_file_name"} . q(': Read File failed with [) . (0 + $!) . ']!!' . "Message: '$!'\n"; $self->{"_error_code"} = 1 if $self->{"_error_code"} < 1; } } } 1 1 2
802 2 $irdcnt = sysread( $self->{'_file'}, $scntntln, $self->{"_package_size"} );
803  
804 2 ▶ if ( defined $irdcnt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $irdcnt) { } 2 0 2
805 2 ▶ ${ $self->{'_file_content'} } .= $scntntln if ( $irdcnt > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $irdcnt > 0 1 1 2
806 }
807 else #An Error has ocurred
808 {
809 $self->{'_error_message'} .=
810 "File '"
811 . $self->{'_directory_name'}
812 0 . $self->{'_file_name'}
813 . "': Read File failed with ["
814 . ( 0 + $! ) . "]!!"
815 . "Message: '$!'\n";
816 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
817 }
818 } while ($irdcnt);
819  
820 1 ▶ $irs = 1 unless ($!);
Branch: 1 branch
BranchTrueFalseTotal
unless $! 1 0 1
821  
822 1 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 1 0 1
823 1 my @arrflstt = stat( $self->{'_file'} );
824  
825 1 ▶ if ( scalar(@arrflstt) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arrflstt > 0) { } 1 0 1
826 1 $self->{'_file_time'} = $arrflstt[9];
827 1 $self->{'_file_size'} = $arrflstt[7];
828 }
829 else #The File Attributes are empty
830 {
831 $self->{'_error_message'} .=
832 0 "File '" . $self->{'_directory_name'} . $self->{'_file_name'} . "': File Attributes failed!\n";
833 }
834 }
835  
836 1 ▶ if ( $self->{'_persistent'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_persistent'}) { } 0 1 1
837 0 ▶ unless ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($irs) 0 0 0
838 $self->{'_error_message'} .=
839 "File '"
840 . $self->{'_directory_name'}
841 0 . $self->{'_file_name'} . "': "
842 . "File closing because of Reading Error ...\n";
843  
844 #Close the File
845 0 $self->_closeFile();
846 }
847 }
848 else #File is not persistent
849 {
850 #Close the File
851 1 $self->_closeFile();
852 }
853 }
854  
855 1 return $irs;
856 }
857  
858 0 ▶sub readContent {
Subroutine readContent: not called CC=1 SCAR=6.9
Pod undocumented
859 0 my $self = $_[0];
860  
861 0 $self->Read();
862  
863 0 return $self->getContent;
864 }
865  
866 0 ▶sub readContentArray {
Subroutine readContentArray: not called CC=1 SCAR=6.9
Pod undocumented
867 0 my $self = $_[0];
868  
869 0 File::Access::Driver::Read $self;
870  
871 0 return $self->getContentArray;
872 }
873  
874 =head3 Truncate ()
875  
876 This method will empty an existing file.
877  
878 If the file does not exist it will be created.
879  
880 =cut
881  
882 1 ▶sub Truncate {
Subroutine Truncate: called CC=1 SCAR=0.0
Pod documented
883 1 my $self = $_[0];
884  
885 1 return $self->writeContent('');
886 }
887  
888 4 ▶sub Write {
Subroutine Write: called CC=12 SCAR=28.9
Pod undocumented
889 4 my $self = $_[0];
890 4 my $irs = 0;
891  
892 4 ▶ $self->{'_buffered'} = 1 unless ( $self->{'_buffered'} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_buffered"} 0 4 4
893 4 $self->{'_file_time'} = -1;
894 4 $self->{'_file_access_time'} = -1;
895 4 $self->{'_file_size'} = -1;
896  
897 4 ▶ unless ( $self->_isWritable() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_isWritable) 4 0 4
898 4 ▶ $self->_closeFile() if ( $self->_isOpen() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isOpen 0 4 4
899  
900 #Open the File in Write Mode
901 4 $self->_openwFile();
902 }
903  
904 4 ▶ if ( $self->_isWritable() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isWritable) 4 0 4
905 4 my $iwrtcnt = -1;
906 4 my $content = $self->getContent();
907 4 my $icntntlen = length( ${$content} );
908  
909 4 $irs = 0;
910  
911 # Write the Content Line to the File
912 4 $iwrtcnt = syswrite( $self->{'_file'}, ${$content} );
913  
914 4 ▶ if ( defined $iwrtcnt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $iwrtcnt) { } 4 0 4
915 4 $irs = 1;
916  
917 4 ▶ if ( $iwrtcnt != $icntntlen ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iwrtcnt != $icntntlen) 0 4 4
918 $self->{'_error_message'} .=
919 "File '"
920 . $self->{'_directory_name'}
921 0 . $self->{'_file_name'}
922 . "': '$iwrtcnt' from '$icntntlen' Bytes written.\n";
923 }
924 }
925 else # An Error has ocurred
926 {
927 $self->{'_error_message'} .=
928 "File '"
929 . $self->{'_directory_name'}
930 0 . $self->{'_file_name'}
931 . "': File Write failed!\n"
932 . "Message: '$!'\n";
933 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
934 }
935  
936 4 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 4 0 4
937 4 my @arrflstt = stat( $self->{'_file'} );
938  
939 4 ▶ if ( scalar(@arrflstt) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arrflstt > 0) { } 4 0 4
940 4 $self->{'_file_time'} = $arrflstt[9];
941 4 $self->{'_file_access_time'} = $arrflstt[8];
942 4 $self->{'_file_size'} = $arrflstt[7];
943 }
944 else #The File Attributes are empty
945 {
946 $self->{'_error_message'} .=
947 0 "File '" . $self->{'_directory_name'} . $self->{'_file_name'} . "': File Attributes failed!\n";
948 }
949 }
950  
951 4 ▶ if ( $self->{'_persistent'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_persistent'}) { } 0 4 4
952 0 ▶ unless ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($irs) 0 0 0
953 $self->{'_error_message'} .=
954 "File '"
955 . $self->{'_directory_name'}
956 0 . $self->{'_file_name'} . "': "
957 . "File closing because of Writing Error ...\n";
958  
959 #Close the File
960 0 $self->_closeFile();
961 }
962 }
963 else #File is not persistent
964 {
965 #Close the File
966 4 $self->_closeFile();
967 }
968 }
969  
970 4 return $irs;
971 }
972  
973 3 ▶sub writeContent {
Subroutine writeContent: called CC=1 SCAR=0.0
Pod undocumented
974 3 my $self = $_[0];
975  
976 3 $self->setContent( $_[1] );
977  
978 3 return File::Access::Driver::Write $self;
979 }
980  
981 0 ▶sub appendLine {
Subroutine appendLine: not called CC=15 SCAR=54.8
Pod undocumented
982 0 my $self = $_[0];
983 0 my $rcntntln = undef;
984 0 my $irs = 0;
985  
986 0 $self->{'_file_time'} = -1;
987 0 $self->{'_file_size'} = -1;
988  
989 0 ▶ if ( scalar(@_) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) 0 0 0
990 0 ▶ if ( ref( $_[1] ) ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] ne '') { } 0 0 0
991 0 $rcntntln = $_[1];
992 }
993 else {
994 0 $rcntntln = \$_[1];
995 }
996  
997 0 ▶ if ( $$rcntntln ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$rcntntln ne "") 0 0 0
998 0 ▶ if ( $self->{'_buffered'} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_buffered"} > 0) 0 0 0
999 0 ${ $self->{'_file_content'} } .= $$rcntntln;
1000 }
1001 }
1002 }
1003  
1004 0 ▶ unless ( $self->_isAppendable() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_isAppendable) 0 0 0
1005 0 ▶ $self->_closeFile() if ( $self->_isOpen() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isOpen 0 0 0
1006  
1007 #Open the File in Appending Mode
1008 0 $self->_openaFile();
1009 }
1010  
1011 0 ▶ if ( $self->_isAppendable() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isAppendable) 0 0 0
1012 0 my $iwrtcnt = -1;
1013 0 my $icntntlen = length($$rcntntln);
1014  
1015 0 $irs = 0;
1016  
1017 #Write the Content Line to the File
1018 0 $iwrtcnt = syswrite( $self->{'_file'}, $$rcntntln );
1019  
1020 0 ▶ if ( defined $iwrtcnt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $iwrtcnt) { } 0 0 0
1021 0 $irs = 1;
1022  
1023 0 ▶ if ( $iwrtcnt != $icntntlen ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iwrtcnt != $icntntlen) 0 0 0
1024 $self->{'_error_message'} .=
1025 "File '"
1026 . $self->{'_directory_name'}
1027 0 . $self->{'_file_name'} . "': "
1028 . "'$iwrtcnt' from '$icntntlen' Bytes written.\n";
1029 }
1030 }
1031 else #An Error has ocurred
1032 {
1033 $self->{'_error_message'} .=
1034 "File '"
1035 . $self->{'_directory_name'}
1036 0 . $self->{'_file_name'}
1037 . "': File Write failed!\n"
1038 . "Message: '$!'\n";
1039 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
1040 }
1041  
1042 0 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 0 0 0
1043 0 my @arrflstt = stat( $self->{'_file'} );
1044  
1045 0 ▶ if ( scalar(@arrflstt) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arrflstt > 0) { } 0 0 0
1046 0 $self->{'_file_time'} = $arrflstt[9];
1047 0 $self->{'_file_size'} = $arrflstt[7];
1048 }
1049 else #The File Attributes are empty
1050 {
1051 $self->{'_error_message'} .=
1052 0 "File '" . $self->{'_directory_name'} . $self->{'_file_name'} . "': File Attributes failed!\n";
1053 }
1054 }
1055  
1056 0 ▶ if ( $self->{'_persistent'} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_persistent'} > 0) { } 0 0 0
1057 0 ▶ unless ( $irs > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($irs > 0) 0 0 0
1058 $self->{'_error_message'} .=
1059 "File '"
1060 . $self->{'_directory_name'}
1061 0 . $self->{'_file_name'} . "': "
1062 . "File closing because of Writing Error ...\n";
1063  
1064 #Close the File
1065 0 $self->_closeFile();
1066 }
1067 }
1068 else #File is not persistent
1069 {
1070 #Close the File
1071 0 $self->_closeFile();
1072 }
1073 } #if($self->_isAppendable())
1074  
1075 0 return $irs;
1076 }
1077  
1078 0 ▶sub writeLine {
Subroutine writeLine: not called CC=1 SCAR=6.9
Pod undocumented
1079 0 my $self = $_[0];
1080  
1081 0 return $self->appendLine( $_[1] );
1082 }
1083  
1084 =head3 Delete ()
1085  
1086 This method deletes the file from the file system.
1087  
1088 It will return C< 1 > if the file could be deleted.
1089  
1090 If it could not be deleted it sets the B<Error Code> and the B<Error String>.
1091  
1092 B<Returns:>
1093  
1094 It returns C< 1 > if the file could be deleted or if it did not exist.
1095  
1096 Or C< 0 > if the file could not be deleted.
1097  
1098 =cut
1099  
1100 4 ▶sub Delete {
Subroutine Delete: called CC=5 SCAR=18.3
Pod documented
1101 4 my $self = $_[0];
1102 4 my $irs = 0;
1103  
1104 #Close the Open File
1105 4 ▶ $self->_closeFile() if ( $self->_isOpen );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isOpen 0 4 4
1106  
1107 4 ▶ if ( $self->Exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Exists) { } 2 2 4
1108 2 $irs = unlink $self->{'_directory_name'} . $self->{'_file_name'};
1109  
1110 2 ▶ if ( $irs < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs < 1) 0 2 2
1111 $self->{'_error_message'} .=
1112 "File '"
1113 . $self->{'_directory_name'}
1114 0 . $self->{'_file_name'}
1115 . "': Delete File failed with ["
1116 . ( 0 + $! ) . "]!"
1117 . "Message: '$!'\n";
1118 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
1119  
1120 0 $irs = 0;
1121 }
1122 }
1123 else #The File does not exist
1124 {
1125 2 $irs = 1;
1126 }
1127  
1128 4 return $irs;
1129 }
1130  
1131 5 ▶sub _closeFile {
Subroutine _closeFile: called CC=7 SCAR=23.0
1132 5 my $self = $_[0];
1133 5 my $irs = 0;
1134  
1135 5 ▶ if ( $self->_isOpen() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isOpen) { } 5 0 5
1136 5 ▶ if ( $self->{'_locked'} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_locked"} > 0) 5 0 5
1137 5 $irs = flock( $self->{'_file'}, LOCK_UN );
1138  
1139 5 ▶ unless ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) { } 5 0 5
1140 $self->{'_error_message'} .=
1141 "File '"
1142 . $self->{'_directory_name'}
1143 0 . $self->{'_file_name'} . "': "
1144 . "Lock Release failed!\n"
1145 . "Message: '$!'\n";
1146 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
1147 }
1148 else #File Lock was released
1149 {
1150 5 $self->{'_locked'} = 0;
1151 }
1152 }
1153  
1154 5 $irs = close $self->{'_file'};
1155  
1156 5 ▶ unless ( $irs > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($irs > 0) 0 5 5
1157 $self->{'_error_message'} .=
1158 "File '"
1159 . $self->{'_directory_name'}
1160 0 . $self->{'_file_name'} . "': "
1161 . "Close failed!\n"
1162 . "Message: '$!'\n";
1163 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
1164 } #unless($irs > 0)
1165  
1166 5 $self->{'_file'} = undef;
1167 5 $self->{'_writable'} = 0;
1168 5 $self->{'_appendable'} = 0;
1169  
1170 #To Refresh on next Request
1171 5 $self->{'_file_access_time'} = -1;
1172 5 $self->{'_file_time'} = -1;
1173 5 $self->{'_file_size'} = -1;
1174  
1175 }
1176 else #The File is not open
1177 {
1178 0 $irs = 1;
1179 }
1180  
1181 5 return $irs;
1182 }
1183  
1184 =head3 Clear ()
1185  
1186 This method closed the file handle and frees the in-memory buffer
1187 and resets also the in-memory file attributes.
1188  
1189 =cut
1190  
1191 10 ▶sub Clear {
Subroutine Clear: called CC=2 SCAR=7.0
Pod documented
1192 10 my $self = $_[0];
1193  
1194 #Close the Open File
1195 10 ▶ $self->_closeFile() if ( $self->_isOpen() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isOpen 0 10 10
1196  
1197 #Clear Content
1198 10 $self->{'_file_content'} = undef;
1199 10 $self->{'_file_content_lines'} = undef;
1200  
1201 10 $self->clearErrors;
1202  
1203 #To Refresh on next Request
1204 10 $self->{'_file_access_time'} = -1;
1205 10 $self->{'_file_time'} = -1;
1206 10 $self->{'_file_size'} = -1;
1207  
1208 }
1209  
1210 =head3 clearErrors ()
1211  
1212 Clear the error and activity report and reset the error code.
1213  
1214 =cut
1215  
1216 11 ▶sub clearErrors {
Subroutine clearErrors: called CC=1 SCAR=0.0
Pod documented
1217 11 my $self = $_[0];
1218  
1219 11 $self->{"_report"} = '';
1220 11 $self->{'_error_message'} = '';
1221 11 $self->{'_error_code'} = 0;
1222 }
1223  
1224 =head3 freeResources ()
1225  
1226 This method closed the file handle and frees the in-memory buffer.
1227  
1228 =cut
1229  
1230 6 ▶sub freeResources {
Subroutine freeResources: called CC=2 SCAR=7.4
Pod documented
1231 6 my $self = $_[0];
1232  
1233 6 ▶ if ( $self->_isOpen ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isOpen) 0 6 6
1234  
1235 # Close the Open File
1236 0 $self->_closeFile();
1237 }
1238  
1239 #Clear Content
1240 6 $self->{'_file_content'} = undef;
1241 6 $self->{'_file_content_lines'} = undef;
1242 }
1243  
1244 #----------------------------------------------------------------------------
1245 #Consultation Methods
1246  
1247 =head3 getFileDirectory ()
1248  
1249 This method returns the directory of the file.
1250  
1251 B<Returns:>
1252  
1253 The directory of the file. It is terminated with a trailing slash C< / >.
1254  
1255 =cut
1256  
1257 3 ▶sub getFileDirectory {
Subroutine getFileDirectory: called CC=1 SCAR=0.0
Pod documented
1258 3 return $_[0]->{'_directory_name'};
1259 }
1260  
1261 =head3 getFileName ()
1262  
1263 This method returns the base name of the file.
1264  
1265 B<Returns:>
1266  
1267 The base name of the file.
1268  
1269 =cut
1270  
1271 14 ▶sub getFileName {
Subroutine getFileName: called CC=1 SCAR=0.0
Pod documented
1272 14 return $_[0]->{'_file_name'};
1273 }
1274  
1275 =head3 getFilePath ()
1276  
1277 This method returns the complete path of the file.
1278  
1279 B<Returns:>
1280  
1281 The complete path of the file.
1282  
1283 =cut
1284  
1285 3 ▶sub getFilePath {
Subroutine getFilePath: called CC=1 SCAR=0.0
Pod documented
1286 3 return $_[0]->{'_directory_name'} . $_[0]->{'_file_name'};
1287 }
1288  
1289 5 ▶sub getContent {
Subroutine getContent: called CC=4 SCAR=13.9
Pod undocumented
1290 5 my $self = $_[0];
1291  
1292 $self->{'_file_content'} = \''
1293 5 ▶ unless ( defined $self->{'_file_content'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_file_content"} 0 5 5
1294  
1295 5 ▶ if ( ${ $self->{'_file_content'} } eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$self->{"_file_content"};} eq "") 3 2 5
1296 3 ▶ if ( scalar( @{ $self->{'_file_content_lines'} } ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"_file_content_lines"};} > 0) 1 2 3
1297 1 my $content = join( "\n", @{ $self->{'_file_content_lines'} } );
1298  
1299 1 $self->{'_file_content'} = \$content;
1300 }
1301 }
1302  
1303 5 return $self->{'_file_content'};
1304 }
1305  
1306 1 ▶sub getContentArray {
Subroutine getContentArray: called CC=4 SCAR=14.1
Pod undocumented
1307 1 my $self = $_[0];
1308  
1309 1 @{ $self->{'_file_content_lines'} } = ()
1310 1 ▶ unless ( defined $self->{'_file_content_lines'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_file_content_lines"} 1 0 1
1311  
1312 1 ▶ if ( scalar( @{ $self->{'_file_content_lines'} } ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"_file_content_lines"};} == 0) 1 0 1
1313 1 ▶ if ( ${ $self->{'_file_content'} } ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$self->{"_file_content"};} ne "") 1 0 1
1314 1 @{ $self->{'_file_content_lines'} } = split( "\n", ${ $self->{'_file_content'} } );
1315 }
1316 }
1317  
1318 1 return $self->{'_file_content_lines'};
1319 }
1320  
1321 0 ▶sub getFileTime {
Subroutine getFileTime: not called CC=5 SCAR=34.0
Pod undocumented
1322 0 my $self = $_[0];
1323  
1324 0 ▶ if ( $self->Exists() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Exists) { } 0 0 0
1325 0 ▶ if ( $self->{'_file_time'} < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_file_time"} < 0) 0 0 0
1326 0 my @arrflstt = stat( $self->{'_directory_name'} . $self->{'_file_name'} );
1327  
1328 0 $self->{'_file_time'} = -1;
1329 0 $self->{'_file_access_time'} = -1;
1330 0 $self->{'_file_size'} = -1;
1331  
1332 0 ▶ if ( scalar(@arrflstt) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arrflstt > 0) { } 0 0 0
1333 0 $self->{'_file_time'} = $arrflstt[9];
1334 0 $self->{'_file_access_time'} = $arrflstt[8];
1335 0 $self->{'_file_size'} = $arrflstt[7];
1336 }
1337 else #The File Attributes are empty
1338 {
1339 $self->{'_error_message'} .=
1340 0 "File '" . $self->{'_directory_name'} . $self->{'_file_name'} . "': File Attributes failed!\n";
1341 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
1342 }
1343 }
1344 }
1345 else #The File does not exist
1346 {
1347 0 $self->{'_file_time'} = -1;
1348 0 $self->{'_file_access_time'} = -1;
1349 0 $self->{'_file_size'} = -1;
1350 }
1351  
1352 0 return $self->{'_file_time'};
1353 }
1354  
1355 5 ▶sub getFileSize {
Subroutine getFileSize: called CC=5 SCAR=18.9
Pod undocumented
1356 5 my $self = $_[0];
1357  
1358 5 ▶ if ( $self->Exists() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Exists) { } 5 0 5
1359 5 ▶ if ( $self->{'_file_size'} < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_file_size"} < 0) 5 0 5
1360 5 my @arrflstt = stat( $self->{'_directory_name'} . $self->{'_file_name'} );
1361  
1362 5 $self->{'_file_time'} = -1;
1363 5 $self->{'_file_access_time'} = -1;
1364 5 $self->{'_file_size'} = -1;
1365  
1366 5 ▶ if ( scalar(@arrflstt) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arrflstt > 0) { } 5 0 5
1367 5 $self->{'_file_time'} = $arrflstt[9];
1368 5 $self->{'_file_access_time'} = $arrflstt[8];
1369 5 $self->{'_file_size'} = $arrflstt[7];
1370 }
1371 else #The File Attributes are empty
1372 {
1373 $self->{'_error_message'} .=
1374 0 "File '" . $self->{'_directory_name'} . $self->{'_file_name'} . "': File Attributes failed!\n";
1375 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
1376 }
1377 }
1378 }
1379 else #The File does not exist
1380 {
1381 0 $self->{'_file_time'} = -1;
1382 0 $self->{'_file_access_time'} = -1;
1383 0 $self->{'_file_size'} = -1;
1384 }
1385  
1386 5 return $self->{'_file_size'};
1387 }
1388  
1389 =head3 Exists ()
1390  
1391 This method checks if the file exists in the file system.
1392  
1393 B<Returns:>
1394  
1395 It returns C< 1 > if the file exists otherwise C< 0 >.
1396  
1397 =cut
1398  
1399 17 ▶sub Exists {
Subroutine Exists: called CC=8 SCAR=23.4
Pod documented
1400 17 my $self = $_[0];
1401 17 my $irs = 0;
1402  
1403 17 ▶ unless ( $self->_isOpen() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isOpen) { } 0 17 17
1404 17 ▶ if ( $self->{'_file_name'} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_file_name'} ne '') { } 17 0 17
1405 17 ▶ if ( $self->{'_directory_name'} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_directory_name'} ne '') { } 17 0 17
1406 17 ▶ $irs = 1 if ( -d $self->{'_directory_name'} );
Branch: 1 branch
BranchTrueFalseTotal
if -d $self->{"_directory_name"} 15 2 17
1407 }
1408 else #The Directory Name isnt set
1409 {
1410 0 $irs = 1;
1411 }
1412  
1413 17 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 15 2 17
1414 15 ▶ $irs = 0 unless ( -e $self->{'_directory_name'} . $self->{'_file_name'} );
Branch: 1 branch
BranchTrueFalseTotal
unless -e $self->{"_directory_name"} . $self->{"_file_name"} 3 12 15
1415 }
1416 }
1417 else #The File Name isnt set
1418 {
1419 0 $self->{'_error_message'} .= "File Name isn't set!\n";
1420 0 ▶ $self->{'_error_code'} = 3 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
1421 }
1422 }
1423 else #The File is already open
1424 {
1425 0 $irs = 1;
1426 }
1427  
1428 17 return $irs;
1429 }
1430  
1431 67 ▶sub _isOpen {
Subroutine _isOpen: called CC=4 SCAR=14.0
1432 67 my $self = $_[0];
1433 67 my $iopn = 0;
1434  
1435 67 ▶ if ( defined $self->{'_file'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_file'}) { } 10 57 67
1436 10 ▶ $iopn = 1 if ( fileno( $self->{'_file'} ) );
Branch: 1 branch
BranchTrueFalseTotal
if fileno $self->{"_file"} 10 0 10
1437 }
1438 else #The File Handle is not set
1439 {
1440 57 ▶ $self->{'_file'} = undef unless ( exists $self->{'_file'} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_file"} 0 57 57
1441 }
1442  
1443 67 return $iopn;
1444 }
1445  
1446 13 ▶sub _isWritable {
Subroutine _isWritable: called CC=3 SCAR=11.0
1447 13 my $self = $_[0];
1448 13 my $iopn = 0;
1449  
1450 13 ▶ if ( $self->_isOpen() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isOpen) 4 9 13
1451 4 ▶ $iopn = $self->{'_writable'} if ( defined $self->{'_writable'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_writable"} 4 0 4
1452 }
1453  
1454 13 return $iopn;
1455 }
1456  
1457 0 ▶sub _isAppendable {
Subroutine _isAppendable: not called CC=3 SCAR=24.8
1458 0 my $self = $_[0];
1459 0 my $iopn = 0;
1460  
1461 0 ▶ if ( $self->_isOpen() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isOpen) 0 0 0
1462 0 ▶ $iopn = $self->{'_appendable'} if ( defined $self->{'_appendable'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_appendable"} 0 0 0
1463 }
1464  
1465 0 return $iopn;
1466 }
1467  
1468 0 ▶sub isBuffered {
Subroutine isBuffered: not called CC=1 SCAR=6.9
Pod undocumented
1469 0 return $_[0]->{'_buffered'};
1470 }
1471  
1472 0 ▶sub isPersistent {
Subroutine isPersistent: not called CC=1 SCAR=6.9
Pod undocumented
1473 0 return $_[0]->{'_persistent'};
1474 }
1475  
1476 3 ▶sub getReportString {
Subroutine getReportString: called CC=1 SCAR=0.0
Pod undocumented
1477 3 return \$_[0]->{'_report'};
1478 }
1479  
1480 6 ▶sub getErrorString {
Subroutine getErrorString: called CC=1 SCAR=0.0
Pod undocumented
1481 6 return \$_[0]->{'_error_message'};
1482 }
1483  
1484 6 ▶sub getErrorCode {
Subroutine getErrorCode: called CC=1 SCAR=0.0
Pod undocumented
1485 6 return $_[0]->{'_error_code'};
1486 }
1487  
1488 2 return 1;
↑ Summary