line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DataStore::CAS::FS::DirCodec; |
2
|
8
|
|
|
8
|
|
6564
|
use 5.008; |
|
8
|
|
|
|
|
30
|
|
|
8
|
|
|
|
|
329
|
|
3
|
8
|
|
|
8
|
|
45
|
use strict; |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
305
|
|
4
|
8
|
|
|
8
|
|
43
|
use warnings; |
|
8
|
|
|
|
|
17
|
|
|
8
|
|
|
|
|
199
|
|
5
|
8
|
|
|
8
|
|
42
|
use Carp; |
|
8
|
|
|
|
|
19
|
|
|
8
|
|
|
|
|
547
|
|
6
|
8
|
|
|
8
|
|
922
|
use Try::Tiny; |
|
8
|
|
|
|
|
1707
|
|
|
8
|
|
|
|
|
9682
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION= '0.010000'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our %_Formats= (); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# ABSTRACT: Abstract base class for directory encoder/decoders |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub load { |
16
|
59
|
|
|
59
|
1
|
42137
|
my $class= shift; |
17
|
59
|
100
|
|
|
|
426
|
my %p= (@_ == 1)? ((ref $_[0] eq 'HASH')? %{$_[0]} : ( file => $_[0] )) : @_; |
|
1
|
50
|
|
|
|
4
|
|
18
|
|
|
|
|
|
|
|
19
|
59
|
50
|
|
|
|
238
|
defined $p{file} or croak "Missing required attribute 'file'"; |
20
|
59
|
50
|
|
|
|
349
|
defined $p{format} or $p{format}= $class->_read_format(\%p); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Once we get the name of the format, we can jump over to the constructor |
23
|
|
|
|
|
|
|
# for the appropriate class |
24
|
59
|
50
|
|
|
|
334
|
my $codec= $_Formats{$p{format}} |
25
|
|
|
|
|
|
|
or croak "Unknown directory format '$p{format}' in ".$p{file}->hash |
26
|
|
|
|
|
|
|
."\n(be sure to load relevant modules)\n"; |
27
|
59
|
|
|
|
|
637
|
return $codec->decode(\%p); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub put { |
32
|
21
|
|
|
21
|
1
|
117122
|
my ($class, $cas, $format, $entries, $metadata)= @_; |
33
|
21
|
50
|
33
|
|
|
186
|
defined $entries and ref $entries eq 'ARRAY' or croak "entries must be an arrayref"; |
34
|
21
|
50
|
|
|
|
97
|
my $codec= $_Formats{$format} |
35
|
|
|
|
|
|
|
or croak "Unknown directory format '$format'" |
36
|
|
|
|
|
|
|
."\n(be sure to load relevant modules)\n"; |
37
|
21
|
|
|
|
|
147
|
my $scalar= $codec->encode($entries, $metadata); |
38
|
21
|
|
|
|
|
171
|
return $cas->put_scalar($scalar); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub decode { |
43
|
0
|
|
|
0
|
1
|
0
|
(shift)->load(@_); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub encode { |
48
|
0
|
|
|
0
|
1
|
0
|
croak "Only implemented in subclasses"; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub register_format { |
53
|
21
|
|
|
21
|
1
|
69
|
my ($class, $format, $codec)= @_; |
54
|
21
|
|
|
|
|
207
|
my $dec= $codec->can('decode'); |
55
|
21
|
50
|
33
|
|
|
217
|
defined $dec && $dec ne \&decode |
56
|
|
|
|
|
|
|
or croak ref($codec)." must implement 'decode'"; |
57
|
21
|
|
|
|
|
114
|
$_Formats{$format}= $codec; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my $_MagicNumber= 'CAS_Dir '; |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
0
|
|
0
|
sub _magic_number { $_MagicNumber } |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub _calc_header_length { |
66
|
60
|
|
|
60
|
|
160
|
my ($class, $format)= @_; |
67
|
|
|
|
|
|
|
# Length of sprintf("CAS_Dir %02X %s\n", length($format), $format) |
68
|
60
|
|
|
|
|
343
|
return length($format)+length($_MagicNumber)+4; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub _read_format { |
73
|
62
|
|
|
62
|
|
121
|
my ($class, $params)= @_; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# The caller is allowed to pre-load the data so that we don't need to read it here. |
76
|
62
|
|
|
|
|
117
|
my $buf= $params->{data}; |
77
|
|
|
|
|
|
|
# If they didn't, we need to load it. |
78
|
62
|
100
|
|
|
|
242
|
if (!defined $params->{data}) { |
79
|
59
|
50
|
|
|
|
3608
|
$params->{handle}= $params->{file}->open |
80
|
|
|
|
|
|
|
unless defined $params->{handle}; |
81
|
59
|
50
|
|
|
|
25009
|
seek($params->{handle}, 0, 0) or croak "seek: $!"; |
82
|
59
|
|
|
|
|
453
|
$class->_readall($params->{handle}, $buf, length($_MagicNumber)+2); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# first 8 bytes are "CAS_Dir " |
86
|
|
|
|
|
|
|
# Next 2 bytes are the length of the format in uppercase ascii hex (limiting format id to 255 characters) |
87
|
62
|
50
|
|
|
|
483
|
substr($buf, 0, length($_MagicNumber)) eq $_MagicNumber |
88
|
|
|
|
|
|
|
or croak "Bad magic number in directory ".$params->{file}->hash; |
89
|
62
|
|
|
|
|
276
|
my $format_len= hex substr($buf, length($_MagicNumber), 2); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# Now we know how many additional bytes we need |
92
|
62
|
100
|
|
|
|
213
|
if (!defined $params->{data}) { |
93
|
59
|
|
|
|
|
558
|
$class->_readall($params->{handle}, $buf, 1+$format_len+1, length($buf)); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# The byte after that is a space character. |
97
|
|
|
|
|
|
|
# The format id string follows, in exactly $format_len bytes |
98
|
|
|
|
|
|
|
# There is a newline (\n) at the end of the format string which is not part of that count. |
99
|
62
|
50
|
33
|
|
|
716
|
substr($buf, length($_MagicNumber)+2, 1) eq ' ' |
100
|
|
|
|
|
|
|
and substr($buf, length($_MagicNumber)+3+$format_len, 1) eq "\n" |
101
|
|
|
|
|
|
|
or croak "Invalid directory encoding in ".$params->{file}->hash; |
102
|
62
|
|
|
|
|
588
|
return substr($buf, length($_MagicNumber)+3, $format_len); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub _readall { |
107
|
148
|
|
100
|
148
|
|
1191
|
my $got= read($_[1], $_[2], $_[3], $_[4]||0); |
108
|
148
|
50
|
33
|
|
|
1350
|
return $got if defined $got and $got == $_[3]; |
109
|
0
|
|
|
|
|
|
my $count= $_[3]; |
110
|
0
|
|
|
|
|
|
while (1) { |
111
|
0
|
0
|
|
|
|
|
if (defined $got) { |
112
|
0
|
0
|
|
|
|
|
croak "unexpected EOF" |
113
|
|
|
|
|
|
|
unless $got > 0; |
114
|
0
|
|
|
|
|
|
$count -= $got; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
else { |
117
|
0
|
0
|
0
|
|
|
|
croak "read: $!" |
118
|
|
|
|
|
|
|
unless $!{EINTR} || $!{EAGAIN}; |
119
|
|
|
|
|
|
|
} |
120
|
0
|
|
|
|
|
|
$got= read($_[1], $_[2], $count, length $_[2]); |
121
|
|
|
|
|
|
|
} |
122
|
0
|
|
|
|
|
|
1; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
1; |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
__END__ |