-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtorrent_fast_resume.pl
executable file
·57 lines (45 loc) · 1.56 KB
/
rtorrent_fast_resume.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/perl
# Perl script to add rTorrent fast resume data to torrent files.
#
# Usage:
# rtorrent_fast_resume.pl with_fast_resume.torrent [base-directory] < plain.torrent
# MODIFIED to print also the resulting torrent hash
use Digest::SHA1 qw(sha1 sha1_hex sha1_base64);
use warnings;
use strict;
use Convert::Bencode qw(bencode bdecode);
$/=undef;
my $t = bdecode(scalar <STDIN>);
my $d = $ARGV[1];
die "$d is not a directory\n" if $d and not -d $d;
$d ||= ".";
$d .= "/" unless $d =~ m#/$#;
die "No info key.\n" unless ref $t eq "HASH" and exists $t->{info};
my $psize = $t->{info}{"piece length"} or die "No piece length key.\n";
my @files;
my $tsize = 0;
if (exists $t->{info}{files}) {
print STDERR "Multi file torrent: $t->{info}{name}\n";
for (@{$t->{info}{files}}) {
push @files, join "/", $t->{info}{name},@{$_->{path}};
$tsize += $_->{length};
}
} else {
print STDERR "Single file torrent: $t->{info}{name}\n";
@files = ($t->{info}{name});
$tsize = $t->{info}{length};
}
my $chunks = int(($tsize + $psize - 1) / $psize);
print STDERR "Total size: $tsize bytes; $chunks chunks; ", scalar @files, " files.\n";
die "Inconsistent piece information!\n" if $chunks*20 != length $t->{info}{pieces};
$t->{libtorrent_resume}{bitfield} = $chunks;
for (0..$#files) {
die "$d$files[$_] not found.\n" unless -e "$d$files[$_]";
my $mtime = (stat "$d$files[$_]")[9];
$t->{libtorrent_resume}{files}[$_] = { priority => 2, mtime => $mtime };
};
open (MYFILE, ">$ARGV[0]");
print MYFILE bencode $t;
close (MYFILE);
#print STDERR "\n";
print uc sha1_hex bencode $t->{info};