Major continuing work on jscan, the userland backend for the journaling
[dragonfly.git] / sbin / jscan / subs.c
CommitLineData
ce5e5ac4
MD
1/*
2 * Copyright (c) 2004,2005 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
712e03b0 34 * $DragonFly: src/sbin/jscan/subs.c,v 1.3 2005/07/05 00:26:03 dillon Exp $
ce5e5ac4
MD
35 */
36
37#include "jscan.h"
38
39void
40jf_warn(struct jfile *jf, const char *ctl, ...)
41{
42 va_list va;
43
44 fprintf(stderr, "@0x%016llx ", jf->jf_pos);
45 va_start(va, ctl);
46 vfprintf(stderr, ctl, va);
47 va_end(va);
48 fprintf(stderr, "\n");
49}
50
51const char *
52type_to_name(int16_t rectype)
53{
54 const char *str;
55
56 switch((u_int16_t)rectype & ~JMASK_LAST) {
57 case JLEAF_PAD:
58 str = "PAD";
59 break;
60 case JLEAF_ABORT:
61 str = "ABORT";
62 break;
63 case JTYPE_ASSOCIATE:
64 str = "ASSOCIATE";
65 break;
66 case JTYPE_DISASSOCIATE:
67 str = "DISASSOCIATE";
68 break;
69 case JTYPE_UNDO:
70 str = "UNDO";
71 break;
72 case JTYPE_AUDIT:
73 str = "AUDIT";
74 break;
75 case JTYPE_SETATTR:
76 str = "SETATTR";
77 break;
78 case JTYPE_WRITE:
79 str = "WRITE";
80 break;
81 case JTYPE_PUTPAGES:
82 str = "PUTPAGES";
83 break;
84 case JTYPE_SETACL:
85 str = "SETACL";
86 break;
87 case JTYPE_SETEXTATTR:
88 str = "SETEXTATTR";
89 break;
90 case JTYPE_CREATE:
91 str = "CREATE";
92 break;
93 case JTYPE_MKNOD:
94 str = "MKNOD";
95 break;
96 case JTYPE_LINK:
97 str = "LINK";
98 break;
99 case JTYPE_SYMLINK:
100 str = "SYMLINK";
101 break;
102 case JTYPE_WHITEOUT:
103 str = "WHITEOUT";
104 break;
105 case JTYPE_REMOVE:
106 str = "REMOVE";
107 break;
108 case JTYPE_MKDIR:
109 str = "MKDIR";
110 break;
111 case JTYPE_RMDIR:
112 str = "RMDIR";
113 break;
114 case JTYPE_RENAME:
115 str = "RENAME";
116 break;
117 case JTYPE_VATTR:
118 str = "vattr";
119 break;
120 case JTYPE_CRED:
121 str = "cred";
122 break;
123 case JLEAF_FILEDATA:
124 str = "filedata";
125 break;
126 case JLEAF_PATH1:
127 str = "path1";
128 break;
129 case JLEAF_PATH2:
130 str = "path2";
131 break;
132 case JLEAF_PATH3:
133 str = "path3";
134 break;
135 case JLEAF_PATH4:
136 str = "path4";
137 break;
138 case JLEAF_UID:
139 str = "uid";
140 break;
141 case JLEAF_GID:
142 str = "gid";
143 break;
712e03b0
MD
144 case JLEAF_VTYPE:
145 str = "vtype";
146 break;
ce5e5ac4
MD
147 case JLEAF_MODES:
148 str = "modes";
149 break;
150 case JLEAF_FFLAGS:
151 str = "fflags";
152 break;
153 case JLEAF_PID:
154 str = "pid";
155 break;
156 case JLEAF_PPID:
157 str = "ppid";
158 break;
159 case JLEAF_COMM:
160 str = "comm";
161 break;
162 case JLEAF_ATTRNAME:
163 str = "attrname";
164 break;
165 case JLEAF_PATH_REF:
166 str = "path_ref";
167 break;
168 case JLEAF_RESERVED_0F:
169 str = "?";
170 break;
171 case JLEAF_SYMLINKDATA:
172 str = "symlinkdata";
173 break;
174 case JLEAF_SEEKPOS:
175 str = "seekpos";
176 break;
177 case JLEAF_INUM:
178 str = "inum";
179 break;
180 case JLEAF_NLINK:
181 str = "nlink";
182 break;
183 case JLEAF_FSID:
184 str = "fsid";
185 break;
186 case JLEAF_SIZE:
187 str = "size";
188 break;
189 case JLEAF_ATIME:
190 str = "atime";
191 break;
192 case JLEAF_MTIME:
193 str = "mtime";
194 break;
195 case JLEAF_CTIME:
196 str = "ctime";
197 break;
198 case JLEAF_GEN:
199 str = "gen";
200 break;
201 case JLEAF_FLAGS:
202 str = "flags";
203 break;
204 case JLEAF_UDEV:
205 str = "udev";
206 break;
207 case JLEAF_FILEREV:
208 str = "filerev";
209 break;
210 default:
211 str = "?";
212 break;
213 }
214 return (str);
215}
216
9c118cb2
MD
217void
218stringout(FILE *fp, char c, int exact)
219{
220 if (c != '\\' && c != '\"' && isprint(c)) {
221 putc(c, fp);
222 } else if (exact == 0) {
223 putc('.', fp);
224 } else if (c == 0) {
225 fprintf(fp, "\\0");
226 } else if (c == '\n') {
227 fprintf(fp, "\\n");
228 } else {
229 fprintf(fp, "\\x%02x", (int)(unsigned char)c);
230 }
231}
232
712e03b0
MD
233void
234jattr_reset(struct jattr *jattr)
235{
236 struct jattr *undo;
237
238 if (jattr->path1)
239 free(jattr->path1);
240 if (jattr->path2)
241 free(jattr->path2);
242 if (jattr->path3)
243 free(jattr->path3);
244 if (jattr->path4)
245 free(jattr->path4);
246 if (jattr->comm)
247 free(jattr->comm);
248 if (jattr->attrname)
249 free(jattr->attrname);
250 if (jattr->pathref)
251 free(jattr->pathref);
252 if (jattr->data)
253 free(jattr->data);
254 if ((undo = jattr->undo) != NULL)
255 jattr_reset(jattr->undo);
256 bzero(jattr, sizeof(*jattr));
257 jattr->undo = undo;
258 jattr->uid = (uid_t)-1;
259 jattr->gid = (gid_t)-1;
260 jattr->size = (off_t)-1;
261 jattr->modes = -1;
262 jattr->flags = -1;
263 jattr->seekpos = -1;
264}
265
266int64_t
267buf_to_int64(const void *buf, int bytes)
268{
269 int64_t v;
270
271 switch(bytes) {
272 case 1:
273 v = (int64_t)*(const u_int8_t *)buf;
274 break;
275 case 2:
276 v = (int64_t)*(const u_int16_t *)buf;
277 break;
278 case 4:
279 v = (int64_t)*(const u_int32_t *)buf;
280 break;
281 case 8:
282 v = *(const int64_t *)buf;
283 break;
284 default:
285 v = 0;
286 }
287 return(v);
288}
289
290void *
291dupdata(const void *buf, int bytes)
292{
293 void *res;
294
295 res = malloc(bytes);
296 bcopy(buf, res, bytes);
297
298 return(res);
299}
300
301char *
302dupdatastr(const void *buf, int bytes)
303{
304 char *res;
305
306 res = malloc(bytes + 1);
307 bcopy(buf, res, bytes);
308 res[bytes] = 0;
309
310 return(res);
311}
312
313/*
314 * Similar to dupdatastr() but contains sanity checks.
315 */
316char *
317dupdatapath(const void *buf, int bytes)
318{
319 char *res;
320 char *scan;
321
322 res = malloc(bytes + 1);
323 bcopy(buf, res, bytes);
324 res[bytes] = 0;
325
326 if (res[0] == '/') {
327 fprintf(stderr, "Bad path: %s\n", res);
328 free(res);
329 return(NULL);
330 }
331 scan = res;
332 for (;;) {
333 if (scan[0] == '.' && scan[1] == '.' &&
334 (scan[2] == 0 || scan[2] == '/')
335 ) {
336 fprintf(stderr, "Bad path: %s\n", res);
337 free(res);
338 return(NULL);
339 }
340 if ((scan = strchr(scan, '/')) == NULL)
341 break;
342 ++scan;
343 }
344 return(res);
345}
ce5e5ac4 346