bfind.c for SCO Unix 3.2v4.x

3.2v4.2 binary (note NOT for Release 5!!) Shift-Click here

If you are running 3.2v5.x, see man badblk
DO NOT USE THIS!!


Hate these ads?



/* bfind.c finds the inode for a given disk block
   or prints block numbers for an inode (-i) (pretty print)
   or prints block numbers for an inode (-l) (simple list)
   (C) October 1993 Anthony Lawrence
(781) 784-7547
*/
#include <stdio.h>  
#include <sys/types.h>
#include <sys/fs/s5param.h>
#include <sys/fs/s5filsys.h>
#include <sys/ino.h>



char SCCSID[]="@(#) bfind.c 1.6 (C) 10/14/93 Lawrence & Clark, Inc";



int inum=0;
int Blocksize=1024; /* default, reset later if necessary */
int pblocks=0; /* default action, looking for blocks, not inodes */
char lister=' ';  /* -i or -l */
int level=0; /* used for tabbing in on -l */
int alevel=0; /* used to determine depth of indirection (see below) */








main(int argc,char **argv)
 {
 struct filsys buf;
 struct dinode node;
 FILE *dsk;
 struct filsys *d=&buf;
 struct dinode *i=&node;
 int x,start_at;
 ulong num_inodes;
 unsigned long wants;



 if (argc < 3)
  {
  printf("bfind filesys block-no\n)");
  exit(1);
  }
 wants=atol(argv[2]);


 
 if ((dsk=fopen(argv[1],"r"))== NULL) 
  {
  printf("bfind filesys block-no\n)");
  exit(1);
  }
 fseek(dsk,512,SEEK_SET);
 fread(d,1,512,dsk);
/* Checking to be sure this is a filesystem that we know how
   to handle, etc.  See /usr/include/sys/filesys.h */
 if (d->s_magic != FsMAGIC && d->s_magic != FsEMAGIC)
   {
   fprintf(stderr,"(%x) Unknown File System: can't.\n",d->s_magic);
   exit(0);
   }
 if (d->s_fsize <=0 || d->s_isize > d->s_fsize)
   {
   fprintf(stderr,"Corrupted File System: can't.\n",d->s_magic);
   exit(0);
   }
 if (d->s_state+(long)d->s_time != FsOKAY && d->s_state != FsACTIVE)
   {
   fprintf(stderr,"State: (%x) Unclean or corrupted File System: can't.\n",d->s_state);
   exit(0);
   }



 switch(d->s_type)
   {
   case 1:
      Blocksize=512;break;
   case 2:
      Blocksize=1024;break;
   case 3:
      Blocksize=2048;break;
   default :
      fprintf(stderr,"I don't know this type of filesystem %d\n",d->s_type);
      exit(0);
   }
 num_inodes=Blocksize * (d->s_isize-2)/(sizeof(node));  
 printf("%u %u byte inodes, %u block-size\n",num_inodes,sizeof(struct dinode),Blocksize);
 fseek(dsk,3 * 512,SEEK_SET);
 fread(d,1,512,dsk);
 fread(i,1,sizeof(node),dsk);


 
 start_at=0;
 if (argc > 3) /* -i or -l */
   {
   start_at=atoi(argv[3])-2;
   lister=argv[2][1];
   if (start_at > num_inodes+2)
     {
     fprintf(stderr,"Too high inum %u only %u\n",start_at,num_inodes);
     exit(1);
     }
   fseek(dsk,start_at * sizeof(node),SEEK_CUR);
   pblocks=1;  
   }

cartoon
Need eyes on the ground at your customer's site?
Installation and light training Boston and New England
Reliable and experienced, punctual and professional.


 
 for (x=start_at ; x < num_inodes-2 ; x++)
  { 
  int deep;
  inum=x+2; /* first inum is 2, but we are indexing from 0 */
  if (pblocks)
   printf("Checking Inode %d\n",inum);
  fread(i,1,sizeof(node),dsk);
  if (i->di_size) /* if this is zero, we have a "special" file: no blocks */
   {
     /* There are 13 sets of 3 byte integers that hold the 
        disk block #'s.  The first 10 (0-9) are direct blocks
        (file data will be found in the blocks), the 11th is
        indirect (it points to direct blocks that point to data)
        and the 12th is Double Indirect (it points to indirect
        blocks that point to direct blocks that point to data).
        The 13th is Triple Indirect (points to double indirect
        which... as usual)



        There are undoubtedly easier ways to extract these numbers,
        but the method below shows (I think!) what's really there.
      */
    for (deep=0; deep < 39; deep += 3)
     {
     unsigned long z;
     z= i->di_addr[deep+0] & 0xff;
     z +=  (i->di_addr[deep+1] << 8) & 0xff00;
     z +=  (i->di_addr[deep+2] << 16) & 0xff0000;
     if (pblocks)
        do_blocks(z,deep);
     if (z == wants && z)  /* wants is zero if pblock is true */
      {
      printf("%lu is inode %d \n",z,inum);
      fclose(dsk);
      exit(0);
      /* found it and done */
      }
      if (deep > 27 && z ) /* indirect block */
        {
        level++;
        alevel=deep/3;
        descend(dsk,deep,wants,z);
        level--;
        }
      }
    }
  if (pblocks) /* done, but may need flushing */
    {
    do_blocks(0,0); /* The zero will force a flush */
    printf("\n");
    exit(0);
    }
   }
 }


 
 int descend(FILE *dsk, int deep, unsigned long wants,unsigned long now)
   {
   unsigned long cpos=ftell(dsk);
   unsigned long z;
   int x=Blocksize / sizeof(long);
   int y=0;


     


   
   fseek(dsk,now * Blocksize,SEEK_SET);
   for ( y=0; y < x; y++)
      {
      fread((char *) &z,sizeof(long),1,dsk);
      if (pblocks)  
        do_blocks(z,deep);
      if (z == wants && z)
        {
        printf("%lu is inode %d \n",z,inum);
        fclose(dsk);
        exit(0);
        }
     if (deep > 30 && z)
       {
       level++;
       alevel=deep/3-1;
       descend( dsk, deep - 3, wants, z);
       level--;
       }
      }


 
   fseek(dsk,cpos,SEEK_SET);
   return(0);
   }



int do_blocks(unsigned z,int depth)
{
static int reached_depth=0;
static int in_range=0;
static int in_zero=0;
static int zeros=0;
static ulong last_block=0;
int tabs;



if (!z)
     {
     zeros++;
     in_zero++;
     }
/* Above counts blocks pointing to 0.  Could be 'holes', so we
   want to print them if they are.
   Below looks to see if we have been counting zeros */


 
if (z && in_zero)      
  {
  printf("\n %d Zero Blocks (holes)\n",zeros);
  in_zero=0;
  zeros=0;
  }



if (lister=='l' && z) /* straight list of blocks */
     {
     for (tabs=0; tabs < level; tabs++)
        printf("   ");
     printf("%u\n",z);
     }
if (lister=='i') /* prettied list of blocks */
  {
  if (alevel > 9)  
  {
  if (in_range)
     {
     printf("- %u ",last_block);
     in_range=0;
     }
    switch(alevel)
      {
      case 10: printf("\n(Indirect) %u\n",last_block);break;
      case 11: printf("\n(Double Indirect) %u\n",last_block);break;
      case 12: printf("\n(Triple Indirect) %u\n",last_block);break;
      }
  last_block=z;
  alevel--;
  printf("%u ",z);
  return(0);
  }
  if (z == last_block+1)
     {
     in_range=1;
     last_block=z;
     return(0);
     }
  if (!in_range && z)
     {
     printf("\n%u ",z);
     last_block=z;
     return(0);
     }
  if (in_range)
     {
     printf("- %u",last_block);
     last_block=z;
     in_range=0;
     printf("\n%u ",z);
     }
  }
}









Comments /Unix/bfind.html


Add your comments

Enter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)

Or use any RSS reader

Delivered by FeedBurner


LOD Communications, Inc.

Views for this page
Today This Week This Month This Year  Overall
1976566 3,185

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

pavatar.jpg
More:
       - Disks/Filesystems
       - Code




Unix/Linux Consultants

Your ad here - $24.00 yearly!

larryi@ccamedical.com SCO OS5, Debian Linux, RedHat Linux, MySQL, Apache, AJAX development using dXport/dL4/Unibasic, Windows Connectivity, Sharing Resouces, Automation, Shell Scripting


http://www.cleverminds.net Need expert advice? Want a second opinion? CleverMinds is a one-stop-shop for a wide range of technology solutions. We support Unix, Linux, SCO as well as CMS, ecom, blogs, podcasts, search engines consulting and more. Contact us at web2.0@cleverminds.net 0r (617) 894-1282


http://www.loch-raven.com/ Over 18 years of experience Unix and Linux servers. Linux and Unix consulting, system administration, remote administration, custom scripting, web desing and hosting.




card_image








Change Congress


Related Posts