blib/lib/Net/BitTorrent/Storage/File.pm untested

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 196 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use v5.40;
2 use feature 'class';
3 no warnings 'experimental::class';
4 use Net::BitTorrent::Emitter;
5 class Net::BitTorrent::Storage::File v2.1.0 : isa(Net::BitTorrent::Emitter) {
6 use Digest::Merkle::SHA256;
7 use Path::Tiny qw();
8 use Digest::SHA qw[sha256];
9 field $file_path : param(path) : reader(path);
10 field $size : param : reader;
11 field $pieces_root : param : reader = undef;
12 field $piece_size : param : reader = 0;
13 field $merkle : reader;
14 use constant MAX_FILE_SIZE => 100 * 1024 * 1024 * 1024; # 100GB
15 ADJUST {
16 $file_path = Path::Tiny::path($file_path)->absolute;
17 $file_path = $file_path->realpath if $file_path->exists;
18 if ($pieces_root) {
19 $merkle = Digest::Merkle::SHA256->new( file_size => $size );
20 }
21 }
22  
23 method verify_block ( $index, $data ) {
24 if ( !$merkle ) {
25 $self->_emit_log( 'fatal', 'File does not have Merkle tree (no pieces root)' );
26 return 0;
27 }
28 my $old_hash = $merkle->get_node( $merkle->height, $index );
29 my $hash = sha256($data);
30 $merkle->set_block( $index, $hash );
31 if ( $merkle->root eq $pieces_root ) {
32 return 1;
33 }
34 else {
35 $merkle->set_block( $index, $old_hash );
36 return 0;
37 }
38 }
39  
40 method verify_block_audit ( $index, $data, $audit_path ) {
41 if ( !$pieces_root ) {
42 $self->_emit_log( 'fatal', 'File does not have pieces root' );
43 return 0;
44 }
45 return Digest::Merkle::SHA256->verify_hash( $index, sha256($data), $audit_path, $pieces_root );
46 }
47  
48 method verify_piece_v2 ( $index, $data, $expected_hash ) {
49  
50 # In BT v2, piece layer hashes are nodes at a specific level in the file's merkle tree.
51 # If the piece size == block size (16KiB), the hash is just sha256(data).
52 # Otherwise, it's the root of a mini-tree of the blocks in that piece.
53 my $block_size = $merkle ? $merkle->block_size : 16384;
54 my $blocks_per_piece = int( $piece_size / $block_size );
55 my $num_blocks = int( ( length($data) + $block_size - 1 ) / $block_size );
56 my $actual_hash;
57 if ( $num_blocks == 1 ) {
58 $actual_hash = sha256($data);
59 }
60 else {
61 my $tmp_merkle = Digest::Merkle::SHA256->new( file_size => length($data), block_size => $block_size );
62 for ( my $i = 0; $i < $num_blocks; $i++ ) {
63 $tmp_merkle->set_block( $i, sha256( substr( $data, $i * $block_size, $block_size ) ) );
64 }
65 $actual_hash = $tmp_merkle->root;
66 }
67 if ( $actual_hash eq $expected_hash ) {
68  
69 # If we have a full merkle tree, we can populate its leaves now
70 if ($merkle) {
71 for ( my $i = 0; $i < $num_blocks; $i++ ) {
72 $merkle->set_block( $index * $blocks_per_piece + $i, sha256( substr( $data, $i * $block_size, $block_size ) ) );
73 }
74 }
75 return 1;
76 }
77 return 0;
78 }
79  
80 method read ( $offset, $length ) {
81 return '' if $length <= 0;
82 return '' if $offset < 0;
83 $length = $size - $offset if $offset + $length > $size;
84 return '' if $length <= 0;
85 return undef unless $file_path->is_file;
86 my $fh = $file_path->openr_raw;
87 seek $fh, $offset, 0;
88 read( $fh, my $chunk, $length );
89 return $chunk;
90 }
91  
92 method write ( $offset, $data ) {
93 $self->_ensure_exists();
94 return if $offset < 0 || $offset > $size;
95 my $max_write = $size - $offset;
96 $data = substr( $data, 0, $max_write ) if length($data) > $max_write;
97 $self->_emit_log( 'debug', 'Writing ' . length($data) . " bytes to $file_path at offset $offset" );
98 my $fh = $file_path->openrw_raw;
99 seek $fh, $offset, 0;
100 print {$fh} $data or do {
101 $self->_emit_log( 'fatal', "Failed to write to $file_path: $!" );
102 return;
103 };
104 $fh->flush();
105 }
106  
107 method _ensure_exists () {
108 return if $file_path->exists;
109 if ( $size > MAX_FILE_SIZE ) {
110 $self->_emit_log( 'error', "File size $size exceeds maximum allowed (" . MAX_FILE_SIZE . ")" );
111 return;
112 }
113 $file_path->parent->mkpath;
114 if ( $^O eq 'MSWin32' ) {
115 try {
116 require Win32::File;
117  
118 # Create empty file first
119 $file_path->touch;
120  
121 # Set sparse attribute
122 Win32::File::SetAttributes( $file_path->stringify, Win32::File::SPARSE_FILE() );
123  
124 # Set size by seeking and writing one byte at the end
125 if ( $size > 0 ) {
126 my $fh = $file_path->openw_raw;
127 seek $fh, $size - 1, 0;
128 print {$fh} "\0";
129 close $fh;
130 }
131 }
132 catch ($e) {
133  
134 # Fallback to simple allocation if Win32::File fails or is missing
135 $self->_simple_allocate();
136 }
137 }
138 else {
139 # Unix-like: truncate is usually enough for sparse files on modern FS (ext4, apfs, etc)
140 try {
141 if ( $size > 0 ) {
142 truncate( $file_path->stringify, $size ) or $self->_simple_allocate();
143 }
144 else {
145 $file_path->touch;
146 }
147 }
148 catch ($e) {
149 $self->_simple_allocate();
150 }
151 }
152 }
153  
154 method _simple_allocate () {
155 if ( $size > 0 ) {
156 my $fh = $file_path->openw_raw;
157 seek $fh, $size - 1, 0;
158 print {$fh} "\0";
159 close $fh;
160 }
161 else {
162 $file_path->touch;
163 }
164 }
165  
166 method dump_state () {
167 return { merkle => ( $merkle ? $merkle->dump_state : undef ), };
168 }
169  
170 method load_state ($state) {
171 if ( $merkle && $state->{merkle} ) {
172 $merkle->load_state( $state->{merkle} );
173 }
174 }
175 } 1;