]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/quotas.c
f569bbd5572b3c84a491c51b3bef483b31fc287b
[thirdparty/cups.git] / scheduler / quotas.c
1 /*
2 * "$Id: quotas.c,v 1.4 2001/01/22 15:04:01 mike Exp $"
3 *
4 * Quota routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2001 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-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
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...
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));
74 strncpy(q->username, username, sizeof(q->username) - 1);
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 {
103 strncpy(match.username, username, sizeof(match.username) - 1);
104 match.username[sizeof(match.username) - 1] = '\0';
105
106 q = bsearch(&match, p->quotas, p->num_quotas, sizeof(quota_t),
107 (int(*)(const void *, const void *))compare);
108 }
109
110 if (q)
111 return (q);
112 else
113 return (AddQuota(p, username));
114 }
115
116
117 /*
118 * 'FreeQuotas()' - Free quotas for a printer.
119 */
120
121 void
122 FreeQuotas(printer_t *p) /* I - Printer */
123 {
124 if (!p)
125 return;
126
127 if (p->num_quotas)
128 free(p->quotas);
129
130 p->num_quotas = 0;
131 p->quotas = NULL;
132 }
133
134
135 /*
136 * 'UpdateQuota()' - Update quota data for the specified printer and user.
137 */
138
139 quota_t * /* O - Quota data */
140 UpdateQuota(printer_t *p, /* I - Printer */
141 const char *username, /* I - User */
142 int pages, /* I - Number of pages */
143 int k) /* I - Number of kilobytes */
144 {
145 quota_t *q; /* Quota data */
146 job_t *job, /* Current job */
147 *next; /* Next job */
148 time_t curtime; /* Current time */
149 ipp_attribute_t *attr; /* Job attribute */
150
151
152 if (!p || !username)
153 return (NULL);
154
155 if (!p->k_limit && !p->page_limit)
156 return (NULL);
157
158 if ((q = FindQuota(p, username)) == NULL)
159 return (NULL);
160
161 curtime = time(NULL);
162
163 if (curtime < q->next_update)
164 {
165 q->page_count += pages;
166 q->k_count += k;
167
168 return (q);
169 }
170
171 if (p->quota_period)
172 curtime -= p->quota_period;
173 else
174 curtime = 0;
175
176 q->next_update = 0;
177 q->page_count = 0;
178 q->k_count = 0;
179
180 for (job = Jobs; job; job = next)
181 {
182 next = job->next;
183
184 if (strcasecmp(job->dest, p->name) != 0 ||
185 strcmp(job->username, q->username) != 0)
186 continue;
187
188 if ((attr = ippFindAttribute(job->attrs, "time-at-completion",
189 IPP_TAG_INTEGER)) == NULL)
190 if ((attr = ippFindAttribute(job->attrs, "time-at-processing",
191 IPP_TAG_INTEGER)) == NULL)
192 attr = ippFindAttribute(job->attrs, "time-at-creation",
193 IPP_TAG_INTEGER);
194
195 if (attr == NULL)
196 break;
197
198 if (attr->values[0].integer < curtime)
199 {
200 if (JobAutoPurge)
201 CancelJob(job->id, 1);
202
203 continue;
204 }
205
206 if (q->next_update == 0)
207 q->next_update = attr->values[0].integer + p->quota_period;
208
209 if ((attr = ippFindAttribute(job->attrs, "job-media-sheets-completed",
210 IPP_TAG_INTEGER)) != NULL)
211 q->page_count += attr->values[0].integer;
212
213 if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
214 IPP_TAG_INTEGER)) != NULL)
215 q->k_count += attr->values[0].integer;
216 }
217
218 return (q);
219 }
220
221
222 /*
223 * 'compare()' - Compare two quota records...
224 */
225
226 static int /* O - Result of comparison */
227 compare(const quota_t *q1, /* I - First quota record */
228 const quota_t *q2) /* I - Second quota record */
229 {
230 return (strcmp(q1->username, q2->username));
231 }
232
233
234 /*
235 * End of "$Id: quotas.c,v 1.4 2001/01/22 15:04:01 mike Exp $".
236 */