Annotation of openbsd/OpenBSDTorrents/MakeTorrents.pl, Revision 1.11
1.1 andrew 1: #!/usr/bin/perl -T
1.11 ! andrew 2: #$Id: MakeTorrents.pl,v 1.10 2005/04/06 22:56:50 andrew Exp andrew $
1.1 andrew 3: use strict;
4: use warnings;
5: use diagnostics;
6:
1.5 andrew 7: use BT::MetaInfo;
8:
1.4 andrew 9: use lib 'lib';
10: use OpenBSDTorrents;
11:
1.1 andrew 12: %ENV = ();
13:
1.11 ! andrew 14: chdir($OBT->{DIR_FTP}) || die "Couldn't change dir to " . $OBT->{DIR_FTP} . ": $!";
1.1 andrew 15:
1.11 ! andrew 16: my $StartDir = '';
! 17: if (@ARGV) {
! 18: foreach (@ARGV) {
! 19: s#/$##;
! 20: Process_Dir($_);
! 21: }
! 22: } else {
! 23: $StartDir = $OBT->{BASENAME};
! 24: Process_Dir($StartDir);
! 25: }
1.1 andrew 26:
27:
28:
29: sub Process_Dir
30: {
31: my $basedir = shift;
32:
33: my ($dirs, $files) = Get_Files_and_Dirs($basedir);
34: if (@$files) {
1.4 andrew 35: my $torrent = Make_Torrent($basedir, $files);
1.1 andrew 36: }
1.3 andrew 37:
38: # don't recurse if we were called on a specific directory
1.10 andrew 39: return 1 if $StartDir ne $OBT->{BASENAME};
1.3 andrew 40:
1.1 andrew 41: foreach my $subdir (@$dirs) {
1.4 andrew 42: next if $subdir eq '.';
43: next if $subdir eq '..';
1.1 andrew 44: Process_Dir("$basedir/$subdir")
45: }
46: }
47:
48: sub Make_Torrent
49: {
1.4 andrew 50: my $basedir = shift;
51: my $files = shift;
1.1 andrew 52:
1.10 andrew 53: if ($#{ $files } < $OBT->{MIN_FILES}) {
1.4 andrew 54: print "Too few files in $basedir, skipping . . .\n";
55: return undef;
56: }
1.3 andrew 57:
1.4 andrew 58: if ($basedir !~ /\.\./ && $basedir =~ /^([\w\/\.-]+)$/) {
59: $basedir = $1;
60: } else {
61: die "Invalid characters in dir '$basedir'";
62: }
1.1 andrew 63:
1.4 andrew 64: foreach (@$files) {
65: if (/^([^\/]+)$/) {
66: $_ = "$basedir/$1";
67: } else {
68: die "Invalid characters in file '$_' in '$basedir'";
69: }
70: }
1.1 andrew 71:
1.4 andrew 72: my $torrent = Name_Torrent($basedir);
1.3 andrew 73:
1.4 andrew 74: print "Creating $torrent\n";
1.1 andrew 75:
1.4 andrew 76: my $comment = "Files from $basedir\n" .
77: "Created by andrew fresh (andrew\@mad-techies.org)\n" .
78: "http://OpenBSD.somedomain.net/";
1.1 andrew 79:
1.7 andrew 80: eval { btmake($torrent, $comment, $files); };
81: if ($@) {
82: print "Error creating $torrent\n";
83: }
1.3 andrew 84:
1.5 andrew 85: # system($BTMake,
86: # '-C',
87: # '-c', $comment,
1.10 andrew 88: # '-n', $OBT->{BASENAME},
89: # '-o', $OBT->{DIR_TORRENT} . "/$torrent",
1.5 andrew 90: # '-a', $Tracker,
91: # @$files
92: # );# || die "Couldn't system $BTMake $torrent: $!";
93:
1.4 andrew 94: return $torrent;
1.5 andrew 95: }
96:
97:
98: # Stole and modified from btmake to work for this.
99: sub btmake {
100: no locale;
101:
102: my $torrent = shift;
103: my $comment = shift;
104: my $files = shift;
105:
1.10 andrew 106: my $name = $OBT->{BASENAME};
107: my $announce = $OBT->{URL_TRACKER};
108: my $piece_len = 2 << ($OBT->{PIECE_LENGTH} - 1);
1.5 andrew 109:
1.10 andrew 110: $torrent = $OBT->{DIR_TORRENT} . "/$torrent";
1.5 andrew 111:
112: my $t = BT::MetaInfo->new();
113: $t->name($name);
114: $t->announce($announce);
115: unless ($announce =~ m!^http://[^/]+/!i) {
116: warn " [ WARNING: announce URL does not look like: http://hostname/ ]\n";
117: }
118: $t->comment($comment);
119: #foreach my $pair (split(/;/, $::opt_f)) {
120: # if (my($key, $val) = split(/,/, $pair, 2)) {
121: # $t->set($key, $val);
122: # }
123: #}
124: $t->piece_length($piece_len);
125: $t->creation_date(time);
1.9 andrew 126: print "Checksumming files. This may take a little while...\n";
1.5 andrew 127: $t->set_files(@$files);
1.8 andrew 128:
1.10 andrew 129: if ($t->total_size < $OBT->{MIN_SIZE}) {
1.8 andrew 130: print "Skipping smaller than minimum size\n";
131: return 0;
132: }
133:
1.5 andrew 134: $t->save("$torrent");
135: print "Created: $torrent\n";
136: #system("btinfo $torrent") if ($::opt_I);
1.1 andrew 137: }
1.3 andrew 138:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>