Slackget/File.pm

filter: Statementstmt 53.8% 84 / 156 Branchbran 44.9% 44 / 98 Conditioncond 37.8% 25 / 66 MC/DCmcdc 8.8% 3 / 34 Subroutinesub 64.0% 16 / 25 Podpod 100% 22 / 22 totaltotal 48.3% 194 / 401 CC 77 SCAR 44.7
CC
77
Cov
48%
Read
226.3
new
85.3
Write
26.0
CRAP 87.4
« Slackget.pm ↑ Summary Slackget/Network/Auth.pm »
1 package Slackware::Slackget::File;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Slackware::Slackget::File - A class to manage files.
9  
10 =head1 VERSION
11  
12 Version 1.0.5
13  
14 =cut
15  
16 5 our $VERSION = '1.0.5';
17  
18 =head1 SYNOPSIS
19  
20 Slackware::Slackget::File is the class which represent a file for slack-get.
21  
22 Access to hard disk are saved by taking a copy of the file in memory, so if you work on big file it may be a bad idea to use this module. Or maybe you have some interest to close the file while you don't work on it.
23  
24 use Slackware::Slackget::File;
25  
26 my $file = Slackware::Slackget::File->new('foo.txt'); # if foo.txt exist the constructor will call the Read() method
27 $file->add("an example\n");
28 $file->Write();
29 $file->Write("bar.txt"); # write foo.txt (plus the addition) into bar.txt
30 $file->Close(); # Free the memory !
31 $file->Read(); # But the Slackware::Slackget::File object is not destroy and you can re-load the file content
32 $file->Read("baz.txt"); # Or changing file (the object will be update with the new file)
33  
34 The main advantage of this module is that you don't work directly on the file but on a copy. So you can make errors, they won't be wrote until you call the Write() method
35  
36 ** ATTENTION ** this module can fail to load file on non-UNIX system because it rely on the "file" and "awk" command line tools. Be sure to use the 'load-raw' => 1 constructor's option on such operating system (most probably the file type will be blank and no problem will happen... but it's still a possibility).
37  
38 ** ATTENTION 2 ** this module rely on bzip2 and gzip command line tools to uncompress the compressed files. On systems which does not support the `gzip -dc` or `bzip2 -dc`, trying to load compressed files will cause crashs, which can eventually lead to the end of the world...
39  
40 =cut
41  
42 sub new
43 6 ▶{
Subroutine new: called CC=29 SCAR=44.5
Pod documented
44 6 my ($class,$file,%args) = @_ ;
45 6 my $self={%args};
46 # print "\nActual file-encoding: $self->{'file-encoding'}\nargs : $args{'file-encoding'}\nFile: $file\n";<STDIN>;
47 6 bless($self,$class);
48 6 ▶ $self->{'file-encoding'} = 'utf8' unless(defined($self->{'file-encoding'}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"file-encoding"} 6 0 6
49 6 ▶ if(defined($file) && -e $file && !defined($args{'load-raw'}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and -e $file and not defined $args{'load-raw'}) { } 2 4 6
Condition83%5/6
defined $file and -e $file and not defined $args{"load-raw"}
!ll&&!rl&&r
222
defined $file and -e $file
!ll&&!rl&&r
024
MC/DC66%2/3
defined $file and -e $file and not defined $args{"load-raw"}
defined $file -e $file not defined $args{"load-raw"}
Truth table75%3/4
A and B and not(C)
A= defined $fileB= -e $fileC= not defined $args{"load-raw"}
ABCresult
0XX0
10X0
1100
1111
50 {
51 2 eval {
52 2 $self->{TYPE} = `LC_ALL=C file -b $file | awk '{print \$1}'`;
53 2 chomp $self->{TYPE};
54 };
55 2 ▶ if($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
56 0 $self->{TYPE} = `LC_ALL=C file $file | awk '{print \$2}'`;
57 0 chomp $self->{TYPE};
58 0 ▶ if($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
59 0 $self->{TYPE} = 'none' ; # Empty the file type if the `file` syscall failed
60 0 $args{'load-raw'}=1; # and set option to load it raw
61 }
62 }
63 2 ▶ $self->{TYPE} = 'none' unless(defined($self->{TYPE}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"TYPE"} 0 2 2
64 2 ▶ $self->{TYPE} = 'ASCII' if($self->{TYPE} eq 'empty');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"TYPE"} eq "empty" 0 2 2
65 2 ▶ $self->{TYPE} = 'ASCII' if($self->{TYPE} eq 'XML' || $self->{TYPE} eq 'Quake');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"TYPE"} eq "XML" or $self->{"TYPE"} eq "Quake" 0 2 2
Condition33%1/3
$self->{"TYPE"} eq "XML" or $self->{"TYPE"} eq "Quake"
l!l&&r!l&&!r
002
MC/DC0%0/2
$self->{"TYPE"} eq "XML" or $self->{"TYPE"} eq "Quake"
$self->{"TYPE"} eq "XML" $self->{"TYPE"} eq "Quake"
Truth table33%1/3
A or B
A= $self->{"TYPE"} eq "XML"B= $self->{"TYPE"} eq "Quake"
ABresult
1X1
011
000
66 2 ▶ die "[Slackware::Slackget::File::constructor] unsupported file type \"$self->{TYPE}\" for file $file. Supported file type are gzip, bzip2, ASCII and XML\n" unless($self->{TYPE} eq 'gzip' || $self->{TYPE} eq 'bzip2' || $self->{TYPE} eq 'ASCII' || $self->{TYPE} eq 'XML' || $self->{TYPE} eq 'none') ;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"TYPE"} eq "gzip" or $self->{"TYPE"} eq "bzip2" or $self->{"TYPE"} eq "ASCII" or $self->{"TYPE"} eq "XML" or $self->{"TYPE"} eq "none" 2 0 2
Condition33%4/12
$self->{"TYPE"} eq "gzip" or $self->{"TYPE"} eq "bzip2" or $self->{"TYPE"} eq "ASCII" or $self->{"TYPE"} eq "XML" or $self->{"TYPE"} eq "none"
l!l&&r!l&&!r
002
$self->{"TYPE"} eq "gzip" or $self->{"TYPE"} eq "bzip2" or $self->{"TYPE"} eq "ASCII" or $self->{"TYPE"} eq "XML"
l!l&&r!l&&!r
002
$self->{"TYPE"} eq "gzip" or $self->{"TYPE"} eq "bzip2" or $self->{"TYPE"} eq "ASCII"
l!l&&r!l&&!r
002
$self->{"TYPE"} eq "gzip" or $self->{"TYPE"} eq "bzip2"
l!l&&r!l&&!r
002
MC/DC0%0/5
$self->{"TYPE"} eq "gzip" or $self->{"TYPE"} eq "bzip2" or $self->{"TYPE"} eq "ASCII" or $self->{"TYPE"} eq "XML" or $self->{"TYPE"} eq "none"
$self->{"TYPE"} eq "gzip" $self->{"TYPE"} eq "bzip2" $self->{"TYPE"} eq "ASCII" $self->{"TYPE"} eq "XML" $self->{"TYPE"} eq "none"
Truth table16%1/6
A or B or C or D or E
A= $self->{"TYPE"} eq "gzip"B= $self->{"TYPE"} eq "bzip2"C= $self->{"TYPE"} eq "ASCII"D= $self->{"TYPE"} eq "XML"E= $self->{"TYPE"} eq "none"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
67 }else{
68 4 $self->{TYPE} = 'ASCII' ;
69 }
70 # print "using $self->{'file-encoding'} as file-encoding for file $file\n";
71 4 $self->{FILENAME} = $file;
72 4 ▶ $self->{MODE} = $args{'mode'} if($args{'mode'} && ($args{'mode'} eq 'write' or $args{'mode'} eq 'append' or $args{'mode'} eq 'rewrite'));
Branch: 1 branch
BranchTrueFalseTotal
if $args{"mode"} and $args{"mode"} eq "write" || $args{"mode"} eq "append" || $args{"mode"} eq "rewrite" 0 4 4
Condition11%1/9
$args{"mode"} and $args{"mode"} eq "write" || $args{"mode"} eq "append" || $args{"mode"} eq "rewrite"
!ll&&!rl&&r
400
$args{"mode"} eq "write" or $args{"mode"} eq "append" or $args{"mode"} eq "rewrite"
l!l&&r!l&&!r
000
$args{"mode"} eq "write" or $args{"mode"} eq "append"
l!l&&r!l&&!r
000
MC/DC0%0/4
$args{"mode"} and $args{"mode"} eq "write" || $args{"mode"} eq "append" || $args{"mode"} eq "rewrite"
$args{"mode"} $args{"mode"} eq "write" $args{"mode"} eq "append" $args{"mode"} eq "rewrite"
Truth table20%1/5
A and B or C or D
A= $args{"mode"}B= $args{"mode"} eq "write"C= $args{"mode"} eq "append"D= $args{"mode"} eq "rewrite"
ABCDresult
0XXX0
10000
11XX1
101X1
10011
73 4 ▶ $self->{MODE} = 'append' if(defined($self->{MODE}) && $self->{MODE} eq 'rewrite');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"MODE"} and $self->{"MODE"} eq "rewrite" 0 4 4
Condition33%1/3
defined $self->{"MODE"} and $self->{"MODE"} eq "rewrite"
!ll&&!rl&&r
400
MC/DC0%0/2
defined $self->{"MODE"} and $self->{"MODE"} eq "rewrite"
defined $self->{"MODE"} $self->{"MODE"} eq "rewrite"
Truth table33%1/3
A and B
A= defined $self->{"MODE"}B= $self->{"MODE"} eq "rewrite"
ABresult
0X0
100
111
74 4 $self->{BINARY} = 0;
75 4 ▶ $self->{BINARY} = $args{'binary'} if($args{'binary'});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"binary"} 0 4 4
76 4 ▶ $self->{SKIP_WL} = $args{'skip-white-line'} if($args{'skip-white-line'});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"skip-white-line"} 0 4 4
77 4 ▶ $self->{SKIP_WL} = $args{'skip-white-lines'} if($args{'skip-white-lines'});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"skip-white-lines"} 0 4 4
78 4 $self->{LOAD_RAW} = 0;
79 4 ▶ $self->{LOAD_RAW} = $args{'load-raw'} if($args{'load-raw'});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"load-raw"} 2 2 4
80 4 ▶ if(defined($file) && -e $file && !defined($self->{'no-auto-load'})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and -e $file and not defined $self->{'no-auto-load'}) { } 2 2 4
Condition66%4/6
defined $file and -e $file and not defined $self->{"no-auto-load"}
!ll&&!rl&&r
202
defined $file and -e $file
!ll&&!rl&&r
022
MC/DC33%1/3
defined $file and -e $file and not defined $self->{"no-auto-load"}
defined $file -e $file not defined $self->{"no-auto-load"}
Truth table50%2/4
A and B and not(C)
A= defined $fileB= -e $fileC= not defined $self->{"no-auto-load"}
ABCresult
0XX0
10X0
1100
1111
81 2 $self->Read();
82 }
83 else
84 {
85 2 $self->{FILE} = [];
86 }
87 4 return $self;
88 }
89  
90 =head1 CONSTRUCTOR
91  
92 =head2 new
93  
94 Take a filename as argument.
95  
96 my $file = Slackware::Slackget::File->new('foo.txt'); # if foo.txt exist the constructor will call the Read() method
97 $file->add("an example\n");
98 $file->Write();
99 $file->Write("bar.txt");
100  
101 This class try to determine the type of the file via the command `file` (so you need `file` in your path). If the type of the file is not in gzip, bzip2, ASCII or XML the constructor die()-ed. You can avoid that, if you need to work with unsupported file, by passing a "load-raw" parameter.
102  
103 Additionnaly you can pass an file encoding (default is utf8). For example as a European I prefer that files are stored and compile in the iso-8859-1 charset so I use the following :
104  
105 my $file = Slackware::Slackget::File->new('foo.txt','file-encoding' => 'iso-8859-1');
106  
107 You can also disabling the auto load of the file by passing a parameter 'no-auto-load' => 1 :
108  
109 my $file = Slackware::Slackget::File->new('foo.txt','file-encoding' => 'iso-8859-1', 'no-auto-load' => 1);
110  
111 You can also pass an argument "mode" which take 'append or 'write' as value :
112  
113 my $file = Slackware::Slackget::File->new('foo.txt','file-encoding' => 'iso-8859-1', 'mode' => 'rewrite');
114  
115 This will decide how to open the file (> or >>). Default is 'write' ('>').
116  
117 Note: for backward compatibility mode => "rewrite" is still accepted as a valid mode. It is an alias for "append"
118  
119 You can also specify if the file must be open as binary or normal text with the "binary" argument. This one is boolean (0 or 1). The default value is 0 :
120  
121 my $file = Slackware::Slackget::File->new('package.tgz','binary' => 1); # In real usage package.tgz will be read UNCOMPRESSED by Read().
122 my $file = Slackware::Slackget::File->new('foo.txt','file-encoding' => 'iso-8859-1', 'mode' => 'rewrite', binary => 0);
123  
124 If you want to load a raw file without uncompressing it you can pass the "load-raw" parameter :
125  
126 my $file = Slackware::Slackget::File->new('package.tgz','binary' => 1, 'load-raw' => 1);
127  
128 =head1 FUNCTIONS
129  
130 =head2 Read
131  
132 Take a filename as argument, and load the file in memory.
133  
134 $file->Read($filename);
135  
136 You can call this method without passing parameters, if you have give a filename to the constructor.
137  
138 $file->Read();
139  
140 This method doesn't return the file, you must call Get_file() to do that.
141  
142 Supported file formats : gzipped, bzipped and ASCII file are natively supported (for compressed formats you need to have gzip and bzip2 installed in your path).
143  
144 If you specify load-raw => 1 to the constructor, read will load in memory a file even if the format is not recognize.
145  
146 =cut
147  
148 sub Read
149 2 ▶{
Subroutine Read: called CC=26 SCAR=54.2
Pod documented
150 2 my ($self,$file)=@_;
151 2 ▶ if($file)
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 0 2 2
152 {
153 0 $self->{FILENAME} = $file ;
154 }
155 else
156 {
157 2 $file = $self->{FILENAME};
158 }
159 2 ▶ unless ( -e $file or -R $file)
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file or -R $file) 0 2 2
Condition33%1/3
-e $file or -R $file
l!l&&r!l&&!r
200
MC/DC0%0/2
-e $file or -R $file
-e $file -R $file
Truth table33%1/3
A or B
A= -e $fileB= -R $file
ABresult
1X1
011
000
160 {
161 0 warn "[Slackware::Slackget::File] unable to read $file : $!\n";
162 0 return undef ;
163 }
164 2 my $tmp;
165 2 my @file = ();
166 2 ▶ if((defined($self->{TYPE}) && ($self->{TYPE} eq 'ASCII' || $self->{TYPE} eq 'XML' || $self->{TYPE} eq 'Quake') ) && !$self->{LOAD_RAW})
Branch: 4 branches
BranchTrueFalseTotal
if (defined $self->{'TYPE'} and $self->{'TYPE'} eq 'ASCII' || $self->{'TYPE'} eq 'XML' || $self->{'TYPE'} eq 'Quake' and not $self->{'LOAD_RAW'}) { } 0 2 2
elsif ($self->{'TYPE'} eq 'bzip2' and not $self->{'LOAD_RAW'}) { } 0 2 2
elsif ($self->{'TYPE'} eq 'gzip' and not $self->{'LOAD_RAW'}) { } 0 2 2
elsif ($self->{'LOAD_RAW'} or $self->{'TYPE'} eq '') { } 2 0 2
Condition33%4/12
defined $self->{"TYPE"} and $self->{"TYPE"} eq "ASCII" || $self->{"TYPE"} eq "XML" || $self->{"TYPE"} eq "Quake" and not $self->{"LOAD_RAW"}
!ll&&!rl&&r
020
defined $self->{"TYPE"} and $self->{"TYPE"} eq "ASCII" || $self->{"TYPE"} eq "XML" || $self->{"TYPE"} eq "Quake"
!ll&&!rl&&r
002
$self->{"TYPE"} eq "ASCII" or $self->{"TYPE"} eq "XML" or $self->{"TYPE"} eq "Quake"
l!l&&r!l&&!r
200
$self->{"TYPE"} eq "ASCII" or $self->{"TYPE"} eq "XML"
l!l&&r!l&&!r
200
MC/DC0%0/5
defined $self->{"TYPE"} and $self->{"TYPE"} eq "ASCII" || $self->{"TYPE"} eq "XML" || $self->{"TYPE"} eq "Quake" and not $self->{"LOAD_RAW"}
defined $self->{"TYPE"} $self->{"TYPE"} eq "ASCII" $self->{"TYPE"} eq "XML" $self->{"TYPE"} eq "Quake" not $self->{"LOAD_RAW"}
Truth table12%1/8
A and B or C or D and not(E)
A= defined $self->{"TYPE"}B= $self->{"TYPE"} eq "ASCII"C= $self->{"TYPE"} eq "XML"D= $self->{"TYPE"} eq "Quake"E= not $self->{"LOAD_RAW"}
ABCDEresult
0XXXX0
1000X0
11XX00
101X00
100100
11XX11
101X11
100111
167 {
168 # print "[DEBUG] [Slackware::Slackget::File] loading $file as 'plain text' file.";
169 0 ▶ if(open (F2,"<:encoding($self->{'file-encoding'})",$file))
Branch: 1 branch
BranchTrueFalseTotal
if (open F2, "<:encoding($self->{'file-encoding'})", $file) { } 0 0 0
170 {
171 0 ▶ binmode(F2) if($self->{'BINARY'}) ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"BINARY"} 0 0 0
172 0 ▶ if($self->{SKIP_WL})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'SKIP_WL'}) { } 0 0 0
173 {
174 0 print "[Slackware::Slackget::File DEBUG] reading and skipping white lines\n";
175 0 ▶ while (defined($tmp=<F2>))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($tmp = readline F2)) 0 0 0
176 {
177 0 ▶ next if($tmp=~ /^\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
if $tmp =~ /^\s*$/ 0 0 0
178 0 push @file,$tmp;
179 }
180 }
181 else
182 {
183 0 ▶ while (defined($tmp=<F2>))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($tmp = readline F2)) 0 0 0
184 {
185 0 push @file,$tmp;
186 }
187 }
188
189 0 close (F2);
190 0 $self->{FILE} = \@file ;
191 0 return 1;
192 }
193 else
194 {
195 0 warn "[Slackware::Slackget::File] cannot open \"$file\" : $!\n";
196 0 return undef;
197 }
198 }
199 ▶ elsif($self->{TYPE} eq 'bzip2' && !$self->{LOAD_RAW})
Condition33%1/3
$self->{"TYPE"} eq "bzip2" and not $self->{"LOAD_RAW"}
!ll&&!rl&&r
200
MC/DC0%0/2
$self->{"TYPE"} eq "bzip2" and not $self->{"LOAD_RAW"}
$self->{"TYPE"} eq "bzip2" not $self->{"LOAD_RAW"}
Truth table33%1/3
A and not(B)
A= $self->{"TYPE"} eq "bzip2"B= not $self->{"LOAD_RAW"}
ABresult
0X0
100
111
200 {
201 # print "[DEBUG] [Slackware::Slackget::File] loading $file as 'bzip2' file.";
202 # my $tmp_file = `bzip2 -dc $file`;
203 0 foreach (split(/\n/,`bzip2 -dc $file`))
204 {
205 0 push @file, "$_\n";
206 }
207 0 $self->{FILE} = \@file ;
208 0 return 1;
209 }
210 ▶ elsif($self->{TYPE} eq 'gzip' && !$self->{LOAD_RAW})
Condition33%1/3
$self->{"TYPE"} eq "gzip" and not $self->{"LOAD_RAW"}
!ll&&!rl&&r
200
MC/DC0%0/2
$self->{"TYPE"} eq "gzip" and not $self->{"LOAD_RAW"}
$self->{"TYPE"} eq "gzip" not $self->{"LOAD_RAW"}
Truth table33%1/3
A and not(B)
A= $self->{"TYPE"} eq "gzip"B= not $self->{"LOAD_RAW"}
ABresult
0X0
100
111
211 {
212 # print "[DEBUG] [Slackware::Slackget::File] loading $file as 'gzip' file.";
213 # my $tmp_file = `gzip -dc $file`;
214 0 foreach (split(/\n/,`gzip -dc $file`))
215 {
216 0 push @file, "$_\n";
217 }
218 0 $self->{FILE} = \@file ;
219 0 return 1;
220 }
221 ▶ elsif($self->{LOAD_RAW} or $self->{TYPE} eq '')
Condition33%1/3
$self->{"LOAD_RAW"} or $self->{"TYPE"} eq ""
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->{"LOAD_RAW"} or $self->{"TYPE"} eq ""
$self->{"LOAD_RAW"} $self->{"TYPE"} eq ""
Truth table33%1/3
A or B
A= $self->{"LOAD_RAW"}B= $self->{"TYPE"} eq ""
ABresult
1X1
011
000
222 {
223 # print "[DEBUG] [Slackware::Slackget::File] loading $file as 'raw' file.";
224 2 ▶ if(open(F2,$file))
Branch: 1 branch
BranchTrueFalseTotal
if (open F2, $file) { } 2 0 2
225 {
226 2 ▶ binmode(F2) if($self->{'BINARY'}) ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"BINARY"} 0 2 2
227 2 ▶ while (defined($tmp=<F2>))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($tmp = readline F2)) 2 2 4
228 {
229 2 push @file,$tmp;
230 }
231 2 close (F2);
232 2 $self->{FILE} = \@file ;
233 2 return 1;
234 }
235 else
236 {
237 0 warn "[Slackware::Slackget::File] cannot (raw) open \"$file\" : $!\n";
238 0 return undef;
239 }
240 }
241 else
242 {
243 0 die "[Slackware::Slackget::File] Read() method cannot load file \"$file\" in memory : \"$self->{TYPE}\" is an unsupported format.\n";
244 }
245  
246 }
247  
248 =head2 Lock_file (deprecated)
249  
250 Same as lock_file, provided for backward compatibility.
251  
252 =cut
253  
254 1 ▶sub Lock_file {
Subroutine Lock_file: called CC=1 SCAR=0.0
Pod documented
255 1 return lock_file(@_);
256 }
257  
258 =head2 lock_file
259  
260 This method lock the file for slack-get application (not really for others...) by creating a file with the name of the current open file plus a ".lock". This is not a protection but an information system for slack-getd sub process. This method return undef if the lock can't be made.
261  
262 my $file = new Slackware::Slackget::File ('test.txt');
263 $file->lock_file ; # create a file test.txt.lock
264  
265 ATTENTION: You can only lock the current file of the object. With the previous example you can't do :
266  
267 $file->Lock_file('toto.txt') ;
268  
269 ATTENTION 2 : Don't forget to unlock your locked file :)
270  
271 =cut
272  
273 sub lock_file
274 2 ▶{
Subroutine lock_file: called CC=3 SCAR=11.0
Pod documented
275 2 my $self = shift;
276 2 ▶ return undef if $self->is_locked ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_locked 1 1 2
277 # print "\t[DEBUG] ( Slackware::Slackget::File in Lock_file() ) locking file $self->{FILENAME} for $self\n";
278 1 ▶ Write({'file-encoding'=>$self->{'file-encoding'}},"$self->{FILENAME}.lock",$self) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
Write({"file-encoding", $self->{"file-encoding"}}, qq[$self->{"FILENAME"}.lock], $self) or return undef 1 0 1
279 1 return 1;
280 }
281  
282 =head2 Unlock_file (deprecated)
283  
284 Same as unlock_file(), provided for backward compatibility.
285  
286 =cut
287  
288 1 ▶sub Unlock_file {
Subroutine Unlock_file: called CC=1 SCAR=0.0
Pod documented
289 1 return unlock_file(@_);
290 }
291  
292 =head2 unlock_file
293  
294 Unlock a locked file. Only the locker object can unlock a file ! Return 1 if all goes well, else return undef. Return 2 if the file was not locked. Return 0 (false in scalar context) if the file was locked but by another Slackware::Slackget::File object.
295  
296 my $status = $file->unlock_file ;
297  
298 Returned value are :
299  
300 0 : error -> the file was locked by another instance of this class
301
302 1 : ok lock removed
303
304 2 : the file was not locked
305
306 undef : unable to remove the lock.
307  
308 =cut
309  
310 sub unlock_file
311 2 ▶{
Subroutine unlock_file: called CC=4 SCAR=22.3
Pod documented
312 2 my $self = shift;
313 2 ▶ if($self->is_locked)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_locked) { } 2 0 2
314 {
315 2 ▶ if($self->_verify_lock_maker)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_verify_lock_maker) { } 0 0 0
316 {
317 0 ▶ unlink "$self->{FILENAME}.lock" or return undef ;
Branch: 1 branch
BranchTrueFalseTotal
unlink qq[$self->{"FILENAME"}.lock] or return undef 0 0 0
318 }
319 else
320 {
321 0 return 0;
322 }
323 }
324 else
325 {
326 # print "\t[DEBUG] ( Slackware::Slackget::File in Unlock_file() ) $self->{FILENAME} is not lock\n";
327 0 return 2;
328 }
329 0 return 1;
330 }
331  
332 sub _verify_lock_maker
333 2 ▶{
Subroutine _verify_lock_maker: called CC=2 SCAR=14.3
334 2 my $self = shift;
335 2 my $file = new Slackware::Slackget::File ("$self->{FILENAME}.lock");
336 0 my $locker = $file->get_line(0) ;
337 # print "\t[DEBUG] ( Slackware::Slackget::File in _verify_lock_maker() ) locker of file \"$self->{FILENAME}\" is $locker and current object is $self\n";
338 0 $file->Close ;
339 0 undef($file);
340 0 my $object = ''.$self;
341 # print "[debug file] compare object=$object and locker=$locker\n";
342 0 ▶ if($locker eq $object)
Branch: 1 branch
BranchTrueFalseTotal
if ($locker eq $object) { } 0 0 0
343 {
344 # print "\t[DEBUG] ( Slackware::Slackget::File in _verify_lock_maker() ) locker access granted for file \"$self->{FILENAME}\"\n";
345 0 return 1;
346 }
347 else
348 {
349 # print "\t[DEBUG] ( Slackware::Slackget::File in _verify_lock_maker() ) locker access ungranted for file \"$self->{FILENAME}\"\n";
350 0 return undef;
351 }
352 }
353  
354 =head2 is_locked
355  
356 Return 1 if the file is locked by a Slackware::Slackget::File object, else return undef.
357  
358 print "File is locked\n" if($file->is_locked);
359  
360 =cut
361  
362 sub is_locked
363 4 ▶{
Subroutine is_locked: called CC=2 SCAR=6.9
Pod documented
364 4 my $self = shift;
365 4 ▶ return 1 if(-e $self->{FILENAME}.".lock");
Branch: 1 branch
BranchTrueFalseTotal
if -e $self->{"FILENAME"} . '.lock' 3 1 4
366 1 return undef;
367 }
368  
369 =head2 Write
370  
371 Take a filename to write data and raw data
372  
373 $file->Write($filename,@data);
374  
375 You can call this method with just a filename (in this case the file currently loaded will be wrote in the file you specify)
376  
377 $file->Write($another_filename) ; # Write the currently loaded file into $another_filename
378  
379 You also can call this method without any parameter :
380  
381 $file->Write ;
382  
383 In this case, the Write() method will wrote data in memory into the last opened file (with Read() or new()).
384  
385 The default encoding of this method is utf-8, pass an extra argument : file-encoding to the constructor to change that.
386  
387 =cut
388  
389 sub Write
390 3 ▶{
Subroutine Write: called CC=13 SCAR=32.6
Pod documented
391 3 my ($self,$name,@data)=@_;
392 3 ▶ $name=$self->{FILENAME} unless($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $name 2 1 3
393 3 ▶ @data = @{$self->{FILE}} unless(@data);
Branch: 1 branch
BranchTrueFalseTotal
unless @data 2 1 3
394 # if(open (FILE, ">$name"))
395 # print "using $self->{'file-encoding'} as file-encoding for writing\n";
396 3 my $mode = '>';
397 3 ▶ if(defined($self->{MODE}) && $self->{MODE} eq 'append')
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"MODE"} and $self->{"MODE"} eq "append") 0 3 3
Condition33%1/3
defined $self->{"MODE"} and $self->{"MODE"} eq "append"
!ll&&!rl&&r
300
MC/DC0%0/2
defined $self->{"MODE"} and $self->{"MODE"} eq "append"
defined $self->{"MODE"} $self->{"MODE"} eq "append"
Truth table33%1/3
A and B
A= defined $self->{"MODE"}B= $self->{"MODE"} eq "append"
ABresult
0X0
100
111
398 {
399 0 $mode = '>>';
400 }
401 3 ▶ if(open (FILE, "$mode:encoding($self->{'file-encoding'})",$name))
Branch: 1 branch
BranchTrueFalseTotal
if (open FILE, "${mode}:encoding($self->{'file-encoding'})", $name) { } 3 0 3
402 {
403 3 ▶ binmode(FILE) if($self->{'BINARY'}) ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"BINARY"} 0 3 3
404 # NOTE: In the case you need to clear the white line of your file, their will be a if() test for each array slot
405 # This is really time consumming, so id you don't need this feature we just test once for all and gain a lot in performance.
406 3 ▶ if($self->{SKIP_WL})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'SKIP_WL'}) { } 0 3 3
407 {
408 0 print "[Slackware::Slackget::File DEBUG] mode 'skip-white-line' activate\n";
409 0 foreach (@data)
410 {
411 0 foreach my $tmp (split(/\n/,$_))
412 {
413 0 ▶ next if($tmp =~ /^\s*$/) ;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp =~ /^\s*$/ 0 0 0
414 0 print FILE "$tmp\n" ;
415 }
416 }
417 }
418 else
419 {
420 3 foreach (@data)
421 {
422 3 print FILE $_;
423 }
424 }
425 3 ▶ close (FILE) or return(undef);
Branch: 1 branch
BranchTrueFalseTotal
close FILE or return undef 3 0 3
426 }
427 else
428 {
429 0 warn "[ Slackware::Slackget::File ] unable to write '$name' : $!\n";
430 0 return undef;
431 }
432 3 return 1;
433 }
434  
435 =head2 Add (deprecated)
436  
437 Same as add(), provided for backward compatibility.
438  
439 =cut
440  
441 1 ▶sub Add {
Subroutine Add: called CC=1 SCAR=0.0
Pod documented
442 1 return add(@_);
443 }
444  
445 =head2 add
446  
447 Take a table of lines and add them to the end of file image (in memory). You need to commit your change by calling the Write() method !
448  
449 $file->add(@data);
450 or
451 $file->add($data);
452 or
453 $file->add("this is some data\n");
454  
455 =cut
456  
457 2 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
458 2 my ($self,@data) = @_;
459 2 $self->{FILE} = [@{$self->{FILE}},@data];
460 }
461  
462 =head2 Get_file (deprecated)
463  
464 Same as get_file(), provided for backward compatibility.
465  
466 =cut
467  
468 0 ▶sub Get_file {
Subroutine Get_file: not called CC=1 SCAR=6.9
Pod documented
469 0 return get_file(@_);
470 }
471  
472 =head2 get_file
473  
474 Return the current file in memory as an array.
475  
476 @file = $file->get_file();
477  
478 =cut
479  
480 0 ▶sub get_file{
Subroutine get_file: not called CC=1 SCAR=6.9
Pod documented
481 0 my $self = shift;
482 0 return @{$self->{FILE}};
483 }
484  
485  
486 =head2 Get_line (deprecated)
487  
488 Same as get_line(), provided for backward compatibility.
489  
490 =cut
491  
492 3 ▶sub Get_line {
Subroutine Get_line: called CC=1 SCAR=0.0
Pod documented
493 3 return get_line(@_);
494 }
495  
496  
497 =head2 get_line
498  
499 Return the $index line of the file (the index start at 0).
500  
501 @file = $file->get_line($index);
502  
503 =cut
504  
505 6 ▶sub get_line {
Subroutine get_line: called CC=1 SCAR=0.0
Pod documented
506 6 my ($self,$index) = @_;
507 6 return $self->{FILE}->[$index];
508 }
509  
510 =head2 Get_selection (deprecated)
511  
512 Same as get_selection(), provided for backward compatibility.
513  
514 =cut
515  
516 0 ▶sub Get_selection {
Subroutine Get_selection: not called CC=1 SCAR=6.9
Pod documented
517 0 return get_selection(@_);
518 }
519  
520 =head2 get_selection
521  
522 Same as get file but return only lines between $start and $stop.
523  
524 my @array = $file->get_selection($start,$stop);
525  
526 You can ommit the $stop parameter (in this case Get_line() return the lines from $start to the end of file)
527  
528 =cut
529  
530 0 ▶sub get_selection {
Subroutine get_selection: not called CC=3 SCAR=24.8
Pod documented
531 0 my ($self,$start,$stop) = @_ ;
532 0 ▶ $start = 0 unless($start);
Branch: 1 branch
BranchTrueFalseTotal
unless $start 0 0 0
533 0 ▶ $stop = $#{$self->{FILE}} unless($stop);
Branch: 1 branch
BranchTrueFalseTotal
unless $stop 0 0 0
534 0 return @{$self->{FILE}}[$start..$stop];
535 }
536  
537  
538 =head2 Close
539  
540 Free the memory. This method close the current file memory image. If you don't call the Write() method before closing, the changes you have made on the file are lost !
541  
542 $file->Close();
543  
544 =cut
545  
546 0 ▶sub Close {
Subroutine Close: not called CC=1 SCAR=6.9
Pod documented
547 0 my $self = shift;
548 0 $self->{FILE} = [];
549 0 return 1;
550 }
551  
552  
553 =head2 Write_and_close (deprecated)
554  
555 Same as write_and_close(), provided for backward compatibility.
556  
557 =cut
558  
559 0 ▶sub Write_and_close {
Subroutine Write_and_close: not called CC=1 SCAR=6.9
Pod documented
560 0 return write_and_close(@_);
561 }
562  
563 =head2 write_and_close
564  
565 An alias which call Write() and then Close();
566  
567 $file->write_and_close();
568 or
569 $file->write_and_close("foo.txt");
570  
571 =cut
572  
573 0 ▶sub write_and_close{
Subroutine write_and_close: not called CC=1 SCAR=6.9
Pod documented
574 0 my ($self,$file) = @_;
575 0 $self->Write($file);
576 0 $self->Close();
577 }
578  
579 =head2 encoding
580  
581 Without parameter return the current file encoding, with a parameter set the encoding for the current file.
582  
583 print "The current file encoding is ",$file->encoding,"\n"; # return the current encoding
584 $file->encoding('utf8'); # set the current file encoding to utf8
585  
586 =cut
587  
588 sub encoding
589 0 ▶{
Subroutine encoding: not called CC=2 SCAR=17.9
Pod documented
590 0 ▶ return $_[1] ? $_[0]->{'file-encoding'}=$_[1] : $_[0]->{'file-encoding'};
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
591 }
592  
593 =head2 filename
594  
595 Return the filename of the file which is currently process by the Slackware::Slackget::File instance.
596  
597 print $file->filename
598  
599 You can also set the filename :
600  
601 $file->filename('foo.txt');
602  
603 =cut
604  
605 sub filename
606 4 ▶{
Subroutine filename: called CC=2 SCAR=6.9
Pod documented
607 4 ▶ return $_[1] ? $_[0]->{FILENAME}=$_[1] : $_[0]->{FILENAME};
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 2 2 4
608 }
609  
610 =head2 type (read only)
611  
612 Return the current file type.
613  
614 print $file->type
615  
616 =cut
617  
618 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
619 0 return $_[0]->{TYPE};
620 }
621  
622 =head1 AUTHOR
623  
624 DUPUIS Arnaud, C<< <a.dupuis@infinityperl.org> >>
625  
626 =head1 BUGS
627  
628 Please report any bugs or feature requests to
629 C<bug-Slackware-Slackget@rt.cpan.org>, or through the web interface at
630 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Slackware-Slackget>.
631 I will be notified, and then you'll automatically be notified of progress on
632 your bug as I make changes.
633  
634 =head1 SUPPORT
635  
636 You can find documentation for this module with the perldoc command.
637  
638 perldoc Slackware::Slackget::File
639  
640  
641 You can also look for information at:
642  
643 =over 4
644  
645 =item * Infinity Perl website
646  
647 L<http://www.infinityperl.org/category/slack-get>
648  
649 =item * slack-get specific website
650  
651 L<http://slackget.infinityperl.org>
652  
653 =item * RT: CPAN's request tracker
654  
655 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Slackware-Slackget>
656  
657 =item * AnnoCPAN: Annotated CPAN documentation
658  
659 L<http://annocpan.org/dist/Slackware-Slackget>
660  
661 =item * CPAN Ratings
662  
663 L<http://cpanratings.perl.org/d/Slackware-Slackget>
664  
665 =item * Search CPAN
666  
667 L<http://search.cpan.org/dist/Slackware-Slackget>
668  
669 =back
670  
671  
672 =head1 ACKNOWLEDGEMENTS
673  
674 Thanks to Bertrand Dupuis (yes my brother) for his contribution to the documentation.
675  
676 =head1 COPYRIGHT & LICENSE
677  
678 Copyright 2005 DUPUIS Arnaud, All Rights Reserved.
679  
680 This program is free software; you can redistribute it and/or modify it
681 under the same terms as Perl itself.
682  
683 =cut
684  
685 5 1; # End of Slackware::Slackget::File
« Slackget.pm ↑ Summary Slackget/Network/Auth.pm »