CalebH has asked for the wisdom of the Perl Monks concerning the following question:
sub filegrabber { ($filed) = @_; print Dumper($filed); print "We Are In FileGrabber Loop now \n\n"; my $file_index = 0; my $this_path = undef; my $cnt_good = 0; my $cnt_bad = 0; my $badfiles = {}; my $goodfiles = {}; for(my $i=0; $i<length($sha); $i+=SHASIZE) { my $this_sha = substr($sha,$i,SHASIZE ); my $this_piece = $i / SHASIZE; my $need_bytes = $piece_length; my $pbuffer = ''; while($need_bytes > 0) { my $src_ref = $files->[$file_index] or last; # got all pie +ces if($src_ref->{path} ne $this_path) { close(FH); $this_path = $src_ref->{path}; open(FH, "<", $filed) or die "Couldn't open $filed because $!\ +n"; } my $buff = ''; my $got_bytes = sysread(FH,$buff,$need_bytes); $pbuffer .= $buff; $need_bytes -= $got_bytes; print "\n"; print "Got Bytes: " . $got_bytes . "\n"; print "Got Bytes Length: " . length($got_bytes) . "\n"; print "Need Bytes: " . $need_bytes . "\n"; print "This Sha (size) " . length($this_sha) . "\n"; print "This Sha (unpack): " . unpack("H*", $this_sha) . "\n"; print "This Sha (shahex): " . sha1_hex($pbuffer) . "\n"; print "This Piece: " . $this_piece . "\n"; print "Piece Size: $piece_length\n"; $file_index++ if $got_bytes < 1; if($got_bytes ne $piece_length) { print "Pbuffer Size: " . length($pbuffer) . "\n"; print "Byte Mismatch \nGot: $got_bytes\nP-Len: $piece_length\n"; next; } } if( unpack("H*",$this_sha) eq sha1_hex($pbuffer)) { $cnt_good++; $goodfiles->{$this_path}++; } else { $cnt_bad++; $badfiles->{$this_path}++; print "Bad: $this_piece\n"; print "Got_bytes = $got_bytes\n"; print "BadPiece : $this_piece - " . unpack("H*", $this_sha) . "\ +n"; print "This Sha : " . length($this_sha) . "\n"; print "Pbuffer length: " . length($pbuffer) . "\n"; print "File index is $file_index\n"; print "BadPiece2 : " . sha1_hex($pbuffer) . "\n"; } } print "\r".(" " x 32 ); print "\rfound $cnt_bad bad piece(s)\n"; print "\rfound $cnt_good good piece(s)\n"; foreach my $this_bad (keys(%$badfiles)) { printf("%-64s : %d bad bytes (%d pieces)\n", $this_bad, get_sized($badfiles->{$this_bad}*$piece_length), $badfiles->{$this_bad +}); } }
sub verify_torrent { my($filename, $basepath) = @_; my $ref = _slurp($filename); my $plen = $ref->{info}->{'piece length'}; my $sha = $ref->{info}->{pieces}; my $files = []; if(ref($ref->{info}->{files}) eq 'ARRAY') { foreach my $fref (@{$ref->{info}->{files}}) { push(@$files, {path=>join("/", @{$fref->{path}}), length=> +$fref->{length}}); } } else { push(@$files, {path=>$ref->{info}->{name}, length=>$ref->{info +}->{length}}); } my $file_index = 0; my $this_path = undef; my $cnt_good = 0; my $cnt_bad = 0; my $badfiles = {}; for(my $i=0; $i<length($sha); $i+=SHASIZE) { my $this_sha = substr($sha,$i,SHASIZE); my $this_piece = $i / SHASIZE; my $need_bytes = $plen; my $pbuffer = ''; while($need_bytes > 0) { my $src_ref = $files->[$file_index] or last; # got all pie +ces if($src_ref->{path} ne $this_path) { # new file -> must up +date FH close(FH); $this_path = $src_ref->{path}; my $vfs = join("/",$basepath, $this_path); open(FH, "<", join("/",$vfs)) or warn "Could not open: + $vfs\n"; } my $buff = ''; my $got_bytes = sysread(FH,$buff,$need_bytes); $pbuffer .= $buff; $need_bytes -= $got_bytes; $file_index++ if $got_bytes < 1; } if( unpack("H*",$this_sha) eq sha1_hex($pbuffer) ) { $cnt_good++; } else { $cnt_bad++; $badfiles->{$this_path}++; } print "\rpiece=$this_piece, ok=$cnt_good, bad=$cnt_bad" if $th +is_piece % 4 == 0; } print "\r".(" " x 32 ); print "\rfound $cnt_bad bad piece(s)\n"; foreach my $this_bad (keys(%$badfiles)) { printf("%-64s : %d bad bytes (%d pieces)\n", $this_bad, $badfi +les->{$this_bad}*$plen, $badfiles->{$this_bad}); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Reading records of various lengths
by Anonymous Monk on Jun 09, 2016 at 14:03 UTC |