PSYC/FileShare.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 117 CC 3 SCAR 13.3
CC
3
Cov
0%
set_store_dir
6.0
msg
6.0
BEGIN
2.0
CRAP 3.8
1 package Net::PSYC::FileShare;
2  
3 use base 'Exporter';
4 use Net::PSYC::Tie::AbbrevHash;
5 use Net::PSYC qw(W send_file);
6 use Fcntl qw(:seek :DEFAULT);
7  
8 use strict;
9  
10 # to some extend similar to Net::PSYC::Client. offers simple functions
11 my (%method, @share_dir, @temp_dir, $store_dir);
12 our @EXPORT = qw(set_store_dir msg);
13  
14 sub set_store_dir {
15 my $dir = shift;
16 return 0 unless (-d $dir && -w $dir);
17  
18 $store_dir = $dir;
19 return $dir;
20 }
21  
22 sub msg {
23 my ($source, $mc, $data, $vars) = @_;
24
25 if (exists $method{$mc}) {
26 return $method{$mc}->(@_);
27 }
28 return;
29 }
30  
31 tie %method, 'Net::PSYC::Tie::AbbrevHash';
32  
33 %method = (
34 '_data_file' => sub {
35 my ($source, $mc, $data, $vars) = @_;
36  
37 my $filename = $vars->{'_name_file'};
38 my $hash = $vars->{'_hash'};
39 my $t;
40 my $seek = int($vars->{'_seek_resume'});
41 my $size = int($vars->{'_size_file'});
42 my $range = int($vars->{'_size_resume'});
43  
44 if ($seek + $range > $size) {
45 sendmsg($source, '_error_file_size', 'Something is very wrong about the size of your file.');
46 return 0;
47 }
48  
49 unless ($filename ||= $hash) {
50 sendmsg($source, '_error_file_identification', 'Your fancy file of yours does not have any name!??');
51 return 0;
52 }
53  
54 if (length($data) != ($range || ($size - $seek))) {
55 sendmsg($source, '_error_file_size', 'Something is very wrong about the size of the data you sent.');
56 return 0;
57 }
58  
59 unless ($range || $seek) {
60 while (-e $store_dir.'/'.$filename.$t) {
61 $t++;
62 }
63 $filename = $store_dir.'/'.$filename.$t;
64 $t = O_EXCL;
65 } else {
66 $filename = $store_dir.'/'.$filename;
67 if (-e $filename && -s $filename != $size) {
68 W0('the actual size of %s on your hd does not match the transfered file', $filename);
69 return 0;
70 }
71 $t = 0;
72 }
73
74 # this is blocking and evil. but i dont care for now.
75 # we could use buffered writes.. and still use sysopen. maybe
76 # a very good idea ,)
77 local *FH;
78 sysopen(*FH, $filename, O_WRONLY|O_NOFOLLOW|O_CREAT|$t) or do {
79 W0('Could not open file %s. sysopen said: %s', $filename, $!);
80 return 0;
81 };
82 binmode(*FH);
83  
84 if ($seek) {
85 sysseek(*FH, $seek, SEEK_SET);
86 }
87  
88 syswrite(*FH, $data);
89 close(*FH);
90 },
91  
92 );
93