]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsdb/fsdb.c
MFC r326276:
[FreeBSD/FreeBSD.git] / sbin / fsdb / fsdb.c
1 /*      $NetBSD: fsdb.c,v 1.2 1995/10/08 23:18:10 thorpej Exp $ */
2
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  *  Copyright (c) 1995 John T. Kohl
7  *  All rights reserved.
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  *  3. The name of the author may not be used to endorse or promote products
18  *     derived from this software without specific prior written permission.
19  * 
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #ifndef lint
34 static const char rcsid[] =
35   "$FreeBSD$";
36 #endif /* not lint */
37
38 #include <sys/param.h>
39 #include <ctype.h>
40 #include <err.h>
41 #include <grp.h>
42 #include <histedit.h>
43 #include <pwd.h>
44 #include <stdint.h>
45 #include <string.h>
46 #include <time.h>
47 #include <timeconv.h>
48
49 #include <ufs/ufs/dinode.h>
50 #include <ufs/ufs/dir.h>
51 #include <ufs/ffs/fs.h>
52
53 #include "fsdb.h"
54 #include "fsck.h"
55
56 static void usage(void) __dead2;
57 int cmdloop(void);
58 static int compare_blk32(uint32_t *wantedblk, uint32_t curblk);
59 static int compare_blk64(uint64_t *wantedblk, uint64_t curblk);
60 static int founddatablk(uint64_t blk);
61 static int find_blks32(uint32_t *buf, int size, uint32_t *blknum);
62 static int find_blks64(uint64_t *buf, int size, uint64_t *blknum);
63 static int find_indirblks32(uint32_t blk, int ind_level, uint32_t *blknum);
64 static int find_indirblks64(uint64_t blk, int ind_level, uint64_t *blknum);
65
66 static void 
67 usage(void)
68 {
69         fprintf(stderr, "usage: fsdb [-d] [-f] [-r] fsname\n");
70         exit(1);
71 }
72
73 int returntosingle;
74 char nflag;
75
76 /*
77  * We suck in lots of fsck code, and just pick & choose the stuff we want.
78  *
79  * fsreadfd is set up to read from the file system, fswritefd to write to
80  * the file system.
81  */
82 int
83 main(int argc, char *argv[])
84 {
85         int ch, rval;
86         char *fsys = NULL;
87
88         while (-1 != (ch = getopt(argc, argv, "fdr"))) {
89                 switch (ch) {
90                 case 'f':
91                         /* The -f option is left for historical
92                          * reasons and has no meaning.
93                          */
94                         break;
95                 case 'd':
96                         debug++;
97                         break;
98                 case 'r':
99                         nflag++; /* "no" in fsck, readonly for us */
100                         break;
101                 default:
102                         usage();
103                 }
104         }
105         argc -= optind;
106         argv += optind;
107         if (argc != 1)
108                 usage();
109         else
110                 fsys = argv[0];
111
112         sblock_init();
113         if (!setup(fsys))
114                 errx(1, "cannot set up file system `%s'", fsys);
115         printf("%s file system `%s'\nLast Mounted on %s\n",
116                nflag? "Examining": "Editing", fsys, sblock.fs_fsmnt);
117         rval = cmdloop();
118         if (!nflag) {
119                 sblock.fs_clean = 0;    /* mark it dirty */
120                 sbdirty();
121                 ckfini(0);
122                 printf("*** FILE SYSTEM MARKED DIRTY\n");
123                 printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
124                 printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n");
125         }
126         exit(rval);
127 }
128
129 #define CMDFUNC(func) int func(int argc, char *argv[])
130 #define CMDFUNCSTART(func) int func(int argc, char *argv[])
131
132 CMDFUNC(helpfn);
133 CMDFUNC(focus);                         /* focus on inode */
134 CMDFUNC(active);                        /* print active inode */
135 CMDFUNC(blocks);                        /* print blocks for active inode */
136 CMDFUNC(focusname);                     /* focus by name */
137 CMDFUNC(zapi);                          /* clear inode */
138 CMDFUNC(uplink);                        /* incr link */
139 CMDFUNC(downlink);                      /* decr link */
140 CMDFUNC(linkcount);                     /* set link count */
141 CMDFUNC(quit);                          /* quit */
142 CMDFUNC(findblk);                       /* find block */
143 CMDFUNC(ls);                            /* list directory */
144 CMDFUNC(rm);                            /* remove name */
145 CMDFUNC(ln);                            /* add name */
146 CMDFUNC(newtype);                       /* change type */
147 CMDFUNC(chmode);                        /* change mode */
148 CMDFUNC(chlen);                         /* change length */
149 CMDFUNC(chaflags);                      /* change flags */
150 CMDFUNC(chgen);                         /* change generation */
151 CMDFUNC(chowner);                       /* change owner */
152 CMDFUNC(chgroup);                       /* Change group */
153 CMDFUNC(back);                          /* pop back to last ino */
154 CMDFUNC(chbtime);                       /* Change btime */
155 CMDFUNC(chmtime);                       /* Change mtime */
156 CMDFUNC(chctime);                       /* Change ctime */
157 CMDFUNC(chatime);                       /* Change atime */
158 CMDFUNC(chinum);                        /* Change inode # of dirent */
159 CMDFUNC(chname);                        /* Change dirname of dirent */
160
161 struct cmdtable cmds[] = {
162         { "help", "Print out help", 1, 1, FL_RO, helpfn },
163         { "?", "Print out help", 1, 1, FL_RO, helpfn },
164         { "inode", "Set active inode to INUM", 2, 2, FL_RO, focus },
165         { "clri", "Clear inode INUM", 2, 2, FL_WR, zapi },
166         { "lookup", "Set active inode by looking up NAME", 2, 2, FL_RO | FL_ST, focusname },
167         { "cd", "Set active inode by looking up NAME", 2, 2, FL_RO | FL_ST, focusname },
168         { "back", "Go to previous active inode", 1, 1, FL_RO, back },
169         { "active", "Print active inode", 1, 1, FL_RO, active },
170         { "print", "Print active inode", 1, 1, FL_RO, active },
171         { "blocks", "Print block numbers of active inode", 1, 1, FL_RO, blocks },
172         { "uplink", "Increment link count", 1, 1, FL_WR, uplink },
173         { "downlink", "Decrement link count", 1, 1, FL_WR, downlink },
174         { "linkcount", "Set link count to COUNT", 2, 2, FL_WR, linkcount },
175         { "findblk", "Find inode owning disk block(s)", 2, 33, FL_RO, findblk},
176         { "ls", "List current inode as directory", 1, 1, FL_RO, ls },
177         { "rm", "Remove NAME from current inode directory", 2, 2, FL_WR | FL_ST, rm },
178         { "del", "Remove NAME from current inode directory", 2, 2, FL_WR | FL_ST, rm },
179         { "ln", "Hardlink INO into current inode directory as NAME", 3, 3, FL_WR | FL_ST, ln },
180         { "chinum", "Change dir entry number INDEX to INUM", 3, 3, FL_WR, chinum },
181         { "chname", "Change dir entry number INDEX to NAME", 3, 3, FL_WR | FL_ST, chname },
182         { "chtype", "Change type of current inode to TYPE", 2, 2, FL_WR, newtype },
183         { "chmod", "Change mode of current inode to MODE", 2, 2, FL_WR, chmode },
184         { "chlen", "Change length of current inode to LENGTH", 2, 2, FL_WR, chlen },
185         { "chown", "Change owner of current inode to OWNER", 2, 2, FL_WR, chowner },
186         { "chgrp", "Change group of current inode to GROUP", 2, 2, FL_WR, chgroup },
187         { "chflags", "Change flags of current inode to FLAGS", 2, 2, FL_WR, chaflags },
188         { "chgen", "Change generation number of current inode to GEN", 2, 2, FL_WR, chgen },
189         { "btime", "Change btime of current inode to BTIME", 2, 2, FL_WR, chbtime },
190         { "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR, chmtime },
191         { "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR, chctime },
192         { "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR, chatime },
193         { "quit", "Exit", 1, 1, FL_RO, quit },
194         { "q", "Exit", 1, 1, FL_RO, quit },
195         { "exit", "Exit", 1, 1, FL_RO, quit },
196         { NULL, 0, 0, 0, 0, NULL },
197 };
198
199 int
200 helpfn(int argc, char *argv[])
201 {
202     struct cmdtable *cmdtp;
203
204     printf("Commands are:\n%-10s %5s %5s   %s\n",
205            "command", "min args", "max args", "what");
206     
207     for (cmdtp = cmds; cmdtp->cmd; cmdtp++)
208         printf("%-10s %5u %5u   %s\n",
209                 cmdtp->cmd, cmdtp->minargc-1, cmdtp->maxargc-1, cmdtp->helptxt);
210     return 0;
211 }
212
213 char *
214 prompt(EditLine *el)
215 {
216     static char pstring[64];
217     snprintf(pstring, sizeof(pstring), "fsdb (inum: %ju)> ",
218         (uintmax_t)curinum);
219     return pstring;
220 }
221
222
223 int
224 cmdloop(void)
225 {
226     char *line;
227     const char *elline;
228     int cmd_argc, rval = 0, known;
229 #define scratch known
230     char **cmd_argv;
231     struct cmdtable *cmdp;
232     History *hist;
233     EditLine *elptr;
234     HistEvent he;
235
236     curinode = ginode(ROOTINO);
237     curinum = ROOTINO;
238     printactive(0);
239
240     hist = history_init();
241     history(hist, &he, H_SETSIZE, 100); /* 100 elt history buffer */
242
243     elptr = el_init("fsdb", stdin, stdout, stderr);
244     el_set(elptr, EL_EDITOR, "emacs");
245     el_set(elptr, EL_PROMPT, prompt);
246     el_set(elptr, EL_HIST, history, hist);
247     el_source(elptr, NULL);
248
249     while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) {
250         if (debug)
251             printf("command `%s'\n", elline);
252
253         history(hist, &he, H_ENTER, elline);
254
255         line = strdup(elline);
256         cmd_argv = crack(line, &cmd_argc);
257         /*
258          * el_parse returns -1 to signal that it's not been handled
259          * internally.
260          */
261         if (el_parse(elptr, cmd_argc, (const char **)cmd_argv) != -1)
262             continue;
263         if (cmd_argc) {
264             known = 0;
265             for (cmdp = cmds; cmdp->cmd; cmdp++) {
266                 if (!strcmp(cmdp->cmd, cmd_argv[0])) {
267                     if ((cmdp->flags & FL_WR) == FL_WR && nflag)
268                         warnx("`%s' requires write access", cmd_argv[0]),
269                             rval = 1;
270                     else if (cmd_argc >= cmdp->minargc &&
271                         cmd_argc <= cmdp->maxargc)
272                         rval = (*cmdp->handler)(cmd_argc, cmd_argv);
273                     else if (cmd_argc >= cmdp->minargc &&
274                         (cmdp->flags & FL_ST) == FL_ST) {
275                         strcpy(line, elline);
276                         cmd_argv = recrack(line, &cmd_argc, cmdp->maxargc);
277                         rval = (*cmdp->handler)(cmd_argc, cmd_argv);
278                     } else
279                         rval = argcount(cmdp, cmd_argc, cmd_argv);
280                     known = 1;
281                     break;
282                 }
283             }
284             if (!known)
285                 warnx("unknown command `%s'", cmd_argv[0]), rval = 1;
286         } else
287             rval = 0;
288         free(line);
289         if (rval < 0)
290             /* user typed "quit" */
291             return 0;
292         if (rval)
293             warnx("rval was %d", rval);
294     }
295     el_end(elptr);
296     history_end(hist);
297     return rval;
298 }
299
300 union dinode *curinode;
301 ino_t curinum, ocurrent;
302
303 #define GETINUM(ac,inum)    inum = strtoul(argv[ac], &cp, 0); \
304     if (inum < ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \
305         printf("inode %ju out of range; range is [%ju,%ju]\n",          \
306             (uintmax_t)inum, (uintmax_t)ROOTINO, (uintmax_t)maxino);    \
307         return 1; \
308     }
309
310 /*
311  * Focus on given inode number
312  */
313 CMDFUNCSTART(focus)
314 {
315     ino_t inum;
316     char *cp;
317
318     GETINUM(1,inum);
319     curinode = ginode(inum);
320     ocurrent = curinum;
321     curinum = inum;
322     printactive(0);
323     return 0;
324 }
325
326 CMDFUNCSTART(back)
327 {
328     curinum = ocurrent;
329     curinode = ginode(curinum);
330     printactive(0);
331     return 0;
332 }
333
334 CMDFUNCSTART(zapi)
335 {
336     ino_t inum;
337     union dinode *dp;
338     char *cp;
339
340     GETINUM(1,inum);
341     dp = ginode(inum);
342     clearinode(dp);
343     inodirty();
344     if (curinode)                       /* re-set after potential change */
345         curinode = ginode(curinum);
346     return 0;
347 }
348
349 CMDFUNCSTART(active)
350 {
351     printactive(0);
352     return 0;
353 }
354
355 CMDFUNCSTART(blocks)
356 {
357     printactive(1);
358     return 0;
359 }
360
361 CMDFUNCSTART(quit)
362 {
363     return -1;
364 }
365
366 CMDFUNCSTART(uplink)
367 {
368     if (!checkactive())
369         return 1;
370     DIP_SET(curinode, di_nlink, DIP(curinode, di_nlink) + 1);
371     printf("inode %ju link count now %d\n",
372         (uintmax_t)curinum, DIP(curinode, di_nlink));
373     inodirty();
374     return 0;
375 }
376
377 CMDFUNCSTART(downlink)
378 {
379     if (!checkactive())
380         return 1;
381     DIP_SET(curinode, di_nlink, DIP(curinode, di_nlink) - 1);
382     printf("inode %ju link count now %d\n",
383         (uintmax_t)curinum, DIP(curinode, di_nlink));
384     inodirty();
385     return 0;
386 }
387
388 const char *typename[] = {
389     "unknown",
390     "fifo",
391     "char special",
392     "unregistered #3",
393     "directory",
394     "unregistered #5",
395     "blk special",
396     "unregistered #7",
397     "regular",
398     "unregistered #9",
399     "symlink",
400     "unregistered #11",
401     "socket",
402     "unregistered #13",
403     "whiteout",
404 };
405
406 int diroff; 
407 int slot;
408
409 int
410 scannames(struct inodesc *idesc)
411 {
412         struct direct *dirp = idesc->id_dirp;
413
414         printf("slot %d off %d ino %d reclen %d: %s, `%.*s'\n",
415                slot++, diroff, dirp->d_ino, dirp->d_reclen,
416                typename[dirp->d_type], dirp->d_namlen, dirp->d_name);
417         diroff += dirp->d_reclen;
418         return (KEEPON);
419 }
420
421 CMDFUNCSTART(ls)
422 {
423     struct inodesc idesc;
424     checkactivedir();                   /* let it go on anyway */
425
426     slot = 0;
427     diroff = 0;
428     idesc.id_number = curinum;
429     idesc.id_func = scannames;
430     idesc.id_type = DATA;
431     idesc.id_fix = IGNORE;
432     ckinode(curinode, &idesc);
433     curinode = ginode(curinum);
434
435     return 0;
436 }
437
438 static int findblk_numtofind;
439 static int wantedblksize;
440
441 CMDFUNCSTART(findblk)
442 {
443     ino_t inum, inosused;
444     uint32_t *wantedblk32;
445     uint64_t *wantedblk64;
446     struct bufarea *cgbp;
447     struct cg *cgp;
448     int c, i, is_ufs2;
449
450     wantedblksize = (argc - 1);
451     is_ufs2 = sblock.fs_magic == FS_UFS2_MAGIC;
452     ocurrent = curinum;
453
454     if (is_ufs2) {
455         wantedblk64 = calloc(wantedblksize, sizeof(uint64_t));
456         if (wantedblk64 == NULL)
457             err(1, "malloc");
458         for (i = 1; i < argc; i++)
459             wantedblk64[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
460     } else {
461         wantedblk32 = calloc(wantedblksize, sizeof(uint32_t));
462         if (wantedblk32 == NULL)
463             err(1, "malloc");
464         for (i = 1; i < argc; i++)
465             wantedblk32[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
466     }
467     findblk_numtofind = wantedblksize;
468     /*
469      * sblock.fs_ncg holds a number of cylinder groups.
470      * Iterate over all cylinder groups.
471      */
472     for (c = 0; c < sblock.fs_ncg; c++) {
473         /*
474          * sblock.fs_ipg holds a number of inodes per cylinder group.
475          * Calculate a highest inode number for a given cylinder group.
476          */
477         inum = c * sblock.fs_ipg;
478         /* Read cylinder group. */
479         cgbp = cgget(c);
480         cgp = cgbp->b_un.b_cg;
481         /*
482          * Get a highest used inode number for a given cylinder group.
483          * For UFS1 all inodes initialized at the newfs stage.
484          */
485         if (is_ufs2)
486             inosused = cgp->cg_initediblk;
487         else
488             inosused = sblock.fs_ipg;
489
490         for (; inosused > 0; inum++, inosused--) {
491             /* Skip magic inodes: 0, WINO, ROOTINO. */
492             if (inum < ROOTINO)
493                 continue;
494             /*
495              * Check if the block we are looking for is just an inode block.
496              *
497              * ino_to_fsba() - get block containing inode from its number.
498              * INOPB() - get a number of inodes in one disk block.
499              */
500             if (is_ufs2 ?
501                 compare_blk64(wantedblk64, ino_to_fsba(&sblock, inum)) :
502                 compare_blk32(wantedblk32, ino_to_fsba(&sblock, inum))) {
503                 printf("block %llu: inode block (%ju-%ju)\n",
504                     (unsigned long long)fsbtodb(&sblock,
505                         ino_to_fsba(&sblock, inum)),
506                     (uintmax_t)(inum / INOPB(&sblock)) * INOPB(&sblock),
507                     (uintmax_t)(inum / INOPB(&sblock) + 1) * INOPB(&sblock));
508                 findblk_numtofind--;
509                 if (findblk_numtofind == 0)
510                     goto end;
511             }
512             /* Get on-disk inode aka dinode. */
513             curinum = inum;
514             curinode = ginode(inum);
515             /* Find IFLNK dinode with allocated data blocks. */
516             switch (DIP(curinode, di_mode) & IFMT) {
517             case IFDIR:
518             case IFREG:
519                 if (DIP(curinode, di_blocks) == 0)
520                     continue;
521                 break;
522             case IFLNK:
523                 {
524                     uint64_t size = DIP(curinode, di_size);
525                     if (size > 0 && size < sblock.fs_maxsymlinklen &&
526                         DIP(curinode, di_blocks) == 0)
527                         continue;
528                     else
529                         break;
530                 }
531             default:
532                 continue;
533             }
534             /* Look through direct data blocks. */
535             if (is_ufs2 ?
536                 find_blks64(curinode->dp2.di_db, NDADDR, wantedblk64) :
537                 find_blks32(curinode->dp1.di_db, NDADDR, wantedblk32))
538                 goto end;
539             for (i = 0; i < NIADDR; i++) {
540                 /*
541                  * Does the block we are looking for belongs to the
542                  * indirect blocks?
543                  */
544                 if (is_ufs2 ?
545                     compare_blk64(wantedblk64, curinode->dp2.di_ib[i]) :
546                     compare_blk32(wantedblk32, curinode->dp1.di_ib[i]))
547                     if (founddatablk(is_ufs2 ? curinode->dp2.di_ib[i] :
548                         curinode->dp1.di_ib[i]))
549                         goto end;
550                 /*
551                  * Search through indirect, double and triple indirect
552                  * data blocks.
553                  */
554                 if (is_ufs2 ? (curinode->dp2.di_ib[i] != 0) :
555                     (curinode->dp1.di_ib[i] != 0))
556                     if (is_ufs2 ?
557                         find_indirblks64(curinode->dp2.di_ib[i], i,
558                             wantedblk64) :
559                         find_indirblks32(curinode->dp1.di_ib[i], i,
560                             wantedblk32))
561                         goto end;
562             }
563         }
564     }
565 end:
566     curinum = ocurrent;
567     curinode = ginode(curinum);
568     return 0;
569 }
570
571 static int
572 compare_blk32(uint32_t *wantedblk, uint32_t curblk)
573 {
574     int i;
575
576     for (i = 0; i < wantedblksize; i++) {
577         if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
578             wantedblk[i] = 0;
579             return 1;
580         }
581     }
582     return 0;
583 }
584
585 static int
586 compare_blk64(uint64_t *wantedblk, uint64_t curblk)
587 {
588     int i;
589
590     for (i = 0; i < wantedblksize; i++) {
591         if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
592             wantedblk[i] = 0;
593             return 1;
594         }
595     }
596     return 0;
597 }
598
599 static int
600 founddatablk(uint64_t blk)
601 {
602
603     printf("%llu: data block of inode %ju\n",
604         (unsigned long long)fsbtodb(&sblock, blk), (uintmax_t)curinum);
605     findblk_numtofind--;
606     if (findblk_numtofind == 0)
607         return 1;
608     return 0;
609 }
610
611 static int
612 find_blks32(uint32_t *buf, int size, uint32_t *wantedblk)
613 {
614     int blk;
615     for (blk = 0; blk < size; blk++) {
616         if (buf[blk] == 0)
617             continue;
618         if (compare_blk32(wantedblk, buf[blk])) {
619             if (founddatablk(buf[blk]))
620                 return 1;
621         }
622     }
623     return 0;
624 }
625
626 static int
627 find_indirblks32(uint32_t blk, int ind_level, uint32_t *wantedblk)
628 {
629 #define MAXNINDIR      (MAXBSIZE / sizeof(uint32_t))
630     uint32_t idblk[MAXNINDIR];
631     int i;
632
633     blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
634     if (ind_level <= 0) {
635         if (find_blks32(idblk, sblock.fs_bsize / sizeof(uint32_t), wantedblk))
636             return 1;
637     } else {
638         ind_level--;
639         for (i = 0; i < sblock.fs_bsize / sizeof(uint32_t); i++) {
640             if (compare_blk32(wantedblk, idblk[i])) {
641                 if (founddatablk(idblk[i]))
642                     return 1;
643             }
644             if (idblk[i] != 0)
645                 if (find_indirblks32(idblk[i], ind_level, wantedblk))
646                     return 1;
647         }
648     }
649 #undef MAXNINDIR
650     return 0;
651 }
652
653 static int
654 find_blks64(uint64_t *buf, int size, uint64_t *wantedblk)
655 {
656     int blk;
657     for (blk = 0; blk < size; blk++) {
658         if (buf[blk] == 0)
659             continue;
660         if (compare_blk64(wantedblk, buf[blk])) {
661             if (founddatablk(buf[blk]))
662                 return 1;
663         }
664     }
665     return 0;
666 }
667
668 static int
669 find_indirblks64(uint64_t blk, int ind_level, uint64_t *wantedblk)
670 {
671 #define MAXNINDIR      (MAXBSIZE / sizeof(uint64_t))
672     uint64_t idblk[MAXNINDIR];
673     int i;
674
675     blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
676     if (ind_level <= 0) {
677         if (find_blks64(idblk, sblock.fs_bsize / sizeof(uint64_t), wantedblk))
678             return 1;
679     } else {
680         ind_level--;
681         for (i = 0; i < sblock.fs_bsize / sizeof(uint64_t); i++) {
682             if (compare_blk64(wantedblk, idblk[i])) {
683                 if (founddatablk(idblk[i]))
684                     return 1;
685             }
686             if (idblk[i] != 0)
687                 if (find_indirblks64(idblk[i], ind_level, wantedblk))
688                     return 1;
689         }
690     }
691 #undef MAXNINDIR
692     return 0;
693 }
694
695 int findino(struct inodesc *idesc); /* from fsck */
696 static int dolookup(char *name);
697
698 static int
699 dolookup(char *name)
700 {
701     struct inodesc idesc;
702
703     if (!checkactivedir())
704             return 0;
705     idesc.id_number = curinum;
706     idesc.id_func = findino;
707     idesc.id_name = name;
708     idesc.id_type = DATA;
709     idesc.id_fix = IGNORE;
710     if (ckinode(curinode, &idesc) & FOUND) {
711         curinum = idesc.id_parent;
712         curinode = ginode(curinum);
713         printactive(0);
714         return 1;
715     } else {
716         warnx("name `%s' not found in current inode directory", name);
717         return 0;
718     }
719 }
720
721 CMDFUNCSTART(focusname)
722 {
723     char *p, *val;
724
725     if (!checkactive())
726         return 1;
727
728     ocurrent = curinum;
729     
730     if (argv[1][0] == '/') {
731         curinum = ROOTINO;
732         curinode = ginode(ROOTINO);
733     } else {
734         if (!checkactivedir())
735             return 1;
736     }
737     for (p = argv[1]; p != NULL;) {
738         while ((val = strsep(&p, "/")) != NULL && *val == '\0');
739         if (val) {
740             printf("component `%s': ", val);
741             fflush(stdout);
742             if (!dolookup(val)) {
743                 curinode = ginode(curinum);
744                 return(1);
745             }
746         }
747     }
748     return 0;
749 }
750
751 CMDFUNCSTART(ln)
752 {
753     ino_t inum;
754     int rval;
755     char *cp;
756
757     GETINUM(1,inum);
758
759     if (!checkactivedir())
760         return 1;
761     rval = makeentry(curinum, inum, argv[2]);
762     if (rval)
763             printf("Ino %ju entered as `%s'\n", (uintmax_t)inum, argv[2]);
764     else
765         printf("could not enter name? weird.\n");
766     curinode = ginode(curinum);
767     return rval;
768 }
769
770 CMDFUNCSTART(rm)
771 {
772     int rval;
773
774     if (!checkactivedir())
775         return 1;
776     rval = changeino(curinum, argv[1], 0);
777     if (rval & ALTERED) {
778         printf("Name `%s' removed\n", argv[1]);
779         return 0;
780     } else {
781         printf("could not remove name ('%s')? weird.\n", argv[1]);
782         return 1;
783     }
784 }
785
786 long slotcount, desired;
787
788 int
789 chinumfunc(struct inodesc *idesc)
790 {
791         struct direct *dirp = idesc->id_dirp;
792
793         if (slotcount++ == desired) {
794             dirp->d_ino = idesc->id_parent;
795             return STOP|ALTERED|FOUND;
796         }
797         return KEEPON;
798 }
799
800 CMDFUNCSTART(chinum)
801 {
802     char *cp;
803     ino_t inum;
804     struct inodesc idesc;
805     
806     slotcount = 0;
807     if (!checkactivedir())
808         return 1;
809     GETINUM(2,inum);
810
811     desired = strtol(argv[1], &cp, 0);
812     if (cp == argv[1] || *cp != '\0' || desired < 0) {
813         printf("invalid slot number `%s'\n", argv[1]);
814         return 1;
815     }
816
817     idesc.id_number = curinum;
818     idesc.id_func = chinumfunc;
819     idesc.id_fix = IGNORE;
820     idesc.id_type = DATA;
821     idesc.id_parent = inum;             /* XXX convenient hiding place */
822
823     if (ckinode(curinode, &idesc) & FOUND)
824         return 0;
825     else {
826         warnx("no %sth slot in current directory", argv[1]);
827         return 1;
828     }
829 }
830
831 int
832 chnamefunc(struct inodesc *idesc)
833 {
834         struct direct *dirp = idesc->id_dirp;
835         struct direct testdir;
836
837         if (slotcount++ == desired) {
838             /* will name fit? */
839             testdir.d_namlen = strlen(idesc->id_name);
840             if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) {
841                 dirp->d_namlen = testdir.d_namlen;
842                 strcpy(dirp->d_name, idesc->id_name);
843                 return STOP|ALTERED|FOUND;
844             } else
845                 return STOP|FOUND;      /* won't fit, so give up */
846         }
847         return KEEPON;
848 }
849
850 CMDFUNCSTART(chname)
851 {
852     int rval;
853     char *cp;
854     struct inodesc idesc;
855     
856     slotcount = 0;
857     if (!checkactivedir())
858         return 1;
859
860     desired = strtoul(argv[1], &cp, 0);
861     if (cp == argv[1] || *cp != '\0') {
862         printf("invalid slot number `%s'\n", argv[1]);
863         return 1;
864     }
865
866     idesc.id_number = curinum;
867     idesc.id_func = chnamefunc;
868     idesc.id_fix = IGNORE;
869     idesc.id_type = DATA;
870     idesc.id_name = argv[2];
871
872     rval = ckinode(curinode, &idesc);
873     if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED))
874         return 0;
875     else if (rval & FOUND) {
876         warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]);
877         return 1;
878     } else {
879         warnx("no %sth slot in current directory", argv[1]);
880         return 1;
881     }
882 }
883
884 struct typemap {
885     const char *typename;
886     int typebits;
887 } typenamemap[]  = {
888     {"file", IFREG},
889     {"dir", IFDIR},
890     {"socket", IFSOCK},
891     {"fifo", IFIFO},
892 };
893
894 CMDFUNCSTART(newtype)
895 {
896     int type;
897     struct typemap *tp;
898
899     if (!checkactive())
900         return 1;
901     type = DIP(curinode, di_mode) & IFMT;
902     for (tp = typenamemap;
903          tp < &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)];
904          tp++) {
905         if (!strcmp(argv[1], tp->typename)) {
906             printf("setting type to %s\n", tp->typename);
907             type = tp->typebits;
908             break;
909         }
910     }
911     if (tp == &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)]) {
912         warnx("type `%s' not known", argv[1]);
913         warnx("try one of `file', `dir', `socket', `fifo'");
914         return 1;
915     }
916     DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~IFMT);
917     DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | type);
918     inodirty();
919     printactive(0);
920     return 0;
921 }
922
923 CMDFUNCSTART(chlen)
924 {
925     int rval = 1;
926     long len;
927     char *cp;
928
929     if (!checkactive())
930         return 1;
931
932     len = strtol(argv[1], &cp, 0);
933     if (cp == argv[1] || *cp != '\0' || len < 0) { 
934         warnx("bad length `%s'", argv[1]);
935         return 1;
936     }
937     
938     DIP_SET(curinode, di_size, len);
939     inodirty();
940     printactive(0);
941     return rval;
942 }
943
944 CMDFUNCSTART(chmode)
945 {
946     int rval = 1;
947     long modebits;
948     char *cp;
949
950     if (!checkactive())
951         return 1;
952
953     modebits = strtol(argv[1], &cp, 8);
954     if (cp == argv[1] || *cp != '\0' || (modebits & ~07777)) { 
955         warnx("bad modebits `%s'", argv[1]);
956         return 1;
957     }
958     
959     DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~07777);
960     DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | modebits);
961     inodirty();
962     printactive(0);
963     return rval;
964 }
965
966 CMDFUNCSTART(chaflags)
967 {
968     int rval = 1;
969     u_long flags;
970     char *cp;
971
972     if (!checkactive())
973         return 1;
974
975     flags = strtoul(argv[1], &cp, 0);
976     if (cp == argv[1] || *cp != '\0' ) { 
977         warnx("bad flags `%s'", argv[1]);
978         return 1;
979     }
980     
981     if (flags > UINT_MAX) {
982         warnx("flags set beyond 32-bit range of field (%lx)\n", flags);
983         return(1);
984     }
985     DIP_SET(curinode, di_flags, flags);
986     inodirty();
987     printactive(0);
988     return rval;
989 }
990
991 CMDFUNCSTART(chgen)
992 {
993     int rval = 1;
994     long gen;
995     char *cp;
996
997     if (!checkactive())
998         return 1;
999
1000     gen = strtol(argv[1], &cp, 0);
1001     if (cp == argv[1] || *cp != '\0' ) { 
1002         warnx("bad gen `%s'", argv[1]);
1003         return 1;
1004     }
1005     
1006     if (gen > INT_MAX || gen < INT_MIN) {
1007         warnx("gen set beyond 32-bit range of field (%lx)\n", gen);
1008         return(1);
1009     }
1010     DIP_SET(curinode, di_gen, gen);
1011     inodirty();
1012     printactive(0);
1013     return rval;
1014 }
1015
1016 CMDFUNCSTART(linkcount)
1017 {
1018     int rval = 1;
1019     int lcnt;
1020     char *cp;
1021
1022     if (!checkactive())
1023         return 1;
1024
1025     lcnt = strtol(argv[1], &cp, 0);
1026     if (cp == argv[1] || *cp != '\0' ) { 
1027         warnx("bad link count `%s'", argv[1]);
1028         return 1;
1029     }
1030     if (lcnt > USHRT_MAX || lcnt < 0) {
1031         warnx("max link count is %d\n", USHRT_MAX);
1032         return 1;
1033     }
1034     
1035     DIP_SET(curinode, di_nlink, lcnt);
1036     inodirty();
1037     printactive(0);
1038     return rval;
1039 }
1040
1041 CMDFUNCSTART(chowner)
1042 {
1043     int rval = 1;
1044     unsigned long uid;
1045     char *cp;
1046     struct passwd *pwd;
1047
1048     if (!checkactive())
1049         return 1;
1050
1051     uid = strtoul(argv[1], &cp, 0);
1052     if (cp == argv[1] || *cp != '\0' ) { 
1053         /* try looking up name */
1054         if ((pwd = getpwnam(argv[1]))) {
1055             uid = pwd->pw_uid;
1056         } else {
1057             warnx("bad uid `%s'", argv[1]);
1058             return 1;
1059         }
1060     }
1061     
1062     DIP_SET(curinode, di_uid, uid);
1063     inodirty();
1064     printactive(0);
1065     return rval;
1066 }
1067
1068 CMDFUNCSTART(chgroup)
1069 {
1070     int rval = 1;
1071     unsigned long gid;
1072     char *cp;
1073     struct group *grp;
1074
1075     if (!checkactive())
1076         return 1;
1077
1078     gid = strtoul(argv[1], &cp, 0);
1079     if (cp == argv[1] || *cp != '\0' ) { 
1080         if ((grp = getgrnam(argv[1]))) {
1081             gid = grp->gr_gid;
1082         } else {
1083             warnx("bad gid `%s'", argv[1]);
1084             return 1;
1085         }
1086     }
1087     
1088     DIP_SET(curinode, di_gid, gid);
1089     inodirty();
1090     printactive(0);
1091     return rval;
1092 }
1093
1094 int
1095 dotime(char *name, time_t *secp, int32_t *nsecp)
1096 {
1097     char *p, *val;
1098     struct tm t;
1099     int32_t nsec;
1100     p = strchr(name, '.');
1101     if (p) {
1102         *p = '\0';
1103         nsec = strtoul(++p, &val, 0);
1104         if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) {
1105                 warnx("invalid nanoseconds");
1106                 goto badformat;
1107         }
1108     } else
1109         nsec = 0;
1110     if (strlen(name) != 14) {
1111 badformat:
1112         warnx("date format: YYYYMMDDHHMMSS[.nsec]");
1113         return 1;
1114     }
1115     *nsecp = nsec;
1116
1117     for (p = name; *p; p++)
1118         if (*p < '0' || *p > '9')
1119             goto badformat;
1120     
1121     p = name;
1122 #define VAL() ((*p++) - '0')
1123     t.tm_year = VAL();
1124     t.tm_year = VAL() + t.tm_year * 10;
1125     t.tm_year = VAL() + t.tm_year * 10;
1126     t.tm_year = VAL() + t.tm_year * 10 - 1900;
1127     t.tm_mon = VAL();
1128     t.tm_mon = VAL() + t.tm_mon * 10 - 1;
1129     t.tm_mday = VAL();
1130     t.tm_mday = VAL() + t.tm_mday * 10;
1131     t.tm_hour = VAL();
1132     t.tm_hour = VAL() + t.tm_hour * 10;
1133     t.tm_min = VAL();
1134     t.tm_min = VAL() + t.tm_min * 10;
1135     t.tm_sec = VAL();
1136     t.tm_sec = VAL() + t.tm_sec * 10;
1137     t.tm_isdst = -1;
1138
1139     *secp = mktime(&t);
1140     if (*secp == -1) {
1141         warnx("date/time out of range");
1142         return 1;
1143     }
1144     return 0;
1145 }
1146
1147 CMDFUNCSTART(chbtime)
1148 {
1149     time_t secs;
1150     int32_t nsecs;
1151
1152     if (dotime(argv[1], &secs, &nsecs))
1153         return 1;
1154     if (sblock.fs_magic == FS_UFS1_MAGIC)
1155         return 1;
1156     curinode->dp2.di_birthtime = _time_to_time64(secs);
1157     curinode->dp2.di_birthnsec = nsecs;
1158     inodirty();
1159     printactive(0);
1160     return 0;
1161 }
1162
1163 CMDFUNCSTART(chmtime)
1164 {
1165     time_t secs;
1166     int32_t nsecs;
1167
1168     if (dotime(argv[1], &secs, &nsecs))
1169         return 1;
1170     if (sblock.fs_magic == FS_UFS1_MAGIC)
1171         curinode->dp1.di_mtime = _time_to_time32(secs);
1172     else
1173         curinode->dp2.di_mtime = _time_to_time64(secs);
1174     DIP_SET(curinode, di_mtimensec, nsecs);
1175     inodirty();
1176     printactive(0);
1177     return 0;
1178 }
1179
1180 CMDFUNCSTART(chatime)
1181 {
1182     time_t secs;
1183     int32_t nsecs;
1184
1185     if (dotime(argv[1], &secs, &nsecs))
1186         return 1;
1187     if (sblock.fs_magic == FS_UFS1_MAGIC)
1188         curinode->dp1.di_atime = _time_to_time32(secs);
1189     else
1190         curinode->dp2.di_atime = _time_to_time64(secs);
1191     DIP_SET(curinode, di_atimensec, nsecs);
1192     inodirty();
1193     printactive(0);
1194     return 0;
1195 }
1196
1197 CMDFUNCSTART(chctime)
1198 {
1199     time_t secs;
1200     int32_t nsecs;
1201
1202     if (dotime(argv[1], &secs, &nsecs))
1203         return 1;
1204     if (sblock.fs_magic == FS_UFS1_MAGIC)
1205         curinode->dp1.di_ctime = _time_to_time32(secs);
1206     else
1207         curinode->dp2.di_ctime = _time_to_time64(secs);
1208     DIP_SET(curinode, di_ctimensec, nsecs);
1209     inodirty();
1210     printactive(0);
1211     return 0;
1212 }