コード例 #1
0
ファイル: minget.c プロジェクト: orionmiller/a4
int main(int argc, char *argv[])
{
  s_block *S_block;
  p_table *P_table=NULL;
  inode *Inode = NULL;
  options *Opt;
  FILE *fs;
  FILE *output; /*where all the data will be outputed*/
  uint32_t part_start; /*beginning file system*/
  uint32_t inode_off; /*beginning of inode zone*/
  uint32_t zone_size;
  uint8_t *data;
  char **path;
 

  if ((Opt = handleOptions(argc, argv))==NULL)
    {
      printf("minget [-v] [-p part [-s subpart]] imagefile srcpath [dstpath]\n");
      exit(EXIT_FAILURE);
    }

  FATALCALL((fs = fopen(Opt->imagefile, "rb"))==NULL,"fopen");

  /*check super block*/


  output = stdout;
  if (Opt->dstpath)
      FATALCALL((output = fopen(Opt->dstpath, "w+b"))==NULL,"fopen");

  part_start = 0; /*0 is start of image*/

  if (Opt->part_num == -1 && Opt->subpart_num == -1) /*no partition or subpartition*/
    {
      if((S_block = getSuperBlock(fs, 0)) == NULL) /*MAGIC NUMBER*/
	{
	  free(S_block);
	  exit(EXIT_FAILURE);
	}
      zone_size = S_block->block_size << S_block->log_zone_size;
      inode_off = part_start + (2 + S_block->imap_blocks + S_block->zmap_blocks) * S_block->block_size;
    }
  else if (Opt->part && !Opt->subpart) /*partition but no subpartition*/
    {
      P_table = getPartTable(fs, part_start, Opt->part_num);
      if (P_table == NULL)
	  exit(EXIT_FAILURE);

      part_start = P_table->lFirst * SECTOR_SIZE;

      if((S_block = getSuperBlock(fs, 0)) == NULL) /*MAGIC NUMBER*/
	{
	  free(S_block);
	  exit(EXIT_FAILURE);
	}
      zone_size = S_block->block_size << S_block->log_zone_size;
      inode_off = part_start + (2 + S_block->imap_blocks + S_block->zmap_blocks) * S_block->block_size;
    }
  else if (Opt->part && Opt->subpart) /* partition and subpartition */
    {
      P_table = getPartTable(fs, part_start, Opt->part_num); /*0 is start of image*/
      if (P_table == NULL)
	{
	  free(P_table);
	  exit(EXIT_FAILURE);
	}

      part_start = P_table->lFirst * SECTOR_SIZE;

      free(P_table);
      P_table = getPartTable(fs, part_start, Opt->subpart_num);
      if (P_table == NULL)
	{
	  free(P_table);
	  exit(EXIT_FAILURE);
	}
      part_start = P_table->lFirst * SECTOR_SIZE;
      if((S_block = getSuperBlock(fs, part_start)) == NULL) /*MAGIC NUMBER*/
	{
	  free(S_block);
	  exit(EXIT_FAILURE);
	}
      zone_size = S_block->block_size << S_block->log_zone_size;
      inode_off = part_start + (2 + S_block->imap_blocks + S_block->zmap_blocks) * S_block->block_size;
    }
  else
    {
      exit(EXIT_FAILURE);
    }
  path =tokstr(Opt->path, "/");
  if ((Inode = getFile(fs, path, inode_off, part_start, zone_size))==NULL)
    {
      fprintf(stderr, "File Doesn't Exist\n");
      exit(EXIT_FAILURE);
    }

  data = getData(fs, Inode, part_start, zone_size);
  if (Opt->verbose)
    verbose(S_block, Inode);
  printFile(output, data, Inode->size);  

  fclose(fs); /*check for errors*/
  if(P_table!=NULL)
    {  
      free(P_table);
    }
  free(Opt);
  free(S_block);
  return EXIT_SUCCESS;
}
コード例 #2
0
ファイル: ext2fsutil.c プロジェクト: Vvnesaa/fsck
void ext2fsutilTest(int start, int length) {
	struct ext2_super_block x;
	getSuperBlock(start, &x);
	int groupNum = getGroupNumber(&x);
	int blockSize = EXT2_BLOCK_SIZE(&x);
	struct ext2_group_desc groupDescs[MAX_GROUP_NUM];
	getGroupDescs(start, groupDescs, groupNum);
	unsigned char *blockBitmap = malloc(blockSize * groupNum + 1);
	getBlockBitmap(start, groupDescs, groupNum, blockBitmap, blockSize);
	unsigned char *inodeBitmap = malloc(blockSize * groupNum + 1);
	getInodeBitmap(start, groupDescs, groupNum, inodeBitmap, blockSize);
	struct ext2_inode *inodeTable = malloc(sizeof(struct ext2_inode) * x.s_inodes_per_group * groupNum);
	getInodeTable(start, &x, groupDescs, groupNum, inodeTable, blockSize);
	unsigned char *buf = malloc(inodeTable[localNo(EXT2_ROOT_INO)].i_size);
	getData(start, inodeTable + 1, blockSize, buf);
	AnalyzeDir(inodeTable + 1, buf);
	
/*	struct ext2_inode *inode;
	//we already know /lion is on inode 4017
	inode = inodeTable + 4016;
	printf("%d\n", isDirectory(inode));
	unsigned char *lionbuf = malloc(inode->i_size);
	getData(start, inode, blockSize, lionbuf);
	AnalyzeDir(inode, lionbuf);

	//we already know /lion/tigers is on inode 4018
	inode = inodeTable + 4017;
	printf("%d\n", isDirectory(inode));
	unsigned char *tigersbuf= malloc(inode->i_size);
	getData(start, inode, blockSize, tigersbuf);
	AnalyzeDir(inode, tigersbuf);

	//we already know /lion/tigers/bears is on inode 4019
	inode = inodeTable + 4018;
	printf("%d\n", isDirectory(inode));
	unsigned char *bearsbuf= malloc(inode->i_size);
	getData(start, inode, blockSize, bearsbuf);
	AnalyzeDir(inode, bearsbuf);
	
	//we already know /lion/tigers/bears/ohmy.txt is on inode 4021
	inode = inodeTable + 4020;
	printf("%d\n", isDirectory(inode)); 
	unsigned char *txtbuf= malloc(inode->i_size);
	getData(start, inode, blockSize, txtbuf);
	//printf("%s", txtbuf);
	printf("%d\n", isBlockBitmapSet(localNo(inode->i_block[0]), blockBitmap, groupNum, blockSize, &x));

	free(txtbuf); 
	free(bearsbuf);	
	free(tigersbuf);
	free(lionbuf); */

/*	struct ext2_inode *inode;
	//we already know /oz/tornado/glinda is on inode 30 
	inode = inodeTable + 29;
	printf("%d\n", isSymbolicLink(inode));
	printf("%d %d %d %d\n", inode->i_size, inode->i_blocks, inode->i_block[0], inode->i_block[1]);
	int i;
	for (i = 0; i < inode->i_size; ++i)
		printf("%c", ((unsigned char *)inode->i_block)[i]);
	printf("\n");
//	unsigned char *ozbuf = malloc(inode->i_size);
//	getData(start, inode, blockSize, ozbuf);
//	printf("%s\n", ozbuf);
//	AnalyzeDir(inode, ozbuf);
	
//	free(ozbuf);
*/
	free(buf);
	free(blockBitmap);
	free(inodeBitmap);
}