blib/lib/Archive/SCS/InMemory.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 10 Podpod n/a totaltotal 0.0% 0 / 90 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use v5.34;
2 use warnings;
3 use Object::Pad 0.73;
4  
5 class Archive::SCS::InMemory 1.12
6 :isa( Archive::SCS::Mountable );
7  
8 use stable 0.031 'isa';
9  
10 use Archive::SCS::CityHash 'cityhash64';
11 use Archive::SCS::DirIndex;
12 use Carp 'croak';
13 use File::Temp 0.05 'mktemp';
14 use List::Util 'first';
15 use Path::Tiny 0.017 ();
16  
17 our @CARP_NOT = qw( Archive::SCS );
18  
19 field $is_mounted = 0;
20 field $path :reader = Path::Tiny::path mktemp 'Archive-SCS-InMemory-XXXXXXXX';
21 # Archive::SCS expects that mounts relate to a file path,
22 # so we need to make up a random one.
23  
24 field %entries;
25 field @dirs;
26 field @files;
27  
28  
29 method file () {
30 warnings::warnif deprecated => "file() is deprecated; use path()";
31 $path
32 }
33  
34  
35 method mount () {
36 $is_mounted = 1;
37 return $self;
38 }
39  
40  
41 method unmount () {
42 $is_mounted = 0;
43 }
44  
45  
46 method is_mounted () {
47 !! $is_mounted
48 }
49 # Archive::SCS expects that mounting is stateful.
50  
51  
52 method read_dir_tree (@roots) {
53 my $root = first { exists $entries{ cityhash64 $_ } } @roots;
54 defined $root or return;
55  
56 @dirs = ($root);
57 @files = ();
58 for (my $i = 0; $i < @dirs; ++$i) {
59 my $dir = $dirs[$i];
60  
61 my $entry = $self->read_entry( cityhash64 $dir );
62 $entry isa Archive::SCS::DirIndex or croak
63 sprintf "%s: Directory '%s' not found", __PACKAGE__, $dir;
64  
65 $dir .= '/' if length $dir;
66 push @dirs, map { "$dir$_" } $entry->dirs;
67 push @files, map { "$dir$_" } $entry->files;
68 }
69 }
70  
71  
72 method list_dirs () {
73 return @dirs;
74 }
75  
76  
77 method list_files () {
78 return @files;
79 }
80  
81  
82 method entry_meta ($hash) {
83 return {
84 is_dir => $entries{ $hash } isa Archive::SCS::DirIndex,
85 };
86 }
87  
88  
89 method add_entry ($path, $entry) {
90 if (ref $entry eq 'HASH') {
91 $entry = Archive::SCS::DirIndex->new($entry->%*);
92 }
93 $entries{ cityhash64 $path } = $entry;
94 return $self
95 }
96  
97  
98 method read_entry ($hash) {
99 return $entries{ $hash };
100 }
101  
102  
103 method entries () {
104 keys %entries
105 }
106  
107 1;
108  
109  
110 =head1 NAME
111  
112 Archive::SCS::InMemory - In-memory SCS archive handler
113  
114 =head1 SYNOPSIS
115  
116 my $mem = Archive::SCS::InMemory->new;
117  
118 # Add regular files:
119 $mem->add_entry('manifest.sii' => $manifest);
120 $mem->add_entry('def/env_data.sii' => $env_data);
121  
122 # Add directory listing for the root dir:
123 $mem->add_entry( '' => {
124 dirs => [ 'def' ],
125 files => [ 'manifest.sii' ],
126 });
127  
128 my $scs = Archive::SCS->new;
129 $scs->mount($mem);
130  
131 =head1 DESCRIPTION
132  
133 Represents a virtual SCS archive file entirely contained in memory.
134 Use cases:
135  
136 =over
137  
138 =item *
139  
140 CI testing (this is why it exists, really)
141  
142 =item *
143  
144 Inject missing files or directory listings into an archive
145 environment. Allows adding file names to orphans (if you know
146 their names, that is).
147  
148 =item *
149  
150 Create archive files on disk from scratch. This assumes there is a
151 way to create a new archive file from an L<Archive::SCS> object.
152  
153 =back
154  
155 Hash values used with this module must be in the internal format
156 (currently, a 16-byte hex scalar in network byte order).
157  
158 =head1 METHODS
159  
160 =head2 add_entry
161  
162 # Regular file:
163 $mem = $mem->add_entry($path => $file_contents);
164  
165 # Directory listing:
166 $mem = $mem->add_entry($path => {
167 dirs => [ @subdirs ],
168 files => [ @filenames ],
169 });
170  
171 =head2 entries
172  
173 $entry_hashes = $mem->entries;
174  
175 =head2 is_mounted
176  
177 $bool = $mem->is_mounted;
178  
179 =head2 list_dirs
180  
181 @directories = $mem->list_dirs;
182  
183 =head2 list_files
184  
185 @files = $mem->list_files;
186  
187 =head2 mount
188  
189 $archive = $mem->mount;
190  
191 =head2 new
192  
193 $mem = Archive::SCS::InMemory->new;
194  
195 =head2 read_dir_tree
196  
197 $mem->read_dir_tree;
198  
199 =head2 read_entry
200  
201 $data = $mem->read_entry($hash);
202  
203 =head2 unmount
204  
205 $mem->unmount;
206  
207 =head1 AUTHOR
208  
209 L<nautofon|https://github.com/nautofon>
210  
211 =head1 COPYRIGHT
212  
213 This software is copyright (c) 2026 by nautofon.
214  
215 This is free software; you can redistribute it and/or modify it under
216 the same terms as the Perl 5 programming language system itself.