summaryrefslogtreecommitdiff
path: root/sbin/fsdb/fsdb.c
blob: 0cb330839f77c80c706169e9570b15f55bd18a2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
/*	$OpenBSD: fsdb.c,v 1.16 2003/09/26 16:09:27 deraadt Exp $	*/
/*	$NetBSD: fsdb.c,v 1.7 1997/01/11 06:50:53 lukem Exp $	*/

/*-
 * Copyright (c) 1996 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by John T. Kohl.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *        This product includes software developed by the NetBSD
 *        Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef lint
static const char rcsid[] = "$OpenBSD: fsdb.c,v 1.16 2003/09/26 16:09:27 deraadt Exp $";
#endif /* not lint */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/mount.h>
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <grp.h>
#include <histedit.h>
#include <limits.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <ufs/ufs/dinode.h>
#include <ufs/ufs/dir.h>
#include <ufs/ffs/fs.h>

#include "fsdb.h"
#include "fsck.h"
#include "extern.h"

extern char *__progname;	/* from crt0.o */

int main(int, char *[]);
static void usage(void);
static int cmdloop(void);
static int helpfn(int, char *[]);
static char *prompt(EditLine *);
static int scannames(struct inodesc *);
static int dolookup(char *);
static int chinumfunc(struct inodesc *);
static int chnamefunc(struct inodesc *);
static int dotime(char *, int32_t *, int32_t *);

int returntosingle = 0;
struct ufs1_dinode *curinode;
ino_t curinum;

static void
usage(void)
{
	fprintf(stderr, "usage: %s [-d] -f <fsname>\n", __progname);
	exit(1);
}

/*
 * We suck in lots of fsck code, and just pick & choose the stuff we want.
 *
 * fsreadfd is set up to read from the file system, fswritefd to write to
 * the file system.
 */
int
main(int argc, char *argv[])
{
	int ch, rval;
	char *fsys = NULL;

	while (-1 != (ch = getopt(argc, argv, "f:d"))) {
		switch (ch) {
		case 'f':
			fsys = optarg;
			break;
		case 'd':
			debug++;
			break;
		default:
			usage();
		}
	}
	if (fsys == NULL)
		usage();
	if (!setup(fsys))
		errx(1, "cannot set up file system `%s'", fsys);
	printf("Editing file system `%s'\nLast Mounted on %s\n", fsys,
	    sblock.fs_fsmnt);
	rval = cmdloop();
	sblock.fs_clean = 0;		/* mark it dirty */
	sbdirty();
	ckfini(0);
	printf("*** FILE SYSTEM MARKED DIRTY\n");
	printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
	printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n");
	exit(rval);
}

#define CMDFUNC(func) static int func(int argc, char *argv[])
#define CMDFUNCSTART(func) static int func(int argc, char *argv[])

CMDFUNC(helpfn);
CMDFUNC(focus);				/* focus on inode */
CMDFUNC(active);			/* print active inode */
CMDFUNC(focusname);			/* focus by name */
CMDFUNC(zapi);				/* clear inode */
CMDFUNC(uplink);			/* incr link */
CMDFUNC(downlink);			/* decr link */
CMDFUNC(linkcount);			/* set link count */
CMDFUNC(quit);				/* quit */
CMDFUNC(ls);				/* list directory */
CMDFUNC(rm);				/* remove name */
CMDFUNC(ln);				/* add name */
CMDFUNC(newtype);			/* change type */
CMDFUNC(chmode);			/* change mode */
CMDFUNC(chlen);				/* change length */
CMDFUNC(chaflags);			/* change flags */
CMDFUNC(chgen);				/* change generation */
CMDFUNC(chowner);			/* change owner */
CMDFUNC(chgroup);			/* Change group */
CMDFUNC(back);				/* pop back to last ino */
CMDFUNC(chmtime);			/* Change mtime */
CMDFUNC(chctime);			/* Change ctime */
CMDFUNC(chatime);			/* Change atime */
CMDFUNC(chinum);			/* Change inode # of dirent */
CMDFUNC(chname);			/* Change dirname of dirent */

static struct cmdtable cmds[] = {
	{ "help", "Print out help", 1, 1, helpfn },
	{ "?", "Print out help", 1, 1, helpfn },
	{ "inode", "Set active inode to INUM", 2, 2, focus },
	{ "clri", "Clear inode INUM", 2, 2, zapi },
	{ "lookup", "Set active inode by looking up NAME", 2, 2, focusname },
	{ "cd", "Set active inode by looking up NAME", 2, 2, focusname },
	{ "back", "Go to previous active inode", 1, 1, back },
	{ "active", "Print active inode", 1, 1, active },
	{ "print", "Print active inode", 1, 1, active },
	{ "uplink", "Increment link count", 1, 1, uplink },
	{ "downlink", "Decrement link count", 1, 1, downlink },
	{ "linkcount", "Set link count to COUNT", 2, 2, linkcount },
	{ "ls", "List current inode as directory", 1, 1, ls },
	{ "rm", "Remove NAME from current inode directory", 2, 2, rm },
	{ "del", "Remove NAME from current inode directory", 2, 2, rm },
	{ "ln", "Hardlink INO into current inode directory as NAME", 3, 3, ln },
	{ "chinum", "Change dir entry number INDEX to INUM", 3, 3, chinum },
	{ "chname", "Change dir entry number INDEX to NAME", 3, 3, chname },
	{ "chtype", "Change type of current inode to TYPE", 2, 2, newtype },
	{ "chmod", "Change mode of current inode to MODE", 2, 2, chmode },
	{ "chown", "Change owner of current inode to OWNER", 2, 2, chowner },
	{ "chlen", "Change length of current inode to LENGTH", 2, 2, chlen },
	{ "chgrp", "Change group of current inode to GROUP", 2, 2, chgroup },
	{ "chflags", "Change flags of current inode to FLAGS", 2, 2, chaflags },
	{ "chgen", "Change generation number of current inode to GEN", 2, 2, chgen },
	{ "mtime", "Change mtime of current inode to MTIME", 2, 2, chmtime },
	{ "ctime", "Change ctime of current inode to CTIME", 2, 2, chctime },
	{ "atime", "Change atime of current inode to ATIME", 2, 2, chatime },
	{ "quit", "Exit", 1, 1, quit },
	{ "q", "Exit", 1, 1, quit },
	{ "exit", "Exit", 1, 1, quit },
	{ NULL, 0, 0, 0 },
};

static int
helpfn(int argc, char *argv[])
{
	struct cmdtable *cmdtp;

	printf("Commands are:\n%-10s %5s %5s   %s\n",
	    "command", "min argc", "max argc", "what");

	for (cmdtp = cmds; cmdtp->cmd; cmdtp++)
		printf("%-10s %5u %5u   %s\n",
		    cmdtp->cmd, cmdtp->minargc, cmdtp->maxargc, cmdtp->helptxt);
	return 0;
}

static char *
prompt(EditLine *el)
{
	static char pstring[64];

	snprintf(pstring, sizeof(pstring), "fsdb (inum: %d)> ", curinum);
	return pstring;
}


static int
cmdloop(void)
{
	char *line = NULL;
	const char *elline;
	int cmd_argc, rval = 0, known;
#define scratch known
	char **cmd_argv;
	struct cmdtable *cmdp;
	History *hist;
	EditLine *elptr;

	curinode = ginode(ROOTINO);
	curinum = ROOTINO;
	printactive();

	hist = history_init();
	history(hist, H_EVENT, 100);	/* 100 elt history buffer */

	elptr = el_init(__progname, stdin, stdout);
	el_set(elptr, EL_EDITOR, "emacs");
	el_set(elptr, EL_PROMPT, prompt);
	el_set(elptr, EL_HIST, history, hist);
	el_source(elptr, NULL);

	while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) {
		if (debug)
			printf("command `%s'\n", line);

		history(hist, H_ENTER, elline);

		line = strdup(elline);
		if (line == NULL)
			errx(1, "out of memory");
		cmd_argv = crack(line, &cmd_argc);
		if (cmd_argc) {
			/*
			 * el_parse returns -1 to signal that it's not been handled
			 * internally.
			 */
			if (el_parse(elptr, cmd_argc, cmd_argv) != -1)
				continue;
			known = 0;
			for (cmdp = cmds; cmdp->cmd; cmdp++) {
				if (!strcmp(cmdp->cmd, cmd_argv[0])) {
					if (cmd_argc >= cmdp->minargc &&
					    cmd_argc <= cmdp->maxargc)
						rval = (*cmdp->handler)(cmd_argc,
						    cmd_argv);
					else
						rval = argcount(cmdp,
						    cmd_argc, cmd_argv);
					known = 1;
					break;
				}
			}
			if (!known) {
				warnx("unknown command `%s'", cmd_argv[0]);
				rval = 1;
			}
		} else
			rval = 0;
		free(line);
		if (rval < 0)
			return rval;
		if (rval)
			warnx("rval was %d", rval);
	}
	el_end(elptr);
	history_end(hist);
	return rval;
}

static ino_t ocurrent;

#define GETINUM(ac,inum)    inum = strtoul(argv[ac], &cp, 0); \
	if (inum < ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \
		printf("inode %d out of range; range is [%d,%d]\n", \
		    inum, ROOTINO, maxino); \
		return 1; \
	}

/*
 * Focus on given inode number
 */
CMDFUNCSTART(focus)
{
	ino_t inum;
	char *cp;

	GETINUM(1,inum);
	curinode = ginode(inum);
	ocurrent = curinum;
	curinum = inum;
	printactive();
	return 0;
}

CMDFUNCSTART(back)
{
	curinum = ocurrent;
	curinode = ginode(curinum);
	printactive();
	return 0;
}

CMDFUNCSTART(zapi)
{
	ino_t inum;
	struct ufs1_dinode *dp;
	char *cp;

	GETINUM(1,inum);
	dp = ginode(inum);
	clearinode(dp);
	inodirty();
	if (curinode)			/* re-set after potential change */
		curinode = ginode(curinum);
	return 0;
}

CMDFUNCSTART(active)
{
	printactive();
	return 0;
}


CMDFUNCSTART(quit)
{
	return -1;
}

CMDFUNCSTART(uplink)
{
	if (!checkactive())
		return 1;
	printf("inode %d link count now %d\n", curinum, ++curinode->di_nlink);
	inodirty();
	return 0;
}

CMDFUNCSTART(downlink)
{
	if (!checkactive())
		return 1;
	printf("inode %d link count now %d\n", curinum, --curinode->di_nlink);
	inodirty();
	return 0;
}

static const char *typename[] = {
	"unknown",
	"fifo",
	"char special",
	"unregistered #3",
	"directory",
	"unregistered #5",
	"blk special",
	"unregistered #7",
	"regular",
	"unregistered #9",
	"symlink",
	"unregistered #11",
	"socket",
	"unregistered #13",
	"whiteout",
};

static int slot;

static int
scannames(struct inodesc *idesc)
{
	struct direct *dirp = idesc->id_dirp;

	printf("slot %d ino %d reclen %d: %s, `%.*s'\n",
	    slot++, dirp->d_ino, dirp->d_reclen, typename[dirp->d_type],
	    dirp->d_namlen, dirp->d_name);
	return (KEEPON);
}

CMDFUNCSTART(ls)
{
	struct inodesc idesc;
	checkactivedir();			/* let it go on anyway */

	slot = 0;
	idesc.id_number = curinum;
	idesc.id_func = scannames;
	idesc.id_type = DATA;
	idesc.id_fix = IGNORE;
	ckinode(curinode, &idesc);
	curinode = ginode(curinum);

	return 0;
}

static int
dolookup(char *name)
{
	struct inodesc idesc;

	if (!checkactivedir())
		return 0;
	idesc.id_number = curinum;
	idesc.id_func = findino;
	idesc.id_name = name;
	idesc.id_type = DATA;
	idesc.id_fix = IGNORE;
	if (ckinode(curinode, &idesc) & FOUND) {
		curinum = idesc.id_parent;
		curinode = ginode(curinum);
		printactive();
		return 1;
	} else {
		warnx("name `%s' not found in current inode directory", name);
		return 0;
	}
}

CMDFUNCSTART(focusname)
{
	char *p, *val;

	if (!checkactive())
		return 1;

	ocurrent = curinum;

	if (argv[1][0] == '/') {
		curinum = ROOTINO;
		curinode = ginode(ROOTINO);
	} else {
		if (!checkactivedir())
		    return 1;
	}
	for (p = argv[1]; p != NULL;) {
		while ((val = strsep(&p, "/")) != NULL && *val == '\0');
			if (val) {
				printf("component `%s': ", val);
				fflush(stdout);
				if (!dolookup(val)) {
					curinode = ginode(curinum);
					return(1);
				}
			}
	}
	return 0;
}

CMDFUNCSTART(ln)
{
	ino_t inum;
	int rval;
	char *cp;

	GETINUM(1,inum);

	if (!checkactivedir())
		return 1;
	rval = makeentry(curinum, inum, argv[2]);
	if (rval)
		printf("Ino %d entered as `%s'\n", inum, argv[2]);
	else
		printf("could not enter name? weird.\n");
	curinode = ginode(curinum);
	return rval;
}

CMDFUNCSTART(rm)
{
	int rval;

	if (!checkactivedir())
		return 1;
	rval = changeino(curinum, argv[1], 0);
	if (rval & ALTERED) {
		printf("Name `%s' removed\n", argv[1]);
		return 0;
	} else {
		printf("could not remove name? weird.\n");
		return 1;
	}
}

static long slotcount, desired;

static int
chinumfunc(struct inodesc *idesc)
{
	struct direct *dirp = idesc->id_dirp;

	if (slotcount++ == desired) {
	    dirp->d_ino = idesc->id_parent;
	    return STOP|ALTERED|FOUND;
	}
	return KEEPON;
}

CMDFUNCSTART(chinum)
{
	char *cp;
	ino_t inum;
	struct inodesc idesc;

	slotcount = 0;
	if (!checkactivedir())
		return 1;
	GETINUM(2,inum);

	desired = strtol(argv[1], &cp, 0);
	if (cp == argv[1] || *cp != '\0' || desired < 0) {
		printf("invalid slot number `%s'\n", argv[1]);
		return 1;
	}

	idesc.id_number = curinum;
	idesc.id_func = chinumfunc;
	idesc.id_fix = IGNORE;
	idesc.id_type = DATA;
	idesc.id_parent = inum;		/* XXX convenient hiding place */

	if (ckinode(curinode, &idesc) & FOUND)
		return 0;
	else {
		warnx("no %sth slot in current directory", argv[1]);
		return 1;
	}
}

static int
chnamefunc(struct inodesc *idesc)
{
	struct direct *dirp = idesc->id_dirp;
	struct direct testdir;

	if (slotcount++ == desired) {
		/* will name fit? */
		testdir.d_namlen = strlen(idesc->id_name);
		if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) {
			dirp->d_namlen = testdir.d_namlen;
			strlcpy(dirp->d_name, idesc->id_name, sizeof dirp->d_name);
			return STOP|ALTERED|FOUND;
		} else
			return STOP|FOUND;	/* won't fit, so give up */
	}
	return KEEPON;
}

CMDFUNCSTART(chname)
{
	int rval;
	char *cp;
	struct inodesc idesc;

	slotcount = 0;
	if (!checkactivedir())
		return 1;

	desired = strtoul(argv[1], &cp, 0);
	if (cp == argv[1] || *cp != '\0') {
		printf("invalid slot number `%s'\n", argv[1]);
		return 1;
	}

	idesc.id_number = curinum;
	idesc.id_func = chnamefunc;
	idesc.id_fix = IGNORE;
	idesc.id_type = DATA;
	idesc.id_name = argv[2];

	rval = ckinode(curinode, &idesc);
	if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED))
		return 0;
	else if (rval & FOUND) {
		warnx("new name `%s' does not fit in slot %s", argv[2], argv[1]);
		return 1;
	} else {
		warnx("no %sth slot in current directory", argv[1]);
		return 1;
	}
}

static struct typemap {
	const char *typename;
	int typebits;
} typenamemap[]  = {
	{"file", IFREG},
	{"dir", IFDIR},
	{"socket", IFSOCK},
	{"fifo", IFIFO},
};

CMDFUNCSTART(newtype)
{
	int type;
	struct typemap *tp;

	if (!checkactive())
		return 1;
	type = curinode->di_mode & IFMT;
	for (tp = typenamemap;
	    tp < &typenamemap[sizeof(typemap)/sizeof(*typemap)];
	    tp++) {
		if (!strcmp(argv[1], tp->typename)) {
			printf("setting type to %s\n", tp->typename);
			type = tp->typebits;
			break;
		}
	}
	if (tp == &typenamemap[sizeof(typemap)/sizeof(*typemap)]) {
		warnx("type `%s' not known", argv[1]);
		warnx("try one of `file', `dir', `socket', `fifo'");
		return 1;
	}
	curinode->di_mode &= ~IFMT;
	curinode->di_mode |= type;
	inodirty();
	printactive();
	return 0;
}

CMDFUNCSTART(chmode)
{
	int rval = 1;
	long modebits;
	char *cp;

	if (!checkactive())
		return 1;

	modebits = strtol(argv[1], &cp, 8);
	if (cp == argv[1] || *cp != '\0' ) {
		warnx("bad modebits `%s'", argv[1]);
		return 1;
	}

	curinode->di_mode &= ~07777;
	curinode->di_mode |= modebits;
	inodirty();
	printactive();
	return rval;
}

CMDFUNCSTART(chlen)
{
	int rval = 1;
	long len;
	char *cp;

	if (!checkactive())
		return 1;

	len = strtol(argv[1], &cp, 0);
	if (cp == argv[1] || *cp != '\0' || len < 0) {
		warnx("bad length '%s'", argv[1]);
		return 1;
	}

	curinode->di_size = len;
	inodirty();
	printactive();
	return rval;
}

CMDFUNCSTART(chaflags)
{
	int rval = 1;
	u_long flags;
	char *cp;

	if (!checkactive())
		return 1;

	flags = strtoul(argv[1], &cp, 0);
	if (cp == argv[1] || *cp != '\0' ) {
		warnx("bad flags `%s'", argv[1]);
		return 1;
	}

	if (flags > UINT_MAX) {
		warnx("flags set beyond 32-bit range of field (%lx)", flags);
		return(1);
	}
	curinode->di_flags = flags;
	inodirty();
	printactive();
	return rval;
}

CMDFUNCSTART(chgen)
{
	int rval = 1;
	long gen;
	char *cp;

	if (!checkactive())
		return 1;

	gen = strtol(argv[1], &cp, 0);
	if (cp == argv[1] || *cp != '\0' ) {
		warnx("bad gen `%s'", argv[1]);
		return 1;
	}

	if (gen > INT_MAX || gen < INT_MIN) {
		warnx("gen set beyond 32-bit range of field (%lx)", gen);
		return(1);
	}
	curinode->di_gen = gen;
	inodirty();
	printactive();
	return rval;
}

CMDFUNCSTART(linkcount)
{
	int rval = 1;
	int lcnt;
	char *cp;

	if (!checkactive())
		return 1;

	lcnt = strtol(argv[1], &cp, 0);
	if (cp == argv[1] || *cp != '\0' ) {
		warnx("bad link count `%s'", argv[1]);
		return 1;
	}
	if (lcnt > USHRT_MAX || lcnt < 0) {
		warnx("max link count is %d", USHRT_MAX);
		return 1;
	}

	curinode->di_nlink = lcnt;
	inodirty();
	printactive();
	return rval;
}

CMDFUNCSTART(chowner)
{
	int rval = 1;
	uid_t uid;
	char *cp;
	struct passwd *pwd;

	if (!checkactive())
		return 1;

	uid = strtoul(argv[1], &cp, 0);
	if (cp == argv[1] || *cp != '\0' ) {
		/* try looking up name */
		if ((pwd = getpwnam(argv[1]))) {
			uid = pwd->pw_uid;
		} else {
			warnx("bad uid `%s'", argv[1]);
			return 1;
		}
	}

	curinode->di_uid = uid;
	inodirty();
	printactive();
	return rval;
}

CMDFUNCSTART(chgroup)
{
	int rval = 1;
	gid_t gid;
	char *cp;
	struct group *grp;

	if (!checkactive())
		return 1;

	gid = strtoul(argv[1], &cp, 0);
	if (cp == argv[1] || *cp != '\0' ) {
		if ((grp = getgrnam(argv[1]))) {
			gid = grp->gr_gid;
		} else {
			warnx("bad gid `%s'", argv[1]);
			return 1;
		}
	}

	curinode->di_gid = gid;
	inodirty();
	printactive();
	return rval;
}

static int
dotime(char *name, int32_t *rsec, int32_t *rnsec)
{
	char *p, *val;
	struct tm t;
	int32_t sec;
	int32_t nsec;

	p = strchr(name, '.');
	if (p) {
		*p = '\0';
		nsec = strtoul(++p, &val, 0);
		if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) {
			warnx("invalid nanoseconds");
			goto badformat;
		}
	} else
		nsec = 0;

	if (strlen(name) != 14) {
badformat:
		warnx("date format: YYYYMMDDHHMMSS[.nsec]");
		return 1;
	}

	for (p = name; *p; p++)
		if (*p < '0' || *p > '9')
			    goto badformat;

	p = name;
#define VAL() ((*p++) - '0')
	t.tm_year = VAL();
	t.tm_year = VAL() + t.tm_year * 10;
	t.tm_year = VAL() + t.tm_year * 10;
	t.tm_year = VAL() + t.tm_year * 10 - 1900;
	t.tm_mon = VAL();
	t.tm_mon = VAL() + t.tm_mon * 10 - 1;
	t.tm_mday = VAL();
	t.tm_mday = VAL() + t.tm_mday * 10;
	t.tm_hour = VAL();
	t.tm_hour = VAL() + t.tm_hour * 10;
	t.tm_min = VAL();
	t.tm_min = VAL() + t.tm_min * 10;
	t.tm_sec = VAL();
	t.tm_sec = VAL() + t.tm_sec * 10;
	t.tm_isdst = -1;

	sec = mktime(&t);
	if (sec == -1) {
		warnx("date/time out of range");
		return 1;
	}
	*rsec = sec;
	*rnsec = nsec;
	return 0;
}

CMDFUNCSTART(chmtime)
{
	if (dotime(argv[1], &curinode->di_mtime, &curinode->di_mtimensec))
		return 1;
	inodirty();
	printactive();
	return 0;
}

CMDFUNCSTART(chatime)
{
	if (dotime(argv[1], &curinode->di_atime, &curinode->di_atimensec))
		return 1;
	inodirty();
	printactive();
	return 0;
}

CMDFUNCSTART(chctime)
{
	if (dotime(argv[1], &curinode->di_ctime, &curinode->di_ctimensec))
		return 1;
	inodirty();
	printactive();
	return 0;
}