subr.c revision 1.3 1 1.3 pooka /* $NetBSD: subr.c,v 1.3 2006/10/23 01:36:13 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2006 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Redistribution and use in source and binary forms, with or without
7 1.1 pooka * modification, are permitted provided that the following conditions
8 1.1 pooka * are met:
9 1.1 pooka * 1. Redistributions of source code must retain the above copyright
10 1.1 pooka * notice, this list of conditions and the following disclaimer.
11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer in the
13 1.1 pooka * documentation and/or other materials provided with the distribution.
14 1.1 pooka * 3. The name of the company nor the name of the author may be used to
15 1.1 pooka * endorse or promote products derived from this software without specific
16 1.1 pooka * prior written permission.
17 1.1 pooka *
18 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 pooka * SUCH DAMAGE.
29 1.1 pooka */
30 1.1 pooka
31 1.1 pooka #include <sys/cdefs.h>
32 1.1 pooka #if !defined(lint)
33 1.3 pooka __RCSID("$NetBSD: subr.c,v 1.3 2006/10/23 01:36:13 pooka Exp $");
34 1.1 pooka #endif /* !lint */
35 1.1 pooka
36 1.1 pooka #include <sys/types.h>
37 1.1 pooka #include <sys/time.h>
38 1.1 pooka #include <sys/vnode.h>
39 1.1 pooka #include <sys/dirent.h>
40 1.1 pooka
41 1.1 pooka #include <assert.h>
42 1.1 pooka #include <puffs.h>
43 1.1 pooka #include <stdlib.h>
44 1.1 pooka #include <stdio.h>
45 1.1 pooka #include <string.h>
46 1.1 pooka
47 1.1 pooka /*
48 1.1 pooka * Well, you're probably wondering why this isn't optimized.
49 1.1 pooka * The reason is simple: my available time is not optimized for
50 1.1 pooka * size ... so please be patient ;)
51 1.1 pooka */
52 1.1 pooka struct puffs_node *
53 1.1 pooka puffs_newpnode(struct puffs_usermount *puser, void *privdata, enum vtype type)
54 1.1 pooka {
55 1.1 pooka struct puffs_node *pn;
56 1.1 pooka
57 1.1 pooka pn = calloc(1, sizeof(struct puffs_node));
58 1.1 pooka if (pn == NULL)
59 1.1 pooka return NULL;
60 1.1 pooka
61 1.1 pooka pn->pn_mnt = puser;
62 1.1 pooka pn->pn_data = privdata;
63 1.1 pooka pn->pn_type = type;
64 1.1 pooka
65 1.1 pooka /* technically not needed */
66 1.1 pooka memset(&pn->pn_va, 0, sizeof(struct vattr));
67 1.1 pooka pn->pn_va.va_type = type;
68 1.1 pooka
69 1.1 pooka LIST_INSERT_HEAD(&puser->pu_pnodelst, pn, pn_entries);
70 1.1 pooka
71 1.1 pooka return pn;
72 1.1 pooka }
73 1.1 pooka
74 1.1 pooka void
75 1.1 pooka puffs_putpnode(struct puffs_node *pn)
76 1.1 pooka {
77 1.1 pooka
78 1.1 pooka if (pn == NULL)
79 1.1 pooka return;
80 1.1 pooka
81 1.1 pooka LIST_REMOVE(pn, pn_entries);
82 1.1 pooka free(pn);
83 1.1 pooka }
84 1.1 pooka
85 1.1 pooka int
86 1.1 pooka puffs_gendotdent(struct dirent **dent, ino_t id, int dotdot, size_t *reslen)
87 1.1 pooka {
88 1.1 pooka const char *name;
89 1.1 pooka
90 1.1 pooka assert(dotdot == 0 || dotdot == 1);
91 1.1 pooka name = dotdot == 0 ? "." : "..";
92 1.1 pooka
93 1.1 pooka return puffs_nextdent(dent, name, id, DT_DIR, reslen);
94 1.1 pooka }
95 1.1 pooka
96 1.1 pooka int
97 1.1 pooka puffs_nextdent(struct dirent **dent, const char *name, ino_t id, uint8_t dtype,
98 1.1 pooka size_t *reslen)
99 1.1 pooka {
100 1.1 pooka struct dirent *d = *dent;
101 1.1 pooka
102 1.1 pooka /* check if we have enough room for our dent-aligned dirent */
103 1.1 pooka if (_DIRENT_RECLEN(d, strlen(name)) > *reslen)
104 1.1 pooka return 0;
105 1.1 pooka
106 1.1 pooka d->d_fileno = id;
107 1.1 pooka d->d_type = dtype;
108 1.1 pooka d->d_namlen = strlen(name);
109 1.2 christos (void)memcpy(&d->d_name, name, (size_t)d->d_namlen);
110 1.1 pooka d->d_name[d->d_namlen] = '\0';
111 1.1 pooka d->d_reclen = _DIRENT_SIZE(d);
112 1.1 pooka
113 1.1 pooka *reslen -= d->d_reclen;
114 1.1 pooka *dent = _DIRENT_NEXT(d);
115 1.1 pooka
116 1.1 pooka return 1;
117 1.1 pooka }
118 1.1 pooka
119 1.1 pooka
120 1.1 pooka /*
121 1.1 pooka * Set vattr values for those applicable (i.e. not PUFFS_VNOVAL).
122 1.1 pooka */
123 1.1 pooka void
124 1.1 pooka puffs_setvattr(struct vattr *vap, const struct vattr *sva)
125 1.1 pooka {
126 1.1 pooka
127 1.2 christos #define SETIFVAL(a, t) if (sva->a != (t)PUFFS_VNOVAL) vap->a = sva->a
128 1.1 pooka if (sva->va_type != VNON)
129 1.1 pooka vap->va_type = sva->va_type;
130 1.2 christos SETIFVAL(va_mode, mode_t);
131 1.2 christos SETIFVAL(va_nlink, nlink_t);
132 1.2 christos SETIFVAL(va_uid, uid_t);
133 1.2 christos SETIFVAL(va_gid, gid_t);
134 1.2 christos SETIFVAL(va_fsid, long);
135 1.2 christos SETIFVAL(va_size, u_quad_t);
136 1.2 christos SETIFVAL(va_blocksize, long);
137 1.2 christos SETIFVAL(va_atime.tv_sec, time_t);
138 1.2 christos SETIFVAL(va_ctime.tv_sec, time_t);
139 1.2 christos SETIFVAL(va_mtime.tv_sec, time_t);
140 1.2 christos SETIFVAL(va_birthtime.tv_sec, time_t);
141 1.2 christos SETIFVAL(va_atime.tv_nsec, long);
142 1.2 christos SETIFVAL(va_ctime.tv_nsec, long);
143 1.2 christos SETIFVAL(va_mtime.tv_nsec, long);
144 1.2 christos SETIFVAL(va_birthtime.tv_nsec, long);
145 1.2 christos SETIFVAL(va_gen, u_long);
146 1.2 christos SETIFVAL(va_flags, u_long);
147 1.2 christos SETIFVAL(va_rdev, dev_t);
148 1.2 christos SETIFVAL(va_bytes, u_quad_t);
149 1.1 pooka #undef SETIFVAL
150 1.1 pooka /* ignore va->va_vaflags */
151 1.1 pooka }
152 1.1 pooka
153 1.1 pooka
154 1.1 pooka static int vdmap[] = {
155 1.1 pooka DT_UNKNOWN, /* VNON */
156 1.1 pooka DT_REG, /* VREG */
157 1.1 pooka DT_DIR, /* VDIR */
158 1.1 pooka DT_BLK, /* VBLK */
159 1.1 pooka DT_CHR, /* VCHR */
160 1.1 pooka DT_LNK, /* VLNK */
161 1.1 pooka DT_SOCK, /* VSUCK*/
162 1.1 pooka DT_FIFO, /* VFIFO*/
163 1.1 pooka DT_UNKNOWN /* VBAD */
164 1.1 pooka };
165 1.1 pooka /* XXX: DT_WHT ? */
166 1.1 pooka int
167 1.1 pooka puffs_vtype2dt(enum vtype vt)
168 1.1 pooka {
169 1.1 pooka
170 1.3 pooka if (vt >= VNON && vt < (sizeof(vdmap)/sizeof(vdmap[0])))
171 1.3 pooka return vdmap[vt];
172 1.1 pooka
173 1.3 pooka return DT_UNKNOWN;
174 1.1 pooka }
175