home *** CD-ROM | disk | FTP | other *** search
- From: rdg@hpfcso.FC.HP.COM (Rob Gardner)
- Date: Wed, 12 Aug 1992 20:42:08 GMT
- Subject: Re: inode to filename
- Message-ID: <7371226@hpfcso.FC.HP.COM>
- Organization: Hewlett-Packard, Fort Collins, CO, USA
- Path: sparky!uunet!cs.utexas.edu!sdd.hp.com!hpscdc!hplextra!hpfcso!rdg
- Newsgroups: comp.sys.hp
- References: <1992Aug06.172513.4063@CS.ORST.EDU>
- Lines: 42
-
-
- > Does anyone know of a reasonable way to obtain a filename from the inode in
- > HP-UX 8.x.I need to do it from a C program & it needs to be fairly efficient,
- > so ncheck isn't an option.
-
- A fast, "heuristic" way to do it exists, but it doesn't always work. The
- idea is not mine originally, but here it is:
-
- You know the inode number (call it I1) and want to find out the name
- of *a* file that has that inode number. Read the superblock (struct fs)
- for the file system containing the inode. Divide the inode number by
- the number of cylinder groups in the file system to get the cylinder
- group containing the inode (I1/fs->fs_ncg).
-
- Now look at just the inodes in that cylinder group, and search for
- directories. When you find one (call it I2), search it for an entry
- matching I1 (the inode number that you're interested in.) If you find
- it, you now know the (base-)name of a file.
-
- (You will find the inode you're looking for very often, because of a
- file system optimization which always *tries* to place the inode for
- a file in the same cylinder group that contains the directory. This
- is *much* faster than "find / -inum etc" because you're not searching
- the entire disk, just a tiny piece of it.)
-
- Now search the same directory (I2) for the ".." entry. When you find
- the inode for "..", search it for inode I2. Now you know the name of
- the directory containing I1! Repeat this process until the inode
- number of ".." is 2, the root directory, and you've got the full
- pathname of the file.
-
- If the file system isn't root, tack on fs->fs_fsmnt to get the directory
- that the disk was mounted on, and now you've got an absolute pathname
- for the file.
-
- Naturally, this scheme won't find any (hard) links to the original
- file if they are at a remote disk location. If links were made to the
- file, and then the file was unlinked from the directory where it was
- created, then the scheme won't find anything. In practice, the scheme
- works pretty well. (This scheme is used by the GlancePlus product.)
-
- Rob
-