[vhffs-dev] [2076] reworked repository quota, added quota_zero function for both user and group

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 2076
Author:   gradator
Date:     2012-02-27 01:29:11 +0100 (Mon, 27 Feb 2012)
Log Message:
-----------
reworked repository quota, added quota_zero function for both user and group

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Robots/Group.pm
    trunk/vhffs-api/src/Vhffs/Robots/Repository.pm
    trunk/vhffs-api/src/Vhffs/Robots/User.pm
    trunk/vhffs-robots/src/repository_quota.pl

Modified: trunk/vhffs-api/src/Vhffs/Robots/Group.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/Group.pm	2012-02-27 00:02:17 UTC (rev 2075)
+++ trunk/vhffs-api/src/Vhffs/Robots/Group.pm	2012-02-27 00:29:11 UTC (rev 2076)
@@ -177,6 +177,30 @@
 	Vhffs::Robots::vhffs_log( $vhffs, 'Updated quota used for group '.$group->get_groupname.' (gid '.$group->get_gid.') to '.$used.' MB');
 }
 
+sub quota_zero {
+	require Quota;
+	my $group = shift;
+	my $path = shift;
+	return undef unless defined $group and defined $path and -d $path;
+
+	my $vhffs = $group->get_main;
+
+	my $dev = Quota::getqcarg($path);
+
+	# Only set quota if filesystem quota is not currently set
+	my (undef,$softblocks,$hardblocks,undef,undef,$softinodes,$hardinodes,undef) = Quota::query($dev, $group->get_gid, 1);
+	next if defined $softblocks and defined $hardblocks and defined $softinodes and defined $hardinodes
+	  and $softblocks == 1 and $hardblocks == 0
+	  and $softinodes == 1 and $hardinodes == 0;
+
+	if( Quota::setqlim($dev, $group->get_gid, 1, 0, 1, 0, 0, 1) ) {
+		Vhffs::Robots::vhffs_log( $vhffs, 'Cannot set quota to zero for group '.$group->get_groupname.' (gid '.$group->get_gid.'), on '.$path.', reason: '.Quota::strerr() );
+		return undef;
+	}
+
+	return 1;
+}
+
 #TODO: Rework everything below this line
 
 =pod

Modified: trunk/vhffs-api/src/Vhffs/Robots/Repository.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/Repository.pm	2012-02-27 00:02:17 UTC (rev 2075)
+++ trunk/vhffs-api/src/Vhffs/Robots/Repository.pm	2012-02-27 00:29:11 UTC (rev 2076)
@@ -31,6 +31,7 @@
 
 use strict;
 use utf8;
+use POSIX;
 use File::Path;
 use File::Basename;
 
@@ -75,6 +76,7 @@
 	Vhffs::Robots::vhffs_log( $vhffs, 'Created downloads repository '.$repository->get_name );
 	$repository->set_status( Vhffs::Constants::ACTIVATED );	
 	$repository->commit;
+	quota($repository);
 
 	return 1;
 }
@@ -122,4 +124,40 @@
 	return 1;
 }
 
+sub quota {
+	require Quota;
+	my $repository = shift;
+	return undef unless defined $repository;
+
+	my $vhffs = $repository->get_main;
+
+	my $dev = Quota::getqcarg($vhffs->get_config->get_datadir.'/repository');
+
+	my $setblocks = POSIX::ceil( ($repository->get_quota*1000000)/1024 );  # Filesystem quota block = 1024B
+	my $setinodes = POSIX::ceil( ($repository->get_quota*1000000)/4096 );  # Filesystem block = 4096B
+
+	my ($blocks,$softblocks,$hardblocks,undef,undef,$softinodes,$hardinodes,undef) = Quota::query($dev, $repository->get_owner_gid, 1);
+
+	# Set quota - only if database and filesystem are out of sync
+	unless( defined $softblocks and defined $hardblocks and defined $softinodes and defined $hardinodes
+	  and $softblocks == $hardblocks and $softinodes == $hardinodes
+	  and $hardblocks == $setblocks and $hardinodes == $setinodes) {
+
+		unless( Quota::setqlim($dev, $repository->get_owner_gid, $setblocks, $setblocks, $setinodes, $setinodes, 0, 1) ) {
+			Vhffs::Robots::vhffs_log( $vhffs, 'Set quota for repository '.$repository->get_name.' (gid '.$repository->get_owner_gid.') to '.$repository->get_quota.' MB');
+			$repository->add_history( 'Disk quota set to '.$repository->get_quota.' MB' );
+		} else {
+			Vhffs::Robots::vhffs_log( $vhffs, 'Cannot set quota for repository '.$repository->get_name.' (gid '.$repository->get_owner_gid.'), reason: '.Quota::strerr() );
+		}
+	}
+
+	# Get quota - only push changes if filesystem and database have different values
+	return undef unless defined $blocks;
+	my $used = POSIX::ceil( ($blocks*1024)/1000000 );
+	return 1 if $used == $repository->get_quota_used;
+	$repository->set_quota_used( $used );
+	$repository->commit;
+	Vhffs::Robots::vhffs_log( $vhffs, 'Updated quota used for repository '.$repository->get_name.' (gid '.$repository->get_owner_gid.') to '.$used.' MB');
+}
+
 1;

Modified: trunk/vhffs-api/src/Vhffs/Robots/User.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/User.pm	2012-02-27 00:02:17 UTC (rev 2075)
+++ trunk/vhffs-api/src/Vhffs/Robots/User.pm	2012-02-27 00:29:11 UTC (rev 2076)
@@ -136,7 +136,7 @@
 
 	my $vhffs = $user->get_main;
 
-	my $dev = Quota::getqcarg($vhffs->get_config->get_datadir);
+	my $dev = quota::getqcarg($vhffs->get_config->get_datadir);
 
 	my $group = $user->get_group;
 	return undef unless defined $group;
@@ -170,4 +170,28 @@
 	return 1;
 }
 
+sub quota_zero {
+	require Quota;
+	my $user = shift;
+	my $path = shift;
+	return undef unless defined $user and defined $path and -d $path;
+
+	my $vhffs = $user->get_main;
+
+	my $dev = Quota::getqcarg($path);
+
+	# Only set quota if filesystem quota is not currently set
+	my (undef,$softblocks,$hardblocks,undef,undef,$softinodes,$hardinodes,undef) = Quota::query($dev, $user->get_gid, 1);
+	next if defined $softblocks and defined $hardblocks and defined $softinodes and defined $hardinodes
+	  and $softblocks == 1 and $hardblocks == 0
+	  and $softinodes == 1 and $hardinodes == 0;
+
+	if( Quota::setqlim($dev, $user->get_gid, 1, 0, 1, 0, 0, 1) ) {
+		Vhffs::Robots::vhffs_log( $vhffs, 'Cannot set quota to zero for user '.$user->get_username.' (gid '.$user->get_gid.'), on '.$path.', reason: '.Quota::strerr() );
+		return undef;
+	}
+
+	return 1;
+}
+
 1;

Modified: trunk/vhffs-robots/src/repository_quota.pl
===================================================================
--- trunk/vhffs-robots/src/repository_quota.pl	2012-02-27 00:02:17 UTC (rev 2075)
+++ trunk/vhffs-robots/src/repository_quota.pl	2012-02-27 00:29:11 UTC (rev 2076)
@@ -29,87 +29,42 @@
 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 # POSSIBILITY OF SUCH DAMAGE.
 
-require 5.004;
 use strict;
 use utf8;
-use POSIX;
-use locale;
-use Locale::gettext;
+
 use lib '%VHFFS_LIB_DIR%';
-use Vhffs::Main;
-use Vhffs::Robots;
-use Vhffs::Group;
-use Vhffs::Services::Repository;
-use Quota;
+use Vhffs::Robots::Repository;
+use Vhffs::Robots::Group;
+use Vhffs::Robots::User;
 
 my $vhffs = init Vhffs::Main;
+exit 1 unless defined $vhffs;
 
 Vhffs::Robots::lock( $vhffs , 'quotarepository' );
 
-my $dev = Quota::getqcarg($vhffs->get_config->get_datadir.'/repository');
-my $repos = Vhffs::Services::Repository::getall( $vhffs, Vhffs::Constants::ACTIVATED, undef, undef );
+my $path = $vhffs->get_config->get_datadir.'/repository';
 my $groupsusingrepo = {};
 
-foreach my $repo ( @{$repos} ) {
-	my $setblocks = ceil( ($repo->get_quota*1000000)/1024 );  # Filesystem quota block = 1024B
-	my $setinodes = ceil( ($repo->get_quota*1000000)/4096 );  # Filesystem block = 4096B
-	$groupsusingrepo->{ $repo->get_owner_gid } = 1;	
-
-	my ($blocks,$softblocks,$hardblocks,undef,undef,$softinodes,$hardinodes,undef) = Quota::query($dev, $repo->get_owner_gid, 1);
-
-	# Set quota - only if database and filesystem are out of sync
-	unless( defined $softblocks and defined $hardblocks and defined $softinodes and defined $hardinodes
-	  and $softblocks == $hardblocks and $softinodes == $hardinodes
-	  and $hardblocks == $setblocks and $hardinodes == $setinodes) {
-
-		unless( Quota::setqlim($dev, $repo->get_owner_gid, $setblocks, $setblocks, $setinodes, $setinodes, 0, 1) ) {
-			Vhffs::Robots::vhffs_log( $vhffs, 'Set quota for repository '.$repo->get_name.' (gid '.$repo->get_owner_gid.') to '.$repo->get_quota.' MB');
-		} else {
-			print 'Cannot set quota for repository '.$repo->get_name.' (gid '.$repo->get_owner_gid.'), reason: '.Quota::strerr()."\n";
-		}
-	}
-
-	# Get quota - only push changes if filesystem and database have different values
-	next unless defined $blocks;
-	my $used = ceil( ($blocks*1024)/1000000 );
-	next if $used == $repo->get_quota_used;
-	$repo->set_quota_used( $used );
-	$repo->commit;
-	Vhffs::Robots::vhffs_log( $vhffs, 'Updated quota used for repository '.$repo->get_name.' (gid '.$repo->get_owner_gid.') to '.$used.' MB');
+my $repos = Vhffs::Services::Repository::getall( $vhffs, Vhffs::Constants::ACTIVATED );
+foreach( @{$repos} ) {
+	$groupsusingrepo->{ $_->get_owner_gid } = 1;
+	Vhffs::Robots::Repository::quota( $_ );
 }
 
 # Give no space for groups without download repository
 # So that a user in more than one group cannot abuse of quota by using a gid without quota yet
 my $groups = Vhffs::Group::getall( $vhffs, Vhffs::Constants::ACTIVATED );
-foreach my $group ( @{$groups} ) {
-	next if $groupsusingrepo->{ $group->get_gid };	
-
-	# Only set quota if filesystem quota is not currently set
-	my (undef,$softblocks,$hardblocks,undef,undef,$softinodes,$hardinodes,undef) = Quota::query($dev, $group->get_gid, 1);
-	next if defined $softblocks and defined $hardblocks and defined $softinodes and defined $hardinodes
-	  and $softblocks == 1 and $hardblocks == 0
-	  and $softinodes == 1 and $hardinodes == 0;
-
-	if( Quota::setqlim($dev, $group->get_gid, 1, 0, 1, 0, 0, 1) ) {
-		print 'Cannot set quota to zero for group '.$group->get_groupname.' (gid '.$group->get_gid.'), reason: '.Quota::strerr()."\n";
-	}
+foreach( @{$groups} ) {
+	next if $groupsusingrepo->{ $_->get_gid };	
+	Vhffs::Robots::Group::quota_zero( $_, $path );
 }
 
 # Give no space for users primary groups
 my $users = Vhffs::User::getall( $vhffs , Vhffs::Constants::ACTIVATED );
-foreach my $user ( @{$users} ) {
-	# Only set quota if filesystem quota is not currently set
-	my (undef,$softblocks,$hardblocks,undef,undef,$softinodes,$hardinodes,undef) = Quota::query($dev, $user->get_gid, 1);
-	next if defined $softblocks and defined $hardblocks and defined $softinodes and defined $hardinodes
-	  and $softblocks == 1 and $hardblocks == 0
-	  and $softinodes == 1 and $hardinodes == 0;
-
-	if( Quota::setqlim($dev, $user->get_gid, 1, 0, 1, 0, 0, 1) ) {
-		print 'Cannot set quota to zero for user '.$user->get_username.' (gid '.$user->get_gid.'), reason: '.Quota::strerr()."\n";
-	}
+foreach( @{$users} ) {
+	Vhffs::Robots::User::quota_zero( $_, $path );
 }
 
-
 Vhffs::Robots::unlock( $vhffs , 'quotarepository' );
 
 exit 0;


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/