quota_cursor.c revision 1.4 1 /* $NetBSD: quota_cursor.c,v 1.4 2012/01/25 17:43:37 dholland Exp $ */
2 /*-
3 * Copyright (c) 2011 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by David A. Holland.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: quota_cursor.c,v 1.4 2012/01/25 17:43:37 dholland Exp $");
33
34 #include <stdlib.h>
35 #include <errno.h>
36
37 #include <quota.h>
38 #include "quotapvt.h"
39
40 struct quotacursor *
41 quota_opencursor(struct quotahandle *qh)
42 {
43 struct quotacursor *qc;
44 int8_t version;
45 int serrno;
46
47 switch (qh->qh_mode) {
48 case QUOTA_MODE_NFS:
49 errno = EOPNOTSUPP;
50 return NULL;
51
52 case QUOTA_MODE_PROPLIB:
53 if (__quota_proplib_getversion(qh, &version)) {
54 return NULL;
55 }
56 break;
57
58 case QUOTA_MODE_OLDFILES:
59 version = 1;
60 break;
61
62 default:
63 errno = EINVAL;
64 break;
65 }
66
67 /*
68 * For the time being at least the version 1 kernel code
69 * cannot do cursors.
70 */
71 if (version == 1 && !qh->qh_oldfilesopen) {
72 if (__quota_oldfiles_initialize(qh)) {
73 return NULL;
74 }
75 }
76
77 qc = malloc(sizeof(*qc));
78 if (qc == NULL) {
79 return NULL;
80 }
81
82 qc->qc_qh = qh;
83
84 if (version == 1) {
85 qc->qc_type = QC_OLDFILES;
86 qc->u.qc_oldfiles = __quota_oldfiles_cursor_create(qh);
87 if (qc->u.qc_oldfiles == NULL) {
88 serrno = errno;
89 free(qc);
90 errno = serrno;
91 return NULL;
92 }
93 } else {
94 qc->qc_type = QC_PROPLIB;
95 qc->u.qc_proplib = __quota_proplib_cursor_create();
96 if (qc->u.qc_proplib == NULL) {
97 serrno = errno;
98 free(qc);
99 errno = serrno;
100 return NULL;
101 }
102 }
103 return qc;
104 }
105
106 void
107 quotacursor_close(struct quotacursor *qc)
108 {
109 switch (qc->qc_type) {
110 case QC_PROPLIB:
111 __quota_proplib_cursor_destroy(qc->u.qc_proplib);
112 break;
113 case QC_OLDFILES:
114 __quota_oldfiles_cursor_destroy(qc->u.qc_oldfiles);
115 break;
116 }
117 free(qc);
118 }
119
120 int
121 quotacursor_skipidtype(struct quotacursor *qc, unsigned idtype)
122 {
123 switch (qc->qc_type) {
124 case QC_PROPLIB:
125 return __quota_proplib_cursor_skipidtype(qc->u.qc_proplib,
126 idtype);
127 case QC_OLDFILES:
128 return __quota_oldfiles_cursor_skipidtype(qc->u.qc_oldfiles,
129 idtype);
130 }
131 errno = EINVAL;
132 return -1;
133 }
134
135 int
136 quotacursor_get(struct quotacursor *qc,
137 struct quotakey *qk_ret, struct quotaval *qv_ret)
138 {
139 switch (qc->qc_type) {
140 case QC_PROPLIB:
141 return __quota_proplib_cursor_get(qc->qc_qh, qc->u.qc_proplib,
142 qk_ret, qv_ret);
143 case QC_OLDFILES:
144 return __quota_oldfiles_cursor_get(qc->qc_qh,
145 qc->u.qc_oldfiles,
146 qk_ret, qv_ret);
147 }
148 errno = EINVAL;
149 return -1;
150 }
151
152 int
153 quotacursor_getn(struct quotacursor *qc,
154 struct quotakey *keys, struct quotaval *vals,
155 unsigned maxnum)
156 {
157 switch (qc->qc_type) {
158 case QC_PROPLIB:
159 return __quota_proplib_cursor_getn(qc->qc_qh, qc->u.qc_proplib,
160 keys, vals, maxnum);
161 case QC_OLDFILES:
162 return __quota_oldfiles_cursor_getn(qc->qc_qh,
163 qc->u.qc_oldfiles,
164 keys, vals, maxnum);
165 }
166 errno = EINVAL;
167 return -1;
168 }
169
170 int
171 quotacursor_atend(struct quotacursor *qc)
172 {
173 switch (qc->qc_type) {
174 case QC_PROPLIB:
175 return __quota_proplib_cursor_atend(qc->qc_qh,
176 qc->u.qc_proplib);
177 case QC_OLDFILES:
178 return __quota_oldfiles_cursor_atend(qc->u.qc_oldfiles);
179 }
180 errno = EINVAL;
181 return -1;
182 }
183
184 int
185 quotacursor_rewind(struct quotacursor *qc)
186 {
187 switch (qc->qc_type) {
188 case QC_PROPLIB:
189 return __quota_proplib_cursor_rewind(qc->u.qc_proplib);
190 case QC_OLDFILES:
191 return __quota_oldfiles_cursor_rewind(qc->u.qc_oldfiles);
192 }
193 errno = EINVAL;
194 return -1;
195 }
196