| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package DataStore::CAS::FS::DirCodec::Minimal; |
|
2
|
5
|
|
|
5
|
|
37663
|
use 5.008001; |
|
|
5
|
|
|
|
|
12
|
|
|
3
|
5
|
|
|
5
|
|
17
|
use strict; |
|
|
5
|
|
|
|
|
5
|
|
|
|
5
|
|
|
|
|
74
|
|
|
4
|
5
|
|
|
5
|
|
16
|
use warnings; |
|
|
5
|
|
|
|
|
5
|
|
|
|
5
|
|
|
|
|
94
|
|
|
5
|
5
|
|
|
5
|
|
13
|
use Try::Tiny; |
|
|
5
|
|
|
|
|
5
|
|
|
|
5
|
|
|
|
|
199
|
|
|
6
|
5
|
|
|
5
|
|
18
|
use Carp; |
|
|
5
|
|
|
|
|
5
|
|
|
|
5
|
|
|
|
|
217
|
|
|
7
|
5
|
|
|
5
|
|
18
|
use JSON 2.53 (); |
|
|
5
|
|
|
|
|
67
|
|
|
|
5
|
|
|
|
|
216
|
|
|
8
|
|
|
|
|
|
|
require DataStore::CAS::FS::InvalidUTF8; |
|
9
|
|
|
|
|
|
|
require DataStore::CAS::FS::Dir; |
|
10
|
|
|
|
|
|
|
*decode_utf8= *DataStore::CAS::FS::InvalidUTF8::decode_utf8; |
|
11
|
|
|
|
|
|
|
|
|
12
|
5
|
|
|
5
|
|
17
|
use parent 'DataStore::CAS::FS::DirCodec'; |
|
|
5
|
|
|
|
|
10
|
|
|
|
5
|
|
|
|
|
19
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION= '0.010000'; |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
__PACKAGE__->register_format('minimal' => __PACKAGE__); |
|
17
|
|
|
|
|
|
|
__PACKAGE__->register_format('' => __PACKAGE__); |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# ABSTRACT: Directory representation with minimal metadata |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our %_TypeToCode= ( file => 'f', dir => 'd', symlink => 'l', chardev => 'c', blockdev => 'b', pipe => 'p', socket => 's', whiteout => 'w' ); |
|
23
|
|
|
|
|
|
|
our %_CodeToType= map { $_TypeToCode{$_} => $_ } keys %_TypeToCode; |
|
24
|
|
|
|
|
|
|
sub encode { |
|
25
|
11
|
|
|
11
|
1
|
3124
|
my ($class, $entry_list, $metadata)= @_; |
|
26
|
|
|
|
|
|
|
my @entries= map { |
|
27
|
11
|
|
|
|
|
17
|
my ($type, $ref, $name)= ref $_ eq 'HASH'? |
|
28
|
|
|
|
|
|
|
( $_->{type}, $_->{ref}, $_->{name} ) |
|
29
|
17
|
50
|
|
|
|
42
|
: ( $_->type, $_->ref, $_->name ); |
|
30
|
|
|
|
|
|
|
|
|
31
|
17
|
100
|
|
|
|
220
|
defined $type |
|
32
|
|
|
|
|
|
|
or croak "'type' attribute is required"; |
|
33
|
16
|
100
|
|
|
|
123
|
my $code= $_TypeToCode{$type} |
|
34
|
|
|
|
|
|
|
or croak "Unknown directory entry type '$type' for entry $_"; |
|
35
|
15
|
100
|
|
|
|
107
|
defined $name |
|
36
|
|
|
|
|
|
|
or croak "'name' attribute is required"; |
|
37
|
14
|
100
|
|
|
|
20
|
_make_utf8($name) |
|
38
|
|
|
|
|
|
|
or croak "'name' must be a unicode scalar or an InvalidUTF8 instance"; |
|
39
|
13
|
100
|
|
|
|
21
|
$ref= '' unless defined $ref; |
|
40
|
13
|
100
|
|
|
|
14
|
_make_utf8($ref) |
|
41
|
|
|
|
|
|
|
or croak "'ref' must be a unicode scalar or an InvalidUTF8 instance"; |
|
42
|
|
|
|
|
|
|
|
|
43
|
12
|
50
|
|
|
|
20
|
croak "'name' too long: '$name'" if 255 < length $name; |
|
44
|
12
|
50
|
|
|
|
17
|
croak "'ref' too long: '$ref'" if 255 < length $ref; |
|
45
|
12
|
|
|
|
|
52
|
pack('CCA', length($name), length($ref), $code).$name."\0".$ref."\0" |
|
46
|
|
|
|
|
|
|
} @$entry_list; |
|
47
|
|
|
|
|
|
|
|
|
48
|
6
|
|
|
|
|
7
|
my $ret= "CAS_Dir 00 \n"; |
|
49
|
6
|
100
|
50
|
|
|
28
|
if ($metadata and scalar keys %$metadata) { |
|
50
|
1
|
|
|
|
|
19
|
my $enc= JSON->new->utf8->canonical->convert_blessed; |
|
51
|
1
|
|
|
|
|
23
|
$ret .= $enc->encode($metadata); |
|
52
|
|
|
|
|
|
|
} |
|
53
|
6
|
|
|
|
|
10
|
$ret .= "\0"; |
|
54
|
6
|
|
|
|
|
12
|
$ret .= join('', sort { substr($a,3) cmp substr($b,3) } @entries ); |
|
|
18
|
|
|
|
|
27
|
|
|
55
|
6
|
50
|
|
|
|
15
|
croak "Accidental unicode concatenation" |
|
56
|
|
|
|
|
|
|
if utf8::is_utf8($ret); |
|
57
|
6
|
|
|
|
|
16
|
$ret; |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# Convert string in-place to utf-8 bytes, or return false. |
|
61
|
|
|
|
|
|
|
# A less speed-obfuscated version might read: |
|
62
|
|
|
|
|
|
|
# my $str= shift; |
|
63
|
|
|
|
|
|
|
# if (ref $str) { |
|
64
|
|
|
|
|
|
|
# return 0 unless ref($str)->can('TO_UTF8'); |
|
65
|
|
|
|
|
|
|
# $_[0]= $str->TO_UTF8; |
|
66
|
|
|
|
|
|
|
# return 1; |
|
67
|
|
|
|
|
|
|
# } elsif (utf8::is_utf8($str)) { |
|
68
|
|
|
|
|
|
|
# utf8::encode($_[0]); |
|
69
|
|
|
|
|
|
|
# return 1; |
|
70
|
|
|
|
|
|
|
# } else { |
|
71
|
|
|
|
|
|
|
# return !($_[0] =~ /[\x7F-\xFF]/); |
|
72
|
|
|
|
|
|
|
# } |
|
73
|
|
|
|
|
|
|
sub _make_utf8 { |
|
74
|
27
|
100
|
33
|
27
|
|
350
|
ref $_[0]? |
|
|
|
|
100
|
|
|
|
|
|
75
|
|
|
|
|
|
|
(ref($_[0])->can('TO_UTF8') && (($_[0]= $_[0]->TO_UTF8) || 1)) |
|
76
|
|
|
|
|
|
|
: &utf8::is_utf8 && (&utf8::encode || 1) || !($_[0] =~ /[\x80-\xFF]/); |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub decode { |
|
81
|
6
|
|
|
6
|
1
|
609
|
my ($class, $params)= @_; |
|
82
|
|
|
|
|
|
|
$params->{format}= $class->_read_format($params) |
|
83
|
6
|
100
|
|
|
|
19
|
unless defined $params->{format}; |
|
84
|
6
|
|
|
|
|
12
|
my $bytes= $params->{data}; |
|
85
|
6
|
|
|
|
|
6
|
my $handle= $params->{handle}; |
|
86
|
|
|
|
|
|
|
# This implementation just processes the file as a whole. |
|
87
|
|
|
|
|
|
|
# Read it in if we don't have it yet. |
|
88
|
6
|
|
|
|
|
27
|
my $header_len= $class->_calc_header_length($params->{format}); |
|
89
|
6
|
100
|
|
|
|
14
|
if (defined $bytes) { |
|
90
|
1
|
|
|
|
|
2
|
substr($bytes, 0, $header_len)= ''; |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
else { |
|
93
|
5
|
50
|
|
|
|
10
|
defined $handle or $handle= $params->{file}->open; |
|
94
|
5
|
50
|
|
|
|
16
|
seek($handle, $header_len, 0) or croak "seek: $!"; |
|
95
|
5
|
|
|
|
|
20
|
local $/= undef; |
|
96
|
5
|
|
|
|
|
19
|
$bytes= <$handle>; |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
|
|
99
|
6
|
|
|
|
|
20
|
my $meta_end= index($bytes, "\0"); |
|
100
|
6
|
50
|
|
|
|
17
|
$meta_end >= 0 or croak "Missing end of metadata"; |
|
101
|
6
|
100
|
|
|
|
16
|
if ($meta_end > 0) { |
|
102
|
1
|
|
|
|
|
7
|
my $enc= JSON->new()->utf8->canonical->convert_blessed; |
|
103
|
1
|
|
|
|
|
4
|
DataStore::CAS::FS::InvalidUTF8->add_json_filter($enc); |
|
104
|
1
|
|
|
|
|
14
|
$params->{metadata}= $enc->decode(substr($bytes, 0, $meta_end)); |
|
105
|
|
|
|
|
|
|
} else { |
|
106
|
5
|
|
|
|
|
9
|
$params->{metadata}= {}; |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
|
|
109
|
6
|
|
|
|
|
12
|
my $pos= $meta_end+1; |
|
110
|
6
|
|
|
|
|
49
|
my @ents; |
|
111
|
6
|
|
|
|
|
21
|
while ($pos < length($bytes)) { |
|
112
|
12
|
|
|
|
|
66
|
my ($nameLen, $refLen, $code)= unpack('CCA', substr($bytes, $pos, 3)); |
|
113
|
12
|
|
|
|
|
34
|
my $end= $pos + 3 + $nameLen + 1 + $refLen + 1; |
|
114
|
12
|
50
|
|
|
|
28
|
($end <= length($bytes)) |
|
115
|
|
|
|
|
|
|
or croak "Unexpected end of file"; |
|
116
|
12
|
|
|
|
|
41
|
my $name= decode_utf8(substr($bytes, $pos+3, $nameLen)); |
|
117
|
12
|
100
|
|
|
|
48
|
my $ref= $refLen? decode_utf8(substr($bytes, $pos+3+$nameLen+1, $refLen)) : undef; |
|
118
|
12
|
|
|
|
|
67
|
push @ents, bless [ $code, $name, $ref ], __PACKAGE__.'::Entry'; |
|
119
|
12
|
|
|
|
|
47
|
$pos= $end; |
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
return DataStore::CAS::FS::Dir->new( |
|
122
|
|
|
|
|
|
|
file => $params->{file}, |
|
123
|
|
|
|
|
|
|
format => 'minimal', # we encode with format string '', but this is what we want the user to see. |
|
124
|
|
|
|
|
|
|
entries => \@ents, |
|
125
|
|
|
|
|
|
|
metadata => $params->{metadata} |
|
126
|
6
|
|
|
|
|
37
|
); |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
package DataStore::CAS::FS::DirCodec::Minimal::Entry; |
|
130
|
5
|
|
|
5
|
|
3529
|
use strict; |
|
|
5
|
|
|
|
|
6
|
|
|
|
5
|
|
|
|
|
107
|
|
|
131
|
5
|
|
|
5
|
|
15
|
use warnings; |
|
|
5
|
|
|
|
|
34
|
|
|
|
5
|
|
|
|
|
125
|
|
|
132
|
5
|
|
|
5
|
|
15
|
use parent 'DataStore::CAS::FS::DirEnt'; |
|
|
5
|
|
|
|
|
4
|
|
|
|
5
|
|
|
|
|
13
|
|
|
133
|
|
|
|
|
|
|
|
|
134
|
12
|
|
|
12
|
|
29
|
sub type { $_CodeToType{$_[0][0]} } |
|
135
|
13
|
|
|
13
|
|
100
|
sub name { $_[0][1] } |
|
136
|
0
|
|
|
0
|
|
0
|
sub ref { $_[0][2] } |
|
137
|
|
|
|
|
|
|
sub as_hash { |
|
138
|
12
|
|
|
12
|
|
15
|
my $self= shift; |
|
139
|
12
|
100
|
50
|
|
|
53
|
return $self->[3] ||= { |
|
140
|
|
|
|
|
|
|
type => $self->type, |
|
141
|
|
|
|
|
|
|
name => $self->name, |
|
142
|
|
|
|
|
|
|
(defined $self->[2]? (ref => $self->[2]) : ()) |
|
143
|
|
|
|
|
|
|
}; |
|
144
|
|
|
|
|
|
|
} |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
1; |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
__END__ |