[vhffs-dev] [2074] reworked group quota

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


Revision: 2074
Author:   gradator
Date:     2012-02-27 01:00:44 +0100 (Mon, 27 Feb 2012)
Log Message:
-----------
reworked group quota

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

Modified: trunk/vhffs-api/src/Vhffs/Robots/Group.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/Group.pm	2012-02-26 23:45:04 UTC (rev 2073)
+++ trunk/vhffs-api/src/Vhffs/Robots/Group.pm	2012-02-27 00:00:44 UTC (rev 2074)
@@ -31,6 +31,7 @@
 
 use strict;
 use utf8;
+use POSIX;
 use File::Path;
 use File::Basename;
 
@@ -73,6 +74,7 @@
 	Vhffs::Robots::vhffs_log( $vhffs, 'Created group '.$group->get_groupname );
 	$group->set_status( Vhffs::Constants::ACTIVATED );	
 	$group->commit;
+	quota($group);
 
 	return 1;
 }
@@ -132,13 +134,48 @@
 }
 
 sub modify {
-	my $user = shift;
-	return undef unless defined $user and $user->get_status == Vhffs::Constants::WAITING_FOR_MODIFICATION;
-	$user->set_status( Vhffs::Constants::ACTIVATED );
-	$user->commit;
+	my $group = shift;
+	return undef unless defined $group and $group->get_status == Vhffs::Constants::WAITING_FOR_MODIFICATION;
+	$group->set_status( Vhffs::Constants::ACTIVATED );
+	$group->commit;
 	return 1;
 }
 
+sub quota {
+	require Quota;
+	my $group = shift;
+	return undef unless defined $group;
+
+	my $vhffs = $group->get_main;
+
+	my $dev = Quota::getqcarg($vhffs->get_config->get_datadir);
+
+	my $setblocks = POSIX::ceil( ($group->get_quota*1000000)/1024 );  # Filesystem quota block = 1024B
+	my $setinodes = POSIX::ceil( ($group->get_quota*1000000)/4096 );  # Filesystem block = 4096B
+
+	my ($blocks,$softblocks,$hardblocks,undef,undef,$softinodes,$hardinodes,undef) = Quota::query($dev, $group->get_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, $group->get_gid, $setblocks, $setblocks, $setinodes, $setinodes, 0, 1) ) {
+			Vhffs::Robots::vhffs_log( $vhffs, 'Set quota for group '.$group->get_groupname.' (gid '.$group->get_gid.') to '.$group->get_quota.' MB');
+		} else {
+			Vhffs::Robots::vhffs_log( $vhffs, 'Cannot set quota for group '.$group->get_groupname.' (gid '.$group->get_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 == $group->get_quota_used;
+	$group->set_quota_used( $used );
+	$group->commit;
+	Vhffs::Robots::vhffs_log( $vhffs, 'Updated quota used for group '.$group->get_groupname.' (gid '.$group->get_gid.') to '.$used.' MB');
+}
+
 #TODO: Rework everything below this line
 
 =pod

Modified: trunk/vhffs-robots/src/group_quota.pl
===================================================================
--- trunk/vhffs-robots/src/group_quota.pl	2012-02-26 23:45:04 UTC (rev 2073)
+++ trunk/vhffs-robots/src/group_quota.pl	2012-02-27 00:00:44 UTC (rev 2074)
@@ -29,52 +29,21 @@
 # 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 Quota;
+
 use lib '%VHFFS_LIB_DIR%';
-use Vhffs::Main;
-use Vhffs::Robots;
-use Vhffs::Group;
+use Vhffs::Robots::Group;
 
 my $vhffs = init Vhffs::Main;
+exit 1 unless defined $vhffs;
 
-Vhffs::Robots::lock( $vhffs , 'quotagroup' );
+Vhffs::Robots::lock( $vhffs, 'quotagroup' );
 
-my $dev = Quota::getqcarg($vhffs->get_config->get_datadir);
-
 my $groups = Vhffs::Group::getall( $vhffs, Vhffs::Constants::ACTIVATED );
-foreach my $group ( @$groups ) {
-	my $setblocks = ceil( ($group->get_quota*1000000)/1024 );  # Filesystem quota block = 1024B
-	my $setinodes = ceil( ($group->get_quota*1000000)/4096 );  # Filesystem block = 4096B
-
-	my ($blocks,$softblocks,$hardblocks,undef,undef,$softinodes,$hardinodes,undef) = Quota::query($dev, $group->get_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, $group->get_gid, $setblocks, $setblocks, $setinodes, $setinodes, 0, 1) ) {
-			Vhffs::Robots::vhffs_log( $vhffs, 'Set quota for group '.$group->get_groupname.' (gid '.$group->get_gid.') to '.$group->get_quota.' MB');
-		} else {
-			print 'Cannot set quota for group '.$group->get_groupname.' (gid '.$group->get_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 == $group->get_quota_used;
-	$group->set_quota_used( $used );
-	$group->commit;
-	Vhffs::Robots::vhffs_log( $vhffs, 'Updated quota used for group '.$group->get_groupname.' (gid '.$group->get_gid.') to '.$used.' MB');
+foreach ( @{$groups} ) {
+	Vhffs::Robots::Group::quota( $_ );
 }
 
-Vhffs::Robots::unlock( $vhffs , 'quotagroup' );
-
+Vhffs::Robots::unlock( $vhffs, 'quotagroup' );
 exit 0;


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