xref: /original-bsd/sbin/clri/clri.c (revision f51da917)
1 /*
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rich $alz of BBN Inc.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char copyright[] =
13 "@(#) Copyright (c) 1990, 1993\n\
14 	The Regents of the University of California.  All rights reserved.\n";
15 #endif /* not lint */
16 
17 #ifndef lint
18 static char sccsid[] = "@(#)clri.c	8.2 (Berkeley) 09/23/93";
19 #endif /* not lint */
20 
21 #include <sys/param.h>
22 #include <sys/time.h>
23 
24 #include <ufs/ufs/dinode.h>
25 #include <ufs/ffs/fs.h>
26 
27 #include <err.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 
35 int
36 main(argc, argv)
37 	int argc;
38 	char *argv[];
39 {
40 	register struct fs *sbp;
41 	register struct dinode *ip;
42 	register int fd;
43 	struct dinode ibuf[MAXBSIZE / sizeof (struct dinode)];
44 	long generation, bsize;
45 	off_t offset;
46 	int inonum;
47 	char *fs, sblock[SBSIZE];
48 
49 	if (argc < 3) {
50 		(void)fprintf(stderr, "usage: clri filesystem inode ...\n");
51 		exit(1);
52 	}
53 
54 	fs = *++argv;
55 
56 	/* get the superblock. */
57 	if ((fd = open(fs, O_RDWR, 0)) < 0)
58 		err(1, "%s", fs);
59 	if (lseek(fd, (off_t)(SBLOCK * DEV_BSIZE), SEEK_SET) < 0)
60 		err(1, "%s", fs);
61 	if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock)) {
62 		(void)fprintf(stderr,
63 		    "clri: %s: can't read the superblock.\n", fs);
64 		exit(1);
65 	}
66 
67 	sbp = (struct fs *)sblock;
68 	if (sbp->fs_magic != FS_MAGIC) {
69 		(void)fprintf(stderr,
70 		    "clri: %s: superblock magic number 0x%x, not 0x%x.\n",
71 		    fs, sbp->fs_magic, FS_MAGIC);
72 		exit(1);
73 	}
74 	bsize = sbp->fs_bsize;
75 
76 	/* remaining arguments are inode numbers. */
77 	while (*++argv) {
78 		/* get the inode number. */
79 		if ((inonum = atoi(*argv)) <= 0) {
80 			(void)fprintf(stderr,
81 			    "clri: %s is not a valid inode number.\n", *argv);
82 			exit(1);
83 		}
84 		(void)printf("clearing %d\n", inonum);
85 
86 		/* read in the appropriate block. */
87 		offset = ino_to_fsba(sbp, inonum);	/* inode to fs blk */
88 		offset = fsbtodb(sbp, offset);		/* fs blk disk blk */
89 		offset *= DEV_BSIZE;			/* disk blk to bytes */
90 
91 		/* seek and read the block */
92 		if (lseek(fd, offset, SEEK_SET) < 0)
93 			err(1, "%s", fs);
94 		if (read(fd, ibuf, bsize) != bsize)
95 			err(1, "%s", fs);
96 
97 		/* get the inode within the block. */
98 		ip = &ibuf[ino_to_fsbo(sbp, inonum)];
99 
100 		/* clear the inode, and bump the generation count. */
101 		generation = ip->di_gen + 1;
102 		memset(ip, 0, sizeof(*ip));
103 		ip->di_gen = generation;
104 
105 		/* backup and write the block */
106 		if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
107 			err(1, "%s", fs);
108 		if (write(fd, ibuf, bsize) != bsize)
109 			err(1, "%s", fs);
110 		(void)fsync(fd);
111 	}
112 	(void)close(fd);
113 	exit(0);
114 }
115