-
Notifications
You must be signed in to change notification settings - Fork 1
/
UGidScan.pm
173 lines (148 loc) · 4.85 KB
/
UGidScan.pm
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
package UGidScan;
use strict;
sub read_text_list {
my ($class, $dbfn) = @_;
my $dbf;
my %uid;
my %gid;
print STDERR "Reading list '$dbfn' ...";
open($dbf, '<', $dbfn) || die("Can't read '$dbfn': $!\n");
local $/ = "\0\n";
while (<$dbf>) {
if (/\Au\s*(\d+):\s*(\d+):(.*)\0\n\z/) {
push @{$uid{$1}}, $3;
} elsif (/\Ag\s*(\d+):\s*(\d+):(.*)\0\n\z/) {
push @{$gid{$1}}, $3;
} else {
die("Unexpected record in '$dbfn':\n$_");
}
}
close $dbf;
printf(STDERR " done (%d uids, %d gids).\n",
scalar keys(%uid), scalar keys(%gid));
my %db = ( uid => \%uid, gid => \%gid );
$db{uids} = [ sort {$a <=> $b} keys %uid ];
$db{gids} = [ sort {$a <=> $b} keys %gid ];
return bless \%db => $class;
}
# Converts from the hash of hashes format used in ugid-scan
# into the hash of arrays format used to serialize into an index file
sub read_hash_list {
my ($class, $uid_hash, $gid_hash) = @_;
my %uid;
my %gid;
for my $u (keys %$uid_hash) {
push @{$uid{$u}}, sort keys %{$uid_hash->{$u}};
}
for my $g (keys %$gid_hash) {
push @{$gid{$g}}, sort keys %{$gid_hash->{$g}};
}
my %db = ( uid => \%uid, gid => \%gid );
$db{uids} = [ sort {$a <=> $b} keys %uid ];
$db{gids} = [ sort {$a <=> $b} keys %gid ];
return bless \%db => $class;
}
# List all directories that contain files with uids from a given range
sub uid_range_dirs {
my ($db, $range) = @_;
my $filter = Filter->new($range);
my @uids = grep { $filter->matches0($_) } @{$db->{uids}};
# warn about searching for excluded uids
if (exists $db->{excluded_uids}) {
my $excluded = Filter->new($db->{excluded_uids});
my @not_in_index = grep { $excluded->matches($_) } $filter->corners;
warn("Warning: uids $db->{excluded_uids} were excluded during scan,\n",
" which includes ", join(', ', @not_in_index), "\n")
if @not_in_index;
}
return [sort map { @{$db->{uid}{$_}} } @uids ];
}
# List all directories that contain files with gids from a given range
sub gid_range_dirs {
my ($db, $range) = @_;
my $filter = Filter->new($range);
my @gids = grep { $filter->matches0($_) } @{$db->{gids}};
# warn about searching for excluded gids
if (exists $db->{excluded_gids}) {
my $excluded = Filter->new($db->{excluded_gids});
my @not_in_index = grep { $excluded->matches($_) } $filter->corners;
warn("Warning: gids $db->{excluded_gids} were excluded during scan,\n",
" which includes ", join(', ', @not_in_index), "\n")
if @not_in_index;
}
return [sort {$a cmp $b} map { @{$db->{gid}{$_}} } @gids ];
}
sub count_dirs {
my ($db) = @_;
my $uid_dirs = 0;
my $gid_dirs = 0;
for my $uid (@{$db->{uids}}) {
$uid_dirs += scalar(@{$db->{uid}{$uid}});
}
for my $gid (@{$db->{gids}}) {
$gid_dirs += scalar(@{$db->{gid}{$gid}});
}
return ($uid_dirs, $gid_dirs);
}
package Filter;
# Prepare a filter (uid/gid ranges) expression
#
# A numeric range expression of the form "-99,300-399,1000-"
# is converted into an array of start-end integer pairs, where
# undef means ±infinity. Example [undef=>99, 300=>399, 1000=>undef].
# Also ^ negates a range. For example the excluding range "^300-399"
# is converted into [undef=>299, 400=>undef]. A number matches a
# range if it is within at least one of the listed intervals.
sub new {
my ($class, $exp) = @_;
my @filter;
for $_ (split(/,/, $exp // '')) {
if (/^(\d+)$/) {
push @filter, $1 => $1;
} elsif (/^(\d*)-(\d*)$/) {
push @filter, $1 => $2;
} elsif (/^\^(\d+)$/) {
push @filter, (undef) => $1-1, $1+1 => undef;
} elsif (/^\^(\d*)-(\d*)$/) {
push @filter, (undef) => $1-1 if length($1);
push @filter, $2+1 => undef if length($2);
} else {
die("Unknown filter expression '$_'!\n")
}
}
# replace empty strings with undef
@filter = map { (defined $_ && length($_) > 0) ? $_ : undef } @filter;
return bless [@filter] => $class;
}
# Apply a filter (uid/gid ranges) expression.
# Return 1 if $id is within the ranges specified by $filter
sub matches {
my ($filter, $id) = @_;
my @filter = @{$filter};
while (my ($min, $max) = splice(@filter, 0, 2)) {
next if defined $min && $id < $min;
next if defined $max && $id > $max;
return 1;
}
return 0;
}
# Apply a filter (uid/gid ranges) expression.
# Return 1 if $id is within the ranges specified by $filter
# of if the filter expression was empty.
sub matches0 {
my ($filter, $id) = @_;
return 1 unless @{$filter};
return $filter->matches($id);
}
# Output corner cases in a filter's ranges
sub corners {
my ($filter) = @_;
my @corners;
my @filter = @{$filter};
while (my ($min, $max) = splice(@filter, 0, 2)) {
push @corners, $min if defined $min;
push @corners, $max if defined $max && (!defined $min || $max > $min);
}
return sort {$a <=> $b} @corners;
}
1;