home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / sys / hp / 9225 < prev    next >
Encoding:
Internet Message Format  |  1992-08-12  |  2.3 KB

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