Remove useless defined statements
authorJamie Cameron <jcameron@webmin.com>
Sun, 18 Jul 2010 01:22:38 +0000 (18:22 -0700)
committerJamie Cameron <jcameron@webmin.com>
Sun, 18 Jul 2010 01:22:38 +0000 (18:22 -0700)
bacula-backup/bacula-backup-lib.pl
disk-usage/disk-usage-lib.pl [new file with mode: 0644]
file/file-lib.pl
firewall/firewall-lib.pl
lpadmin/aix-lib.pl
mailboxes/folders-lib.pl
miniserv.pl
samba/samba-lib.pl
usermin/usermin-lib.pl
web-lib-funcs.pl

index 367f1f0..9060922 100755 (executable)
@@ -1280,7 +1280,7 @@ return $job;
 # Returns a link for editing some job, if possible
 sub joblink
 {
-if (!defined(%joblink_jobs)) {
+if (!%joblink_jobs) {
        local $conf = &get_director_config();
        %joblink_jobs = map { $n=&find_value("Name", $_->{'members'}), 1 }
                        &find("Job", $conf);
diff --git a/disk-usage/disk-usage-lib.pl b/disk-usage/disk-usage-lib.pl
new file mode 100644 (file)
index 0000000..0eb432b
--- /dev/null
@@ -0,0 +1,219 @@
+# Functions for getting usage
+
+do '../web-lib.pl';
+&init_config();
+do '../ui-lib.pl';
+
+$usage_tree_file = "$module_config_directory/tree";
+$heiropen_file = "$module_config_directory/heiropen";
+$cron_cmd = "$module_config_directory/usage.pl";
+
+# build_root_usage_tree(&dirs)
+# Returns a usage tree from / containing all the specified directories
+sub build_root_usage_tree
+{
+local $root = { 'dir' => '/',
+               'total' => 0,
+               'files' => 0 };
+foreach my $dir (@{$_[0]}) {
+       # No need to do a directory that has already been done by a parent
+       local $already = &find_in_tree($root, $dir);
+       next if ($already && $dir ne "/");
+
+       local $tree = &build_usage_tree($dir);
+       if ($dir eq "/") {
+               $root = $tree;
+               }
+       else {
+               # Insert into root at correct location
+               while(1) {
+                       $tree->{'dir'} =~ /^(.*)\/(.*)$/;
+                       local $pdir = $1 || "/";
+                       local $file = $2;
+                       local $par = &find_in_tree($root, $pdir);
+                       if ($par) {
+                               # Found a parent .. link to it
+                               push(@{$par->{'subs'}}, $tree);
+                               $tree->{'parent'} = $parent;
+
+                               # Increase the totals for all parents
+                               while($par) {
+                                       $par->{'total'} += $tree->{'total'};
+                                       $par = $par->{'parent'};
+                                       }
+                               last;
+                               }
+                       else {
+                               # Need to make up a parent
+                               $par = { 'dir' => $pdir, 'subs' => [ $tree ],
+                                        'total' => $tree->{'total'},
+                                        'files' => 0 };
+                               $tree->{'parent'} = $par;
+                               $tree = $par;
+                               }
+                       }
+               }
+       }
+return $root;
+}
+
+# build_usage_tree(dir)
+# Given a base directory, returns a structure containing details about it and
+# all sub-directories
+sub build_usage_tree
+{
+local ($dir) = @_;
+local ($total, $files) = (0, 0);
+opendir(DIR, $dir);
+local @files = readdir(DIR);
+closedir(DIR);
+local $rv = { 'dir' => $dir, 'subs' => [ ] };
+local @pst = stat($dir);
+
+local $skip = &get_skip_dirs();
+foreach my $f (@files) {
+       next if ($f eq "." || $f eq "..");
+       local $path = $dir eq "/" ? "/$f" : "$dir/$f";
+       next if ($skip->{$path});
+       local @st = lstat($path);
+       if ($config{'bsize'}) {
+               $total += $st[12]*$config{'bsize'};
+               $files += $st[12]*$config{'bsize'};
+               }
+       else {
+               $total += $st[7];
+               $files += $st[7];
+               }
+       if ($config{'xdev'} && $st[0] != $pst[0]) {
+               next;   # Don't go to another filesystem
+               }
+       if (-d _ && !-l _) {
+               # A directory .. recurse into it
+               local $subdir = &build_usage_tree($path, $rv);
+               $subdir->{'parent'} = $rv;
+               $total += $subdir->{'total'};
+               push(@{$rv->{'subs'}}, $subdir);
+               }
+       }
+$rv->{'total'} = $total;
+$rv->{'files'} = $files;
+return $rv;
+}
+
+# get_usage_tree()
+sub get_usage_tree
+{
+local (%tree, %pmap);
+&read_file($usage_tree_file, \%tree) || return undef;
+foreach my $k (keys %tree) {
+       if ($k ne "/" && $k =~ /^(.*)\/(.*)$/) {
+               local $dir = $1 || "/";
+               local $file = $2;
+               push(@{$pmap{$dir}}, $k);
+               }
+       }
+return &hash_to_tree($tree{'root'}, \%tree, \%pmap);
+}
+
+# hash_to_tree(dir, &hash, &parentmap)
+sub hash_to_tree
+{
+local ($dir, $hash, $pmap) = @_;
+local $rv = { 'dir' => $dir, 'subs' => [ ] };
+($rv->{'total'}, $rv->{'files'}) = split(/ /, $hash->{$dir});
+foreach my $subdir (@{$pmap->{$dir}}) {
+       local $substr = &hash_to_tree($subdir, $hash, $pmap);
+       $substr->{'parent'} = $rv;
+       push(@{$rv->{'subs'}}, $substr);
+       }
+return $rv;
+}
+
+# save_usage_tree(&tree)
+sub save_usage_tree
+{
+local ($dir) = @_;
+local %tree;
+&tree_to_hash($dir, \%tree);
+$tree{'root'} = $dir->{'dir'};
+&write_file($usage_tree_file, \%tree);
+}
+
+# tree_to_hash(&dir, &hash)
+# Adds to the hash entries for some tree node and sub-nodes
+sub tree_to_hash
+{
+local ($dir, $hash) = @_;
+$hash->{$dir->{'dir'}} = $dir->{'total'}." ".$dir->{'files'};
+foreach my $subdir (@{$dir->{'subs'}}) {
+       &tree_to_hash($subdir, $hash);
+       }
+}
+
+# find_in_tree(&tree, dir)
+# Returns the node for some directory, or undef
+sub find_in_tree
+{
+local ($tree, $dir) = @_;
+return $tree if ($tree->{'dir'} eq $dir);
+if ($tree->{'dir'} eq "/" ||
+    $dir =~ /^$tree->{'dir'}\//) {
+       foreach my $subdir (@{$tree->{'subs'}}) {
+               local $found = &find_in_tree($subdir, $dir);
+               return $found if ($found);
+               }
+       }
+return undef;
+}
+
+# get_heiropen()
+# Returns an array of open categories
+sub get_heiropen
+{
+open(HEIROPEN, $heiropen_file);
+local @heiropen = <HEIROPEN>;
+chop(@heiropen);
+close(HEIROPEN);
+return @heiropen;
+}
+
+# save_heiropen(&heir)
+sub save_heiropen
+{
+&open_tempfile(HEIR, ">$heiropen_file");
+foreach $h (@{$_[0]}) {
+       &print_tempfile(HEIR, $h,"\n");
+       }
+&close_tempfile(HEIR);
+}
+
+sub find_cron_job
+{
+local @jobs = &cron::list_cron_jobs();
+local ($job) = grep { $_->{'user'} eq 'root' &&
+                     $_->{'command'} eq $cron_cmd } @jobs;
+return $job;
+}
+
+# get_skip_dirs()
+# Returns a hash reference of directories to skip, based on the skip list
+# and filesystems
+sub get_skip_dirs
+{
+if (!%skip_cache) {
+       %skip_cache = map { $_, 1 } split(/\t+/, $config{'skip'});
+       if (&foreign_check("mount")) {
+               &foreign_require("mount", "mount-lib.pl");
+               local %fsskip = map { $_, 1 } split(/\s+/, $config{'fs'});
+               foreach my $m (&mount::list_mounted()) {
+                       if ($fsskip{$m->[2]}) {
+                               $skip_cache{$m->[0]} = 1;
+                               }
+                       }
+               }
+       }
+return \%skip_cache;
+}
+
+1;
+
index b2c4c70..c15e691 100755 (executable)
@@ -130,9 +130,9 @@ if (!@st) {
        return undef if ($?);
        $out =~ /^(.*)\s+(\d+)\s+(\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/;
        local $type = defined($icon_map{$ext}) ? $icon_map{$ext} : 4;
-       local $user = defined(%uid_to_user) ? $uid_to_user{$5} : getpwuid($5);
+       local $user = %uid_to_user ? $uid_to_user{$5} : getpwuid($5);
        $user = $5 if (!$user);
-       local $group = defined(%gid_to_group) ? $gid_to_group{$6} :getgrgid($6);
+       local $group = %gid_to_group ? $gid_to_group{$6} :getgrgid($6);
        $group = $6 if (!$group);
        local $size = $2;
        local $mtime = $13;
@@ -147,10 +147,10 @@ local $type = $islink && !$f ? 5 :
              -p _ ? 7 :
              -S _ ? 7 : defined($icon_map{$ext}) ? $icon_map{$ext} : 4;
 local $user = !&supports_users() ? "root" :
-             defined(%uid_to_user) ? $uid_to_user{$st[4]} : getpwuid($st[4]);
+             %uid_to_user ? $uid_to_user{$st[4]} : getpwuid($st[4]);
 $user = $st[4] if (!$user);
 local $group = !&supports_users() ? "root" :
-              defined(%gid_to_group) ? $gid_to_group{$st[5]} :getgrgid($st[5]);
+              %gid_to_group ? $gid_to_group{$st[5]} :getgrgid($st[5]);
 $group = $st[5] if (!$group);
 local $rl = readlink($_[0]);
 return join("\t", $dp, $type,
index dc52545..b5b9cec 100755 (executable)
@@ -330,7 +330,7 @@ return $access{$_[0]};
 sub can_jump
 {
 return 1 if (!$access{'jumps'});
-if (!defined(%can_jumps_cache)) {
+if (!%can_jumps_cache) {
        %can_jumps_cache = map { lc($_), 1 } split(/\s+/, $access{'jumps'});
        }
 local $j = ref($_[0]) ? $_[0]->{'j'}->[1] : $_[0];
index 0aa2f48..5b81bb1 100755 (executable)
@@ -49,7 +49,7 @@ return &unique(@rv);
 # Returns a reference to an associative array of printer details
 sub get_printer
 {
-if (!defined(%enabled_cache)) {
+if (!%enabled_cache) {
        %enabled_cache = &get_enabled();
        }
 
index f36c1e8..5783875 100755 (executable)
@@ -2627,11 +2627,11 @@ sub quoted_message
 local ($mail, $qu, $sig, $bodymode) = @_;
 local $mode = $bodymode == 1 ? 1 :
              $bodymode == 2 ? 2 :
-             defined(%userconfig) ? $userconfig{'view_html'} :
-                                    $config{'view_html'};
+             %userconfig ? $userconfig{'view_html'} :
+                           $config{'view_html'};
 local ($plainbody, $htmlbody) = &find_body($mail, $mode);
 local ($quote, $html_edit, $body);
-local $cfg = defined(%userconfig) ? \%userconfig : \%config;
+local $cfg = %userconfig ? \%userconfig : \%config;
 local @writers = &split_addresses($mail->{'header'}->{'from'});
 local $writer;
 if ($writers[0]->[1]) {
@@ -2647,8 +2647,7 @@ if ($cfg->{'reply_date'} &&
        local $tmstr = &make_date($tm);
        $writer = "On $tmstr $writer";
        }
-local $qm = defined(%userconfig) ? $userconfig{'html_quote'}
-                                : $config{'html_quote'};
+local $qm = %userconfig ? $userconfig{'html_quote'} : $config{'html_quote'};
 if (($cfg->{'html_edit'} == 2 ||
      $cfg->{'html_edit'} == 1 && $htmlbody) &&
      $bodymode != 1) {
@@ -3031,7 +3030,7 @@ if (ref($mails) ne 'ARRAY') {
        }
 
 # Open cache DBM
-if (!defined(%hasattach)) {
+if (!%hasattach) {
        local $hasattach_file = $module_info{'usermin'} ?
                "$user_module_config_directory/attach" :
                "$module_config_directory/attach";
index 44efa6f..cac9e04 100755 (executable)
@@ -3237,7 +3237,7 @@ if (!$users{$_[0]}) {
                push(@doms, $2);
                }
 
-       if ($config{'user_mapping'} && !defined(%user_mapping)) {
+       if ($config{'user_mapping'} && !%user_mapping) {
                # Read the user mapping file
                %user_mapping = ();
                open(MAPPING, $config{'user_mapping'});
index 4711737..a234c8e 100755 (executable)
@@ -772,7 +772,7 @@ return 1;
 sub save_samba_acl
 {
 local ($p, $a, $s)=@_;
-defined(%share) || &get_share($s); # use global %share
+%share || &get_share($s); # use global %share
 local $t=&istrue('printable') ? 'ps' : 'fs';
 $a->{'ACL'. $t .'_'. $s} = $p;
 #undef($can_cache);
@@ -783,7 +783,7 @@ return &save_module_acl($a);
 sub drop_samba_acl
 {
 local ($a, $s)=@_;
-defined(%share) || &get_share($s); # use global %share
+%share || &get_share($s); # use global %share
 local $t=&istrue('printable') ? 'ps' : 'fs';
 delete($a->{'ACL'. $t .'_' . $s});
 #undef($can_cache);
index 4fffeb3..25968eb 100755 (executable)
@@ -303,7 +303,7 @@ Reads the acl file into the given hashes. The first maps user,module to
 sub read_usermin_acl
 {
 local($user, $_, @mods);
-if (!defined(%usermin_acl_hash_cache)) {
+if (!%usermin_acl_hash_cache) {
        open(ACL, &usermin_acl_filename());
        while(<ACL>) {
                if (/^(\S+):\s*(.*)/) {
index bae275a..7b0c14d 100755 (executable)
@@ -1690,7 +1690,7 @@ more comprehensive check of module availability.
 =cut
 sub read_acl
 {
-if (!defined(%main::acl_hash_cache)) {
+if (!%main::acl_hash_cache) {
        local $_;
        open(ACL, &acl_filename());
        while(<ACL>) {
@@ -5462,7 +5462,7 @@ if (ref($_[0])) {
        }
 elsif ($_[0]) {
        # lookup the server in the webmin servers module if needed
-       if (!defined(%main::remote_servers_cache)) {
+       if (!%main::remote_servers_cache) {
                &foreign_require("servers", "servers-lib.pl");
                foreach $s (&foreign_call("servers", "list_servers")) {
                        $main::remote_servers_cache{$s->{'host'}} = $s;
@@ -6258,7 +6258,7 @@ Puts the environment back how it was before clean_environment was callled.
 =cut
 sub reset_environment
 {
-if (defined(%UNCLEAN_ENV)) {
+if (%UNCLEAN_ENV) {
        foreach my $k (keys %UNCLEAN_ENV) {
                $ENV{$k} = $UNCLEAN_ENV{$k};
                }
@@ -6917,7 +6917,7 @@ ascii values (like 246). Mainly for internal use.
 =cut
 sub load_entities_map
 {
-if (!defined(%entities_map_cache)) {
+if (!%entities_map_cache) {
        local $_;
        open(EMAP, "$root_directory/entities_map.txt");
        while(<EMAP>) {