home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / unix_c / sysadmin / scani.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  3.0 KB  |  154 lines

  1.  7-Sep-85 22:05:04-MDT,3181;000000000001
  2. Return-Path: <unix-sources-request@BRL.ARPA>
  3. Received: from BRL-TGR.ARPA by SIMTEL20.ARPA with TCP; Sat 7 Sep 85 22:04:57-MDT
  4. Received: from usenet by TGR.BRL.ARPA id a001898; 7 Sep 85 16:07 EDT
  5. From: Fred Toth #7252 <fpt@wgivax.uucp>
  6. Newsgroups: net.sources
  7. Subject: disappearing disk space (V7)
  8. Message-ID: <126@wgivax.UUCP>
  9. Date: 6 Sep 85 19:04:06 GMT
  10. To:       unix-sources@BRL-TGR.ARPA
  11.  
  12. /*
  13.  * This posting is for those interested in the 'disappearing disk space'
  14.  * discussion in net.unix-wizards.
  15.  *
  16.  * Fred Toth
  17.  * Washburn Graphics, Inc.
  18.  * Charlotte, NC
  19.  * decvax!mcnc!unccvax!wgivax!fpt
  20.  *
  21.  * scani.c
  22.  *
  23.  * This program scans a v7 file system, looking for inodes
  24.  * that have more blocks allocated to them then they need
  25.  * based on the size field.
  26.  *
  27.  * It reads it's standard input and reports inode numbers.
  28.  * Ncheck can then be used to find the files.
  29.  * Copying them and deleting the original will free the blocks.
  30.  */
  31. #include <stdio.h>
  32. #include <sys/param.h>
  33. #include <sys/filsys.h>
  34. #include <sys/ino.h>
  35.  
  36. struct filsys sb;
  37.  
  38. struct dinode mynod;
  39.  
  40. daddr_t addr[13];    /* store converted disk addresses */
  41.  
  42. long ci, cii, ciii;
  43.  
  44. main()
  45. {
  46.     int i;
  47.     unsigned toti, in;
  48.     long has, need, countb(), extra, totex, totb;
  49.     int files;
  50.  
  51.     files = 0;
  52.     totb = totex = 0L;
  53.     fseek(stdin, 512L, 0);
  54.     fread(&sb, sizeof(struct filsys), 1, stdin);
  55.     toti = (sb.s_isize - 2) * 8;
  56.     printf("%u inodes\n", toti);
  57.     fseek(stdin, 1024L, 0);
  58.     in = 0;
  59.     while (toti--) {
  60.         in++;
  61.         fseek(stdin, (((long) in - 1L) * 64L) + 1024L, 0);
  62.         if (fread(&mynod, sizeof(struct dinode), 1, stdin) == NULL)
  63.             break;
  64.         if (mynod.di_mode) {
  65.             files++;
  66.             l3tol(addr, &mynod.di_addr[0], 13);
  67.             need = (mynod.di_size + 511) / 512;
  68.             if ((has = countb()) > need) {
  69.                 extra = has - need;
  70.                 totex += extra;
  71.                 printf("i %u, needs %D, has %D, %D extra\n", in, need, has, extra);
  72.             }
  73.             totb += has;
  74.         }
  75.     }
  76.     printf("Did %u inodes, %d files\n", in, files);
  77.     printf("%D extra allocated blocks\n", totex);
  78.     printf("%D data, %D i, %D, ii, %D iii\n", totb, ci, cii, ciii);
  79. }
  80. long
  81. countb()
  82. {
  83.     int i;
  84.     long count, cindir(), cdoub(), ctrip();
  85.  
  86.     count = 0;
  87.     for (i = 0; i < 10; i++)
  88.         if (addr[i])
  89.             count++;
  90.     if (addr[10])
  91.         count += cindir(addr[10]);
  92.     if (addr[11])
  93.         count += cdoub(addr[11]);
  94.     if (addr[12])
  95.         count += ctrip(addr[12]);
  96.     return(count);
  97. }
  98. long
  99. cindir(blk)
  100. daddr_t blk;
  101. {
  102.     long count;
  103.     daddr_t ib[128];
  104.     int i;
  105.  
  106.     count = 0;
  107.     getblk(ib, blk);
  108.     ci++;
  109.     for (i = 0; i < 128; i++)
  110.         if (ib[i])
  111.             count++;
  112.     return(count);
  113. }
  114. getblk(p, no)
  115. char *p;
  116. daddr_t no;
  117. {
  118.     fseek(stdin, no * 512, 0);
  119.     if (fread(p, 512, 1, stdin) <= 0)
  120.         printf("bad read, b %D\n", no);
  121. }
  122. long
  123. cdoub(blk)
  124. daddr_t blk;
  125. {
  126.     daddr_t dib[128];
  127.     long count;
  128.     int i;
  129.  
  130.     count = 0;
  131.     getblk(dib, blk);
  132.     cii++;
  133.     for (i = 0; i < 128; i++)
  134.         if (dib[i])
  135.             count += cindir(dib[i]);
  136.     return(count);
  137. }
  138. long
  139. ctrip(blk)
  140. daddr_t blk;
  141. {
  142.     daddr_t tib[128];
  143.     long count;
  144.     int i;
  145.  
  146.     count = 0;
  147.     getblk(tib, blk);
  148.     ciii++;
  149.     for (i = 0; i < 128; i++)
  150.         if (tib[i])
  151.             count += cdoub(tib[i]);
  152.     return(count);
  153. }
  154.