[comp.unix.wizards] given inode and fs, determine if dir or not?

mkhaw@teknowledge-vaxc.ARPA (Mike Khaw) (11/23/88)

If I have an inode (and fs root) on hand, how do I tell whether that
inode is a directory or not?  The definition of struct inode suggests
that it's possible, but I can't find a man entry that says how.

If it matters, this would be for SunOS 3.x and Ultrix 2.x

Thanks,
Mike Khaw
-- 
internet: mkhaw@teknowledge.arpa
uucp:	  {uunet|sun|ucbvax|decwrl|ames|hplabs}!mkhaw%teknowledge.arpa
hardcopy: Teknowledge Inc, 1850 Embarcadero Rd, POB 10119, Palo Alto, CA 94303

gwyn@smoke.BRL.MIL (Doug Gwyn ) (11/23/88)

In article <25943@teknowledge-vaxc.ARPA> mkhaw@teknowledge-vaxc.ARPA (Mike Khaw) writes:
>If I have an inode (and fs root) on hand, how do I tell whether that
>inode is a directory or not?  The definition of struct inode suggests
>that it's possible, but I can't find a man entry that says how.

Assuming you have already figured out how to find the disk block
containing the start of the inode, the start of an inode on disk
normally consists of essentially the same information that would
be returned for stat(2) or fstat(2) and a handful of pointers to
the data blocks associated with the inode.  (Slightly
oversimplified.)  There is often a system header file <sys/inode.h>
that describes the on-disk inode structure; watch out, because it
typically includes descriptions of in-core (kernel) data structures,
of which the disk inode information is only a part.

Look in your System Administrator Reference Manual under FS(*)
(where * depends on the vendor) for information about the file
system.

ed@mtxinu.UUCP (Ed Gould) (11/24/88)

>If I have an inode (and fs root) on hand, how do I tell whether that
>inode is a directory or not?  The definition of struct inode suggests
>that it's possible, but I can't find a man entry that says how.

If inode.i_mode == IFDIR, then inode is a directory.

-- 
Ed Gould                    mt Xinu, 2560 Ninth St., Berkeley, CA  94710  USA
{ucbvax,uunet}!mtxinu!ed    +1 415 644 0146

"I'll fight them as a woman, not a lady.  I'll fight them as an engineer."

ron@ron.rutgers.edu (Ron Natalie) (11/24/88)

I'm not sure what you mean by "I have an inode."  If you have the
inode, the you look at the IFMT bits for IFDIR.  Users don't normally
get "inodes."  What they can do is run "stat" on a file specification
or fstat on an open file descriptor and get returned the inode information
for that file.  Again you check the IFMT bits.

-Ron

mkhaw@teknowledge-vaxc.ARPA (Mike Khaw) (11/24/88)

> I'm not sure what you mean by "I have an inode."  If you have the

Sorry: an int that represents an inode number.

Mike Khaw
-- 
internet: mkhaw@teknowledge.arpa
uucp:	  {uunet|sun|ucbvax|decwrl|ames|hplabs}!mkhaw%teknowledge.arpa
hardcopy: Teknowledge Inc, 1850 Embarcadero Rd, POB 10119, Palo Alto, CA 94303

guy@auspex.UUCP (Guy Harris) (11/25/88)

 >If I have an inode (and fs root) on hand, how do I tell whether that
 >inode is a directory or not?  The definition of struct inode suggests
 >that it's possible, but I can't find a man entry that says how.
 >
 >If it matters, this would be for SunOS 3.x and Ultrix 2.x

It doesn't matter greatly; both those systems use the 4.2BSD file
system, but Berkeley pretty much left the part of the inode that comes
into play here alone, so the same technique can be used on V7/4.1BSD/S5
file systems.

I presume (since you refer to "struct inode") by "inode" you mean "inode
structure", either on disk or in core, rather than inumber.  If you have
an inumber, you have to get the inode structure first....

The "i_mode" field of the inode is laid out the exact same way the
"st_mode" field of a "stat" structure is.  Even the bits used in that
field are the same in most UNIX implementations; you shouldn't, and
don't have to, rely on this, however.  Just check whether

	(i_mode & IFMT) == IFDIR

(the analogy of checking whether "(st_mode & S_IFMT) == S_IFDIR)" for a
"stat" structure).

guy@auspex.UUCP (Guy Harris) (11/26/88)

>Sorry: an int that represents an inode number.

In that case, life gets more complicated; you have to open the file
system (which means you have to figure out which device it is - if you
have only the major/minor, you'd have to scan "/dev" looking for the
appropriate block special file) and decipher the i-list.  This is not
*too* painful for a V7/S5 file system; it's more work for a 4.2BSD file
system.

Note also that if the file system in question is, say, an NFS or RFS
file system, you're probably out of luck.