2 * Copyright (c) 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
34 * $DragonFly: src/sbin/jscan/dump_mirror.c,v 1.3 2005/07/05 04:08:07 dillon Exp $
39 static void dump_mirror_stream(struct jstream *js);
40 static int dump_mirror_toprecord(struct jstream *js, off_t *off,
41 off_t recsize, int level);
42 static int dump_mirror_subrecord(struct jstream *js, off_t *off,
43 off_t recsize, int level, struct jattr *jattr);
44 static int dump_mirror_payload(int16_t rectype, struct jstream *js, off_t off,
45 int recsize, int level, struct jattr *jattr);
46 static int dump_mirror_rebuild(u_int16_t rectype, struct jstream *js, struct jattr *jattr);
49 dump_mirror(struct jfile *jf)
53 while ((js = jscan_stream(jf)) != NULL) {
54 dump_mirror_stream(js);
60 dump_mirror_stream(struct jstream *js)
62 struct journal_rawrecbeg head;
66 save_umask = umask(0);
67 jsread(js, 0, &head, sizeof(head));
69 sid = head.streamid & JREC_STREAMID_MASK;
71 printf("STREAM %04x DATA (%lld) {\n",
72 (int)(u_int16_t)head.streamid, js->js_normalized_total);
74 if (sid >= JREC_STREAMID_JMIN && sid < JREC_STREAMID_JMAX) {
75 off_t off = sizeof(head);
76 dump_mirror_toprecord(js, &off, js->js_normalized_total -
77 sizeof(struct journal_rawrecbeg),
80 switch(head.streamid & JREC_STREAMID_MASK) {
81 case JREC_STREAMID_SYNCPT & JREC_STREAMID_MASK:
85 case JREC_STREAMID_PAD & JREC_STREAMID_MASK:
89 case JREC_STREAMID_DISCONT & JREC_STREAMID_MASK:
93 case JREC_STREAMID_ANNOTATE & JREC_STREAMID_MASK:
95 printf(" ANNOTATION\n");
111 dump_mirror_toprecord(struct jstream *js, off_t *off, off_t recsize, int level)
113 struct journal_subrecord sub;
121 bzero(&jattr, sizeof(jattr));
124 while (recsize > 0) {
125 if ((error = jsread(js, base, &sub, sizeof(sub))) != 0)
128 printf("%*.*s", level * 4, level * 4, "");
129 printf("@%lld ", base);
130 printf("RECORD %s [%04x/%d]", type_to_name(sub.rectype),
131 (int)(u_int16_t)sub.rectype, sub.recsize);
133 if (sub.recsize == -1) {
134 if ((sub.rectype & JMASK_NESTED) == 0) {
135 printf("Record size of -1 only works for nested records\n");
139 payload = 0x7FFFFFFF;
140 subsize = 0x7FFFFFFF;
142 payload = sub.recsize - sizeof(sub);
143 subsize = (sub.recsize + 7) & ~7;
145 if (sub.rectype & JMASK_NESTED) {
148 *off = base + sizeof(sub);
149 error = dump_mirror_subrecord(js, off,
150 payload, level + 1, &jattr);
152 printf("%*.*s}\n", level * 4, level * 4, "");
153 } else if (sub.rectype & JMASK_SUBRECORD) {
155 printf(" DATA (%d)", payload);
156 error = dump_debug_payload(sub.rectype, js, base + sizeof(sub), payload, level);
158 *off = base + sizeof(sub) + payload;
161 } else if ((sub.rectype & JTYPE_MASK) == JLEAF_PAD) {
164 printf(" DATA (%d)", payload);
169 printf("[%d bytes of unknown content]\n", payload);
171 dump_mirror_rebuild(sub.rectype, js, &jattr);
175 if (sub.recsize == -1) {
176 if ((sub.rectype & JMASK_NESTED) == 0) {
177 printf("Record size of -1 only works for nested records\n");
181 recsize -= ((*off + 7) & ~7) - base;
182 base = (*off + 7) & ~7;
185 subsize = sizeof(sub);
189 if (sub.rectype & JMASK_LAST)
197 dump_mirror_subrecord(struct jstream *js, off_t *off, off_t recsize, int level,
200 struct journal_subrecord sub;
208 while (recsize > 0) {
209 if ((error = jsread(js, base, &sub, sizeof(sub))) != 0)
211 rectype = sub.rectype & JTYPE_MASK;
213 printf("%*.*s", level * 4, level * 4, "");
214 printf("@%lld ", base);
215 printf("SRECORD %s [%04x/%d]", type_to_name(sub.rectype),
216 (int)(u_int16_t)sub.rectype, sub.recsize);
218 if (sub.recsize == -1) {
219 payload = 0x7FFFFFFF;
220 subsize = 0x7FFFFFFF;
222 payload = sub.recsize - sizeof(sub);
223 subsize = (sub.recsize + 7) & ~7;
225 if (sub.rectype & JMASK_NESTED) {
230 * Only recurse through vattr records. XXX currently assuming
231 * only on VATTR subrecord.
233 *off = base + sizeof(sub);
234 if (payload && rectype == JTYPE_VATTR) {
235 error = dump_mirror_subrecord(js, off,
236 payload, level + 1, jattr);
238 error = dump_mirror_subrecord(js, off,
239 payload, level + 1, NULL);
242 printf("%*.*s}\n", level * 4, level * 4, "");
243 } else if (sub.rectype & JMASK_SUBRECORD) {
245 printf(" DATA (%d)", payload);
246 dump_debug_payload(sub.rectype, js, base + sizeof(sub),
249 error = dump_mirror_payload(sub.rectype, js, base + sizeof(sub),
250 payload, level, jattr);
251 *off = base + sizeof(sub) + payload;
254 } else if ((sub.rectype & JTYPE_MASK) == JLEAF_PAD) {
257 printf(" DATA (%d)", payload);
262 printf("[%d bytes of unknown content]\n", sub.recsize);
266 if (sub.recsize == -1) {
267 recsize -= ((*off + 7) & ~7) - base;
268 base = (*off + 7) & ~7;
271 subsize = sizeof(sub);
275 if (sub.rectype & JMASK_LAST)
283 dump_mirror_payload(int16_t rectype, struct jstream *js, off_t off,
284 int recsize, int level __unused, struct jattr *jattr)
287 struct jattr_data *data;
293 if ((rectype & ~JMASK_LAST) != JLEAF_FILEDATA) {
294 error = jsreadp(js, off, (const void **)&buf, recsize);
302 switch(rectype & ~JMASK_LAST) {
306 case JLEAF_SYMLINKDATA:
308 if ((data = jattr->last_data) == NULL) {
309 jattr->data.off = off;
310 jattr->data.bytes = recsize;
311 jattr->last_data = &jattr->data;
313 data->next = malloc(sizeof(jattr->data));
316 data->bytes = recsize;
318 jattr->last_data = data;
322 jattr->path1 = dupdatapath(buf, recsize);
325 jattr->path2 = dupdatapath(buf, recsize);
328 jattr->path3 = dupdatapath(buf, recsize);
331 jattr->path4 = dupdatapath(buf, recsize);
334 jattr->uid = buf_to_int64(buf, recsize);
337 jattr->gid = buf_to_int64(buf, recsize);
340 jattr->vtype = buf_to_int64(buf, recsize);
343 jattr->modes = buf_to_int64(buf, recsize);
346 jattr->fflags = buf_to_int64(buf, recsize);
349 jattr->pid = buf_to_int64(buf, recsize);
352 jattr->ppid = buf_to_int64(buf, recsize);
355 jattr->comm = dupdatastr(buf, recsize);
358 jattr->attrname = dupdatastr(buf, recsize);
361 jattr->pathref = dupdatapath(buf, recsize);
363 case JLEAF_RESERVED_0F:
366 jattr->seekpos = buf_to_int64(buf, recsize);
369 jattr->inum = buf_to_int64(buf, recsize);
372 jattr->nlink = buf_to_int64(buf, recsize);
375 jattr->fsid = buf_to_int64(buf, recsize);
378 jattr->size = buf_to_int64(buf, recsize);
381 jattr->atime = *(const struct timeval *)buf;
384 jattr->mtime = *(const struct timeval *)buf;
387 jattr->ctime = *(const struct timeval *)buf;
390 jattr->gen = buf_to_int64(buf, recsize);
393 jattr->flags = buf_to_int64(buf, recsize);
396 jattr->udev = buf_to_int64(buf, recsize);
399 jattr->filerev = buf_to_int64(buf, recsize);
408 dump_mirror_rebuild(u_int16_t rectype, struct jstream *js, struct jattr *jattr)
410 struct jattr_data *data;
417 if (jattr->pathref) {
418 if (jattr->uid != (uid_t)-1)
419 chown(jattr->pathref, jattr->uid, -1);
420 if (jattr->gid != (gid_t)-1)
421 chown(jattr->pathref, -1, jattr->gid);
422 if (jattr->modes != (mode_t)-1)
423 chmod(jattr->pathref, jattr->modes);
424 if (jattr->fflags != -1)
425 chflags(jattr->pathref, jattr->fflags);
426 if (jattr->size != -1)
427 truncate(jattr->pathref, jattr->size);
432 if (jattr->pathref && jattr->seekpos != -1) {
433 if ((fd = open(jattr->pathref, O_RDWR)) >= 0) {
434 lseek(fd, jattr->seekpos, 0);
435 for (data = &jattr->data; data; data = data->next) {
437 jsreadcallback(js, write, fd, data->off, data->bytes);
445 case JTYPE_SETEXTATTR:
449 * note: both path1 and pathref will exist.
451 if (jattr->path1 && jattr->modes != (mode_t)-1) {
452 if ((fd = open(jattr->path1, O_CREAT, jattr->modes)) >= 0) {
454 rectype = JTYPE_SETATTR;
469 remove(jattr->path1);
473 if (jattr->path1 && jattr->modes != (mode_t)-1) {
474 mkdir(jattr->path1, jattr->modes);
483 if (jattr->path1 && jattr->path2) {
484 rename(jattr->path1, jattr->path2);