Merge branch 'vendor/BYACC'
[dragonfly.git] / lib / libpuffs / pnode.c
1 /*      $NetBSD: pnode.c,v 1.10 2008/08/12 19:44:39 pooka Exp $ */
2
3 /*
4  * Copyright (c) 2006 Antti Kantee.  All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/types.h>
29
30 #include <assert.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 #include "puffs.h"
36 #include "puffs_priv.h"
37
38 /*
39  * Well, you're probably wondering why this isn't optimized.
40  * The reason is simple: my available time is not optimized for
41  * size ... so please be patient ;)
42  */
43 struct puffs_node *
44 puffs_pn_new(struct puffs_usermount *pu, void *privdata)
45 {
46         struct puffs_node *pn;
47
48         pn = calloc(1, sizeof(struct puffs_node));
49         if (pn == NULL)
50                 return NULL;
51
52         pn->pn_data = privdata;
53         pn->pn_mnt = pu;
54         puffs_vattr_null(&pn->pn_va);
55
56         LIST_INSERT_HEAD(&pu->pu_pnodelst, pn, pn_entries);
57
58         return pn;
59 }
60
61 void
62 puffs_pn_remove(struct puffs_node *pn)
63 {
64
65         LIST_REMOVE(pn, pn_entries);
66         pn->pn_flags |= PUFFS_NODE_REMOVED;
67 }
68
69 void
70 puffs_pn_put(struct puffs_node *pn)
71 {
72         struct puffs_usermount *pu = pn->pn_mnt;
73
74         pu->pu_pathfree(pu, &pn->pn_po);
75         if ((pn->pn_flags & PUFFS_NODE_REMOVED) == 0)
76                 LIST_REMOVE(pn, pn_entries);
77         free(pn);
78 }
79
80 /* walk list, rv can be used either to halt or to return a value */
81 void *
82 puffs_pn_nodewalk(struct puffs_usermount *pu, puffs_nodewalk_fn fn, void *arg)
83 {
84         struct puffs_node *pn_cur, *pn_next;
85         void *rv;
86
87         pn_cur = LIST_FIRST(&pu->pu_pnodelst);
88         while (pn_cur) {
89                 pn_next = LIST_NEXT(pn_cur, pn_entries);
90                 rv = fn(pu, pn_cur, arg);
91                 if (rv)
92                         return rv;
93                 pn_cur = pn_next;
94         }
95
96         return NULL;
97 }
98
99 struct vattr *
100 puffs_pn_getvap(struct puffs_node *pn)
101 {
102
103         return &pn->pn_va;
104 }
105
106 void *
107 puffs_pn_getpriv(struct puffs_node *pn)
108 {
109
110         return pn->pn_data;
111 }
112
113 void
114 puffs_pn_setpriv(struct puffs_node *pn, void *priv)
115 {
116
117         pn->pn_data = priv;
118 }
119
120 struct puffs_pathobj *
121 puffs_pn_getpo(struct puffs_node *pn)
122 {
123
124         return &pn->pn_po;
125 }
126
127 struct puffs_usermount *
128 puffs_pn_getmnt(struct puffs_node *pn)
129 {
130
131         return pn->pn_mnt;
132 }
133
134 /* convenience / shortcut */
135 void *
136 puffs_pn_getmntspecific(struct puffs_node *pn)
137 {
138
139         return pn->pn_mnt->pu_privdata;
140 }
141
142 /*
143  * newnode parameters
144  */
145 void
146 puffs_newinfo_setcookie(struct puffs_newinfo *pni, puffs_cookie_t cookie)
147 {
148
149         assert(pni->pni_cookie != NULL);
150         *pni->pni_cookie = cookie;
151 }
152
153 void
154 puffs_newinfo_setvtype(struct puffs_newinfo *pni, enum vtype vt)
155 {
156
157         if (pni->pni_vtype != NULL)
158                 *pni->pni_vtype = vt;
159 }
160
161 void
162 puffs_newinfo_setsize(struct puffs_newinfo *pni, voff_t size)
163 {
164
165         if (pni->pni_size != NULL)
166                 *pni->pni_size = size;
167 }