]> 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/*
f7deaa1a 2 * "$Id: quotas.c 5969 2006-09-19 20:09:24Z 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 *
ef416fc2 26 * cupsdFreeQuotas() - Free quotas for a printer.
27 * cupsdUpdateQuota() - Update quota data for the specified printer and user.
e1d6a774 28 * add_quota() - Add a quota record for this printer and user.
fa73b229 29 * compare_quotas() - Compare two quota records...
e1d6a774 30 * find_quota() - Find a quota record.
ef416fc2 31 */
32
33/*
34 * Include necessary headers...
35 */
36
37#include "cupsd.h"
38
39
40/*
41 * Local functions...
42 */
43
e1d6a774 44static cupsd_quota_t *add_quota(cupsd_printer_t *p, const char *username);
45static int compare_quotas(const cupsd_quota_t *q1,
46 const cupsd_quota_t *q2);
47static cupsd_quota_t *find_quota(cupsd_printer_t *p, const char *username);
ef416fc2 48
49
50/*
51 * 'cupsdFreeQuotas()' - Free quotas for a printer.
52 */
53
54void
fa73b229 55cupsdFreeQuotas(cupsd_printer_t *p) /* I - Printer */
ef416fc2 56{
fa73b229 57 cupsd_quota_t *q; /* Current quota record */
58
59
ef416fc2 60 if (!p)
61 return;
62
fa73b229 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);
ef416fc2 69
fa73b229 70 p->quotas = NULL;
ef416fc2 71}
72
73
74/*
75 * 'cupsdUpdateQuota()' - Update quota data for the specified printer and user.
76 */
77
78cupsd_quota_t * /* O - Quota data */
79cupsdUpdateQuota(
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
e1d6a774 97 if ((q = find_quota(p, username)) == NULL)
ef416fc2 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 curtime = time(NULL);
105
106 if (curtime < q->next_update)
107 {
108 q->page_count += pages;
109 q->k_count += k;
110
111 return (q);
112 }
113
114 if (p->quota_period)
115 curtime -= p->quota_period;
116 else
117 curtime = 0;
118
119 q->next_update = 0;
120 q->page_count = 0;
121 q->k_count = 0;
122
123 for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
124 job;
125 job = (cupsd_job_t *)cupsArrayNext(Jobs))
126 {
127 if (strcasecmp(job->dest, p->name) != 0 ||
128 strcasecmp(job->username, q->username) != 0)
129 continue;
130
131 if ((attr = ippFindAttribute(job->attrs, "time-at-completion",
132 IPP_TAG_INTEGER)) == NULL)
133 if ((attr = ippFindAttribute(job->attrs, "time-at-processing",
134 IPP_TAG_INTEGER)) == NULL)
135 attr = ippFindAttribute(job->attrs, "time-at-creation",
136 IPP_TAG_INTEGER);
137
138 if (attr == NULL)
139 break;
140
141 if (attr->values[0].integer < curtime)
142 {
143 if (JobAutoPurge)
07725fee 144 cupsdCancelJob(job, 1, IPP_JOB_CANCELED);
ef416fc2 145
146 continue;
147 }
148
149 if (q->next_update == 0)
150 q->next_update = attr->values[0].integer + p->quota_period;
151
152 if ((attr = ippFindAttribute(job->attrs, "job-media-sheets-completed",
153 IPP_TAG_INTEGER)) != NULL)
154 q->page_count += attr->values[0].integer;
155
156 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
157 IPP_TAG_INTEGER)) != NULL)
158 q->k_count += attr->values[0].integer;
159 }
160
161 return (q);
162}
163
164
e1d6a774 165/*
166 * 'add_quota()' - Add a quota record for this printer and user.
167 */
168
07725fee 169static cupsd_quota_t * /* O - Quota data */
e1d6a774 170add_quota(cupsd_printer_t *p, /* I - Printer */
171 const char *username) /* I - User */
172{
173 cupsd_quota_t *q; /* New quota data */
174
175
176 if (!p || !username)
177 return (NULL);
178
179 if (!p->quotas)
180 p->quotas = cupsArrayNew((cups_array_func_t)compare_quotas, NULL);
181
182 if (!p->quotas)
183 return (NULL);
184
185 if ((q = calloc(1, sizeof(cupsd_quota_t))) == NULL)
186 return (NULL);
187
188 strlcpy(q->username, username, sizeof(q->username));
189
190 cupsArrayAdd(p->quotas, q);
191
192 return (q);
193}
194
195
ef416fc2 196/*
fa73b229 197 * 'compare_quotas()' - Compare two quota records...
ef416fc2 198 */
199
200static int /* O - Result of comparison */
fa73b229 201compare_quotas(const cupsd_quota_t *q1, /* I - First quota record */
202 const cupsd_quota_t *q2) /* I - Second quota record */
ef416fc2 203{
204 return (strcasecmp(q1->username, q2->username));
205}
206
207
208/*
e1d6a774 209 * 'find_quota()' - Find a quota record.
210 */
211
07725fee 212static cupsd_quota_t * /* O - Quota data */
e1d6a774 213find_quota(cupsd_printer_t *p, /* I - Printer */
214 const char *username) /* I - User */
215{
216 cupsd_quota_t *q, /* Quota data pointer */
217 match; /* Search data */
218
219
220 if (!p || !username)
221 return (NULL);
222
223 strlcpy(match.username, username, sizeof(match.username));
224
225 if ((q = (cupsd_quota_t *)cupsArrayFind(p->quotas, &match)) != NULL)
226 return (q);
227 else
228 return (add_quota(p, username));
229}
230
231
232/*
f7deaa1a 233 * End of "$Id: quotas.c 5969 2006-09-19 20:09:24Z mike $".
ef416fc2 234 */