[comp.sources.misc] v07i061: Fragmentation Check

allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc) (07/09/89)

Posting-number: Volume 7, Issue 61
Submitted-by: dold@mitisft.convergent.com (Clarence Dold)
Archive-name: frag

This turned out to be so short, I should have done it quite some time ago.
A really short, non-error checking tool that accepts one file name,
finds it's inode and invokes a pipe from bcheck to scan the assigned block
numbers.
These block numbers are examined to make sure they are contiguous, else it
prints some info.

Do I really need a makefile?
all:	frag

frag: frag.o

frag.o:
	cc -o frag frag.c

---------------------------------------snip----------------------
/* program to check for fragmentation of one file.
 * Arbitrarily chose 3 block gap as acceptable, to allow for
 * necessary inode gaps in large files.
 * Super User Usage: frag filename
 * CADold - dold@Convergent.COM - Fri Jun  9 19:02:38 PDT 1989
 */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/gdisk.h>

#ifndef AUTHOR
#define AUTHOR
static char *Author=" dold@Convergent.COM";
#endif		/* If there is no author specified, stick my name in the code */

char cmd[80] ="/usr/local/bin/bcheck -i ";
main(argc, argv)
int argc;
char **argv;
{
struct stat statbuf;
int stat(), cont, drive, slice;
FILE *pipe;
char line[BUFSIZ];
long this, last, contig;
long jumps=0L, blocks=0L, restart=0L;


if (argc != 2){
	printf("Usage: %s filename\n", argv[0]);
	exit(1);
}

stat (argv[1], &statbuf);
slice = statbuf.st_dev % GDMAXSLICE;
drive = ((statbuf.st_dev % (GDMAXSLICE * DRIVES)) - slice)/GDMAXSLICE;
cont = statbuf.st_dev / (GDMAXSLICE * DRIVES);

sprintf(line, "%d /dev/rdsk/c%dd%ds%d", statbuf.st_ino, cont, drive, slice);
strcat(cmd, line);
if ((pipe = popen(cmd, "r")) != NULL){
	fgets(line,BUFSIZ-1,pipe);
	last = atol(line);
	blocks++;

	while (fgets(line,BUFSIZ-1,pipe) != NULL ) {
		this = atol(line);
		if  ( (this -3 > last) || (this < last)  ){
			jumps++;
			contig = blocks - restart;
			restart = blocks;
			printf("Block jump from %ld to %ld, after %ld contiguous blocks\n",
				last, this, contig);
		}
		blocks++;
		last = this;
	}
} else {
	exit(1);
}
pclose(pipe);
printf("%ld jumps in %ld blocks\n", jumps, blocks);
return(0);
}

---------------------------------------snip----------------------
-- 
---
Clarence A Dold - dold@tsmiti.Convergent.COM		(408) 434-5293
		...pyramid!ctnews!tsmiti!dold
		P.O.Box 6685, San Jose, CA 95150-6685	MS#10-007