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