| 1 | use v5.34; | ||
| 2 | use warnings; | ||
| 3 | use Object::Pad 0.73 ':experimental(adjust_params)'; | ||
| 4 | |||
| 5 | class Archive::SCS::Directory 1.12 | ||
| 6 | :isa( Archive::SCS::Mountable ); | ||
| 7 | |||
| 8 | use builtin::compat 'reftype'; | ||
| 9 | use stable 0.031 'isa'; | ||
| 10 | |||
| 11 | use Archive::SCS::CityHash 'cityhash64'; | ||
| 12 | use Archive::SCS::DirIndex; | ||
| 13 | use Carp 'croak'; | ||
| 14 | use List::Util 1.33 'none'; | ||
| 15 | use Path::Tiny 0.062 (); | ||
| 16 | |||
| 17 | our @CARP_NOT = qw( Archive::SCS ); | ||
| 18 | our $follow_symlinks = 1; | ||
| 19 | our @skip = ( qr{^\.git(?:$|/)}, qr{(?:^|/)\.DS_Store$} ); | ||
| 20 | |||
| 21 | field $path :param :reader; | ||
| 22 | field @ignores; | ||
| 23 | |||
| 24 | field %entries; | ||
| 25 | field @dirs; | ||
| 26 | field @files; | ||
| 27 | |||
| 28 | ADJUST :params ( :$ignore = undef ) { | ||
| 29 | $path isa Path::Tiny && $path->exists or croak | ||
| 30 | "Param file must be a valid Path::Tiny object"; | ||
| 31 | |||
| 32 | if (defined $ignore) { | ||
| 33 | reftype $ignore eq 'REGEXP' or croak | ||
| 34 | "Param ignore must be a regular expression"; | ||
| 35 | @ignores = ( $ignore ); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | sub handles_path ($class, $path, $header) { | ||
| 41 | $path->is_dir | ||
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | method mount () { | ||
| 46 | $self->is_mounted and croak sprintf "%s: Already mounted", $path->basename; | ||
| 47 | |||
| 48 | push @dirs, ''; | ||
| 49 | my $index = {}; | ||
| 50 | $path->visit( | ||
| 51 | sub { | ||
| 52 | |||
| 53 | my $real_path = shift; | ||
| 54 | my $scs_path = $real_path->relative($path); | ||
| 55 | none { $scs_path =~ $_ } @skip or return; | ||
| 56 | none { $scs_path =~ $_ } @ignores or return; | ||
| 57 | |||
| 58 | my $dir = $index->{ $scs_path->parent } //= {}; | ||
| 59 | |||
| 60 | if ( $real_path->is_dir ) { | ||
| 61 | push $dir->{dirs}->@*, $scs_path->basename; | ||
| 62 | push @dirs, $scs_path; | ||
| 63 | $index->{ $scs_path } //= {}; | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | push $dir->{files}->@*, $scs_path->basename; | ||
| 67 | push @files, $scs_path; | ||
| 68 | $entries{ cityhash64 $scs_path } = $real_path; | ||
| 69 | } | ||
| 70 | |||
| 71 | }, | ||
| 72 | { | ||
| 73 | recurse => 1, | ||
| 74 | follow_symlinks => $follow_symlinks, | ||
| 75 | }, | ||
| 76 | ); | ||
| 77 | |||
| 78 | $index->{''} = delete $index->{'.'}; | ||
| 79 | $entries{ cityhash64 $_ } = Archive::SCS::DirIndex->new($index->{$_}->%*) | ||
| 80 | for keys $index->%*; | ||
| 81 | |||
| 82 | return $self; | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | method unmount () { | ||
| 87 | undef %entries; | ||
| 88 | undef @dirs; | ||
| 89 | undef @files; | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | method is_mounted () { | ||
| 94 | !! %entries | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | method read_dir_tree (@roots) { | ||
| 99 | # mount() does all the work | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | method ignore ($re) { | ||
| 104 | $self->is_mounted and croak | ||
| 105 | sprintf "%s: Call ignore() while unmounted", $path->basename; | ||
| 106 | |||
| 107 | reftype $re eq 'REGEXP' or croak | ||
| 108 | "ignore() argument must be a regular expression (qr//)"; | ||
| 109 | |||
| 110 | push @ignores, $re; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | method list_dirs () { | ||
| 115 | return @dirs; | ||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | method list_files () { | ||
| 120 | return @files; | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | method entry_meta ($hash) { | ||
| 125 | return { | ||
| 126 | is_dir => $entries{ $hash } isa Archive::SCS::DirIndex, | ||
| 127 | }; | ||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | method read_entry ($hash) { | ||
| 132 | my $entry = $entries{ $hash }; | ||
| 133 | $entry isa Archive::SCS::DirIndex ? $entry : $entry->slurp_raw | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | method entries () { | ||
| 138 | keys %entries | ||
| 139 | } | ||
| 140 | |||
| 141 | 1; | ||
| 142 | |||
| 143 | |||
| 144 | =head1 NAME | ||
| 145 | |||
| 146 | Archive::SCS::Directory - File system directory format handler | ||
| 147 | |||
| 148 | =head1 SYNOPSIS | ||
| 149 | |||
| 150 | my $path = Path::Tiny->new('dir/'); | ||
| 151 | my $dir = Archive::SCS::Directory->new(path => $path)->mount; | ||
| 152 | |||
| 153 | my @contents = sort $dir->list_dirs, $dir->list_files; | ||
| 154 | |||
| 155 | my $hash = Archive::SCS::CityHash::cityhash64('def/city.sii'); | ||
| 156 | my $data = $dir->read_entry($hash); | ||
| 157 | |||
| 158 | =head1 DESCRIPTION | ||
| 159 | |||
| 160 | Represents an SCS archive that's actually a directory in the | ||
| 161 | file system. Useful for partially extracted archive files. | ||
| 162 | |||
| 163 | Symbolic links are supported, as long as they don't form a loop. | ||
| 164 | Changes to the contents of a mounted directory are not observed. | ||
| 165 | The directory tree is only read during mounting. To update the | ||
| 166 | object's view of a directory, C<unmount()>, then C<mount()>. | ||
| 167 | |||
| 168 | Hash values used with this module must be in the internal format | ||
| 169 | (currently, a 16-byte hex scalar in network byte order). | ||
| 170 | |||
| 171 | I<Since version 1.06.> | ||
| 172 | |||
| 173 | =head1 METHODS | ||
| 174 | |||
| 175 | =head2 entries | ||
| 176 | |||
| 177 | $entry_hashes = $dir->entries; | ||
| 178 | |||
| 179 | =head2 ignore | ||
| 180 | |||
| 181 | $dir->ignore($regexp); | ||
| 182 | |||
| 183 | =head2 is_mounted | ||
| 184 | |||
| 185 | $bool = $dir->is_mounted; | ||
| 186 | |||
| 187 | =head2 handles_path | ||
| 188 | |||
| 189 | $bool = Archive::SCS::Directory->handles_path($path_tiny, $header); | ||
| 190 | |||
| 191 | =head2 list_dirs | ||
| 192 | |||
| 193 | @subdirs = $dir->list_dirs; | ||
| 194 | |||
| 195 | =head2 list_files | ||
| 196 | |||
| 197 | @files = $dir->list_files; | ||
| 198 | |||
| 199 | =head2 mount | ||
| 200 | |||
| 201 | $dir = $dir->mount; | ||
| 202 | |||
| 203 | =head2 new | ||
| 204 | |||
| 205 | $dir = Archive::SCS::Directory->new(path => $path_tiny); | ||
| 206 | $dir = Archive::SCS::Directory->new(path => $path_tiny, ignore => qr//); | ||
| 207 | |||
| 208 | =head2 path | ||
| 209 | |||
| 210 | $path_tiny = $dir->path; | ||
| 211 | |||
| 212 | =head2 read_dir_tree | ||
| 213 | |||
| 214 | $dir->read_dir_tree; # no-op | ||
| 215 | |||
| 216 | =head2 read_entry | ||
| 217 | |||
| 218 | $data = $dir->read_entry($hash); | ||
| 219 | |||
| 220 | =head2 unmount | ||
| 221 | |||
| 222 | $dir->unmount; | ||
| 223 | |||
| 224 | =head1 AUTHOR | ||
| 225 | |||
| 226 | L<nautofon|https://github.com/nautofon> | ||
| 227 | |||
| 228 | =head1 COPYRIGHT | ||
| 229 | |||
| 230 | This software is copyright (c) 2026 by nautofon. | ||
| 231 | |||
| 232 | This is free software; you can redistribute it and/or modify it under | ||
| 233 | the same terms as the Perl 5 programming language system itself. |