blib/lib/Net/BitTorrent/File.pm

filter: Statementstmt 100% 95 / 95 Branchbran 95.4% 21 / 22 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 21 / 21 Podpod 100% 14 / 14 totaltotal 96.8% 152 / 157 CC 13 SCAR 5.9
CC
13
Cov
98%
new
3.1
gen_pieces_array
2.0
name
2.0
CRAP 1.8
↑ Summary
1  
2 package Net::BitTorrent::File;
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Convert::Bencode qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Digest::SHA1 qw(sha1);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶ use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 $VERSION = 1.02;
12 3 @ISA = qw (Exporter);
13 #Give a hoot don't pollute, do not export more than needed by default
14 3 @EXPORT = qw ();
15 3 @EXPORT_OK = qw ();
16 3 %EXPORT_TAGS = ();
17 }
18  
19 =head1 NAME
20  
21 Net::BitTorrent::File - Object for manipulating .torrent files
22  
23 =head1 SYNOPSIS
24  
25 use Net::BitTorrent::File
26  
27 # Empty N::BT::File object, ready to be filled with info
28 my $torrent = new Net::BitTorrent::File;
29  
30 # Or, create one from a existing .torrent file
31 my $fromfile = new Net::BitTorrent::File ('somefile.torrent');
32  
33 $torrent->name('Some_File_to_distribute.tar.gz');
34 $torrent->announce('http://address.of.tracker:6695');
35 # etc.
36  
37 print $torrent->name()."\n";
38 # would print "Some_File_to_distribute.tar.gz" in this case.
39  
40 =head1 DESCRIPTION
41  
42 This module handles loading and saveing of .torrent files as well as
43 providing a convenient way to store torrent file info in memory.
44 Most users of the module will most likely just call the new method
45 with the name of a existing torrent file and use the data from that.
46  
47 =head1 USAGE
48  
49 The same method is used for setting and retrieving a value, and the
50 methods have the same name as the key in the torrent file, such as C<name()>,
51 and C<announce()>. If the method is called with no arguments or a undefined
52 value, then the current value is returned, otherwise its set to the value
53 passed in.
54  
55 There are two methods for generating info based on torrent data, but not
56 stored within the torrent itself. These are C<gen_info_hash()> and C<gen_pieces_array()>.
57 You can use the methods C<info_hash()> and C<pieces_array()> to return the calculated
58 values after calling there respective C<gen_X()> methods.
59  
60 C<info_hash()> returns the SHA1 hash of the info portion of the torrent which is
61 used in the bittorrent protocol.
62  
63 C<pieces_array()> returns a array ref of the pieces field of the torrent split
64 into the individual 20 byte SHA1 hashes. For further details on what exactly
65 these are used for, see the docs for the bittorrent protocol mentioned in
66 the SEE ALSO section.
67  
68 =head2 Methods
69  
70 =over 4
71  
72 =item * new( [$filename] )
73  
74 Creates a new Net::BitTorrent::File object, and if a filename is
75 supplied will call the load method with that filename.
76  
77 =item * load( $filename )
78  
79 Loads the file passed into it and generates the C<info_hash> and C<pieces_array>
80 propertys.
81  
82 =item * save( $filename )
83  
84 Saves the torrent to I<$filename>. Note that C<info_hash> and C<pieces_array> are
85 not saved to the torrent file and must be regenerated when the torrent is
86 loaded (but the C<load()> method does this for you anyway).
87  
88 =item * info_hash( [$new_value] )
89  
90 When called with no arguments returns the I<info_hash> value, otherwise it sets
91 it to the value in I<$new_value>. Note: Its very unlikely anyone will be using
92 to set the value of I<info_hash>, rather you should populate all the info
93 fields then call C<gen_info_hash()> to set this property.
94  
95 =item * gen_info_hash( )
96  
97 Calculates the SHA1 hash of the torrents I<info> field and stores this in the
98 I<info_hash> property which can be retrieved using the C<info_hash()> method.
99  
100 =item * pieces_array( [$new_array] )
101  
102 When called with no arguments returns a array ref whose values are the
103 SHA1 hashes contained in the I<pieces> property. To set this value, do not use
104 this method, rather use the C<gen_pieces_array()> method, after setting the
105 I<pieces> property.
106  
107 =item * gen_pieces_array( )
108  
109 Divides the I<pieces> property into its component 20 byte SHA1 hashes, and
110 stores them as a array ref in the I<pieces_array> property.
111  
112 =item * name( [$value] )
113  
114 When called with no arguments returns the I<name> propertys current value, else
115 it sets it to I<$value>. If this value is changed, the I<info_hash> property needs
116 to be regenerated.
117  
118 =item * announce( [$value] )
119  
120 When called with no arguments returns the I<announce> propertys current value, else
121 it sets it to I<$value>.
122  
123 =item * piece_length( [$value] )
124  
125 When called with no arguments returns the I<piece_length> propertys current value, else
126 it sets it to I<$value>. If this value is changed, the I<info_hash> property needs
127 to be regenerated.
128  
129 =item * length( [$value] )
130  
131 When called with no arguments returns the I<length> propertys current value, else
132 it sets it to I<$value>. If this value is changed, the I<info_hash> property needs
133 to be regenerated.
134  
135 =item * pieces( [$value] )
136  
137 When called with no arguments returns the I<pieces> propertys current value, else
138 it sets it to I<$value>. If this value is changed, the I<info_hash> and I<pieces_array>
139 propertys need to be regenerated.
140  
141 =item * files( [$value] )
142  
143 When called with no arguments returns the I<files> propertys current value, else
144 it sets it to I<$value>. I<$value> should be a array ref filled with hash refs
145 containing the keys I<path> and I<length>. If this value is changed, the I<info_hash>
146 property needs to be regenerated.
147  
148 =item * info( [$value] )
149  
150 When called with no arguments returns the I<info> propertys current value, else
151 it sets it to I<$value>. I<$value> should be a hash ref containing the keys
152 I<files>, I<pieces>, I<length>, I<piece_length>, and I<name>. If this value is changed, the
153 I<info_hash> property needs to be regenerated.
154  
155 =back
156  
157 =head1 BUGS
158  
159 None that I know of yet.
160  
161 =head1 SUPPORT
162  
163 Any bugs/suggestions/problems, feel free to send me a e-mail, I'm usually
164 glad to help, and enjoy hearing from people using my code. My e-mail is
165 listed in the AUTHOR section.
166  
167 =head1 AUTHOR
168  
169 R. Kyle Murphy
170 orclev@rejectedmaterial.com
171  
172 =head1 COPYRIGHT
173  
174 This program is free software; you can redistribute
175 it and/or modify it under the same terms as Perl itself.
176  
177 The full text of the license can be found in the
178 LICENSE file included with this module.
179  
180  
181 =head1 SEE ALSO
182  
183 L<Convert::Bencode>, http://bitconjurer.org/BitTorrent/protocol.html
184  
185 =cut
186  
187 sub new
188 4 ▶{
Subroutine new: called CC=3 SCAR=11.2
Pod documented
189 4 my ($class, $file) = @_;
190  
191 4 ▶ my $self = bless ({}, ref ($class) || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
192  
193 4 ▶ if(defined($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 2 2 4
194 2 $self->load($file);
195 }
196  
197 4 return ($self);
198 }
199  
200 2 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
201 2 my $self = shift;
202 2 my $name = shift;
203 2 ▶ if(defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 1 1 2
204 1 $self->{'data'}->{'info'}->{'name'} = $name;
205 }
206 2 return $self->{'data'}->{'info'}->{'name'};
207 }
208  
209 2 ▶sub announce {
Subroutine announce: called CC=2 SCAR=6.9
Pod documented
210 2 my $self = shift;
211 2 my $announce = shift;
212 2 ▶ if(defined($announce)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $announce) 1 1 2
213 1 $self->{'data'}->{'announce'} = $announce;
214 }
215 2 return $self->{'data'}->{'announce'};
216 }
217  
218 2 ▶sub piece_length {
Subroutine piece_length: called CC=2 SCAR=6.9
Pod documented
219 2 my $self = shift;
220 2 my $len = shift;
221 2 ▶ if(defined($len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $len) 1 1 2
222 1 $self->{'data'}->{'info'}->{'piece_length'} = $len;
223 }
224 2 return $self->{'data'}->{'info'}->{'piece_length'};
225 }
226  
227 2 ▶sub length {
Subroutine length: called CC=2 SCAR=6.9
Pod documented
228 2 my $self = shift;
229 2 my $len = shift;
230 2 ▶ if(defined($len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $len) 1 1 2
231 1 $self->{'data'}->{'info'}->{'length'} = $len;
232 }
233 2 return $self->{'data'}->{'info'}->{'length'};
234 }
235  
236 8 ▶sub pieces {
Subroutine pieces: called CC=2 SCAR=6.9
Pod documented
237 8 my $self = shift;
238 8 my $pieces = shift;
239 8 ▶ if(defined($pieces)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pieces) 1 7 8
240 1 $self->{'data'}->{'info'}->{'pieces'} = $pieces;
241 }
242 8 return $self->{'data'}->{'info'}->{'pieces'};
243 }
244  
245 6 ▶sub pieces_array {
Subroutine pieces_array: called CC=2 SCAR=6.9
Pod documented
246 6 my $self = shift;
247 6 my $array = shift;
248 6 ▶ if(defined($array)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $array) 3 3 6
249 3 $self->{'pieces_array'} = $array;
250 }
251 6 return $self->{'pieces_array'};
252 }
253  
254 3 ▶sub gen_pieces_array {
Subroutine gen_pieces_array: called CC=2 SCAR=7.0
Pod documented
255 3 my $self = shift;
256
257 3 ▶ if(defined($self->pieces())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->pieces) 3 0 3
258 3 my @pieces = $self->pieces() =~ /.{20}/sg;
259 3 $self->pieces_array(\@pieces);
260 }
261 }
262  
263 2 ▶sub files {
Subroutine files: called CC=2 SCAR=6.9
Pod documented
264 2 my $self = shift;
265 2 my $files = shift;
266 2 ▶ if(defined($files)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $files) 1 1 2
267 1 $self->{'data'}->{'info'}->{'files'} = $files;
268 }
269 2 return $self->{'data'}->{'info'}->{'files'};
270 }
271  
272 7 ▶sub info {
Subroutine info: called CC=2 SCAR=6.9
Pod documented
273 7 my $self = shift;
274 7 my $info = shift;
275 7 ▶ if(defined($info)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info) 1 6 7
276 1 $self->{'data'}->{'info'} = $info;
277 }
278 7 return $self->{'data'}->{'info'};
279 }
280  
281 6 ▶sub info_hash {
Subroutine info_hash: called CC=2 SCAR=6.9
Pod documented
282 6 my $self = shift;
283 6 my $hash = shift;
284 6 ▶ if(defined($hash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash) 3 3 6
285 3 $self->{'info_hash'} = $hash;
286 }
287 6 return $self->{'info_hash'};
288 }
289  
290 3 ▶sub gen_info_hash {
Subroutine gen_info_hash: called CC=1 SCAR=0.0
Pod documented
291 3 my ($self) = shift;
292 3 $self->info_hash(sha1(bencode($self->info())));
293 }
294  
295 2 ▶sub load {
Subroutine load: called CC=1 SCAR=0.0
Pod documented
296 2 my ($self, $file) = @_;
297 2 my $buff = '';
298  
299 2 open(FILE, '< '.$file);
300 2 local $/;
301 2 $buff = <FILE>;
302 2 close(FILE);
303 2 my $root = bdecode($buff);
304 2 $self->{'data'} = $root;
305 2 $self->gen_info_hash;
306 2 $self->gen_pieces_array;
307 }
308  
309 1 ▶sub save {
Subroutine save: called CC=1 SCAR=0.0
Pod documented
310 1 my ($self, $file) = @_;
311  
312 1 my $data = bencode($self->{'data'});
313 1 open(FILE, '> '.$file);
314 1 print FILE $data;
315 1 close(FILE);
316 }
317  
318 3 1;
319 __END__
↑ Summary