File: diskuse

package info (click to toggle)
proftpd-dfsg 1.3.5-1.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 30,228 kB
  • sloc: perl: 211,735; ansic: 153,584; php: 11,586; sh: 11,085; makefile: 2,323; xml: 93
file content (232 lines) | stat: -rw-r--r-- 6,225 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/perl
# -------------------------------------------------------------------------
# Copyright (C) 2002 TJ Saunders <tj@castaglia.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
#
#  $Id: 14.quotatab_modules.diff,v 1.4 2003/11/17 15:30:12 frankie Exp $
#
# -------------------------------------------------------------------------

use strict;

use Fcntl;
use File::Basename qw(basename);
use Getopt::Long;

my $program = basename($0);
my %opts = {};

GetOptions(\%opts, 'G', 'K', 'M', 'dir-tally', 'group=s', 'help',
  'user=s', 'verbose');

my $verbose = 0;
my $user_id = -1;
my $group_id = -1;

usage() if defined($opts{'help'});

if (length(@ARGV) < 1) {
  print STDOUT "$program: wrong number of parameters\n";
  exit 0;
}

unless (defined($opts{'user'}) || defined($opts{'group'})) {
  print STDOUT "$program: missing required --group or --user option\n";
  exit 0;
}

if (defined($opts{'user'})) {
  $user_id = getpwnam($opts{'user'}) or
    die "$program: no such user: $opts{'user'}\n";
}

if (defined($opts{'group'})) {
  $group_id = getgrnam($opts{'group'}) or
    die "$program: no such group: $opts{'group'}\n";
}

$verbose = 1 if (defined($opts{'verbose'}));

my ($total_bytes, $total_files);

foreach my $dir (@ARGV) {
  my ($bytes, $files) = get_dir_disk_use($dir);

  $total_bytes += $bytes;
  $total_files += $files;
}

my $byte_units = "bytes";

if (defined($opts{'K'})) {
  $total_bytes /= 1024;
  $byte_units = "KB";

} elsif (defined($opts{'M'})) {
  $total_bytes /= (1024 * 1024);
  $byte_units = "MB";

} elsif (defined($opts{'G'})) {
  $total_bytes /= (1024 * 1024 * 1024);
  $byte_units = "GB";
}

my $file_units = "files";
$file_units = "file" if ($total_files == 1);

print STDOUT "$program: $total_bytes $byte_units in $total_files $file_units\n";

# done
exit 0;

# --------------------------------------------------------------------------
sub get_dir_disk_use {
  my ($dir) = @_;

  print STDOUT "$program: examining directory '$dir'\n" if $verbose;

  my @subdirs = ();
  my $nbytes = 0;
  my $nfiles = 0;

  unless (opendir(DIR, $dir)) {
    print STDERR "$program: unable to open directory '$dir': $!\n";
    return (0, 0);
  }

  my @files = readdir(DIR);
  close(DIR);

  foreach my $file (@files) {
    my ($mode, $uid, $gid, $size) = (lstat("$dir/$file"))[2, 4, 5, 7];

    # Is this entry a regular file, or a directory?
    unless (-f "$dir/$file" || (-l "$dir/$file" && !-f "$dir/$file") || -d "$dir/$file") {
      print STDOUT "$program: skipping '$dir/$file': not a file or directory\n" if $verbose;
      next;
    }

    # Does this file match the given user/group ownership?
    if ($user_id != -1 && $group_id != -1) {

      if ($uid == $user_id && $gid == $group_id) {

        if (defined($opts{'dir-tally'}) && $file eq ".") {
          $nbytes += $size;
          $nfiles++;
 
        } elsif (! -d "$dir/$file") {
          $nbytes += $size;
          $nfiles++;
        }

      } else {
        print STDOUT "$program: '$file' does not match UID $user_id, GID $group_id\n" if $verbose;
      }

    } elsif ($user_id != -1) {

      if ($uid == $user_id) {

        if (defined($opts{'dir-tally'}) && $file eq ".") {
          $nbytes += $size;
          $nfiles++;

        } elsif (! -d "$dir/$file") {
          $nbytes += $size;
          $nfiles++;
        }

      } else {
        print STDOUT "$program: '$file' does not match UID $user_id\n" if $verbose;
      }

    } elsif ($group_id != -1) {

      if ($gid == $group_id) {

        if (defined($opts{'dir-tally'}) && $file eq ".") {
          $nbytes += $size;
          $nfiles++;

        } elsif (! -d "$dir/$file") {
          $nbytes += $size;
          $nfiles++;
        }

      } else {
        print STDOUT "$program: '$file' does not match GID $group_id\n" if $verbose;
      }
    }

    # Is this entry a directory?  If so, add it to the subdir list.
    if (-d "$dir/$file") {

      # Skip dot directories
      push(@subdirs, "$dir/$file") unless ($file eq "." || $file eq "..");
    }
  }

  # Now, recurse through the directory's subdirectories
  foreach my $subdir (@subdirs) {
    my ($bytecount, $filecount) = get_dir_disk_use($subdir);
    $nbytes += $bytecount;
    $nfiles += $filecount;
  }

  return ($nbytes, $nfiles);
}

# --------------------------------------------------------------------------
sub usage {

  print STDOUT <<END_OF_USAGE;

usage: $program [options] dir1 dir2 ... dirN

 $program calculates the amount of disk space used underneath the given
 directory (or directories).  Either a --group or a --user option is required.
 If both are used, only files whose ownership matches both the given name and
 group will be added to the disk usage sum.

 Options:

  --G          Display the number of bytes in units of gigabytes.  The default
               number is in bytes.

  --K          Display the number of bytes in units of kilobytes.  The default
               number is in bytes.

  --M          Display the number of bytes in units of megabytes.  The default
               number is in bytes.

  --dir-tally  Count the size of directories toward the disk usage sum.  The
               default is to consider regular files only.

  --group      Count files owned by this group in the disk usage sum.

  --help       Display this message.

  --user       Count files owned by this user in the disk usage sum.

  --verbose    Display diagnostic output while $program is running.

END_OF_USAGE

  exit 0;
}

# --------------------------------------------------------------------------