[vhffs-dev] [2073] reworked user quota

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


Revision: 2073
Author:   gradator
Date:     2012-02-27 00:45:04 +0100 (Mon, 27 Feb 2012)
Log Message:
-----------
reworked user quota

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Robots/User.pm
    trunk/vhffs-robots/src/user.pl
    trunk/vhffs-robots/src/user_quota.pl

Modified: trunk/vhffs-api/src/Vhffs/Robots/User.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/User.pm	2012-02-26 17:34:12 UTC (rev 2072)
+++ trunk/vhffs-api/src/Vhffs/Robots/User.pm	2012-02-26 23:45:04 UTC (rev 2073)
@@ -31,6 +31,7 @@
 
 use strict;
 use utf8;
+use POSIX;
 use File::Path;
 use File::Basename;
 
@@ -74,6 +75,7 @@
 	Vhffs::Robots::vhffs_log( $vhffs, 'Created home dir for user '.$user->get_username );
 	$user->set_status( Vhffs::Constants::ACTIVATED );	
 	$user->commit;
+	quota($user);
 
 	return 1;
 }
@@ -127,4 +129,45 @@
 	return 1;
 }
 
+sub quota {
+	require Quota;
+	my $user = shift;
+	return undef unless defined $user;
+
+	my $vhffs = $user->get_main;
+
+	my $dev = Quota::getqcarg($vhffs->get_config->get_datadir);
+
+	my $group = $user->get_group;
+	return undef unless defined $group;
+
+	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 user '.$user->get_username.' (gid '.$group->get_gid.') to '.$group->get_quota.' MB');
+			$user->add_history( 'Disk quota set to '.$group->get_quota.' MB' );
+		} else {
+			Vhffs::Robots::vhffs_log( $vhffs, 'Cannot set quota for user '.$user->get_username.' (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 user '.$user->get_username.' (gid '.$group->get_gid.') to '.$used.' MB');
+
+	return 1;
+}
+
 1;

Modified: trunk/vhffs-robots/src/user.pl
===================================================================
--- trunk/vhffs-robots/src/user.pl	2012-02-26 17:34:12 UTC (rev 2072)
+++ trunk/vhffs-robots/src/user.pl	2012-02-26 23:45:04 UTC (rev 2073)
@@ -40,18 +40,18 @@
 
 Vhffs::Robots::lock( $vhffs, 'user' );
 
-my $repos = Vhffs::User::getall( $vhffs, Vhffs::Constants::WAITING_FOR_CREATION );
-foreach ( @{$repos} ) {
+my $users = Vhffs::User::getall( $vhffs, Vhffs::Constants::WAITING_FOR_CREATION );
+foreach ( @{$users} ) {
 	Vhffs::Robots::User::create( $_ );
 }
 
-$repos = Vhffs::User::getall( $vhffs, Vhffs::Constants::WAITING_FOR_DELETION );
-foreach ( @{$repos} ) {
+$users = Vhffs::User::getall( $vhffs, Vhffs::Constants::WAITING_FOR_DELETION );
+foreach ( @{$users} ) {
 	Vhffs::Robots::User::delete( $_ );
 }
 
-$repos = Vhffs::User::getall( $vhffs, Vhffs::Constants::WAITING_FOR_MODIFICATION );
-foreach ( @{$repos} ) {
+$users = Vhffs::User::getall( $vhffs, Vhffs::Constants::WAITING_FOR_MODIFICATION );
+foreach ( @{$users} ) {
 	Vhffs::Robots::User::modify( $_ );
 }
 

Modified: trunk/vhffs-robots/src/user_quota.pl
===================================================================
--- trunk/vhffs-robots/src/user_quota.pl	2012-02-26 17:34:12 UTC (rev 2072)
+++ trunk/vhffs-robots/src/user_quota.pl	2012-02-26 23:45:04 UTC (rev 2073)
@@ -29,51 +29,20 @@
 # 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::User;
 
 my $vhffs = init Vhffs::Main;
+exit 1 unless defined $vhffs;
 
 Vhffs::Robots::lock( $vhffs, 'quotauser' );
 
-my $dev = Quota::getqcarg($vhffs->get_config->get_datadir);
-
-my $users = Vhffs::User::getall( $vhffs , Vhffs::Constants::ACTIVATED );
-foreach my $user ( @$users ) {
-	my $group = $user->get_group;
-	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 user '.$user->get_username.' (gid '.$group->get_gid.') to '.$group->get_quota.' MB');
-		} else {
-			print 'Cannot set quota for user '.$user->get_username.' (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 user '.$user->get_username.' (gid '.$group->get_gid.') to '.$used.' MB');
+my $users = Vhffs::User::getall( $vhffs, Vhffs::Constants::ACTIVATED );
+foreach ( @{$users} ) {
+	Vhffs::Robots::User::quota( $_ );
 }
 
 Vhffs::Robots::unlock( $vhffs, 'quotauser' );


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