Home | History | Annotate | Download | only in mongo
      1 /*
      2  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
      3  */
      4 
      5 #include <stdio.h>
      6 #include <sys/types.h>
      7 #include <sys/stat.h>
      8 #include <fcntl.h>
      9 #include <linux/fs.h>
     10 #include <errno.h>
     11 
     12 int main(int argc, char **argv)
     13 {
     14 	int fd;
     15 	int block;
     16 	int first_block;
     17 	int last_block;
     18 	int totals_block;
     19 	int fragments;
     20 	int i;
     21 	int n;
     22 
     23 	for (n = 1; n < argc; n++) {
     24 		if (argc < 2) {
     25 			printf
     26 			    ("Used to see file maps \nUsage: %s filename1 [[..[filename2]...filename(N-1)] filenameN]\n",
     27 			     argv[0]);
     28 			return 0;
     29 		}
     30 		fd = open(argv[n], O_RDONLY);
     31 		if (fd == -1) {
     32 			perror("open failed");
     33 			continue;
     34 		}
     35 		// printf ("file %s occupies blocks: \n", argv[1]);
     36 		// printf ("START\tEND\tCOUNT\n");
     37 		i = 0;
     38 		block = 0;
     39 		first_block = 0;
     40 		last_block = 0;
     41 		fragments = 0;
     42 		totals_block = 0;
     43 
     44 		while (ioctl(fd, FIBMAP, &block) == 0) {
     45 			if (first_block == 0) {
     46 				last_block = block - 1;
     47 				first_block = block;
     48 			}
     49 			if (block != last_block + 1) {
     50 				// printf ("%d\t%d\t%d\n",first_block,last_block,last_block-first_block+1);
     51 				totals_block += last_block - first_block + 1;
     52 				fragments++;
     53 				first_block = block;
     54 				last_block = block;
     55 			} else {
     56 				last_block++;
     57 			}
     58 
     59 			if (!block) {
     60 				//printf ("Fragments: %d\tBlocks: %d\n",fragments,totals_block);
     61 				//printf ("%d:%d\t",fragments,totals_block);
     62 				//if (fragments == 1) printf(".",totals_block);
     63 				//else printf("%d_",fragments,totals_block);
     64 				printf("%d\n", fragments);
     65 				break;
     66 			}
     67 
     68 			i++;
     69 			block = i;
     70 		}
     71 		if (errno) {
     72 			perror("FIBMAP failed");
     73 		}
     74 		close(fd);
     75 		// printf ("\n");
     76 	}
     77 	return 0;
     78 }
     79