use JSON::XS;
use Data::Dump qw(dump);
use autodie;
+use File::Path qw(make_path remove_tree);
-sub home_dir {
+sub user_info {
my $login = shift;
my ( undef, undef, $uid, $gid, undef, undef, $email, $dir, $shell ) = getpwnam $login;
- return $dir;
+ return ( $uid, $gid, $dir );
}
use Gearman::Worker;
$j->allow_blessed(1);
$j->convert_blessed(0);
-use TokyoCabinet;
+use lib 'lib';
+use CloudStore::MD5sum;
my $name = $ENV{ZSLICE} || die "need ZSLICE";
my $torrent_dir = "/$name/torrent";
$name =~ s/\W+/_/g;
+=head1 host_s1_torrent_list
+
+Returns JSON encoded list of all torrents
+
+=cut
+
$worker->register_function( $name . '_torrent_list' => sub {
my ($job) = @_;
my @cols = split(/\s+/,$job->arg);
return $j->encode( \@list );
} );
+=head1 host_s1_torrent_download ~login/path.torrent
+
+Start download of torrent
+
+=cut
+
$worker->register_function( $name . '_torrent_download' => sub {
my $work = $_[0]->arg;
chomp $work;
my $dir = (getpwnam($user))[7] || die "no user $user";
- my $to_dir = "$torrent_dir/download/$user";
- if ( ! -e $to_dir ) {
- mkdir $to_dir;
- # fix perm
- my ($uid,$gid) = (stat($torrent_dir))[4,5];
- chown $uid, $gid, $to_dir;
- }
+ my $torrent_file = "$dir/$path";
- $tc->add( filename => "$dir/$path", 'download-dir' => $to_dir );
+ $path =~ s/\.torrent$//;
+ my $to_dir = "$torrent_dir/download/$user/$path";
+ my ($uid,$gid) = (stat($torrent_dir))[4,5];
+ make_path $to_dir, { uid => $uid, gid => $gid };
+
+ my $t = $tc->add( filename => $torrent_file, 'download-dir' => $to_dir );
+ warn "# added $to_dir = ",dump($t);
return $to_dir;
} );
+$worker->register_function( $name . '_torrent_download_done' => sub {
+ my $work = $_[0]->arg;
+ chomp $work;
+ warn "# torrent_download_done [$work]\n";
+
+ my ( $id, $dir, $name, $hash ) = split(/\s+#\s+/,$work);
+
+ if ( $dir =~ m{/download/(\w+)/(.+)$} ) {
+ my $user = $1;
+ my $path = $2;
+
+ warn "# check $name md5sum";
+ my $md5sum = `md5sum "$dir/$name"`;
+ $md5sum =~ s/\s+.*$//s;
+ if ( $name ne $md5sum ) {
+ warn "ERROR: invalid md5sum filename ",dump( $name, $md5sum );
+ }
+
+ warn "# move $name to $user $path\n";
+ my ( $uid, $gid, $home_dir ) = user_info $user;
+ link "$dir/$name", "$home_dir/$path";
+ chown $uid, $gid, "$home_dir/$path";
+ warn "FIXME: add to md5 pool?";
+
+ my $t = $tc->remove( $id );
+ warn "# remove $id = ",dump($t);
+
+ warn "# $id removed $dir $name $hash\n";
+
+ remove_tree $dir;
+ } else {
+ warn "ERROR: can't find user in $dir for $name\n";
+ }
+
+
+} );
+
+=head1 host_s1_torrent_share ~login/file.txt
+
+Create torrent and start seeding it
+
+=cut
+
$worker->register_function( $name . '_torrent_share' => sub {
my $work = $_[0]->arg;
chomp $work;
my $path = $work;
my $dir = (getpwnam($user))[7] || die "no user $user";
- my $md5_path = "$dir/.md5";
- my %h;
- tie %h, "TokyoCabinet::HDB", $md5_path || die "$md5_path: $!";
- my $md5 = $h{$path} || die "can't find $path in $md5_path";
- untie %h;
+ my $md5 = CloudStore::MD5sum->md5_get( "$dir/$path" );
warn "# $md5 $path";