// $Id: ex4b.c,v 1.5 2004-12-09 03:19:22+09 kaiya Exp kaiya $ /* Spec Get a list of blocks for a path Usage ./a.out path < filesystem_file Output List of block numbers, or error. Limit block_number: natural number and it should specify directory block. 0<=block_number<1440 filesytem_file should be simple ext2 file system dump; i.e. block size=1024B, number of blocks=1440. path should be started with '/', should not be ended with '/'. Compile gcc -I/usr/src/linux-2.4/include/ thisfile.c or gcc thisfile.c using ext2_os2004.h Platform gcc in intel i386 */ #include #include #include #include // #include #include "ext2_os2004.h" // 1024*1440 = 1474560 #define ALLBLOCKSIZE 1474560 #define BLOCKSIZE 1024 #define BLOCKNUMBER 1440 // data in all blocks are stored here. unsigned char blocks[ALLBLOCKSIZE]; /* Get the beginning of n'th block. Argument: n ro: specify the number of block. (0, 1, 2, ...) Return: pointer to the beginning of the block. NULL if fail. */ unsigned char* nth_block(int n){ if(n<0 || BLOCKNUMBER<=n) return NULL; // out of range return blocks+(n*BLOCKSIZE); } /* Get dir_entry from byte array. Arguments: *block ro: begin of byte array *de rw: begin address of gotten dir_entry. de->name is terminated. *resta rw: number of bytes still remained. Return: pointer to the next dir entry. NULL if fail. */ unsigned char* get_dir_entry( struct ext2_dir_entry_2* de, unsigned char* block, int* resta ){ if(*resta<=0) return NULL; memcpy(de, block, sizeof(*de)); de->name[de->name_len]='\0'; *resta -= (int)de->rec_len; return block+(int)de->rec_len; } /* Get information of n'th inode in *inodep */ int get_inode(int n, struct ext2_inode* inodep){ // n=1, 2, 3 ... unsigned char* s=nth_block(5); if(s==NULL) return 0; // fail memcpy(inodep, s+(n-1)*sizeof(*inodep), sizeof(*inodep)); #ifdef DEBUG printf("inode %d: size = %d\n", n, inodep->i_size); // debug #endif return 1; } /* Show the list of block numbers of inode. */ void show_blocks(struct ext2_inode inode){ int i; for(i=0; i*BLOCKSIZE<(int)inode.i_size && i