ftp.nice.ch/pub/next/unix/disk/Fd.IHS.s.tar.gz#/Fd/clri/clri.c

This is clri.c in view mode; [Download] [Up]

/*
 * Copyright (c) 1990, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Rich $alz of BBN Inc.
 *
 * 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 University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University 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 REGENTS 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.
 */

/*
 *	Modified by John L. Chmielewski on Sat Dec 30, 1995
 *	Hacked code to work under NEXTSTEP/FIP 3.3
 *	Modified by Mark Salyzyn on Sun Dec 31, 1995 to add some UI.
 */

#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1990, 1993\n\
	The Regents of the University of California.  All rights reserved.\n";
#endif /* not lint */

#ifndef lint
static char sccsid[] = "@(#)clri.c	8.2 (Berkeley) 9/23/93";
#endif /* not lint */

#include <sys/param.h>
#include <sys/time.h>

#ifdef NeXT
#include <sys/vnode.h>
#include <ufs/inode.h>
#include <ufs/fs.h>
#include <dev/disk.h>
/* DEV_BSIZE should not be hardcoded */
#define	DEV_BSIZE	fsize	/*	512 for some >1G drives
					1024 othersize	*/
#define ino_to_fsba itod
#define ino_to_fsbo itoo
#include <architecture/byte_order.h>
/* must redefine header file macros for LITTLE_ENDIAN, BIG_ENDIAN also works */
#undef fsbtodb
#define fsbtodb(fs, b)	((b) << NXSwapBigLongToHost((fs)->fs_fsbtodb))
#undef INOPB
#define	INOPB(fs)	(NXSwapBigLongToHost((fs)->fs_inopb))
#undef itog
#define	itog(fs, x)	((x) / NXSwapBigLongToHost((fs)->fs_ipg))
#undef blkstofrags
#define blkstofrags(fs, blks)	/* calculates (blks * fs->fs_frag) */ \
	((blks) << NXSwapBigLongToHost((fs)->fs_fragshift))
#undef cgbase
#define	cgbase(fs, c)	((daddr_t)(NXSwapBigLongToHost((fs)->fs_fpg) * (c)))
#undef cgstart
#define cgstart(fs, c) \
	(cgbase(fs, c) + NXSwapBigLongToHost((fs)->fs_cgoffset) * ((c) \
	& ~(NXSwapBigLongToHost((fs)->fs_cgmask))))
#undef cgimin
#define	cgimin(fs, c)	(cgstart(fs, c) + NXSwapBigLongToHost((fs)->fs_iblkno))
#undef itod
#define	itod(fs, x) \
	((daddr_t)(cgimin(fs, itog(fs, x)) + \
	(blkstofrags((fs), (((x) % NXSwapBigLongToHost((fs)->fs_ipg)) / \
	INOPB(fs))))))
#else
#include <ufs/ufs/dinode.h>
#include <ufs/ffs/fs.h>

#include <err.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

int
main(argc, argv)
	int argc;
	char *argv[];
{
	register struct fs *sbp;
	register struct dinode *ip;
	register int fd;
	struct dinode ibuf[MAXBSIZE / sizeof (struct dinode)];
	long generation, bsize, fsize = 1024;
	off_t offset;
	int inonum, i, c;
	char *fs, sblock[SBSIZE];
	char nowrite = 0, noask = 0 ;

	while (fs = *++argv) {
		if (--argc == 0)
			break ;
		if (strcmp (fs, "-w") == 0)
			nowrite = 1 ;
		else if (strcmp (fs, "-y") == 0)
			noask = 1 ;
		else if (strcmp (fs, "-b") == 0)
			fsize = 512 ;
		else	break ;
	}
	if ((argc <= 0) || (fs == NULL)) {
usage:		(void)fprintf(stderr, "usage: clri [-w] [-y] [-b] filesystem inode ...\n");
		exit(1);
	}

	/* get the superblock. */
	if ((fd = open(fs, O_RDWR, 0)) < 0)
		err(1, "%s", fs);
#ifndef NeXT
	if (lseek(fd, (off_t)(SBLOCK * DEV_BSIZE), SEEK_SET) < 0)
#else
	if (lseek(fd, (off_t)(SBLOCK), SEEK_SET) < 0)
#endif
		err(1, "%s", fs);
	if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock)) {
		(void)fprintf(stderr,
		    "clri: %s: can't read the superblock.\n", fs);
		exit(1);
	}

	sbp = (struct fs *)sblock;
#ifndef NeXT
	if (sbp->fs_magic != FS_MAGIC) {
#else
	if (NXSwapBigLongToHost(sbp->fs_magic) != FS_MAGIC) {
#endif
		(void)fprintf(stderr,
		    "clri: %s: superblock magic number 0x%x, not 0x%x.\n",
		    fs, sbp->fs_magic, FS_MAGIC);
		exit(1);
	}
#ifndef NeXT
	bsize = sbp->fs_bsize;
#else
	bsize = NXSwapBigLongToHost(sbp->fs_bsize);
#endif

	/* remaining arguments are inode numbers. */
	while (*++argv) {
		/* get the inode number. */
		if ((inonum = atoi(*argv)) <= 0) {
			(void)fprintf(stderr,
			    "clri: %s is not a valid inode number.\n", *argv);
			exit(1);
		}
		(void)printf("%sinode: %d ", nowrite?"":"clearing ", inonum);

		/* read in the appropriate block. */
		offset = ino_to_fsba(sbp, inonum);	/* inode to fs blk */
		offset = fsbtodb(sbp, offset);		/* fs blk disk blk */
		offset *= DEV_BSIZE;			/* disk blk to bytes */

		/* seek and read the block */
		if (lseek(fd, offset, SEEK_SET) < 0)
			err(1, "%s", fs);
		if (read(fd, ibuf, bsize) != bsize)
			err(1, "%s", fs);

		/* get the inode within the block. */
		ip = &ibuf[ino_to_fsbo(sbp, inonum)];

		/* clear the inode, and bump the generation count. */
#ifndef NeXT
		(void)printf("mode: %o nlink: %d uid: %d gid: %d size: %d\n",
			ip->di_mode, ip->di_nlink, ip->di_uid,
			ip->di_gid, ip->di_size);
		generation = ip->di_gen + 1;
#else
		(void)printf("mode: %o nlink: %d uid: %d gid: %d size: %d\n",
			NXSwapBigShortToHost(ip->di_mode),
			NXSwapBigShortToHost(ip->di_nlink),
			NXSwapBigShortToHost(ip->di_uid),
			NXSwapBigShortToHost(ip->di_gid),
			NXSwapBigLongToHost(ip->di_ic.ic_size.val[1]));
		generation = NXSwapBigLongToHost(ip->di_gen) + 1;
#endif
		memset(ip, 0, sizeof(*ip));
#ifndef NeXT
		ip->di_gen = generation;
#else
		ip->di_gen = NXSwapHostLongToBig(generation);
#endif

		if (nowrite == 0) {
			if (noask == 0) {
				(void)fprintf (stderr, "Clear? ") ;
again:				c = getchar () ;
				while (((i = getchar ()) != EOF)
				 && (i != '\n') && (i != '\r')) ;
				switch (c) {
				case 'Y': case 'y':
					break ;
				case 'N': case 'n':
					continue ;
				default:
					(void)fprintf (stderr, "Yes or No? ") ;
					goto again;
				}
			}
			/* backup and write the block */
			if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
				err(1, "%s", fs);
			if (write(fd, ibuf, bsize) != bsize)
				err(1, "%s", fs);
			(void)fsync(fd);
		}
	}
	(void)close(fd);
	exit(0);
}

#ifdef NeXT
err(a, b, fs)
	int a;
	char *b;
	char *fs;
{
	perror(fs);
}
#endif

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.