2 # Quota functions for openbsd
7 if (-x "/usr/etc/repquota") {
11 return "The quotas product does not appear to be installed on ".
17 # Returns 1 for user quotas, 2 for group quotas or 3 for both
23 # free_space(filesystem)
24 # Returns an array containing btotal, bfree, ftotal, ffree
28 `df -i '$_[0]'` =~ /Mounted\n\S+\s+\S+\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/;
29 return ($1, $3, $5+$6, $6);
32 # quota_can(&mnttab, &fstab)
33 # Can this filesystem type support quotas?
34 # 0 = No quota support (or not turned on in /etc/fstab)
35 # 1 = User quotas only
36 # 2 = Group quotas only
37 # 3 = User and group quotas
40 return $_[1]->[2] eq 'xfs' && $_[1]->[3] =~ /quota|qnoenforce/ ? 1 : 0;
43 # quota_now(&mnttab, &fstab)
44 # Are quotas currently active?
46 # 1 = User quotas active
47 # 2 = Group quotas active
49 # Adding 4 means they cannot be turned off (such as for XFS)
52 return $_[0]->[3] =~ /quota|qnoenforce/ ? 5 : 0;
55 # quotaon(filesystem, mode)
56 # Activate quotas and create quota files for some filesystem. The mode can
57 # be 1 for user only, 2 for group only or 3 for user and group
60 # Does nothing, because XFS quotas are only enabled at mount/boot
64 # quotaoff(filesystem, mode)
65 # Turn off quotas for some filesystem
68 # Does nothing, because XFS quotas are never turned off
72 # user_filesystems(user)
73 # Fills the array %filesys with details of all filesystem some user has
78 open(QUOTA, "$config{'user_quota_command'} ".quotemeta($_[0])." |");
79 $n=0; while(<QUOTA>) {
81 if (/^\s*(Disk|Filesystem)/) { next; }
84 $filesys{$n,'filesys'} = $1;
85 <QUOTA> =~ /^\s+(\d+)\s+(\d+)\s+(\d+)\s.{0,17}\s(\d+)\s+(\d+)\s+(\d+)/;
86 $filesys{$n,'ublocks'} = int($1);
87 $filesys{$n,'sblocks'} = int($2);
88 $filesys{$n,'hblocks'} = int($3);
89 $filesys{$n,'ufiles'} = int($4);
90 $filesys{$n,'sfiles'} = int($5);
91 $filesys{$n,'hfiles'} = int($6);
94 elsif (/^(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s.{0,17}\s(\d+)\s+(\d+)\s+(\d+)/) {
95 $filesys{$n,'ublocks'} = int($2);
96 $filesys{$n,'sblocks'} = int($3);
97 $filesys{$n,'hblocks'} = int($4);
98 $filesys{$n,'ufiles'} = int($5);
99 $filesys{$n,'sfiles'} = int($6);
100 $filesys{$n,'hfiles'} = int($7);
101 $filesys{$n,'filesys'} = $1;
102 $filesys{$n,'filesys'} =~ s/^\s+//g;
110 # filesystem_users(filesystem)
111 # Fills the array %user with information about all users with quotas
112 # on this filesystem. This may not be all users on the system..
115 local($rep, @rep, $n = 0, $r);
116 $rep = `$config{'user_repquota_command'} $_[0] 2>&1`;
117 if ($?) { return -1; }
118 @rep = split(/\n/, $rep);
120 if ($r =~ /(\S+)\s*[\-\+]{2}\s+(\d+)\s+(\d+)\s+(\d+)\s.{0,16}\s(\d+)\s+(\d+)\s+(\d+)/) {
121 $user{$n,'user'} = $1;
122 $user{$n,'ublocks'} = int($2);
123 $user{$n,'sblocks'} = int($3);
124 $user{$n,'hblocks'} = int($4);
125 $user{$n,'ufiles'} = int($5);
126 $user{$n,'sfiles'} = int($6);
127 $user{$n,'hfiles'} = int($7);
134 # edit_quota_file(data, filesys, sblocks, hblocks, sfiles, hfiles)
137 local($rv, $line, %mtab, @m);
138 @line = split(/\n/, $_[0]);
139 for($i=0; $i<@line; $i++) {
140 if ($line[$i] =~ /^fs\s+(\S+)\s+kbytes\s+\(soft = (\d+), hard = (\d+)\)\s+inodes\s+\(soft = (\d+), hard = (\d+)\)/ && $1 eq $_[1]) {
141 # found line to change
142 $rv .= "fs $1 kbytes (soft = $_[2], hard = $_[3]) inodes (soft = $_[4], hard = $_[5])\n";
144 else { $rv .= "$line[$i]\n"; }
149 # quotacheck(filesystem, mode)
150 # Runs quotacheck on some filesystem
153 $out = &backquote_logged("$config{'quotacheck_command'} $_[0] 2>&1");
154 if ($?) { return $out; }
158 # copy_user_quota(user, [user]+)
159 # Copy the quotas for some user to many others
162 for($i=1; $i<@_; $i++) {
163 $out = &backquote_logged("$config{'user_copy_command'} ".
164 quotemeta($_[0])." ".quotemeta($_[$i])." 2>&1");
165 if ($?) { return $out; }
171 # Returns 0 if grace time can be 0, 1 if zero grace means default
177 # get_user_grace(filesystem)
178 # Returns an array containing btime, bunits, ftime, funits
179 # The units can be 0=sec, 1=min, 2=hour, 3=day
182 local(@rv, %mtab, @m);
183 $ENV{'EDITOR'} = $ENV{'VISUAL'} = "cat";
184 open(GRACE, "$config{'user_grace_command'} $_[0] 2>/dev/null |");
186 if (/^fs\s+(\S+)\s+kbytes\s+time\s+limit\s+=\s+(\S+)\s+(\S+),\s+files\s+time\s+limit\s+=\s+(\S+)\s+(\S+)/) {
187 @rv = ($2, $name_to_unit{$3}, $4, $name_to_unit{$5});
194 # edit_grace_file(data, filesystem, btime, bunits, ftime, funits)
197 local($rv, $line, @m, %mtab);
198 foreach $line (split(/\n/, $_[0])) {
199 if ($line =~ /^fs\s+(\S+)\s+kbytes\s+time\s+limit\s+=\s+(\S+)\s+(\S+),\s+files\s+time\s+limit\s+=\s+(\S+)\s+(\S+)/ && $1 eq $_[1]) {
201 $line = "fs $1 kbytes time limit = $_[2] $unit_to_name{$_[3]}, files time limit = $_[4] $unit_to_name{$_[5]}";
209 # Returns an array of possible units for grace periods
212 return ($text{'grace_seconds'}, $text{'grace_minutes'}, $text{'grace_hours'},
213 $text{'grace_days'}, $text{'grace_weeks'}, $text{'grace_months'});
216 %name_to_unit = ( "second", 0, "seconds", 0,
217 "minute", 1, "minutes", 1,
218 "hour", 2, "hours", 2,
220 "week", 4, "weeks", 4,
221 "month", 5, "months", 5,
223 foreach $k (keys %name_to_unit) {
224 $unit_to_name{$name_to_unit{$k}} = $k;