SVN: toys/rsget.pl/RSGet: Curl.pm Get.pm Main.pm Quota.pm

sparky sparky at pld-linux.org
Mon Dec 6 14:35:24 CET 2010


Author: sparky
Date: Mon Dec  6 14:35:24 2010
New Revision: 11959

Added:
   toys/rsget.pl/RSGet/Quota.pm
Modified:
   toys/rsget.pl/RSGet/Curl.pm
   toys/rsget.pl/RSGet/Get.pm
   toys/rsget.pl/RSGet/Main.pm
Log:
- added download quota support, may be buggy


Modified: toys/rsget.pl/RSGet/Curl.pm
==============================================================================
--- toys/rsget.pl/RSGet/Curl.pm	(original)
+++ toys/rsget.pl/RSGet/Curl.pm	Mon Dec  6 14:35:24 2010
@@ -323,6 +323,8 @@
 		$supercurl->{size_total} = $supercurl->{force_size};
 	}
 
+	$get_obj->{_quota}->update( $supercurl->{size_total} );
+
 	$get_obj->dump( $supercurl->{head}, "head" ) if verbose( 5 );
 	my $fname;
 	if ( $supercurl->{force_name} ) {
@@ -479,6 +481,9 @@
 	if ( $supercurl->{file} ) {
 		close $supercurl->{file};
 		$get_obj->print( "DONE " . donemsg( $supercurl ) );
+
+		$get_obj->{_quota}->confirm( $curl->getinfo( CURLINFO_SIZE_DOWNLOAD ) );
+
 	}
 
 	$get_obj->linedata();

Modified: toys/rsget.pl/RSGet/Get.pm
==============================================================================
--- toys/rsget.pl/RSGet/Get.pm	(original)
+++ toys/rsget.pl/RSGet/Get.pm	Mon Dec  6 14:35:24 2010
@@ -12,6 +12,7 @@
 use RSGet::Form;
 use RSGet::Wait;
 use RSGet::Hook;
+use RSGet::Quota;
 use URI;
 set_rev qq$Id$;
 
@@ -84,6 +85,19 @@
 		$self->print( "start" );
 		$self->linedata();
 	}
+	if ( $cmd eq "get" ) {
+		local $SIG{__DIE__};
+		delete $SIG{__DIE__};
+		my $size = RSGet::ListManager::size_to_range( $self->{bestsize} );
+		eval {
+			hadd %$self,
+				_quota => RSGet::Quota->new( $size->[1] || 50 * 1024 * 1024 );
+		};
+		if ( $@ ) {
+			$self->delay( 600, "Quota reached: $@" );
+			return undef;
+		}
+	}
 
 	$self->start();
 	return $self;

Modified: toys/rsget.pl/RSGet/Main.pm
==============================================================================
--- toys/rsget.pl/RSGet/Main.pm	(original)
+++ toys/rsget.pl/RSGet/Main.pm	Mon Dec  6 14:35:24 2010
@@ -93,6 +93,7 @@
 	RSGet::Curl::init();
 	RSGet::FileList::set_file();
 	set_interfaces( $ifs );
+	RSGet::Quota::_init();
 
 	new RSGet::Line();
 

Added: toys/rsget.pl/RSGet/Quota.pm
==============================================================================
--- (empty file)
+++ toys/rsget.pl/RSGet/Quota.pm	Mon Dec  6 14:35:24 2010
@@ -0,0 +1,101 @@
+package RSGet::Quota;
+# This file is an integral part of rsget.pl downloader.
+#
+# 2009-2010 (c) Przemysław Iskra <sparky at pld-linux.org>
+#		This program is free software,
+# you may distribute it under GPL v2 or newer.
+
+use strict;
+use warnings;
+use RSGet::Tools;
+set_rev qq$Id: Wait.pm 11838 2010-10-09 18:56:30Z sparky $;
+
+def_settings(
+	quota_soft => {
+		desc => "Start downloads only if ammount of downloaded data is less "
+			. "than quota_soft bytes.",
+		allowed => qr/\d+[gmk]?b?/i,
+	},
+	quota_hard => {
+		desc => "Don't start downloads if they may excede quota_hard bytes.",
+		allowed => qr/\d+[gmk]?b?/i,
+	},
+);
+
+my $quota_soft = undef;
+my $quota_hard = undef;
+
+my $quota_used = 0;
+
+sub _get_quota
+{
+	my $name = shift;
+
+	my $s = setting( $name );
+	return undef unless $s;
+
+	return RSGet::ListManager::size_to_range( $s )->[0];
+}
+
+sub _init
+{
+	$quota_soft = _get_quota( "quota_soft" );
+	$quota_hard = _get_quota( "quota_hard" );
+	$quota_used = 0;
+}
+
+sub new
+{
+	my $class = shift;
+	my $size = shift;
+
+	die "soft quota reached\n"
+		if $quota_soft and $quota_used > $quota_soft;
+	die "hard quota reached\n"
+		if $quota_hard and $quota_used + $size > $quota_hard;
+
+	$quota_used += $size;
+	my $self = \$size;
+
+	bless $self, $class;
+}
+
+# update (i.e. no we know we won't have to download whole file)
+sub update
+{
+	my $self = shift;
+	my $size = shift;
+
+	$quota_used -= $$self;
+	$$self = $size;
+	$quota_used += $$self;
+
+	return $quota_used <= $quota_hard
+		if $quota_hard;
+	return 1;
+}
+
+# confirm number of bytes downloaded
+sub confirm
+{
+	my $self = shift;
+	my $size = shift;
+
+	$quota_used -= $$self;
+	$$self = 0;
+	$quota_used += $size;
+
+	return $size;
+}
+
+# undo unless confirmed
+sub DESTROY
+{
+	my $self = shift;
+
+	$quota_used -= $$self;
+}
+
+1;
+
+# vim: ts=4:sw=4


More information about the pld-cvs-commit mailing list