xref: /netbsd-src/lib/libquota/quota_cursor.c (revision 1897181a7231d5fc7ab48994d1447fcbc4e13a49)
1 /*	$NetBSD: quota_cursor.c,v 1.3 2012/01/09 15:41:58 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.3 2012/01/09 15:41:58 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 	if (qh->qh_isnfs) {
48 		errno = EOPNOTSUPP;
49 		return NULL;
50 	}
51 
52 	if (__quota_proplib_getversion(qh, &version)) {
53 		return NULL;
54 	}
55 
56 	/*
57 	 * For the time being at least the version 1 kernel code
58 	 * cannot do cursors.
59 	 */
60 	if (version == 1 && !qh->qh_hasoldfiles) {
61 		if (__quota_oldfiles_initialize(qh)) {
62 			return NULL;
63 		}
64 	}
65 
66 	qc = malloc(sizeof(*qc));
67 	if (qc == NULL) {
68 		return NULL;
69 	}
70 
71 	qc->qc_qh = qh;
72 
73 	if (version == 1) {
74 		qc->qc_type = QC_OLDFILES;
75 		qc->u.qc_oldfiles = __quota_oldfiles_cursor_create(qh);
76 		if (qc->u.qc_oldfiles == NULL) {
77 			serrno = errno;
78 			free(qc);
79 			errno = serrno;
80 			return NULL;
81 		}
82 	} else {
83 		qc->qc_type = QC_PROPLIB;
84 		qc->u.qc_proplib = __quota_proplib_cursor_create();
85 		if (qc->u.qc_proplib == NULL) {
86 			serrno = errno;
87 			free(qc);
88 			errno = serrno;
89 			return NULL;
90 		}
91 	}
92 	return qc;
93 }
94 
95 void
96 quotacursor_close(struct quotacursor *qc)
97 {
98 	switch (qc->qc_type) {
99 	    case QC_PROPLIB:
100 		__quota_proplib_cursor_destroy(qc->u.qc_proplib);
101 		break;
102 	    case QC_OLDFILES:
103 		__quota_oldfiles_cursor_destroy(qc->u.qc_oldfiles);
104 		break;
105 	}
106 	free(qc);
107 }
108 
109 int
110 quotacursor_skipidtype(struct quotacursor *qc, unsigned idtype)
111 {
112 	switch (qc->qc_type) {
113 	    case QC_PROPLIB:
114 		return __quota_proplib_cursor_skipidtype(qc->u.qc_proplib,
115 							 idtype);
116 	    case QC_OLDFILES:
117 		return __quota_oldfiles_cursor_skipidtype(qc->u.qc_oldfiles,
118 							  idtype);
119 	}
120 	errno = EINVAL;
121 	return -1;
122 }
123 
124 int
125 quotacursor_get(struct quotacursor *qc,
126 		struct quotakey *qk_ret, struct quotaval *qv_ret)
127 {
128 	switch (qc->qc_type) {
129 	    case QC_PROPLIB:
130 		return __quota_proplib_cursor_get(qc->qc_qh, qc->u.qc_proplib,
131 						  qk_ret, qv_ret);
132 	    case QC_OLDFILES:
133 		return __quota_oldfiles_cursor_get(qc->qc_qh,
134 						   qc->u.qc_oldfiles,
135 						   qk_ret, qv_ret);
136 	}
137 	errno = EINVAL;
138 	return -1;
139 }
140 
141 int
142 quotacursor_getn(struct quotacursor *qc,
143 		 struct quotakey *keys, struct quotaval *vals,
144 		 unsigned maxnum)
145 {
146 	switch (qc->qc_type) {
147 	    case QC_PROPLIB:
148 		return __quota_proplib_cursor_getn(qc->qc_qh, qc->u.qc_proplib,
149 						   keys, vals, maxnum);
150 	    case QC_OLDFILES:
151 		return __quota_oldfiles_cursor_getn(qc->qc_qh,
152 						    qc->u.qc_oldfiles,
153 						    keys, vals, maxnum);
154 	}
155 	errno = EINVAL;
156 	return -1;
157 }
158 
159 int
160 quotacursor_atend(struct quotacursor *qc)
161 {
162 	switch (qc->qc_type) {
163 	    case QC_PROPLIB:
164 		return __quota_proplib_cursor_atend(qc->qc_qh,
165 						    qc->u.qc_proplib);
166 	    case QC_OLDFILES:
167 		return __quota_oldfiles_cursor_atend(qc->u.qc_oldfiles);
168 	}
169 	errno = EINVAL;
170 	return -1;
171 }
172 
173 int
174 quotacursor_rewind(struct quotacursor *qc)
175 {
176 	switch (qc->qc_type) {
177 	    case QC_PROPLIB:
178 		return __quota_proplib_cursor_rewind(qc->u.qc_proplib);
179 	    case QC_OLDFILES:
180 		return __quota_oldfiles_cursor_rewind(qc->u.qc_oldfiles);
181 	}
182 	errno = EINVAL;
183 	return -1;
184 }
185