Lufs/Glue.pm untested

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 134 CC 15 SCAR 34.3
CC
15
Cov
0%
TRACE
72.0
modes
20.0
_truncdata
6.0
CRAP 30.9
1 package Lufs::Glue;
2 $|++;
3  
4 use Fcntl;
5 use Linux::Pid;
6  
7 our $trace = 0;
8  
9 sub TRACE {
10 my $self = shift;
11 my $method = shift;
12 return unless $trace;
13 my (@arg) = @_;
14 my $ret = pop(@arg);
15 if ($method eq 'create') {
16 $arg[1] = $self->mode($arg[1]);
17 }
18 if ($method eq 'stat' or $method eq 'setattr') {
19 $arg[1] = $self->hashdump($arg[1]);
20 }
21 if ($method eq 'write' or $method eq 'read') {
22 $arg[3] = $self->_truncdata($arg[3]);
23 }
24 if ($method eq '_init') {
25 $arg[0] = $self->hashdump($arg[0]);
26 }
27 if ($method eq 'GET' or $method eq 'HEAD') {
28 $ret = pop(@arg).' '.pop(@arg).' '.$self->hashdump($ret);
29 }
30 if ($method eq 'readdir') {
31 $arg[-1] = '['.join(', ', @{$arg[-1]}).']';
32 }
33 $arg[0] = "'$arg[0]'";
34 print STDERR '['.Linux::Pid::getpid()."] $method (".join(', ', @arg).") = $ret\n";
35 }
36  
37 sub _truncdata {
38 my $self = shift;
39 my $data = shift;
40 no warnings;
41 my $s = $data;
42 $s=~s{\n}{\\n}g;
43 $s=~s{([^ -~])}{sprintf"0x%02x",ord($1)}ge;
44 my $pad = (length($s)>32) ? '...' : '';
45 $s="'".substr($s, 0, 32)."$pad'";
46 }
47  
48 sub modes { # this generates the %m hash
49 my $self = shift;
50 my $arg = shift;
51 if ($arg eq 'S') {
52 return (Fcntl::S_IFREG() => 'S_IFREG',
53 Fcntl::S_IFDIR() => 'S_IFDIR',
54 Fcntl::S_IFLNK() => 'S_IFLNK',
55 Fcntl::S_IFCHR() => 'S_IFCHR',
56 Fcntl::S_IFIFO() => 'S_IFIFO',
57 Fcntl::S_IFSOCK() => 'S_IFSOCK');
58 }
59 my %m;
60 for (grep /^O/, keys %{Fcntl::}) {
61 my $v = eval "Fcntl::$_";
62 next unless (int($v) eq $v);
63 $m{$v} = $_ if $v;
64 }
65 %m;
66 }
67  
68 sub mode {
69 my $self = shift;
70 my $mode = shift;
71 my @m;
72 my %m = $self->modes('O');
73 for (keys %m) {
74 if (($mode & $_) == $_) {
75 push @m, $m{$_};
76 }
77 }
78 join(' | ', @m);
79 }
80  
81 sub fmode {
82 my $self = shift;
83 my $mode = shift;
84 my @m;
85 my %m = $self->modes('S');
86 for (keys %m) {
87 if (($mode & $_) == $_) {
88 push @m, $m{$_};
89 }
90 }
91 join(' | ', @m);
92 }
93  
94 sub hashdump {
95 my $self = shift;
96 my $h = shift;
97 '{ '.join(', ',
98 map { "$_->[0] => $_->[1]" }
99 map { $_ eq 'f_mode' ? [$_, $self->fmode($h->{$_})] : [$_, $h->{$_}] }
100 keys %{$h}).'}';
101 }
102 1;
103 __END__