blib/lib/File/Information/Chunk.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 36 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 134 CC 14 SCAR 38.4
CC
14
Cov
0%
peek
72.0
_new
56.0
BEGIN
2.0
CRAP 46.7
1 # Copyright (c) 2024-2025 Philipp Schafft <lion@cpan.org>
2  
3 # licensed under Artistic License 2.0 (see LICENSE file)
4  
5 # ABSTRACT: generic module for extracting information from filesystems
6  
7  
8 package File::Information::Chunk;
9  
10 use v5.10;
11 use strict;
12 use warnings;
13  
14 use parent 'File::Information::Base';
15  
16 use Carp;
17 use Fcntl qw(SEEK_SET);
18  
19 our $VERSION = v0.16;
20  
21 my %_properties = (
22 chunk_cachehash => {},
23 chunk_subchunks => {rawtype => __PACKAGE__},
24 chunk_outer_type => {},
25 chunk_inner_type => {},
26 chunk_outer_size => {},
27 chunk_inner_size => {},
28 chunk_outer_magic => {},
29 );
30  
31  
32 sub peek {
33 my ($self, %opts) = @_;
34 my $wanted = $opts{wanted} || 0;
35 my $required = $opts{required} || 0;
36 my $buffer;
37  
38 if (defined($self->{_peek_buffer}) && length($self->{_peek_buffer}) >= $required) {
39 return $self->{_peek_buffer};
40 }
41  
42 croak 'Cannot peek as no offset and size is known' unless defined($self->{inner_start}) && defined($self->{inner_size});
43  
44 $wanted = $required if $required > $wanted;
45 $wanted = 4096 if $wanted < 4096; # enforce some minimum
46  
47 croak 'Requested peek too big: '.$wanted if $wanted > 65536;
48  
49 $wanted = $self->{inner_size} if $wanted > $self->{inner_size};
50  
51 {
52 my $fh = $self->{inode}->_get_fh;
53 $fh->seek($self->{inner_start}, SEEK_SET) or die $!;
54 $fh->read($buffer, $wanted);
55 }
56  
57 croak 'Cannot peek required amount of data' if length($buffer) < $required;
58  
59 return $self->{_peek_buffer} = $buffer;
60 }
61 # ----------------
62  
63 sub _new {
64 my ($pkg, %opts) = @_;
65 my $self = $pkg->SUPER::_new(%opts, properties => \%_properties);
66  
67 # Complete...
68 $self->{lifecycle} //= 'current';
69 $self->{end} //= $self->{start} + $self->{size};
70 $self->{size} //= $self->{end} - $self->{start};
71  
72 if (defined($self->{inner_start}) && (defined($self->{inner_end}) || defined($self->{inner_end}))) {
73 $self->{inner_end} //= $self->{inner_start} + $self->{inner_size};
74 $self->{inner_size} //= $self->{inner_end} - $self->{inner_start}
75 }
76  
77 {
78 my $pv = ($self->{properties_values} //= {})->{$self->{lifecycle}} //= {};
79 my $inode = $self->{inode};
80  
81 $pv->{chunk_outer_size} = {raw => $self->{size}};
82 $pv->{chunk_inner_size} = {raw => $self->{inner_size}};
83  
84 $pv->{chunk_subchunks} = [map {{raw => $_}} @{$self->{subchunks}}] if defined $self->{subchunks};
85 $pv->{chunk_inner_type} = $self->{inner_type} if defined $self->{inner_type};
86 $pv->{chunk_outer_type} = $self->{outer_type} if defined $self->{outer_type};
87 $pv->{chunk_outer_magic} = $self->{outer_magic} if defined $self->{outer_magic};
88  
89 if (defined $inode) {
90 eval { $pv->{chunk_cachehash} = {raw => sprintf('%u-%u@%s', $self->{start}, $self->{end}, $inode->get('stat_cachehash'))} };
91 }
92 }
93  
94 return $self;
95 }
96  
97 1;
98  
99 __END__
100  
101 =pod
102  
103 =encoding UTF-8
104  
105 =head1 NAME
106  
107 File::Information::Chunk - generic module for extracting information from filesystems
108  
109 =head1 VERSION
110  
111 version v0.16
112  
113 =head1 SYNOPSIS
114  
115 use File::Information;
116  
117 my File::Information::Inode $inode = ...;
118  
119 my File::Information::Chunk $chunk = $inode->get(...);
120  
121 This package inherits from L<File::Information::VerifyBase>.
122  
123 =head1 METHODS
124  
125 =head2 peek
126  
127 my $data = $chunk->peek( [ %opts ] );
128  
129 Peeks the first few bytes of a chunk's body.
130 This implements the same interface as L<File::Information::Inode/peek>.
131 See there for details.
132  
133 =head1 AUTHOR
134  
135 Philipp Schafft <lion@cpan.org>
136  
137 =head1 COPYRIGHT AND LICENSE
138  
139 This software is Copyright (c) 2024-2025 by Philipp Schafft <lion@cpan.org>.
140  
141 This is free software, licensed under:
142  
143 The Artistic License 2.0 (GPL Compatible)
144  
145 =cut