]>
Commit | Line | Data |
---|---|---|
d7845573 | 1 | /* |
17438bf4 | 2 | * "$Id: quotas.c,v 1.7 2002/05/16 13:45:03 mike Exp $" |
d7845573 | 3 | * |
04d756fc | 4 | * Quota routines for the Common UNIX Printing System (CUPS). |
d7845573 | 5 | * |
efb2f309 | 6 | * Copyright 1997-2002 by Easy Software Products. |
d7845573 | 7 | * |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636-3111 USA | |
19 | * | |
20 | * Voice: (301) 373-9603 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
04d756fc | 26 | * AddQuota() - Add a quota record for this printer and user. |
27 | * FindQuota() - Find a quota record. | |
28 | * FreeQuotas() - Free quotas for a printer. | |
29 | * UpdateQuota() - Update quota data for the specified printer and user. | |
30 | * compare() - Compare two quota records... | |
d7845573 | 31 | */ |
32 | ||
33 | /* | |
34 | * Include necessary headers... | |
35 | */ | |
36 | ||
37 | #include "cupsd.h" | |
38 | ||
39 | ||
40 | /* | |
41 | * Local functions... | |
42 | */ | |
43 | ||
44 | static int compare(const quota_t *q1, const quota_t *q2); | |
45 | ||
46 | ||
47 | /* | |
48 | * 'AddQuota()' - Add a quota record for this printer and user. | |
49 | */ | |
50 | ||
51 | quota_t * /* O - Quota data */ | |
52 | AddQuota(printer_t *p, /* I - Printer */ | |
53 | const char *username) /* I - User */ | |
54 | { | |
55 | quota_t *q; /* New quota data */ | |
56 | ||
57 | ||
58 | if (!p || !username) | |
59 | return (NULL); | |
60 | ||
61 | if (p->num_quotas == 0) | |
62 | q = malloc(sizeof(quota_t)); | |
63 | else | |
64 | q = realloc(p->quotas, sizeof(quota_t) * (p->num_quotas + 1)); | |
65 | ||
66 | if (!q) | |
67 | return (NULL); | |
68 | ||
69 | p->quotas = q; | |
70 | q += p->num_quotas; | |
71 | p->num_quotas ++; | |
72 | ||
73 | memset(q, 0, sizeof(quota_t)); | |
17438bf4 | 74 | strlcpy(q->username, username, sizeof(q->username)); |
d7845573 | 75 | |
76 | if (p->num_quotas > 1) | |
77 | qsort(p->quotas, p->num_quotas, sizeof(quota_t), | |
78 | (int (*)(const void *, const void *))compare); | |
79 | ||
80 | return (FindQuota(p, username)); | |
81 | } | |
82 | ||
83 | ||
84 | /* | |
85 | * 'FindQuota()' - Find a quota record. | |
86 | */ | |
87 | ||
88 | quota_t * /* O - Quota data */ | |
89 | FindQuota(printer_t *p, /* I - Printer */ | |
90 | const char *username) /* I - User */ | |
91 | { | |
92 | quota_t *q, /* Quota data pointer */ | |
93 | match; /* Search data */ | |
94 | ||
95 | ||
96 | if (!p || !username) | |
97 | return (NULL); | |
98 | ||
99 | if (p->num_quotas == 0) | |
100 | q = NULL; | |
101 | else | |
102 | { | |
17438bf4 | 103 | strlcpy(match.username, username, sizeof(match.username)); |
d7845573 | 104 | |
105 | q = bsearch(&match, p->quotas, p->num_quotas, sizeof(quota_t), | |
106 | (int(*)(const void *, const void *))compare); | |
107 | } | |
108 | ||
109 | if (q) | |
110 | return (q); | |
111 | else | |
112 | return (AddQuota(p, username)); | |
113 | } | |
114 | ||
115 | ||
04d756fc | 116 | /* |
117 | * 'FreeQuotas()' - Free quotas for a printer. | |
118 | */ | |
119 | ||
120 | void | |
121 | FreeQuotas(printer_t *p) /* I - Printer */ | |
122 | { | |
123 | if (!p) | |
124 | return; | |
125 | ||
126 | if (p->num_quotas) | |
127 | free(p->quotas); | |
128 | ||
129 | p->num_quotas = 0; | |
130 | p->quotas = NULL; | |
131 | } | |
132 | ||
133 | ||
d7845573 | 134 | /* |
135 | * 'UpdateQuota()' - Update quota data for the specified printer and user. | |
136 | */ | |
137 | ||
138 | quota_t * /* O - Quota data */ | |
139 | UpdateQuota(printer_t *p, /* I - Printer */ | |
140 | const char *username, /* I - User */ | |
141 | int pages, /* I - Number of pages */ | |
142 | int k) /* I - Number of kilobytes */ | |
143 | { | |
144 | quota_t *q; /* Quota data */ | |
145 | job_t *job, /* Current job */ | |
146 | *next; /* Next job */ | |
147 | time_t curtime; /* Current time */ | |
148 | ipp_attribute_t *attr; /* Job attribute */ | |
149 | ||
150 | ||
151 | if (!p || !username) | |
152 | return (NULL); | |
153 | ||
b521f3fc | 154 | if (!p->k_limit && !p->page_limit) |
155 | return (NULL); | |
156 | ||
d7845573 | 157 | if ((q = FindQuota(p, username)) == NULL) |
158 | return (NULL); | |
159 | ||
160 | curtime = time(NULL); | |
161 | ||
162 | if (curtime < q->next_update) | |
163 | { | |
164 | q->page_count += pages; | |
165 | q->k_count += k; | |
166 | ||
167 | return (q); | |
168 | } | |
169 | ||
04d756fc | 170 | if (p->quota_period) |
171 | curtime -= p->quota_period; | |
172 | else | |
173 | curtime = 0; | |
174 | ||
d7845573 | 175 | q->next_update = 0; |
176 | q->page_count = 0; | |
177 | q->k_count = 0; | |
178 | ||
179 | for (job = Jobs; job; job = next) | |
180 | { | |
181 | next = job->next; | |
182 | ||
183 | if (strcasecmp(job->dest, p->name) != 0 || | |
f2ac2ffc | 184 | strcasecmp(job->username, q->username) != 0) |
d7845573 | 185 | continue; |
186 | ||
187 | if ((attr = ippFindAttribute(job->attrs, "time-at-completion", | |
188 | IPP_TAG_INTEGER)) == NULL) | |
189 | if ((attr = ippFindAttribute(job->attrs, "time-at-processing", | |
190 | IPP_TAG_INTEGER)) == NULL) | |
191 | attr = ippFindAttribute(job->attrs, "time-at-creation", | |
192 | IPP_TAG_INTEGER); | |
193 | ||
194 | if (attr == NULL) | |
195 | break; | |
196 | ||
197 | if (attr->values[0].integer < curtime) | |
198 | { | |
199 | if (JobAutoPurge) | |
200 | CancelJob(job->id, 1); | |
201 | ||
202 | continue; | |
203 | } | |
204 | ||
205 | if (q->next_update == 0) | |
206 | q->next_update = attr->values[0].integer + p->quota_period; | |
207 | ||
208 | if ((attr = ippFindAttribute(job->attrs, "job-media-sheets-completed", | |
209 | IPP_TAG_INTEGER)) != NULL) | |
210 | q->page_count += attr->values[0].integer; | |
211 | ||
212 | if ((attr = ippFindAttribute(job->attrs, "job-k-octets", | |
213 | IPP_TAG_INTEGER)) != NULL) | |
214 | q->k_count += attr->values[0].integer; | |
215 | } | |
216 | ||
217 | return (q); | |
218 | } | |
219 | ||
220 | ||
221 | /* | |
222 | * 'compare()' - Compare two quota records... | |
223 | */ | |
224 | ||
225 | static int /* O - Result of comparison */ | |
226 | compare(const quota_t *q1, /* I - First quota record */ | |
227 | const quota_t *q2) /* I - Second quota record */ | |
228 | { | |
f2ac2ffc | 229 | return (strcasecmp(q1->username, q2->username)); |
d7845573 | 230 | } |
231 | ||
232 | ||
233 | /* | |
17438bf4 | 234 | * End of "$Id: quotas.c,v 1.7 2002/05/16 13:45:03 mike Exp $". |
d7845573 | 235 | */ |