]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/quotas.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / quotas.c
1 /*
2 * "$Id: quotas.c 6365 2007-03-19 20:56:57Z mike $"
3 *
4 * Quota routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2007 by Easy Software Products.
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 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * cupsdFreeQuotas() - Free quotas for a printer.
27 * cupsdUpdateQuota() - Update quota data for the specified printer and user.
28 * add_quota() - Add a quota record for this printer and user.
29 * compare_quotas() - Compare two quota records...
30 * find_quota() - Find a quota record.
31 */
32
33 /*
34 * Include necessary headers...
35 */
36
37 #include "cupsd.h"
38
39
40 /*
41 * Local functions...
42 */
43
44 static cupsd_quota_t *add_quota(cupsd_printer_t *p, const char *username);
45 static int compare_quotas(const cupsd_quota_t *q1,
46 const cupsd_quota_t *q2);
47 static cupsd_quota_t *find_quota(cupsd_printer_t *p, const char *username);
48
49
50 /*
51 * 'cupsdFreeQuotas()' - Free quotas for a printer.
52 */
53
54 void
55 cupsdFreeQuotas(cupsd_printer_t *p) /* I - Printer */
56 {
57 cupsd_quota_t *q; /* Current quota record */
58
59
60 if (!p)
61 return;
62
63 for (q = (cupsd_quota_t *)cupsArrayFirst(p->quotas);
64 q;
65 q = (cupsd_quota_t *)cupsArrayNext(p->quotas))
66 free(q);
67
68 cupsArrayDelete(p->quotas);
69
70 p->quotas = NULL;
71 }
72
73
74 /*
75 * 'cupsdUpdateQuota()' - Update quota data for the specified printer and user.
76 */
77
78 cupsd_quota_t * /* O - Quota data */
79 cupsdUpdateQuota(
80 cupsd_printer_t *p, /* I - Printer */
81 const char *username, /* I - User */
82 int pages, /* I - Number of pages */
83 int k) /* I - Number of kilobytes */
84 {
85 cupsd_quota_t *q; /* Quota data */
86 cupsd_job_t *job; /* Current job */
87 time_t curtime; /* Current time */
88 ipp_attribute_t *attr; /* Job attribute */
89
90
91 if (!p || !username)
92 return (NULL);
93
94 if (!p->k_limit && !p->page_limit)
95 return (NULL);
96
97 if ((q = find_quota(p, username)) == NULL)
98 return (NULL);
99
100 cupsdLogMessage(CUPSD_LOG_DEBUG,
101 "cupsdUpdateQuota: p=%s username=%s pages=%d k=%d",
102 p->name, username, pages, k);
103
104 #if defined(__APPLE__) && defined(HAVE_DLFCN_H)
105 /*
106 * Use Apple PrintService quota enforcement if installed (X Server only)
107 */
108
109 if (AppleQuotas && PSQUpdateQuotaProc)
110 {
111 q->page_count = (*PSQUpdateQuotaProc)(p->name, p->info, username, pages, 0);
112
113 return (q);
114 }
115 #endif /* __APPLE__ && HAVE_DLFCN_H */
116
117 curtime = time(NULL);
118
119 if (curtime < q->next_update)
120 {
121 q->page_count += pages;
122 q->k_count += k;
123
124 return (q);
125 }
126
127 if (p->quota_period)
128 curtime -= p->quota_period;
129 else
130 curtime = 0;
131
132 q->next_update = 0;
133 q->page_count = 0;
134 q->k_count = 0;
135
136 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
137 job;
138 job = (cupsd_job_t *)cupsArrayNext(Jobs))
139 {
140 if (strcasecmp(job->dest, p->name) != 0 ||
141 strcasecmp(job->username, q->username) != 0)
142 continue;
143
144 if ((attr = ippFindAttribute(job->attrs, "time-at-completion",
145 IPP_TAG_INTEGER)) == NULL)
146 if ((attr = ippFindAttribute(job->attrs, "time-at-processing",
147 IPP_TAG_INTEGER)) == NULL)
148 attr = ippFindAttribute(job->attrs, "time-at-creation",
149 IPP_TAG_INTEGER);
150
151 if (attr == NULL)
152 break;
153
154 if (attr->values[0].integer < curtime)
155 {
156 if (JobAutoPurge)
157 cupsdCancelJob(job, 1, IPP_JOB_CANCELED);
158
159 continue;
160 }
161
162 if (q->next_update == 0)
163 q->next_update = attr->values[0].integer + p->quota_period;
164
165 if ((attr = ippFindAttribute(job->attrs, "job-media-sheets-completed",
166 IPP_TAG_INTEGER)) != NULL)
167 q->page_count += attr->values[0].integer;
168
169 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
170 IPP_TAG_INTEGER)) != NULL)
171 q->k_count += attr->values[0].integer;
172 }
173
174 return (q);
175 }
176
177
178 /*
179 * 'add_quota()' - Add a quota record for this printer and user.
180 */
181
182 static cupsd_quota_t * /* O - Quota data */
183 add_quota(cupsd_printer_t *p, /* I - Printer */
184 const char *username) /* I - User */
185 {
186 cupsd_quota_t *q; /* New quota data */
187
188
189 if (!p || !username)
190 return (NULL);
191
192 if (!p->quotas)
193 p->quotas = cupsArrayNew((cups_array_func_t)compare_quotas, NULL);
194
195 if (!p->quotas)
196 return (NULL);
197
198 if ((q = calloc(1, sizeof(cupsd_quota_t))) == NULL)
199 return (NULL);
200
201 strlcpy(q->username, username, sizeof(q->username));
202
203 cupsArrayAdd(p->quotas, q);
204
205 return (q);
206 }
207
208
209 /*
210 * 'compare_quotas()' - Compare two quota records...
211 */
212
213 static int /* O - Result of comparison */
214 compare_quotas(const cupsd_quota_t *q1, /* I - First quota record */
215 const cupsd_quota_t *q2) /* I - Second quota record */
216 {
217 return (strcasecmp(q1->username, q2->username));
218 }
219
220
221 /*
222 * 'find_quota()' - Find a quota record.
223 */
224
225 static cupsd_quota_t * /* O - Quota data */
226 find_quota(cupsd_printer_t *p, /* I - Printer */
227 const char *username) /* I - User */
228 {
229 cupsd_quota_t *q, /* Quota data pointer */
230 match; /* Search data */
231
232
233 if (!p || !username)
234 return (NULL);
235
236 strlcpy(match.username, username, sizeof(match.username));
237
238 if ((q = (cupsd_quota_t *)cupsArrayFind(p->quotas, &match)) != NULL)
239 return (q);
240 else
241 return (add_quota(p, username));
242 }
243
244
245 /*
246 * End of "$Id: quotas.c 6365 2007-03-19 20:56:57Z mike $".
247 */