2 # Quota functions for openbsd
11 # Returns 1 for user quotas, 2 for group quotas or 3 for both
17 # free_space(filesystem)
18 # Returns an array containing btotal, bfree, ftotal, ffree
22 $ENV{'BLOCKSIZE'} = 1024;
23 `df -i $_[0]` =~ /Mounted on\n\S+\s+(\d+)\s+\d+\s+(\d+)\s+\S+\s+(\d+)\s+(\d+)/;
24 return ($1, $2, $3+$4, $4);
27 # quota_can(&mnttab, &fstab)
28 # Can this filesystem type support quotas?
29 # 0 = No quota support (or not turned on in /etc/fstab)
30 # 1 = User quotas only
31 # 2 = Group quotas only
32 # 3 = User and group quotas
35 return ($_[1]->[3] =~ /userquota/ ? 1 : 0) +
36 ($_[1]->[3] =~ /groupquota/ ? 2 : 0);
39 # quota_now(&mnttab, &fstab)
40 # Are quotas currently active?
42 # 1 = User quotas active
43 # 2 = Group quotas active
47 return $_[0]->[3] =~ /quota/ ? 3 : 0;
50 # quotaon(filesystem, mode)
51 # Activate quotas and create quota files for some filesystem. The mode can
52 # be 1 for user only, 2 for group only or 3 for user and group
55 return if (&is_readonly_mode());
56 local($out, $qf, @qfile, $flags);
59 $qf = "$_[0]/quota.user";
61 &open_tempfile(QUOTAFILE, ">$qf", 0, 1);
62 &close_tempfile(QUOTAFILE);
63 &set_ownership_permissions(undef, undef, 0600, $qf);
64 &system_logged("$config{'quotacheck_command'} $_[0]");
66 $out = &backquote_logged("$config{'user_quotaon_command'} $_[0] 2>&1");
67 if ($?) { return $out; }
70 # turn on group quotas
71 $qf = "$_[0]/quota.group";
73 &open_tempfile(QUOTAFILE, ">$qf", 0, 1);
74 &close_tempfile(QUOTAFILE);
75 &set_ownership_permissions(undef, undef, 0600, $qf);
76 &system_logged("$config{'quotacheck_command'} $_[0]");
78 $out = &backquote_logged("$config{'group_quotaon_command'} $_[0] 2>&1");
79 if ($?) { return $out; }
84 # quotaoff(filesystem, mode)
85 # Turn off quotas for some filesystem
88 return if (&is_readonly_mode());
91 $out = &backquote_logged("$config{'user_quotaoff_command'} $_[0] 2>&1");
92 if ($?) { return $out; }
95 $out = &backquote_logged("$config{'group_quotaoff_command'} $_[0] 2>&1");
96 if ($?) { return $out; }
101 # user_filesystems(user)
102 # Fills the array %filesys with details of all filesystem some user has
106 local($n, $_, %mtab);
107 open(QUOTA, "$config{'user_quota_command'} ".quotemeta($_[0])." |");
108 $n=0; while(<QUOTA>) {
110 if (/^(Disk|\s+Filesystem)/) { next; }
113 $filesys{$n,'filesys'} = $1;
115 $nl =~ /^\s+(\S+)\s+(\S+)\s+(\S+)(.{8}\s+)(\S+)\s+(\S+)\s+(\S+)(.*)/ ||
116 $nl =~ /^.{15}(.{8}).(.{7})(.{8}).{8}(.{8}).(.{7})(.{8})/;
117 $filesys{$n,'ublocks'} = int($1);
118 $filesys{$n,'sblocks'} = int($2);
119 $filesys{$n,'hblocks'} = int($3);
120 $filesys{$n,'ufiles'} = int($4);
121 $filesys{$n,'sfiles'} = int($5);
122 $filesys{$n,'hfiles'} = int($6);
125 elsif (/^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)(.{8}\s+)(\S+)\s+(\S+)\s+(\S+)(.*)/ ||
126 /^(.{15})(.{8}).(.{7})(.{8}).{8}(.{8}).(.{7})(.{8})/) {
127 $filesys{$n,'ublocks'} = int($2);
128 $filesys{$n,'sblocks'} = int($3);
129 $filesys{$n,'hblocks'} = int($4);
130 $filesys{$n,'ufiles'} = int($5);
131 $filesys{$n,'sfiles'} = int($6);
132 $filesys{$n,'hfiles'} = int($7);
133 $filesys{$n,'filesys'} = $1;
134 $filesys{$n,'filesys'} =~ s/^\s+//g;
142 # group_filesystems(group)
143 # Fills the array %filesys with details of all filesystem some group has
145 sub group_filesystems
147 local($n, $_, %mtab);
148 open(QUOTA, "$config{'group_quota_command'} ".quotemeta($_[0])." |");
149 $n=0; while(<QUOTA>) {
151 if (/^(Disk|\s+Filesystem)/) { next; }
154 $filesys{$n,'filesys'} = $1;
155 <QUOTA>=~/^.{15}(.{8}).(.{7})(.{8}).{8}(.{8}).(.{7})(.{8})/;
156 $filesys{$n,'ublocks'} = int($1);
157 $filesys{$n,'sblocks'} = int($2);
158 $filesys{$n,'hblocks'} = int($3);
159 $filesys{$n,'ufiles'} = int($4);
160 $filesys{$n,'sfiles'} = int($5);
161 $filesys{$n,'hfiles'} = int($6);
164 elsif (/^(.{15})(.{8}).(.{7})(.{8}).{8}(.{8}).(.{7})(.{8})/) {
165 $filesys{$n,'ublocks'} = int($2);
166 $filesys{$n,'sblocks'} = int($3);
167 $filesys{$n,'hblocks'} = int($4);
168 $filesys{$n,'ufiles'} = int($5);
169 $filesys{$n,'sfiles'} = int($6);
170 $filesys{$n,'hfiles'} = int($7);
171 $filesys{$n,'filesys'} = $1;
172 $filesys{$n,'filesys'} =~ s/^\s+//g;
180 # filesystem_users(filesystem)
181 # Fills the array %user with information about all users with quotas
182 # on this filesystem. This may not be all users on the system..
185 local($rep, @rep, $n, $what);
186 $rep = `$config{'user_repquota_command'} $_[0] 2>&1`;
187 if ($?) { return -1; }
188 @rep = split(/\n/, $rep);
189 @rep = grep { !/^root\s/ } @rep[3..$#rep];
190 for($n=0; $n<@rep; $n++) {
191 if ($rep[$n] =~ /(\S+)\s*[\-\+]{2}\s+(\d+)\s+(\d+)\s+(\d+)\s(.{0,15})\s(\d+)\s+(\d+)\s+(\d+)(.*)/ || $rep[$n] =~ /(\S+)\s+..(.{8})(.{8})(.{8})(.{7})(.{8})(.{8})(.{8})(.*)/) {
192 $user{$n,'user'} = $1;
193 $user{$n,'ublocks'} = int($2);
194 $user{$n,'sblocks'} = int($3);
195 $user{$n,'hblocks'} = int($4);
196 $user{$n,'gblocks'} = $5;
197 $user{$n,'ufiles'} = int($6);
198 $user{$n,'sfiles'} = int($7);
199 $user{$n,'hfiles'} = int($8);
200 $user{$n,'gfiles'} = $9;
201 $user{$n,'gblocks'} = &trunc_space($user{$n,'gblocks'});
202 $user{$n,'gfiles'} = &trunc_space($user{$n,'gfiles'});
208 # filesystem_groups(filesystem)
209 # Fills the array %group with information about all groups with quotas
210 # on this filesystem. This may not be all groups on the system..
211 sub filesystem_groups
213 local($rep, @rep, $n, $what);
214 $rep = `$config{'group_repquota_command'} $_[0] 2>&1`;
215 if ($?) { return -1; }
216 @rep = split(/\n/, $rep);
217 @rep = @rep[3..$#rep];
218 for($n=0; $n<@rep; $n++) {
219 if ($rep[$n] =~ /(\S+)\s*[\-\+]{2}\s+(\d+)\s+(\d+)\s+(\d+)\s(.{0,15})\s(\d+)\s+(\d+)\s+(\d+)(.*)/ || $rep[$n] =~ /(\S+)\s+..(.{8})(.{8})(.{8})(.{7})(.{8})(.{8})(.{8})/) {
220 $group{$n,'group'} = $1;
221 $group{$n,'ublocks'} = int($2);
222 $group{$n,'sblocks'} = int($3);
223 $group{$n,'hblocks'} = int($4);
224 $group{$n,'gblocks'} = $5;
225 $group{$n,'ufiles'} = int($6);
226 $group{$n,'sfiles'} = int($7);
227 $group{$n,'hfiles'} = int($8);
228 $group{$n,'gfiles'} = $9;
229 $group{$n,'gblocks'} = &trunc_space($group{$n,'gblocks'});
230 $group{$n,'gfiles'} = &trunc_space($group{$n,'gfiles'});
236 # edit_quota_file(data, filesys, sblocks, hblocks, sfiles, hfiles)
239 local($rv, $line, %mtab, @m);
240 @line = split(/\n/, $_[0]);
241 for($i=0; $i<@line; $i++) {
242 if ($line[$i] =~ /^(\S+): (blocks|kbytes) in use: (\d+), limits \(soft = (\d+), hard = (\d+)\)$/ && $1 eq $_[1]) {
243 # found lines to change
244 $rv .= "$1: $2 in use: $3, limits (soft = $_[2], hard = $_[3])\n";
245 $line[++$i] =~ /^\s*inodes in use: (\d+), limits \(soft = (\d+), hard = (\d+)\)$/;
246 $rv .= "\tinodes in use: $1, limits (soft = $_[4], hard = $_[5])\n";
248 else { $rv .= "$line[$i]\n"; }
253 # quotacheck(filesystem, mode)
254 # Runs quotacheck on some filesystem
257 $out = &backquote_logged("$config{'quotacheck_command'} $_[0] 2>&1");
258 if ($?) { return $out; }
262 # copy_user_quota(user, [user]+)
263 # Copy the quotas for some user to many others
266 for($i=1; $i<@_; $i++) {
267 $out = &backquote_logged("$config{'user_copy_command'} ".
268 quotemeta($_[0])." ".quotemeta($_[$i])." 2>&1");
269 if ($?) { return $out; }
274 # copy_group_quota(group, [group]+)
275 # Copy the quotas for some group to many others
278 for($i=1; $i<@_; $i++) {
279 $out = &backquote_logged("$config{'group_copy_command'} ".
280 quotemeta($_[0])." ".quotemeta($_[$i])." 2>&1");
281 if ($?) { return $out; }
287 # Returns 0 if grace time can be 0, 1 if zero grace means default
293 # get_user_grace(filesystem)
294 # Returns an array containing btime, bunits, ftime, funits
295 # The units can be 0=sec, 1=min, 2=hour, 3=day
298 local(@rv, %mtab, @m);
299 $ENV{'EDITOR'} = $ENV{'VISUAL'} = "cat";
300 open(GRACE, "$config{'user_grace_command'} $_[0] |");
302 if (/^(\S+): block grace period: (\d+) (\S+), file grace period: (\d+) (\S+)/ && $1 eq $_[0]) {
303 @rv = ($2, $name_to_unit{$3}, $4, $name_to_unit{$5});
310 # get_group_grace(filesystem)
311 # Returns an array containing btime, bunits, ftime, funits
312 # The units can be 0=sec, 1=min, 2=hour, 3=day
315 local(@rv, %mtab, @m);
316 $ENV{'EDITOR'} = $ENV{'VISUAL'} = "cat";
317 open(GRACE, "$config{'group_grace_command'} $_[0] |");
319 if (/^(\S+): block grace period: (\d+) (\S+), file grace period: (\d+) (\S+)/ && $1 eq $_[0]) {
320 @rv = ($2, $name_to_unit{$3}, $4, $name_to_unit{$5});
327 # edit_grace_file(data, filesystem, btime, bunits, ftime, funits)
330 local($rv, $line, @m, %mtab);
331 foreach $line (split(/\n/, $_[0])) {
332 if ($line =~ /^(\S+): block grace period: (\d+) (\S+), file grace period: (\d+) (\S+)/ && $1 eq $_[1]) {
334 $line = "$1: block grace period: $_[2] $unit_to_name{$_[3]}, file grace period: $_[4] $unit_to_name{$_[5]}";
342 # Returns an array of possible units for grace periods
345 return ($text{'grace_seconds'}, $text{'grace_minutes'}, $text{'grace_hours'},
346 $text{'grace_days'});
349 # fs_block_size(dir, device, filesystem)
350 # Returns the size of blocks on some filesystem, or undef if unknown.
351 # Always 1024, because the ENV setting above forces this.
357 %name_to_unit = ( "second", 0, "seconds", 0,
358 "minute", 1, "minutes", 1,
359 "hour", 2, "hours", 2,
362 foreach $k (keys %name_to_unit) {
363 $unit_to_name{$name_to_unit{$k}} = $k;