\!/ KyuuKazami \!/

Path : /scripts/
Upload :
Current File : //scripts/migrate_quota_conf_to_cpuser_files

#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - scripts/migrate_quota_conf_to_cpuser_files Copyright 2018 cPanel, Inc.
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

package scripts::migrate_quota_conf_to_cpuser_files;

=encoding utf-8

=head1 NAME

migrate_quota_conf_to_cpuser_files

=head1 USAGE

    migrate_quota_conf_to_cpuser_files [--help]

=head1 DESCRIPTION

This script migrates entries from /etc/quota.conf to the DISK_BLOCK_LIMIT
key for cpanel users if it does not already exist. This script is safe to
re-run since it will not fill in the DISK_BLOCK_LIMIT value if it already
exists and is valid.

For a given user, if DISK_BLOCK_LIMIT is unset and /etc/quota.conf’s value
is invalid (e.g., an integer greater than the maximum unsigned 64-bit value),
or if DISK_BLOCK_LIMIT is invalid, the user’s quota is set to unlimited.

=cut

use strict;
use warnings;

use Try::Tiny;

use parent qw( Cpanel::HelpfulScript );

use Cpanel::ConfigFiles            ();
use Cpanel::Config::LoadConfig     ();
use Cpanel::Config::CpUserGuard    ();
use Cpanel::Config::HasCpUserFile  ();
use Cpanel::Config::LoadCpUserFile ();
use Cpanel::Exception              ();
use Cpanel::Quota::Constants       ();
use Cpanel::Validate::Integer      ();

our $MEGABYTES_TO_BLOCKS = 1024;

__PACKAGE__->new(@ARGV)->run() if !caller;

use constant _OPTIONS => ();

sub run {
    my ($self) = @_;

    my $disk_usage = Cpanel::Config::LoadConfig::loadConfig( $Cpanel::ConfigFiles::QUOTA_CONF_FILE, undef, '=' );

    foreach my $user ( keys %$disk_usage ) {
        next if !Cpanel::Config::HasCpUserFile::has_readable_cpuser_file($user);
        my $cpuser_ref = Cpanel::Config::LoadCpUserFile::load($user);

        my $block_limit_value;

        if ( exists $cpuser_ref->{'DISK_BLOCK_LIMIT'} ) {
            try {
                Cpanel::Validate::Integer::unsigned_and_less_than(
                    $cpuser_ref->{'DISK_BLOCK_LIMIT'},
                    Cpanel::Quota::Constants::MAXIMUM_BLOCKS(),
                );
            }
            catch {
                my $err_str = Cpanel::Exception::get_string($_);
                warn "“$user”’s previously migrated value ($cpuser_ref->{'DISK_BLOCK_LIMIT'}) is invalid. ($err_str) Setting to unlimited …\n";
                $block_limit_value = 0;
            };
        }
        else {
            $block_limit_value = $disk_usage->{$user} * $MEGABYTES_TO_BLOCKS;

            try {
                Cpanel::Validate::Integer::unsigned_and_less_than(
                    $block_limit_value,
                    Cpanel::Quota::Constants::MAXIMUM_BLOCKS(),
                );
            }
            catch {
                my $err_str = Cpanel::Exception::get_string($_);
                warn "“$user” had an invalid quota value ($disk_usage->{$user} bytes - $err_str); setting to unlimited …\n";
                $block_limit_value = 0;
            };
        }

        if ( defined $block_limit_value ) {
            my $cpuser_guard = Cpanel::Config::CpUserGuard->new($user);
            $cpuser_guard->{'data'}->{'DISK_BLOCK_LIMIT'} = $block_limit_value;
            $cpuser_guard->save();
        }
    }

    return;
}

1;

@KyuuKazami