Anything like getpwent() for inodes?

Chris Torek chris at umcp-cs.UUCP
Tue Oct 28 21:29:25 AEST 1986


In article <117 at BASKIN.UUCP> peter at BASKIN.UUCP (Peter Klosky) writes:
>I have been trying to get this [...] to work on the 4.2 file
>system layout, but I am confused about this new file system.  

Finding inodes is not too hard.  The main trick is that you must first
read the super-block:

	#include <sys/param.h>
	#ifdef sun		/* really NFS */
	#include <ufs/inode.h>
	#include <I forget, but it was obvious>
	#else
	#include <sys/inode.h>
	#endif
	#include <sys/fs.h>

	union {
		struct	fs sblk;
		char	pad[SBSIZE];
	} un;
	#define	sblock	un.sblk

	int diskfd;
	struct dinode *iget();
	...

		diskfd = open(rawdisk, 0);
		if (diskfd < 0)
			error...
		bread(SBLOCK, (char *)&sblock, SBSIZE);

	...
		ip = iget(inode_number);
	...

	struct dinode *
	iget(ino)
		ino_t ino;
	{
		static daddr_t iblk;
		static struct dinode *itab;
		static u_int itabsize;
		register daddr_t b;

	#ifdef fancy
		if (ino < ROOTINO)
			return (NULL);
	#endif
		if (itab == NULL) {
			/*
			 * Allocate a table of the appropriate size.
			 */
			iblk = (daddr_t) -1;
			itabsize = INOPB(&sblock) * sizeof (*itab);
			itab = (struct dinode *) malloc(itabsize);
			if (itab == NULL)
				error...
		}

		/*
		 * Inode is in block `File System Block to Disk Block' of
		 * file system block `Inode to Disk block' of inode.
		 */
		b = fsbtodb(&sblock, itod(&sblock, ino));
		if (b != iblk) {
			b = iblk;
			bread(b, (char *)itab, itabsize);
		}
		return (&itab[ino % INOPB(&sblock)]);
	}

	bread(bno, buf, cnt)
		daddr_t bno;
		char *buf;
	{

		lseek(diskfd, (off_t) dbtob(bno), 0);
		if (read(diskfd, buf, cnt) != cnt)
			error...
	}
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.unix mailing list