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