Ejemplo n.º 1
0
Archivo: fs.c Proyecto: mainboy/xv6
// Read and validate the file system bitmap.
//
// Read all the bitmap blocks into memory.
// Set the "bitmap" pointer to point at the beginning of the first
// bitmap block.
// 
// Check that all reserved blocks -- 0, 1, and the bitmap blocks themselves --
// are all marked as in-use
// (for each block i, assert(!block_is_free(i))).
//
// Hint: Assume that the superblock has already been loaded into
// memory (in variable 'super').  Check out super->s_nblocks.
void
read_bitmap(void)
{
	int r;
	uint32_t i;
	char *blk;

	// Read the bitmap into memory.
	// The bitmap consists of one or more blocks.  A single bitmap block
	// contains the in-use bits for BLKBITSIZE blocks.  There are
	// super->s_nblocks blocks in the disk altogether.
	// Set 'bitmap' to point to the first address in the bitmap.
	// Hint: Use read_block.

	// LAB 5: Your code here.
	uint32_t bitmap_blkno = super->s_nblocks / BLKBITSIZE;
	if(super -> s_nblocks % BLKBITSIZE != 0)
		bitmap_blkno++;

	for(i=0;i<bitmap_blkno;i++){
		if(read_block(2+i,&blk) < 0)
			panic("read_bitmap: read_block fail!\n");
	}

	bitmap = (uint32_t *)diskaddr(2);
	// Make sure the reserved and root blocks are marked in-use.
	assert(!block_is_free(0));
	assert(!block_is_free(1));
	assert(bitmap);

	// Make sure that the bitmap blocks are marked in-use.
	// LAB 5: Your code here.
	for(i=0;i<bitmap_blkno;i++)
		assert(!block_is_free(2+i));
	cprintf("read_bitmap is good\n");
}
Ejemplo n.º 2
0
boost::string_ref HDFSFileSplitter::fetch_block(bool is_next) {
    int nbytes = 0;
    if (is_next) {
        // directly read the next block using the current file
        nbytes = hdfsRead(fs_, file_, data_, hdfs_block_size);
        if (nbytes == 0)
            return "";
        if (nbytes == -1) {
            throw base::HuskyException("read next block error!");
        }
    } else {
        // Ask the master for a new block
        BinStream question;
        question << url_ << husky::Context::get_param("hostname");
        BinStream answer = husky::Context::get_coordinator()->ask_master(question, husky::TYPE_HDFS_BLK_REQ);
        std::string fn;
        answer >> fn;
        answer >> offset_;

        if (fn == "") {
            // no more files
            return "";
        }

        if (file_ != NULL) {
            int rc = hdfsCloseFile(fs_, file_);
            assert(rc == 0);
            // Notice that "file" will be deleted inside hdfsCloseFile
            file_ = NULL;
        }

        // read block
        nbytes = read_block(fn);
    }
    return boost::string_ref(data_, nbytes);
}
Ejemplo n.º 3
0
main (int argc, char *argv[])
{
  float block[GULP],copy[GULP];
  int ixnc,kxnc,i,j,k,n,off,nadd,nread,headersize=0;

  input=fopen(argv[1],"rb");
  output=fopen(argv[2],"wb");
  nadd=atoi(argv[3]);

  for (i=0;i<4;i++) ifstream[i]='Y';

  if (!(headersize=read_header(input))) 
    error_message("could not read header parameters!");
  obits=32;
  tsamp*=nadd;
  filterbank_header(output);
  for (i=0;i<GULP;i++) block[i]=copy[i]=0.0;
  nread=GULP/nchans;

  while(n=read_block(input,nbits,block,nread)) {
    k=off=0;
    for (i=0;i<n/nchans;i++) {
      ixnc=i*nchans;
      kxnc=k*nchans;
      for (j=0;j<nchans;j++) copy[off+j]+=block[ixnc+j];
      k++;
      if (k==nadd) {
	off+=nchans;
	k=0;
      }
    }
    for (j=0;j<n/nadd;j++) copy[j]/=(float)nadd;
    fwrite(copy,sizeof(float),n/nadd,output);
    for (j=0;j<n/nadd;j++) copy[j]=0.0;
  }
}
Ejemplo n.º 4
0
/*
 * Update dedup structure with block's hash and crc
 */
void dedup_calc_block_hash_crc(sector_t block)
{
	size_t block_size = dedup_get_block_size();
	char *block_data;

	if (block >= blocks_count)
		// outside dedup range
		return;

	block_data = (char*)kmalloc(block_size, GFP_KERNEL);
	if (block_data == NULL) {
		printk(KERN_ERR "failed allocating block data buffer.\n");
		return;
	}

	// Read block
	read_block(block_data, block_size, start_block + block);
	// Calc hash
	calc_hash(block_data, block_size, blocksArray.hashes[block]);
	// Calc crc32
	blocksArray.hash_crc[block] = crc32_le(0, blocksArray.hashes[block], SHA256_DIGEST_SIZE);

	kfree(block_data);
}
Ejemplo n.º 5
0
size_t ext2_read(Ext2_file *file, uint8_t *buf, size_t count)
{
	// Check if we would read past the end of the file
	if (file->pos + count > file->inode.size)
		count = file->inode.size - file->pos;

	size_t bytes_left   = count;

	while (bytes_left > 0) {
		size_t to_copy = bytes_left;

		// Check if this read will go beyond the current buffer
		bool new_block = file->curr_block_pos + to_copy >= block_size;
		if (new_block)
			to_copy = block_size - file->curr_block_pos;

		// Copy across from the buffer in the *file and advance the position
		memcpy(buf + (count - bytes_left),
				file->buf + file->curr_block_pos, to_copy);
		file->curr_block_pos += to_copy;
		file->pos            += to_copy;
		bytes_left           -= to_copy;

		// If we read to the end of the buffer then read the next block
		if (new_block) {
			file->curr_block_pos = 0;
			file->block_index++;
			if (file->block_index >= 12)
				PANIC("Indirect block pointers are currently unsupported");

			read_block(file->inode.dbp[file->block_index], file->buf);
		}
	}

	return count;
}
Ejemplo n.º 6
0
DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber (0) */
	BYTE *buff,		/* Data buffer to store read data */
	DWORD sector,	/* Sector number (LBA) */
	UINT count		/* Sector count (1..128) */
)
{
	if (pdrv || !count) return RES_PARERR;
	if (Stat & STA_NOINIT) return RES_NOTRDY;

	/* Issue Read Setor(s) command */
	if (!issue_rwcmd(CMD_READ, sector, count)) return RES_ERROR;

	/* Receive data blocks */
	do {
		if (!wait_stat(2000, DRQ)) return RES_ERROR;
		read_block(buff);
		buff += 512;
	} while (--count);
	read_ata(REG_ALTSTAT);
	read_ata(REG_STATUS);

	return RES_OK;
}
int sba_reiserfs_find_journal_entries(void)
{
	int i;
	char *data;
	
	if ((data = read_block(REISER_SUPER)) != NULL) {
		struct reiserfs_super_block *rsb = (struct reiserfs_super_block *)data;

		if (sba_reiserfs_is_any_reiserfs_magic_string (rsb)) {
			sba_debug(1,"SUCCESS ! Found reiserfs superblock\n");

			sba_debug(1,"Journal begin = %u Journal size = %u\n", 
			rsb->s_v1.s_journal.jp_journal_1st_block, rsb->s_v1.s_journal.jp_journal_size);

			sba_debug(1,"Trans max = %u Max batch = %u\n", 
			rsb->s_v1.s_journal.jp_journal_trans_max, rsb->s_v1.s_journal.jp_journal_max_batch);

			sba_debug(1,"Commit age = %u Trans age = %u\n", 
			rsb->s_v1.s_journal.jp_journal_max_commit_age, rsb->s_v1.s_journal.jp_journal_max_trans_age);

			reiser_jour_start = rsb->s_v1.s_journal.jp_journal_1st_block;
			reiser_jour_size = rsb->s_v1.s_journal.jp_journal_size;
		}

		free_page((int)data);
	}

	
	for (i = 0; i < reiser_jour_size; i ++) {
		ht_add(h_sba_reiserfs_journal, reiser_jour_start + i);
	}

	sba_debug(1, "Begin = %d End = %d\n", reiser_jour_start, reiser_jour_start + reiser_jour_size - 1);

	return 1;
}
Ejemplo n.º 8
0
static int comment_43_uncompress(FILE *infp, FILE *outfp, int skip_magic)
{
  block_data_t *block_data = malloc(sizeof(block_data_t));
  size_t work_len = snappy_max_compressed_length(UINT16_MAX); /* length of worst case */
  char *work = malloc(work_len);
  int err = 1;
  stream_state_t state = skip_magic ? PROCESSING_STATE : INITIAL_STATE;

  if (block_data == NULL || work == NULL) {
    print_error("out of memory\n");
    goto cleanup;
  }

  while (state != ERROR_STATE) {
    switch (read_block(infp, block_data)) {
    case EOF:
      if (state == END_OF_STREAM_STATE) {
        err = 0; /* success */
        goto cleanup;
      }
      /* FALLTHROUGH */
    case TOO_SHORT_DATA_BLOCK:
      if (feof_unlocked(infp)) {
        print_error("Unexpected end of file\n");
      } else {
        print_error("Failed to read a file: %s\n", strerror(errno));
      }
      goto cleanup;
    }
    state = process_block(outfp, state, block_data, work, work_len);
  }
 cleanup:
  free(block_data);
  free(work);
  return err;
}
Ejemplo n.º 9
0
struct compressor *read_super(int fd, struct squashfs_super_block *sBlk, char *source)
{
	int res, bytes = 0;
	char buffer[SQUASHFS_METADATA_SIZE] __attribute__ ((aligned));

	res = read_fs_bytes(fd, SQUASHFS_START, sizeof(struct squashfs_super_block),
		sBlk);
	if(res == 0) {
		ERROR("Can't find a SQUASHFS superblock on %s\n",
				source);
		ERROR("Wrong filesystem or filesystem is corrupted!\n");
		goto failed_mount;
	}

	SQUASHFS_INSWAP_SUPER_BLOCK(sBlk);

	if(sBlk->s_magic != SQUASHFS_MAGIC) {
		if(sBlk->s_magic == SQUASHFS_MAGIC_SWAP)
			ERROR("Pre 4.0 big-endian filesystem on %s, appending"
				" to this is unsupported\n", source);
		else {
			ERROR("Can't find a SQUASHFS superblock on %s\n",
				source);
			ERROR("Wrong filesystem or filesystem is corrupted!\n");
		}
		goto failed_mount;
	}

	/* Check the MAJOR & MINOR versions */
	if(sBlk->s_major != SQUASHFS_MAJOR || sBlk->s_minor > SQUASHFS_MINOR) {
		if(sBlk->s_major < 4)
			ERROR("Filesystem on %s is a SQUASHFS %d.%d filesystem."
				"  Appending\nto SQUASHFS %d.%d filesystems is "
				"not supported.  Please convert it to a "
				"SQUASHFS 4 filesystem\n", source,
				sBlk->s_major,
				sBlk->s_minor, sBlk->s_major, sBlk->s_minor);
		else
			ERROR("Filesystem on %s is %d.%d, which is a later "
				"filesystem version than I support\n",
				source, sBlk->s_major, sBlk->s_minor);
		goto failed_mount;
	}

	/* Check the compression type */
	comp = lookup_compressor_id(sBlk->compression);
	if(!comp->supported) {
		ERROR("Filesystem on %s uses %s compression, this is "
			"unsupported by this version\n", source, comp->name);
		ERROR("Compressors available:\n");
		display_compressors("", "");
		goto failed_mount;
	}

	/*
	 * Read extended superblock information from disk.
	 *
	 * Read compressor specific options from disk if present, and pass
	 * to compressor to set compressor options.
	 *
	 * Note, if there's no compressor options present, the compressor
	 * is still called to set the default options (the defaults may have
	 * been changed by the user specifying options on the command
	 * line which need to be over-ridden).
	 *
	 * Compressor_extract_options is also used to ensure that 
	 * we know how decompress a filesystem compressed with these
	 * compression options.
	 */
	if(SQUASHFS_COMP_OPTS(sBlk->flags)) {
		bytes = read_block(fd, sizeof(*sBlk), NULL, 0, buffer);

		if(bytes == 0) {
			ERROR("Failed to read compressor options from append "
				"filesystem\n");
			ERROR("Filesystem corrupted?\n");
			goto failed_mount;
		}
	}

	res = compressor_extract_options(comp, sBlk->block_size, buffer, bytes);
	if(res == -1) {
		ERROR("Compressor failed to set compressor options\n");
		goto failed_mount;
	}

	printf("Found a valid %sSQUASHFS superblock on %s.\n",
		SQUASHFS_EXPORTABLE(sBlk->flags) ? "exportable " : "", source);
	printf("\tCompression used %s\n", comp->name);
	printf("\tInodes are %scompressed\n",
		SQUASHFS_UNCOMPRESSED_INODES(sBlk->flags) ? "un" : "");
	printf("\tData is %scompressed\n",
		SQUASHFS_UNCOMPRESSED_DATA(sBlk->flags) ? "un" : "");
	printf("\tFragments are %scompressed\n",
		SQUASHFS_UNCOMPRESSED_FRAGMENTS(sBlk->flags) ? "un" : "");
	printf("\tXattrs are %scompressed\n",
		SQUASHFS_UNCOMPRESSED_XATTRS(sBlk->flags) ? "un" : "");
	printf("\tFragments are %spresent in the filesystem\n",
		SQUASHFS_NO_FRAGMENTS(sBlk->flags) ? "not " : "");
	printf("\tAlways-use-fragments option is %sspecified\n",
		SQUASHFS_ALWAYS_FRAGMENTS(sBlk->flags) ? "" : "not ");
	printf("\tDuplicates are %sremoved\n",
		SQUASHFS_DUPLICATES(sBlk->flags) ? "" : "not ");
	printf("\tXattrs are %sstored\n",
		SQUASHFS_NO_XATTRS(sBlk->flags) ? "not " : "");
	printf("\tFilesystem size %.2f Kbytes (%.2f Mbytes)\n",
		sBlk->bytes_used / 1024.0, sBlk->bytes_used
		/ (1024.0 * 1024.0));
	printf("\tBlock size %d\n", sBlk->block_size);
	printf("\tNumber of fragments %d\n", sBlk->fragments);
	printf("\tNumber of inodes %d\n", sBlk->inodes);
	printf("\tNumber of ids %d\n", sBlk->no_ids);
	TRACE("sBlk->inode_table_start %llx\n", sBlk->inode_table_start);
	TRACE("sBlk->directory_table_start %llx\n",
		sBlk->directory_table_start);
	TRACE("sBlk->id_table_start %llx\n", sBlk->id_table_start);
	TRACE("sBlk->fragment_table_start %llx\n", sBlk->fragment_table_start);
	TRACE("sBlk->lookup_table_start %llx\n", sBlk->lookup_table_start);
	TRACE("sBlk->xattr_id_table_start %llx\n", sBlk->xattr_id_table_start);
	printf("\n");

	return comp;

failed_mount:
	return NULL;
}
Ejemplo n.º 10
0
void decode_block(decoder_info_t *decoder_info,int size,int ypos,int xpos){

  int width = decoder_info->width;
  int height = decoder_info->height;
  int xposY = xpos;
  int yposY = ypos;
  int xposC = xpos/2;
  int yposC = ypos/2;
  int sizeY = size;
  int sizeC = size/2;

  block_mode_t mode;
  mv_t mv;
  intra_mode_t intra_mode;

  frame_type_t frame_type = decoder_info->frame_info.frame_type;
  int bipred = decoder_info->bipred;

  int qpY = decoder_info->frame_info.qpb;
  int qpC = chroma_qp[qpY];

  /* Intermediate block variables */
  uint8_t *pblock_y = thor_alloc(MAX_BLOCK_SIZE*MAX_BLOCK_SIZE, 16);
  uint8_t *pblock_u = thor_alloc(MAX_BLOCK_SIZE*MAX_BLOCK_SIZE, 16);
  uint8_t *pblock_v = thor_alloc(MAX_BLOCK_SIZE*MAX_BLOCK_SIZE, 16);
  int16_t *coeff_y = thor_alloc(2*MAX_TR_SIZE*MAX_TR_SIZE, 16);
  int16_t *coeff_u = thor_alloc(2*MAX_TR_SIZE*MAX_TR_SIZE, 16);
  int16_t *coeff_v = thor_alloc(2*MAX_TR_SIZE*MAX_TR_SIZE, 16);

  /* Block variables for bipred */
  uint8_t *pblock0_y = thor_alloc(MAX_BLOCK_SIZE*MAX_BLOCK_SIZE, 16);
  uint8_t *pblock0_u = thor_alloc(MAX_BLOCK_SIZE*MAX_BLOCK_SIZE, 16);
  uint8_t *pblock0_v = thor_alloc(MAX_BLOCK_SIZE*MAX_BLOCK_SIZE, 16);
  uint8_t *pblock1_y = thor_alloc(MAX_BLOCK_SIZE*MAX_BLOCK_SIZE, 16);
  uint8_t *pblock1_u = thor_alloc(MAX_BLOCK_SIZE*MAX_BLOCK_SIZE, 16);
  uint8_t *pblock1_v = thor_alloc(MAX_BLOCK_SIZE*MAX_BLOCK_SIZE, 16);
  yuv_frame_t *rec = decoder_info->rec;
  yuv_frame_t *ref = decoder_info->ref[0];

  /* Calculate position in padded reference frame */
  int ref_posY = yposY*ref->stride_y+xposY;
  int ref_posC = yposC*ref->stride_c+xposC;

  /* Pointers to current position in reconstructed frame*/
  uint8_t *rec_y = &rec->y[yposY*rec->stride_y+xposY];
  uint8_t *rec_u = &rec->u[yposC*rec->stride_c+xposC];
  uint8_t *rec_v = &rec->v[yposC*rec->stride_c+xposC];

  /* Pointers to colocated block position in reference frame */
  uint8_t *ref_y = ref->y + ref_posY;
  uint8_t *ref_u = ref->u + ref_posC;
  uint8_t *ref_v = ref->v + ref_posC;

  stream_t *stream = decoder_info->stream;

  /* Read data from bitstream */
  block_info_dec_t block_info;
  block_info.block_pos.size = size;
  block_info.block_pos.ypos = ypos;
  block_info.block_pos.xpos = xpos;
  block_info.coeffq_y = coeff_y;
  block_info.coeffq_u = coeff_u;
  block_info.coeffq_v = coeff_v;

  /* Used for rectangular skip blocks */
  int bwidth = min(size,width - xpos);
  int bheight = min(size,height - ypos);
  block_info.block_pos.bwidth = bwidth;
  block_info.block_pos.bheight = bheight;

  read_block(decoder_info,stream,&block_info,frame_type);
  mode = block_info.block_param.mode;

  if (mode == MODE_INTRA){
    /* Dequantize, inverse tranform, predict and reconstruct */
    intra_mode = block_info.block_param.intra_mode;
    int upright_available = get_upright_available(ypos,xpos,size,width);
    int downleft_available = get_downleft_available(ypos,xpos,size,height);
    int tb_split = block_info.block_param.tb_split;
    decode_and_reconstruct_block_intra(rec_y,rec->stride_y,sizeY,qpY,pblock_y,coeff_y,tb_split,upright_available,downleft_available,intra_mode,yposY,xposY,width,0);
    decode_and_reconstruct_block_intra(rec_u,rec->stride_c,sizeC,qpC,pblock_u,coeff_u,tb_split&&size>8,upright_available,downleft_available,intra_mode,yposC,xposC,width/2,1);
    decode_and_reconstruct_block_intra(rec_v,rec->stride_c,sizeC,qpC,pblock_v,coeff_v,tb_split&&size>8,upright_available,downleft_available,intra_mode,yposC,xposC,width/2,2);
  }
  else
  {
    if (mode==MODE_SKIP){
      if (block_info.block_param.dir==2){
        uint8_t *ref0_y,*ref0_u,*ref0_v;
        uint8_t *ref1_y,*ref1_u,*ref1_v;

        int r0 = decoder_info->frame_info.ref_array[block_info.block_param.ref_idx0];
        yuv_frame_t *ref0 = r0>=0 ? decoder_info->ref[r0] : decoder_info->interp_frames[0];
        ref0_y = ref0->y + ref_posY;
        ref0_u = ref0->u + ref_posC;
        ref0_v = ref0->v + ref_posC;

        int r1 = decoder_info->frame_info.ref_array[block_info.block_param.ref_idx1];
        yuv_frame_t *ref1 = r1>=0 ? decoder_info->ref[r1] : decoder_info->interp_frames[0];
        ref1_y = ref1->y + ref_posY;
        ref1_u = ref1->u + ref_posC;
        ref1_v = ref1->v + ref_posC;
        int sign0 = ref0->frame_num >= rec->frame_num;
        int sign1 = ref1->frame_num >= rec->frame_num;

        mv = block_info.block_param.mv_arr0[0];
        get_inter_prediction_luma  (pblock0_y, ref0_y, bwidth,   bheight,   ref->stride_y, sizeY, &mv, sign0, bipred);
        get_inter_prediction_chroma(pblock0_u, ref0_u, bwidth/2, bheight/2, ref->stride_c, sizeC, &mv, sign0);
        get_inter_prediction_chroma(pblock0_v, ref0_v, bwidth/2, bheight/2, ref->stride_c, sizeC, &mv, sign0);
        mv = block_info.block_param.mv_arr1[0];
        get_inter_prediction_luma  (pblock1_y, ref1_y, bwidth,   bheight,   ref->stride_y, sizeY, &mv, sign1, bipred);
        get_inter_prediction_chroma(pblock1_u, ref1_u, bwidth/2, bheight/2, ref->stride_c, sizeC, &mv, sign1);
        get_inter_prediction_chroma(pblock1_v, ref1_v, bwidth/2, bheight/2, ref->stride_c, sizeC, &mv, sign1);

        int i,j;
        for (i=0;i<bheight;i++){
          for (j=0;j<bwidth;j++){
            rec_y[i*rec->stride_y+j] = (uint8_t)(((int)pblock0_y[i*sizeY+j] + (int)pblock1_y[i*sizeY+j])>>1);
          }
        }

        for (i=0;i<bheight/2;i++){
          for (j=0;j<bwidth/2;j++){
            rec_u[i*rec->stride_c+j] = (uint8_t)(((int)pblock0_u[i*sizeC+j] + (int)pblock1_u[i*sizeC+j])>>1);
            rec_v[i*rec->stride_c+j] = (uint8_t)(((int)pblock0_v[i*sizeC+j] + (int)pblock1_v[i*sizeC+j])>>1);
          }
        }
        copy_deblock_data(decoder_info,&block_info);
      }
      else{
        mv = block_info.block_param.mv_arr0[0];
        int ref_idx = block_info.block_param.ref_idx0; //TODO: Move to top
        int r = decoder_info->frame_info.ref_array[ref_idx];
        ref = r>=0 ? decoder_info->ref[r] : decoder_info->interp_frames[0];
        int sign = ref->frame_num > rec->frame_num;
        ref_y = ref->y + ref_posY;
        ref_u = ref->u + ref_posC;
        ref_v = ref->v + ref_posC;
        get_inter_prediction_luma  (pblock_y, ref_y, bwidth, bheight, ref->stride_y, sizeY, &mv, sign, bipred);
        get_inter_prediction_chroma(pblock_u, ref_u, bwidth/2, bheight/2, ref->stride_c, sizeC, &mv, sign);
        get_inter_prediction_chroma(pblock_v, ref_v, bwidth/2, bheight/2, ref->stride_c, sizeC, &mv, sign);

        int j;
        for (j=0;j<bheight;j++){
          memcpy(&rec_y[j*rec->stride_y],&pblock_y[j*sizeY],bwidth*sizeof(uint8_t));
        }
        for (j=0;j<bheight/2;j++){
          memcpy(&rec_u[j*rec->stride_c],&pblock_u[j*sizeC],(bwidth/2)*sizeof(uint8_t));
          memcpy(&rec_v[j*rec->stride_c],&pblock_v[j*sizeC],(bwidth/2)*sizeof(uint8_t));
        }
        copy_deblock_data(decoder_info,&block_info);
      }
      return;
    }
Ejemplo n.º 11
0
static int write_tind_metadata(struct defrag_ctx *c, struct data_extent *e,
                              __u32 tind_block, __u32 *cur_logical,
                              __u32 *cur_block)
{
	struct inode *inode = c->inodes[e->inode_nr];
	__u32 offset = *cur_logical;
	__u32 ind_blocks = EXT2_ADDR_PER_BLOCK(&c->sb);
	__u32 blocks_per_ind = 1 + ind_blocks;
	__u32 blocks_per_dind = 1 + ind_blocks * blocks_per_ind;
	__u32 blocks_per_tind = 1 + ind_blocks * blocks_per_dind;
	__u32 buffer[EXT2_ADDR_PER_BLOCK(&c->sb)];
	int ret;
	char to_sync = 0;

	if (tind_block == 0) {
		*cur_logical += blocks_per_tind;
		return 0;
	}
	ret = read_block(c, buffer, tind_block);
	if (ret)
		return -1;
	offset -= EXT2_TIND_LBLOCK(&c->sb) + 1;
	offset = offset % blocks_per_tind;
	if (offset % blocks_per_dind) {
		offset = offset / blocks_per_dind;
		ret = write_dind_metadata(c, e, buffer[offset], cur_logical,
		                         cur_block);
		if (ret)
			return ret;
		offset++;
	} else {
		offset = offset / blocks_per_dind;
	}
	while (offset < EXT2_ADDR_PER_BLOCK(&c->sb)
	                                        && *cur_block <= e->end_block) {
		__u32 new_block;
		if (is_sparse(inode, *cur_logical))
			new_block = 0;
		else
			new_block = (*cur_block)++;
		(*cur_logical)++;
		if (new_block) {
			ret = write_dind_metadata(c, e, new_block,
			                          cur_logical, cur_block);
			if (ret)
				return ret;
		} else {
			*cur_logical += blocks_per_dind - 1;
		}
		if (buffer[offset] != new_block) {
			to_sync = 1;
			buffer[offset] = new_block;
		}
		offset++;
	}
	if (to_sync) {
		ret = write_block(c, buffer, tind_block);
		return ret;
	}
	return 0;
}
/*
 * Read and return the next packet from the savefile.  Return the header
 * in hdr and a pointer to the contents in data.  Return 0 on success, 1
 * if there were no more packets, and -1 on an error.
 */
static int
pcap_ng_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data)
{
	struct pcap_ng_sf *ps = p->priv;
	struct block_cursor cursor;
	int status;
	struct enhanced_packet_block *epbp;
	struct simple_packet_block *spbp;
	struct packet_block *pbp;
	bpf_u_int32 interface_id = 0xFFFFFFFF;
	struct interface_description_block *idbp;
	struct section_header_block *shbp;
	FILE *fp = p->rfile;
	u_int64_t t, sec, frac;

	/*
	 * Look for an Enhanced Packet Block, a Simple Packet Block,
	 * or a Packet Block.
	 */
	for (;;) {
		/*
		 * Read the block type and length; those are common
		 * to all blocks.
		 */
		status = read_block(fp, p, &cursor, p->errbuf);
		if (status == 0)
			return (1);	/* EOF */
		if (status == -1)
			return (-1);	/* error */
		switch (cursor.block_type) {

		case BT_EPB:
			/*
			 * Get a pointer to the fixed-length portion of the
			 * EPB.
			 */
			epbp = get_from_block_data(&cursor, sizeof(*epbp),
			    p->errbuf);
			if (epbp == NULL)
				return (-1);	/* error */

			/*
			 * Byte-swap it if necessary.
			 */
			if (p->swapped) {
				/* these were written in opposite byte order */
				interface_id = SWAPLONG(epbp->interface_id);
				hdr->caplen = SWAPLONG(epbp->caplen);
				hdr->len = SWAPLONG(epbp->len);
				t = ((u_int64_t)SWAPLONG(epbp->timestamp_high)) << 32 |
				    SWAPLONG(epbp->timestamp_low);
			} else {
				interface_id = epbp->interface_id;
				hdr->caplen = epbp->caplen;
				hdr->len = epbp->len;
				t = ((u_int64_t)epbp->timestamp_high) << 32 |
				    epbp->timestamp_low;
			}
			goto found;

		case BT_SPB:
			/*
			 * Get a pointer to the fixed-length portion of the
			 * SPB.
			 */
			spbp = get_from_block_data(&cursor, sizeof(*spbp),
			    p->errbuf);
			if (spbp == NULL)
				return (-1);	/* error */

			/*
			 * SPB packets are assumed to have arrived on
			 * the first interface.
			 */
			interface_id = 0;

			/*
			 * Byte-swap it if necessary.
			 */
			if (p->swapped) {
				/* these were written in opposite byte order */
				hdr->len = SWAPLONG(spbp->len);
			} else
				hdr->len = spbp->len;

			/*
			 * The SPB doesn't give the captured length;
			 * it's the minimum of the snapshot length
			 * and the packet length.
			 */
			hdr->caplen = hdr->len;
			if ((int)hdr->caplen > p->snapshot)
				hdr->caplen = p->snapshot;
			t = 0;	/* no time stamps */
			goto found;

		case BT_PB:
			/*
			 * Get a pointer to the fixed-length portion of the
			 * PB.
			 */
			pbp = get_from_block_data(&cursor, sizeof(*pbp),
			    p->errbuf);
			if (pbp == NULL)
				return (-1);	/* error */

			/*
			 * Byte-swap it if necessary.
			 */
			if (p->swapped) {
				/* these were written in opposite byte order */
				interface_id = SWAPSHORT((uint32_t)pbp->interface_id);
				hdr->caplen = SWAPLONG(pbp->caplen);
				hdr->len = SWAPLONG(pbp->len);
				t = ((u_int64_t)SWAPLONG(pbp->timestamp_high)) << 32 |
				    SWAPLONG(pbp->timestamp_low);
			} else {
				interface_id = pbp->interface_id;
				hdr->caplen = pbp->caplen;
				hdr->len = pbp->len;
				t = ((u_int64_t)pbp->timestamp_high) << 32 |
				    pbp->timestamp_low;
			}
			goto found;

		case BT_IDB:
			/*
			 * Interface Description Block.  Get a pointer
			 * to its fixed-length portion.
			 */
			idbp = get_from_block_data(&cursor, sizeof(*idbp),
			    p->errbuf);
			if (idbp == NULL)
				return (-1);	/* error */

			/*
			 * Byte-swap it if necessary.
			 */
			if (p->swapped) {
				idbp->linktype = SWAPSHORT((uint32_t)idbp->linktype);
				idbp->snaplen = SWAPLONG(idbp->snaplen);
			}

			/*
			 * If the link-layer type or snapshot length
			 * differ from the ones for the first IDB we
			 * saw, quit.
			 *
			 * XXX - just discard packets from those
			 * interfaces?
			 */
			if (p->linktype != idbp->linktype) {
				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
				    "an interface has a type %u different from the type of the first interface",
				    idbp->linktype);
				return (-1);
			}
			if (p->snapshot != (int)idbp->snaplen) {
				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
				    "an interface has a snapshot length %u different from the type of the first interface",
				    idbp->snaplen);
				return (-1);
			}

			/*
			 * Try to add this interface.
			 */
			if (!add_interface(p, &cursor, p->errbuf))
				return (-1);
			break;

		case BT_SHB:
			/*
			 * Section Header Block.  Get a pointer
			 * to its fixed-length portion.
			 */
			shbp = get_from_block_data(&cursor, sizeof(*shbp),
			    p->errbuf);
			if (shbp == NULL)
				return (-1);	/* error */

			/*
			 * Assume the byte order of this section is
			 * the same as that of the previous section.
			 * We'll check for that later.
			 */
			if (p->swapped) {
				shbp->byte_order_magic =
				    SWAPLONG(shbp->byte_order_magic);
				shbp->major_version =
				    SWAPSHORT((uint32_t)shbp->major_version);
			}

			/*
			 * Make sure the byte order doesn't change;
			 * pcap_is_swapped() shouldn't change its
			 * return value in the middle of reading a capture.
			 */
			switch (shbp->byte_order_magic) {

			case BYTE_ORDER_MAGIC:
				/*
				 * OK.
				 */
				break;

			case SWAPLONG(BYTE_ORDER_MAGIC):
				/*
				 * Byte order changes.
				 */
				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
				    "the file has sections with different byte orders");
				return (-1);

			default:
				/*
				 * Not a valid SHB.
				 */
				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
				    "the file has a section with a bad byte order magic field");
				return (-1);
			}

			/*
			 * Make sure the major version is the version
			 * we handle.
			 */
			if (shbp->major_version != PCAP_NG_VERSION_MAJOR) {
				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
				    "unknown pcap-ng savefile major version number %u",
				    shbp->major_version);
				return (-1);
			}

			/*
			 * Reset the interface count; this section should
			 * have its own set of IDBs.  If any of them
			 * don't have the same interface type, snapshot
			 * length, or resolution as the first interface
			 * we saw, we'll fail.  (And if we don't see
			 * any IDBs, we'll fail when we see a packet
			 * block.)
			 */
			ps->ifcount = 0;
			break;

		default:
			/*
			 * Not a packet block, IDB, or SHB; ignore it.
			 */
			break;
		}
	}

found:
	/*
	 * Is the interface ID an interface we know?
	 */
	if (interface_id >= ps->ifcount) {
		/*
		 * Yes.  Fail.
		 */
		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
		    "a packet arrived on interface %u, but there's no Interface Description Block for that interface",
		    interface_id);
		return (-1);
	}

	/*
	 * Convert the time stamp to seconds and fractions of a second,
	 * with the fractions being in units of the file-supplied resolution.
	 */
	sec = t / ps->ifaces[interface_id].tsresol + ps->ifaces[interface_id].tsoffset;
	frac = t % ps->ifaces[interface_id].tsresol;

	/*
	 * Convert the fractions from units of the file-supplied resolution
	 * to units of the user-requested resolution.
	 */
	switch (ps->ifaces[interface_id].scale_type) {

	case PASS_THROUGH:
		/*
		 * The interface resolution is what the user wants,
		 * so we're done.
		 */
		break;

	case SCALE_UP:
	case SCALE_DOWN:
		/*
		 * The interface resolution is different from what the
		 * user wants; convert the fractions to units of the
		 * resolution the user requested by multiplying by the
		 * quotient of the user-requested resolution and the
		 * file-supplied resolution.  We do that by multiplying
		 * by the user-requested resolution and dividing by the
		 * file-supplied resolution, as the quotient might not
		 * fit in an integer.
		 *
		 * XXX - if ps->ifaces[interface_id].tsresol is a power
		 * of 10, we could just multiply by the quotient of
		 * ps->user_tsresol and ps->ifaces[interface_id].tsresol
		 * in the scale-up case, and divide by the quotient of
		 * ps->ifaces[interface_id].tsresol and ps->user_tsresol
		 * in the scale-down case, as we know those will be integers.
		 * That would involve fewer arithmetic operations, and
		 * would run less risk of overflow.
		 *
		 * Is there something clever we could do if
		 * ps->ifaces[interface_id].tsresol is a power of 2?
		 */
		frac *= ps->user_tsresol;
		frac /= ps->ifaces[interface_id].tsresol;
		break;
	}
	hdr->ts.tv_sec = sec;
	hdr->ts.tv_usec = (suseconds_t)frac;

	/*
	 * Get a pointer to the packet data.
	 */
	*data = get_from_block_data(&cursor, hdr->caplen, p->errbuf);
	if (*data == NULL)
		return (-1);

	if (p->swapped)
		swap_pseudo_headers(p->linktype, hdr, *data);

	return (0);
}
Ejemplo n.º 13
0
void
Playback_DS::disk_thread ( void )
{
    _thread.name( "Playback" );

    DMESSAGE( "playback thread running" );

    /* buffer to hold the interleaved data returned by the track reader */
    sample_t *buf = buffer_alloc( _nframes * channels() * _disk_io_blocks );
    sample_t *cbuf = buffer_alloc( _nframes );

    const nframes_t nframes = _nframes;
    nframes_t blocks_written;

    while ( ! _terminate )
    {

    seek:

        blocks_written = 0;
        read_block( buf, nframes * _disk_io_blocks );

        while ( blocks_written < _disk_io_blocks &&
                wait_for_block() )
        {
//        lock(); // for seeking
            
            if ( _pending_seek )
            {
                /* FIXME: non-RT-safe IO */
                DMESSAGE( "performing seek to frame %lu", (unsigned long)_seek_frame );
                
                _frame = _seek_frame;
                _pending_seek = false;

                flush();
                
                goto seek;
            }

            /* might have received terminate signal while waiting for block */
            if ( _terminate )
                goto done;
        
//        unlock(); // for seeking
        
            /* deinterleave the buffer and stuff it into the per-channel ringbuffers */

            const size_t block_size = nframes * sizeof( sample_t );

            for ( int i = 0; i < channels(); i++ )
            {
                buffer_deinterleave_one_channel( cbuf,
                                                 buf + ( blocks_written * nframes * channels() ),
                                                 i,
                                                 channels(), 
                                                 nframes );

                while ( jack_ringbuffer_write_space( _rb[ i ] ) < block_size )
                    usleep( 100 * 1000 );

                jack_ringbuffer_write( _rb[ i ], ((char*)cbuf), block_size );
            }

            blocks_written++;
        }
    }

done:

    DMESSAGE( "playback thread terminating" );

    free(buf);
    free(cbuf);

//    flush();

    _terminate = false;

    _thread.exit();
}
Ejemplo n.º 14
0
static int do_readpage(struct ubifs_info *c, struct inode *inode,
		       struct page *page, int last_block_size)
{
	void *addr;
	int err = 0, i;
	unsigned int block, beyond;
	struct ubifs_data_node *dn;
	loff_t i_size = inode->i_size;

	dbg_gen("ino %lu, pg %lu, i_size %lld",
		inode->i_ino, page->index, i_size);

	addr = kmap(page);

	block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
	beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
	if (block >= beyond) {
		/* Reading beyond inode */
		memset(addr, 0, PAGE_CACHE_SIZE);
		goto out;
	}

	dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
	if (!dn)
		return -ENOMEM;

	i = 0;
	while (1) {
		int ret;

		if (block >= beyond) {
			/* Reading beyond inode */
			err = -ENOENT;
			memset(addr, 0, UBIFS_BLOCK_SIZE);
		} else {
			/*
			 * Reading last block? Make sure to not write beyond
			 * the requested size in the destination buffer.
			 */
			if (((block + 1) == beyond) || last_block_size) {
				void *buff;
				int dlen;

				/*
				 * We need to buffer the data locally for the
				 * last block. This is to not pad the
				 * destination area to a multiple of
				 * UBIFS_BLOCK_SIZE.
				 */
				buff = malloc(UBIFS_BLOCK_SIZE);
				if (!buff) {
					printf("%s: Error, malloc fails!\n",
					       __func__);
					err = -ENOMEM;
					break;
				}

				/* Read block-size into temp buffer */
				ret = read_block(inode, buff, block, dn);
				if (ret) {
					err = ret;
					if (err != -ENOENT) {
						free(buff);
						break;
					}
				}

				if (last_block_size)
					dlen = last_block_size;
				else
					dlen = le32_to_cpu(dn->size);

				/* Now copy required size back to dest */
				memcpy(addr, buff, dlen);

				free(buff);
			} else {
				ret = read_block(inode, addr, block, dn);
				if (ret) {
					err = ret;
					if (err != -ENOENT)
						break;
				}
			}
		}
		if (++i >= UBIFS_BLOCKS_PER_PAGE)
			break;
		block += 1;
		addr += UBIFS_BLOCK_SIZE;
	}
	if (err) {
		if (err == -ENOENT) {
			/* Not found, so it must be a hole */
			dbg_gen("hole");
			goto out_free;
		}
		ubifs_err("cannot read page %lu of inode %lu, error %d",
			  page->index, inode->i_ino, err);
		goto error;
	}

out_free:
	kfree(dn);
out:
	return 0;

error:
	kfree(dn);
	return err;
}
Ejemplo n.º 15
0
int fbcopy(     FILE            *fout, 
                CIFBLK          *cif,
                DADSM           *dadsm,
                int             tran, 
                int             verbose) {

        FORMAT1_DSCB    *f1dscb = &dadsm->f1buf;
        DSXTENT         extent[MAX_EXTENTS];
        int     rc, trk = 0, trkconv = 999, rec = 1;
        int     cyl = 0, head = 0, rc_rb, len, offset;
        int     rc_copy = 0;
        int     recs_written = 0, lrecl, num_extents;
        int     lstartrack = 0, lstarrec = 0, lstarvalid = 0;
        BYTE    *buffer;
        char    *pascii = NULL;
        char    zdsn[sizeof(f1dscb->ds1dsnam) + 1];     // ascii dsn

    // Kludge to avoid rewriting this code (for now):
    memcpy(&extent, (void *)&(dadsm->f1ext), sizeof(extent));

    num_extents = f1dscb->ds1noepv;
    lrecl = (f1dscb->ds1lrecl[0] << 8) | (f1dscb->ds1lrecl[1]);
    if (absvalid) {
        strcpy(zdsn, argdsn);
        if (debug) fprintf(stderr, "fbcopy absvalid\n");
    } else {
        make_asciiz(zdsn, sizeof(zdsn), 
                f1dscb->ds1dsnam, sizeof(f1dscb->ds1dsnam));
        if ((f1dscb->ds1lstar[0] !=0) || 
                (f1dscb->ds1lstar[1] != 0) || 
                (f1dscb->ds1lstar[2] != 0)) {
            lstartrack = (f1dscb->ds1lstar[0] << 8) | (f1dscb->ds1lstar[1]);
            lstarrec = f1dscb->ds1lstar[2];
            lstarvalid = 1;     // DS1LSTAR valid
        }
    }
    if (debug) {
        fprintf(stderr, "fbcopy zdsn %s\n", zdsn);
        fprintf(stderr, "fbcopy num_extents %d\n", num_extents);
        fprintf(stderr, "fbcopy lrecl %d\n", lrecl);
        fprintf(stderr, "fbcopy F1 DSCB\n");
        data_dump(f1dscb, sizeof(FORMAT1_DSCB));
        sayext(num_extents, (void *)&extent);
    }   
    if (verbose)                // DS1LSTAR = last block written TTR
        fprintf(stderr, 
                "fbcopy DS1LSTAR %2.2X%2.2X%2.2X lstartrack %d "
                "lstarrec %d lstarvalid %d\n",
                f1dscb->ds1lstar[0], f1dscb->ds1lstar[1], f1dscb->ds1lstar[2],
                lstartrack, lstarrec, lstarvalid);

    if (tran) {                 // need ASCII translation buffer?
        pascii = malloc(lrecl + 1);
        if (pascii == NULL) {
            fprintf(stderr, "fbcopy unable to allocate ascii buffer\n");
            return -1;
        }
    }

    while (1) {                 // output records until something stops us

//  Honor DS1LSTAR when valid

        if ((lstarvalid) && (trk == lstartrack) && (rec > lstarrec)) {
            if (verbose)
                fprintf(stderr, "fbcopy DS1LSTAR indicates EOF\n"
                        "fbcopy DS1LSTAR %2.2X%2.2X%2.2X "
                        "track %d record %d\n",
                        f1dscb->ds1lstar[0], f1dscb->ds1lstar[1], 
                        f1dscb->ds1lstar[2], trk, rec);
            rc_copy = recs_written;
            break;
        }

//  Convert TT to CCHH for upcoming read_block call

        if (trkconv != trk) {           // avoid converting for each block
            trkconv = trk;              // current track converted
            rc = convert_tt(trk, num_extents, extent, cif->heads, &cyl, &head);
            if (rc < 0) {
                fprintf(stderr, 
                        "fbcopy convert_tt track %5.5d, rc %d\n", trk, rc);
                if (absvalid) 
                    rc_copy = recs_written;
                else 
                    rc_copy = -1;
                break;
            }
            if (verbose > 1) 
                fprintf(stderr, "fbcopy convert TT %5.5d CCHH %4.4X %4.4X\n", 
                        trk, cyl, head);
        }

//  Read block from dasd

        if (verbose > 2) 
            fprintf(stderr, "fbcopy reading track %d "
                "record %d CCHHR = %4.4X %4.4X %2.2X\n",
                 trk, rec, cyl, head, rec);
        rc_rb = read_block(cif, cyl, head, rec, NULL, NULL, &buffer, &len);
        if (rc_rb < 0) {                        // error
            fprintf(stderr, "fbcopy error reading %s, rc %d\n", zdsn, rc_rb);
            rc_copy = -1;
            break;
        }

//  Handle end of track return from read_block

        if (rc_rb > 0) {                        // end of track
            if (verbose > 2)
                fprintf(stderr, "fbcopy End Of Track %d rec %d\n", trk, rec);
            trk++;                              // next track
            rec = 1;                            // record 1 on new track
            continue;
        }

//  Check for dataset EOF

        if (len == 0) {                         // EOF
            if (verbose) 
                fprintf(stderr, "fbcopy EOF track %5.5d rec %d\n", trk, rec);
            if (absvalid) {     // capture as much -abs data as possible
                if (verbose) fprintf(stderr, "fbcopy ignoring -abs EOF\n");
            } else {
                rc_copy = recs_written;
                break;
            }
        }
        if (verbose > 3) 
            fprintf(stderr, "fbcopy read %d bytes\n", len);
        if (verbose > 2) {
            data_dump(buffer, len);
            fprintf(stderr, "\n");
        }

//  Deblock input dasd block, write records to output dataset

        for (offset = 0; offset < len; offset += lrecl) {
            if (verbose > 3) {
                fprintf(stderr, "fbcopy offset %d length %d rec %d\n", 
                        offset, lrecl, recs_written);
            }

            if (tran) {                 // ASCII output
                memset(pascii, 0, lrecl + 1);
                make_asciiz(pascii, lrecl + 1, buffer + offset, lrecl);
                if (verbose > 4) {
                    fprintf(stderr, "fbcopy buffer offset %d rec %d\n", 
                                offset, rec);
                    data_dump(buffer + offset, lrecl);
                }
                if (verbose > 3) {
                    fprintf(stderr, "->%s<-\n", pascii);
                    data_dump(pascii, lrecl);
                }
                fprintf(fout, "%s\n", pascii);

            } else {                    // EBCDIC output
                if (verbose > 3) {
                    fprintf(stderr, "fbcopy EBCDIC buffer\n");
                    data_dump(buffer + offset, lrecl);
                }
                fwrite(buffer + offset, lrecl, 1, fout);
            }
            if (ferror(fout)) {
                fprintf(stderr, "fbcopy error writing %s\n", zdsn);
                fprintf(stderr, "%s\n", strerror(errno));
                rc_copy = -1;
            } 
            recs_written++;
        }
        if (rc_copy != 0)
            break;
        else
            rec++;                      // next record on track
    } /* while (1) */
    if (pascii)
        free(pascii);                   // release ASCII conversion buffer
    return rc_copy;

} /* fbcopy */
Ejemplo n.º 16
0
Archivo: cache.c Proyecto: wieck/minix
/*===========================================================================*
 *				lmfs_get_block_ino			     *
 *===========================================================================*/
struct buf *lmfs_get_block_ino(dev_t dev, block_t block, int only_search,
	ino_t ino, u64_t ino_off)
{
/* Check to see if the requested block is in the block cache.  If so, return
 * a pointer to it.  If not, evict some other block and fetch it (unless
 * 'only_search' is 1).  All the blocks in the cache that are not in use
 * are linked together in a chain, with 'front' pointing to the least recently
 * used block and 'rear' to the most recently used block.  If 'only_search' is
 * 1, the block being requested will be overwritten in its entirety, so it is
 * only necessary to see if it is in the cache; if it is not, any free buffer
 * will do.  It is not necessary to actually read the block in from disk.
 * If 'only_search' is PREFETCH, the block need not be read from the disk,
 * and the device is not to be marked on the block, so callers can tell if
 * the block returned is valid.
 * In addition to the LRU chain, there is also a hash chain to link together
 * blocks whose block numbers end with the same bit strings, for fast lookup.
 */

  int b;
  static struct buf *bp;
  u64_t dev_off = (u64_t) block * fs_block_size;
  struct buf *prev_ptr;

  assert(buf_hash);
  assert(buf);
  assert(nr_bufs > 0);

  ASSERT(fs_block_size > 0);

  assert(dev != NO_DEV);

  if((ino_off % fs_block_size)) {

	printf("cache: unaligned lmfs_get_block_ino ino_off %llu\n",
		ino_off);
  	util_stacktrace();
  }

  /* Search the hash chain for (dev, block). */
  b = BUFHASH(block);
  bp = buf_hash[b];
  while (bp != NULL) {
  	if (bp->lmfs_blocknr == block && bp->lmfs_dev == dev) {
  		if(bp->lmfs_flags & VMMC_EVICTED) {
  			/* We had it but VM evicted it; invalidate it. */
  			ASSERT(bp->lmfs_count == 0);
  			ASSERT(!(bp->lmfs_flags & VMMC_BLOCK_LOCKED));
  			ASSERT(!(bp->lmfs_flags & VMMC_DIRTY));
  			bp->lmfs_dev = NO_DEV;
  			bp->lmfs_bytes = 0;
  			bp->data = NULL;
  			break;
  		}
  		ASSERT(bp->lmfs_needsetcache == 0);
  		/* Block needed has been found. */
  		if (bp->lmfs_count == 0) {
			rm_lru(bp);
  			ASSERT(!(bp->lmfs_flags & VMMC_BLOCK_LOCKED));
			bp->lmfs_flags |= VMMC_BLOCK_LOCKED;
		}
		raisecount(bp);
  		ASSERT(bp->lmfs_bytes == fs_block_size);
  		ASSERT(bp->lmfs_dev == dev);
  		ASSERT(bp->lmfs_dev != NO_DEV);
 		ASSERT(bp->lmfs_flags & VMMC_BLOCK_LOCKED);
  		ASSERT(bp->data);

		if(ino != VMC_NO_INODE) {
			if(bp->lmfs_inode == VMC_NO_INODE
			|| bp->lmfs_inode != ino
			|| bp->lmfs_inode_offset != ino_off) {
				bp->lmfs_inode = ino;
				bp->lmfs_inode_offset = ino_off;
				bp->lmfs_needsetcache = 1;
			}
		}

  		return(bp);
  	} else {
  		/* This block is not the one sought. */
  		bp = bp->lmfs_hash; /* move to next block on hash chain */
  	}
  }

  /* Desired block is not on available chain. Find a free block to use. */
  if(bp) {
  	ASSERT(bp->lmfs_flags & VMMC_EVICTED);
  } else {
	if ((bp = front) == NULL) panic("all buffers in use: %d", nr_bufs);
  }
  assert(bp);

  rm_lru(bp);

  /* Remove the block that was just taken from its hash chain. */
  b = BUFHASH(bp->lmfs_blocknr);
  prev_ptr = buf_hash[b];
  if (prev_ptr == bp) {
	buf_hash[b] = bp->lmfs_hash;
  } else {
	/* The block just taken is not on the front of its hash chain. */
	while (prev_ptr->lmfs_hash != NULL)
		if (prev_ptr->lmfs_hash == bp) {
			prev_ptr->lmfs_hash = bp->lmfs_hash;	/* found it */
			break;
		} else {
			prev_ptr = prev_ptr->lmfs_hash;	/* keep looking */
		}
  }

  freeblock(bp);

  bp->lmfs_inode = ino;
  bp->lmfs_inode_offset = ino_off;

  bp->lmfs_flags = VMMC_BLOCK_LOCKED;
  bp->lmfs_needsetcache = 0;
  bp->lmfs_dev = dev;		/* fill in device number */
  bp->lmfs_blocknr = block;	/* fill in block number */
  ASSERT(bp->lmfs_count == 0);
  raisecount(bp);
  b = BUFHASH(bp->lmfs_blocknr);
  bp->lmfs_hash = buf_hash[b];

  buf_hash[b] = bp;		/* add to hash list */

  assert(dev != NO_DEV);

  /* Block is not found in our cache, but we do want it
   * if it's in the vm cache.
   */
  assert(!bp->data);
  assert(!bp->lmfs_bytes);
  if(vmcache) {
	if((bp->data = vm_map_cacheblock(dev, dev_off, ino, ino_off,
		&bp->lmfs_flags, fs_block_size)) != MAP_FAILED) {
		bp->lmfs_bytes = fs_block_size;
		ASSERT(!bp->lmfs_needsetcache);
		return bp;
	}
  }
  bp->data = NULL;

  /* Not in the cache; reserve memory for its contents. */

  lmfs_alloc_block(bp);

  assert(bp->data);

  if(only_search == PREFETCH) {
	/* PREFETCH: don't do i/o. */
	bp->lmfs_dev = NO_DEV;
  } else if (only_search == NORMAL) {
	read_block(bp);
  } else if(only_search == NO_READ) {
  	/* This block will be overwritten by new contents. */
  } else
	panic("unexpected only_search value: %d", only_search);

  assert(bp->data);

  return(bp);			/* return the newly acquired block */
}
Ejemplo n.º 17
0
/*===========================================================================*
 *				get_block				     *
 *===========================================================================*/
struct buf *get_block(
  register dev_t dev,		/* on which device is the block? */
  register block_t block,	/* which block is wanted? */
  int only_search		/* if NO_READ, don't read, else act normal */
)
{
/* Check to see if the requested block is in the block cache.  If so, return
 * a pointer to it.  If not, evict some other block and fetch it (unless
 * 'only_search' is 1).  All the blocks in the cache that are not in use
 * are linked together in a chain, with 'front' pointing to the least recently
 * used block and 'rear' to the most recently used block.  If 'only_search' is
 * 1, the block being requested will be overwritten in its entirety, so it is
 * only necessary to see if it is in the cache; if it is not, any free buffer
 * will do.  It is not necessary to actually read the block in from disk.
 * If 'only_search' is PREFETCH, the block need not be read from the disk,
 * and the device is not to be marked on the block, so callers can tell if
 * the block returned is valid.
 * In addition to the LRU chain, there is also a hash chain to link together
 * blocks whose block numbers end with the same bit strings, for fast lookup.
 */

  int b;
  static struct buf *bp, *prev_ptr;
  u64_t yieldid = VM_BLOCKID_NONE, getid = make64(dev, block);

  assert(buf_hash);
  assert(buf);
  assert(nr_bufs > 0);

  ASSERT(fs_block_size > 0);

  /* Search the hash chain for (dev, block). Do_read() can use 
   * get_block(NO_DEV ...) to get an unnamed block to fill with zeros when
   * someone wants to read from a hole in a file, in which case this search
   * is skipped
   */
  if (dev != NO_DEV) {
	b = BUFHASH(block);
	bp = buf_hash[b];
	while (bp != NULL) {
		if (bp->b_blocknr == block && bp->b_dev == dev) {
			/* Block needed has been found. */
			if (bp->b_count == 0) rm_lru(bp);
			bp->b_count++;	/* record that block is in use */
			ASSERT(bp->b_bytes == fs_block_size);
			ASSERT(bp->b_dev == dev);
			ASSERT(bp->b_dev != NO_DEV);
			ASSERT(bp->bp);
			return(bp);
		} else {
			/* This block is not the one sought. */
			bp = bp->b_hash; /* move to next block on hash chain */
		}
	}
  }

  /* Desired block is not on available chain.  Take oldest block ('front'). */
  if ((bp = front) == NULL) panic("all buffers in use: %d", nr_bufs);

  if(bp->b_bytes < fs_block_size) {
	ASSERT(!bp->bp);
	ASSERT(bp->b_bytes == 0);
	if(!(bp->bp = alloc_contig( (size_t) fs_block_size, 0, NULL))) {
		printf("MFS: couldn't allocate a new block.\n");
		for(bp = front;
			bp && bp->b_bytes < fs_block_size; bp = bp->b_next)
			;
		if(!bp) {
			panic("no buffer available");
		}
	} else {
  		bp->b_bytes = fs_block_size;
	}
  }

  ASSERT(bp);
  ASSERT(bp->bp);
  ASSERT(bp->b_bytes == fs_block_size);
  ASSERT(bp->b_count == 0);

  rm_lru(bp);

  /* Remove the block that was just taken from its hash chain. */
  b = BUFHASH(bp->b_blocknr);
  prev_ptr = buf_hash[b];
  if (prev_ptr == bp) {
	buf_hash[b] = bp->b_hash;
  } else {
	/* The block just taken is not on the front of its hash chain. */
	while (prev_ptr->b_hash != NULL)
		if (prev_ptr->b_hash == bp) {
			prev_ptr->b_hash = bp->b_hash;	/* found it */
			break;
		} else {
			prev_ptr = prev_ptr->b_hash;	/* keep looking */
		}
  }

  /* If the block taken is dirty, make it clean by writing it to the disk.
   * Avoid hysteresis by flushing all other dirty blocks for the same device.
   */
  if (bp->b_dev != NO_DEV) {
	if (ISDIRTY(bp)) flushall(bp->b_dev);

	/* Are we throwing out a block that contained something?
	 * Give it to VM for the second-layer cache.
	 */
	yieldid = make64(bp->b_dev, bp->b_blocknr);
	assert(bp->b_bytes == fs_block_size);
	BP_CLEARDEV(bp);
  }

  /* Fill in block's parameters and add it to the hash chain where it goes. */
  if(dev == NO_DEV) BP_CLEARDEV(bp);
  else BP_SETDEV(bp, dev);
  bp->b_blocknr = block;	/* fill in block number */
  bp->b_count++;		/* record that block is being used */
  b = BUFHASH(bp->b_blocknr);
  bp->b_hash = buf_hash[b];

  buf_hash[b] = bp;		/* add to hash list */

  if(dev == NO_DEV) {
	if(vmcache && cmp64(yieldid, VM_BLOCKID_NONE) != 0) {
		vm_yield_block_get_block(yieldid, VM_BLOCKID_NONE,
			bp->bp, fs_block_size);
	}
	return(bp);	/* If the caller wanted a NO_DEV block, work is done. */
  }

  /* Go get the requested block unless searching or prefetching. */
  if(only_search == PREFETCH || only_search == NORMAL) {
	/* Block is not found in our cache, but we do want it
	 * if it's in the vm cache.
	 */
	if(vmcache) {
		/* If we can satisfy the PREFETCH or NORMAL request 
		 * from the vm cache, work is done.
		 */
		if(vm_yield_block_get_block(yieldid, getid,
			bp->bp, fs_block_size) == OK) {
			return bp;
		}
	}
  }

  if(only_search == PREFETCH) {
	/* PREFETCH: don't do i/o. */
	BP_CLEARDEV(bp);
  } else if (only_search == NORMAL) {
	read_block(bp);
  } else if(only_search == NO_READ) {
	/* we want this block, but its contents
	 * will be overwritten. VM has to forget
	 * about it.
	 */
	if(vmcache) {
		vm_forgetblock(getid);
	}
  } else
	panic("unexpected only_search value: %d", only_search);

  assert(bp->bp);

  return(bp);			/* return the newly acquired block */
}
Ejemplo n.º 18
0
int getF4dscb(
                CIFBLK          *cif, 
                FORMAT4_DSCB    *f4dscb,
                DASD_VOL_LABEL  *volrec,
                DSXTENT         *vtocx,
                int             verbose) {

        char            vtockey[sizeof(f4dscb->ds4keyid)];
        void            *f4key, *f4data;
        int             f4kl, f4dl;
        int             cyl, head, rec, rc;

//  Extract VTOC's CCHHR from volume label

    cyl = (volrec->volvtoc[0] << 8) | volrec->volvtoc[1];
    head = (volrec->volvtoc[2] << 8) | volrec->volvtoc[3];
    rec = volrec->volvtoc[4];
    if (verbose > 1) 
        fprintf(stderr, "getF4dscb VTOC F4 at cyl %d head %d rec %d\n", cyl, head, rec);

//  Read VTOC's Format 4 DSCB (VTOC self-descriptor)

    if (verbose)
        fprintf(stderr, "getF4dscb reading VTOC F4 DSCB\n");
    rc = read_block(cif, cyl, head, rec, (void *) &f4key, &f4kl, (void *) &f4data, &f4dl);
    if (rc) {
        fprintf(stderr, "getF4dscb error reading F4 DSCB, rc %d\n", rc);
        return 1;
    }

//  Verify correct key and data length

    if ((f4kl != sizeof(f4dscb->ds4keyid)) ||
        (f4dl != (sizeof(FORMAT4_DSCB) - sizeof(f4dscb->ds4keyid)))) {
        fprintf(stderr, "getF4dscb erroneous key length %d or data length %d\n",
                f4kl, f4dl);
        return 2;
    }

//  Return data to caller

    memcpy((void *) &f4dscb->ds4keyid, f4key, f4kl);    // copy F4 key into buffer
    memcpy((void *) &f4dscb->ds4fmtid, f4data, f4dl);   // copy F4 data into buffer
    memcpy((void *) vtocx, (void *)&f4dscb->ds4vtoce, 
        sizeof(f4dscb->ds4vtoce));                      // copy VTOC extent entry
    if (verbose > 1) {
        fprintf(stderr, "getF4dscb F4 DSCB\n");
        data_dump((void *) f4dscb, sizeof(FORMAT4_DSCB));
    }

//  Verify DS4FMTID byte = x'F4', DS4KEYID key = x'04', and DS4NOEXT = x'01'
//  Do this after copying data to caller's buffer so we can use struct fields
//  rather than having to calculate offset to verified data; little harm done
//  if it doesn't verify since we're toast if they're bad.

    memset(vtockey, 0x04, sizeof(vtockey));
    if ((f4dscb->ds4fmtid != 0xf4) || 
        (f4dscb->ds4noext != 0x01) ||
        (memcmp(&f4dscb->ds4keyid, vtockey, sizeof(vtockey)))) {
        fprintf(stderr, "getF4dscb "
                "VTOC format id byte invalid (DS4IDFMT) %2.2X, \n"
                "VTOC key invalid, or multi-extent VTOC\n",
                f4dscb->ds4fmtid);
        return 3;
    }

//  Display VTOC extent info (always one extent, never more)

    if (verbose > 1) {
        fprintf (stderr, "getF4dscb "
                "VTOC start CCHH=%2.2X%2.2X %2.2X%2.2X "
                       "end CCHH=%2.2X%2.2X %2.2X%2.2X\n",
                vtocx->xtbcyl[0], vtocx->xtbcyl[1],
                vtocx->xtbtrk[0], vtocx->xtbtrk[1],
                vtocx->xtecyl[0], vtocx->xtecyl[1],
                vtocx->xtetrk[0], vtocx->xtetrk[1]);
    }
    return 0;
} /* getF4dscb */
Ejemplo n.º 19
0
int iaea_header_type::read_header ()
{
    char line[MAX_STR_LEN]; 
    
    if(fheader==NULL)
    {
      printf("\n ERROR: Unable to open header file \n"); 
        return(FAIL);
    }

  // ******************************************************************************
  // 1. PHSP format

      /*********************************************/
      if ( read_block(line,"FILE_TYPE") == FAIL ) 
      {
            printf("\nMandatory keyword FILE_TYPE is not defined in input\n");
            return FAIL;
      }
      else file_type = atoi(line); 

      /*********************************************/
      if ( read_block(line,"CHECKSUM") == FAIL ) 
      {
            printf("\nMandatory keyword CHECKSUM is not defined in input\n");
            return FAIL;
      }
      else checksum = atol(line); 

      /*********************************************/
      if ( read_block(line,"RECORD_LENGTH") == FAIL ) 
      {
            printf("\nMandatory keyword RECORD_LENGTH is not defined in input\n");
            return FAIL;
      }
      else record_length = atoi(line); 

      /*********************************************/
      if ( read_block(line,"BYTE_ORDER") == FAIL ) 
      {
            printf("\nMandatory keyword BYTE_ORDER is not defined in input\n");
            return FAIL;
      }
      else byte_order = atoi(line); 

      /*********************************************/
    if( get_blockname(line,"RECORD_CONTENTS") == FAIL) 
    {
       printf("\nMandatory keyword RECORD_CONTENTS is not defined in input\n");
         return FAIL; 
    }

    int i;
    for (i=0;i<9;i++) record_contents[i] = 0;

    for (i=0;i<9;i++)
    {
      if( get_string(fheader,line) == FAIL ) return FAIL;
      if( *line == SEGMENT_BEG_TOKEN ) break; 
      record_contents[i] = atoi(line); 
    };

    for(i=0;i<record_contents[7];i++) 
    {
      if( get_string(fheader,line) == FAIL ) return FAIL;
      if( *line == SEGMENT_BEG_TOKEN ) break; 
      extrafloat_contents[i] = atoi(line); 
    }
    
    for(i=0;i<record_contents[8];i++) 
    {
      if( get_string(fheader,line) == FAIL ) return FAIL;
      if( *line == SEGMENT_BEG_TOKEN ) break; 
      extralong_contents[i] = atoi(line); 
    }

    /*********************************************/
    if( get_blockname(line,"RECORD_CONSTANT") == FAIL) 
    {
       printf("\nMandatory keyword RECORD_CONSTANT is not defined in input\n");
         return FAIL; 
    }

    for (i=0;i<7;i++)
    {
        record_constant[i] = 32000.f;
        if(record_contents[i] > 0) continue;
        if( get_string(fheader,line) == FAIL ) return FAIL;
        if( *line == SEGMENT_BEG_TOKEN ) break; 
        record_constant[i] = (float)atof(line); 
    };

// ******************************************************************************
// 2. Mandatory description of the phsp

      if ( read_block(coordinate_system_description,"COORDINATE_SYSTEM_DESCRIPTION") == FAIL)   
      {
            printf("\nMandatory keyword COORDINATE_SYSTEM_DESCRIPTION is not defined in input\n");
            return FAIL;
      }

  if(file_type == 1) // For event generators
  {
    /*********************************************/
      if ( read_block(line,"INPUT_FILE_FOR_EVENT_GENERATOR") == FAIL ) 
      {
            printf("\nMandatory keyword INPUT_FILE_FOR_EVENT_GENERATOR is not defined in input\n");
            return FAIL;
      }
  }

  if(file_type == 0) // for phsp files
  {
      /*********************************************/
      if ( read_block(line,"ORIG_HISTORIES") == FAIL ) 
      {
            printf("\nMandatory keyword ORIG_HISTORIES is not defined in input\n");
            return FAIL;
      }
      else {
          orig_histories = atol(line);  
          if( orig_histories == 0) printf(
           "\n The number of primary particles (ORIG_HISTORIES) is zero in the HEADER !\n");
      }

      /*********************************************/
      if ( read_block(line,"PARTICLES") == FAIL ) 
      {
            printf("\nMandatory keyword PARTICLES is not defined in input\n");
            return FAIL;
      }
      else nParticles = atol(line); 

      /*********************************************/
      for(int itmp=0;itmp<MAX_NUM_PARTICLES;itmp++) particle_number[itmp]=0;
      IAEA_I64 npart;
      if ( read_block(line,"PHOTONS") == OK )  
            {npart = atol(line); particle_number[0] = npart;}
      if ( read_block(line,"ELECTRONS") == OK ) 
            {npart = atol(line); particle_number[1] = npart;}
      if ( read_block(line,"POSITRONS") == OK ) 
            {npart = atol(line); particle_number[2] = npart;}
      if ( read_block(line,"NEUTRONS") == OK )  
            {npart = atol(line); particle_number[3] = npart;}
      if ( read_block(line,"PROTONS") == OK )   
            {npart = atol(line); particle_number[4] = npart;}
  }
// ******************************************************************************
// 3. Mandatory additional information
      if ( read_block(line,"IAEA_INDEX") == FAIL ) 
      {
            printf("\nMandatory keyword IAEA_INDEX is not defined in input\n");
            return FAIL;
      }
      else iaea_index = atoi(line); 

      /*********************************************/
      if ( read_block(title,"TITLE") == FAIL ) 
      {
            printf("\nMandatory keyword TITLE is not defined in input\n");
            return FAIL;
      }

      /*********************************************/
      if ( read_block(machine_type,"MACHINE_TYPE") == FAIL) 
      {
            printf("\nMandatory keyword MACHINE_TYPE is not defined in input\n");
            return FAIL;
      }
 
      /*********************************************/
      if ( read_block(MC_code_and_version,"MONTE_CARLO_CODE_VERSION") == FAIL ) 
      {
            printf("\nMandatory keyword MONTE_CARLO_CODE_VERSION is not defined in input\n");
            return FAIL;
      }

      /*********************************************/
      if ( read_block(line,"GLOBAL_PHOTON_ENERGY_CUTOFF") == FAIL ) 
      {
            printf("\nMandatory keyword GLOBAL_PHOTON_ENERGY_CUTOFF is not defined in input\n");
            return FAIL;
      }
      else global_photon_energy_cutoff = (float)atof(line); 

      /*********************************************/
      if ( read_block(line,"GLOBAL_PARTICLE_ENERGY_CUTOFF") == FAIL ) 
      {
            printf("\nMandatory keyword GLOBAL_PARTICLE_ENERGY_CUTOFF is not defined in input\n");
            return FAIL;
      }
      else global_particle_energy_cutoff = (float)atof(line); 

      /*********************************************/
      if ( read_block(transport_parameters,"TRANSPORT_PARAMETERS") == FAIL ) 
      {
            printf("\nMandatory keyword TRANSPORT_PARAMETERS is not defined in input\n");
            return FAIL;
      }

// ******************************************************************************
// 4. Optional description

      if ( read_block(beam_name,"BEAM_NAME") == FAIL ) 
            printf("ERROR reading BEAM_NAME\n");
      if ( read_block(field_size,"FIELD_SIZE") == FAIL ) 
            printf("ERROR reading FIELD_SIZE\n");
      if ( read_block(nominal_SSD,"NOMINAL_SSD") == FAIL ) 
            printf("ERROR reading NOMINAL_SSD\n");
      if ( read_block(variance_reduction_techniques,
                        "VARIANCE_REDUCTION_TECHNIQUES") == FAIL ) 
            printf("VARIANCE_REDUCTION_TECHNIQUES\n");
      if ( read_block(initial_source_description,"INITIAL_SOURCE_DESCRIPTION") == FAIL ) 
            printf("INITIAL_SOURCE_DESCRIPTION:\n");
  
      // Documentation sub-section
      /*********************************************/
      if ( read_block(MC_input_filename,"MC_INPUT_FILENAME") == FAIL ) 
            printf("MC_INPUT_FILENAME\n");
      if ( read_block(published_reference,"PUBLISHED_REFERENCE") == FAIL ) 
            printf("PUBLISHED_REFERENCE\n");
      if ( read_block(authors,"AUTHORS") == FAIL ) printf("AUTHORS\n");
      if ( read_block(institution,"INSTITUTION") == FAIL ) printf("INSTITUTION\n");
      if ( read_block(link_validation,"LINK_VALIDATION") == FAIL ) 
            printf("LINK_VALIDATION\n");
      if ( read_block(additional_notes,"ADDITIONAL_NOTES") == FAIL ) 
            printf("ADDITIONAL_NOTES\n");

// ******************************************************************************
// 5. Statistical information
      /*********************************************/
    if( get_blockname(line,"STATISTICAL_INFORMATION_PARTICLES") == OK) 
    {
        for(i=0;i<MAX_NUM_PARTICLES;i++) 
        {
              if( get_string(fheader,line) == FAIL ) return FAIL;
              if( *line == SEGMENT_BEG_TOKEN ) break;

              if(particle_number[i] == 0) continue;
              // -------------------------------------------------------
              // The fragment below replaces buggy sscanf() function
              int index0 =0, index1 =0, len = strlen(line), icnt =0;
              float fbuff[6];
              do 
              {   if(advance(line, &index1, len) != OK) break; // Finding first number
                    fbuff[icnt++] = (float)atof(line+index1);
              if(advance(line, &index0, len) != OK) break; // Skipping spaces
              }   while (icnt < 6);
              // -------------------------------------------------------
              sumParticleWeight[i] = fbuff[0];
              minimumWeight[i]         = fbuff[1];
            maximumWeight[i]     = fbuff[2];
          averageKineticEnergy[i] = fbuff[3];
              minimumKineticEnergy[i] = fbuff[4];
              maximumKineticEnergy[i] = fbuff[5];
        }
    }              

    if( get_blockname(line,"STATISTICAL_INFORMATION_GEOMETRY") == OK) 
      {
        minimumX = minimumY = minimumZ = 32000.f;
        maximumX = maximumY = maximumZ = 0.f;

        for(i=0;i<3;i++) 
        {
            if(record_contents[i] == 1) 
            {
                  if( get_string(fheader,line) == FAIL ) return FAIL;
                  if( *line == SEGMENT_BEG_TOKEN ) break;

                // -------------------------------------------------------
                // The fragment below replaces buggy sscanf() function
                int index0 =0, index1 =0, len = strlen(line), icnt =0;
                float fbuff[2];
                do 
                {   if(advance(line, &index1, len) != OK) break; // Finding first number
                      fbuff[icnt++] = (float)atof(line+index1);
                if(advance(line, &index0, len) != OK) break; // Skipping spaces
                } while (icnt < 2);
                // -------------------------------------------------------

                  if( i == 0) {minimumX = fbuff[0]; maximumX  = fbuff[1];}
                  if( i == 1) {minimumY = fbuff[0]; maximumY  = fbuff[1];}
                  if( i == 2) {minimumZ = fbuff[0]; maximumZ  = fbuff[1];}
            }
        }
      }

    return(OK);  
}
Ejemplo n.º 20
0
int getF1dscb(
                CIFBLK          *cif, 
                char            *pdsn[],
                FORMAT1_DSCB    *f1dscb, 
                DSXTENT         *vtocext[],
                int             verbose) {

        char    zdsn[sizeof(f1dscb->ds1dsnam) + 1];     // zASCII dsn
        BYTE    edsn[sizeof(f1dscb->ds1dsnam)];         // EBCDIC dsn
        void    *f1key, *f1data;
        int     f1kl, f1dl;
        int     cyl, head, rec, rc;
        int     vtocextents = 1;        // VTOC has only one extent

//  Locate dataset's F1 DSCB

    memset(zdsn, 0, sizeof(zdsn));
    strncpy(zdsn, *pdsn, sizeof(zdsn) - 1);
    string_to_upper(zdsn);
    convert_to_ebcdic(edsn, sizeof(edsn), zdsn);
    if (verbose)
        fprintf(stderr, "getF1dscb searching VTOC for %s\n", zdsn);
    rc = search_key_equal(cif, edsn, sizeof(edsn), 
                        vtocextents, (DSXTENT *)vtocext, 
                        &cyl, &head, &rec);
    if (rc) {
        fprintf(stderr, "getF1dscb search_key_equal rc %d\n", rc);
        if (verbose) {
            fprintf(stderr, "getF1dscb key\n");
            data_dump(edsn, sizeof(edsn));
        }
        if (rc == 1)
            fprintf(stderr, "getF1dscb no DSCB found for %s\n", zdsn);
        return 1;
    }

//  Read F1 DSCB describing dataset

    if (verbose)
        fprintf(stderr, "getF1dscb reading F1 DSCB\n");
    rc = read_block(cif, cyl, head, rec, 
                (void *)&f1key, &f1kl, 
                (void *) &f1data, &f1dl);
    if (rc) {
        fprintf(stderr, "getF1dscb error reading F1 DSCB, rc %d\n", rc);
        return 2;
    }

//  Return data to caller

    if ((f1kl == sizeof(f1dscb->ds1dsnam)) && 
        (f1dl == (sizeof(FORMAT1_DSCB) - sizeof(f1dscb->ds1dsnam)))) {
        memcpy((void *) &f1dscb->ds1dsnam, 
                f1key, f1kl);           // copy F1 key to buffer
        memcpy((void *) &f1dscb->ds1fmtid, 
                f1data, f1dl);          // copy F1 data to buffer
    } else {
        fprintf(stderr, "getF1dscb bad key %d or data length %d\n",
                f1kl, f1dl);
        return 3;
    }
    if (verbose > 1) {
        fprintf(stderr, "getF1dscb F1 DSCB\n");
        data_dump((void *) f1dscb, sizeof(FORMAT1_DSCB));
    }

//  Verify DS1FMTID byte = x'F1'
//  Do this after copying data to caller's buffer so we can use struct fields
//  rather than having to calculate offset to verified data; little harm done
//  if it doesn't verify since we're toast if it's bad.

    if (f1dscb->ds1fmtid != 0xf1) {
        fprintf(stderr, "getF1dscb "
                "F1 DSCB format id byte invalid (DS1IDFMT) %2.2X\n",
                f1dscb->ds1fmtid);
        return 4;
    }
    return 0;
} /* getF1dscb */
/*
 * Check whether this is a pcap-ng savefile and, if it is, extract the
 * relevant information from the header.
 */
pcap_t *
pcap_ng_check_header(bpf_u_int32 magic, FILE *fp, u_int precision, char *errbuf,
    int *err)
{
	size_t amt_read;
	bpf_u_int32 total_length;
	bpf_u_int32 byte_order_magic;
	struct block_header *bhdrp;
	struct section_header_block *shbp;
	pcap_t *p;
	int swapped = 0;
	struct pcap_ng_sf *ps;
	int status;
	struct block_cursor cursor;
	struct interface_description_block *idbp;

	/*
	 * Assume no read errors.
	 */
	*err = 0;

	/*
	 * Check whether the first 4 bytes of the file are the block
	 * type for a pcap-ng savefile.
	 */
	if (magic != BT_SHB) {
		/*
		 * XXX - check whether this looks like what the block
		 * type would be after being munged by mapping between
		 * UN*X and DOS/Windows text file format and, if it
		 * does, look for the byte-order magic number in
		 * the appropriate place and, if we find it, report
		 * this as possibly being a pcap-ng file transferred
		 * between UN*X and Windows in text file format?
		 */
		return (NULL);	/* nope */
	}

	/*
	 * OK, they are.  However, that's just \n\r\r\n, so it could,
	 * conceivably, be an ordinary text file.
	 *
	 * It could not, however, conceivably be any other type of
	 * capture file, so we can read the rest of the putative
	 * Section Header Block; put the block type in the common
	 * header, read the rest of the common header and the
	 * fixed-length portion of the SHB, and look for the byte-order
	 * magic value.
	 */
	amt_read = fread(&total_length, 1, sizeof(total_length), fp);
	if (amt_read < sizeof(total_length)) {
		if (ferror(fp)) {
			snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "error reading dump file: %s",
			    pcap_strerror(errno));
			*err = 1;
			return (NULL);	/* fail */
		}

		/*
		 * Possibly a weird short text file, so just say
		 * "not pcap-ng".
		 */
		return (NULL);
	}
	amt_read = fread(&byte_order_magic, 1, sizeof(byte_order_magic), fp);
	if (amt_read < sizeof(byte_order_magic)) {
		if (ferror(fp)) {
			snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "error reading dump file: %s",
			    pcap_strerror(errno));
			*err = 1;
			return (NULL);	/* fail */
		}

		/*
		 * Possibly a weird short text file, so just say
		 * "not pcap-ng".
		 */
		return (NULL);
	}
	if (byte_order_magic != BYTE_ORDER_MAGIC) {
		byte_order_magic = SWAPLONG(byte_order_magic);
		if (byte_order_magic != BYTE_ORDER_MAGIC) {
			/*
			 * Not a pcap-ng file.
			 */
			return (NULL);
		}
		swapped = 1;
		total_length = SWAPLONG(total_length);
	}

	/*
	 * Check the sanity of the total length.
	 */
	if (total_length < sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer)) {
		snprintf(errbuf, PCAP_ERRBUF_SIZE,
		    "Section Header Block in pcap-ng dump file has a length of %u < %lu",
		    total_length,
		    (unsigned long)(sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer)));
		*err = 1;
		return (NULL);
	}

	/*
	 * OK, this is a good pcap-ng file.
	 * Allocate a pcap_t for it.
	 */
	p = pcap_open_offline_common(errbuf, sizeof (struct pcap_ng_sf));
	if (p == NULL) {
		/* Allocation failed. */
		*err = 1;
		return (NULL);
	}
	p->swapped = swapped;
	ps = p->priv;

	/*
	 * What precision does the user want?
	 */
	switch (precision) {

	case PCAP_TSTAMP_PRECISION_MICRO:
		ps->user_tsresol = 1000000;
		break;

	case PCAP_TSTAMP_PRECISION_NANO:
		ps->user_tsresol = 1000000000;
		break;

	default:
		snprintf(errbuf, PCAP_ERRBUF_SIZE,
		    "unknown time stamp resolution %u", precision);
		free(p);
		*err = 1;
		return (NULL);
	}

	p->opt.tstamp_precision = precision;

	/*
	 * Allocate a buffer into which to read blocks.  We default to
	 * the maximum of:
	 *
	 *	the total length of the SHB for which we read the header;
	 *
	 *	2K, which should be more than large enough for an Enhanced
	 *	Packet Block containing a full-size Ethernet frame, and
	 *	leaving room for some options.
	 *
	 * If we find a bigger block, we reallocate the buffer.
	 */
	p->bufsize = 2048;
	if (p->bufsize < (int)total_length)
		p->bufsize = total_length;
	p->buffer = malloc(p->bufsize);
	if (p->buffer == NULL) {
		snprintf(errbuf, PCAP_ERRBUF_SIZE, "out of memory");
		free(p);
		*err = 1;
		return (NULL);
	}

	/*
	 * Copy the stuff we've read to the buffer, and read the rest
	 * of the SHB.
	 */
	bhdrp = (struct block_header *)(void *)p->buffer;
	shbp = (struct section_header_block *)(void *)(p->buffer + sizeof(struct block_header));
	bhdrp->block_type = magic;
	bhdrp->total_length = total_length;
	shbp->byte_order_magic = byte_order_magic;
	if (read_bytes(fp,
	    p->buffer + (sizeof(magic) + sizeof(total_length) + sizeof(byte_order_magic)),
	    total_length - (sizeof(magic) + sizeof(total_length) + sizeof(byte_order_magic)),
	    1, errbuf) == -1)
		goto fail;

	if (p->swapped) {
		/*
		 * Byte-swap the fields we've read.
		 */
		shbp->major_version = SWAPSHORT((uint32_t)shbp->major_version);
		shbp->minor_version = SWAPSHORT((uint32_t)shbp->minor_version);

		/*
		 * XXX - we don't care about the section length.
		 */
	}
	if (shbp->major_version != PCAP_NG_VERSION_MAJOR) {
		snprintf(errbuf, PCAP_ERRBUF_SIZE,
		    "unknown pcap-ng savefile major version number %u",
		    shbp->major_version);
		goto fail;
	}
	p->version_major = shbp->major_version;
	p->version_minor = shbp->minor_version;

	/*
	 * Save the time stamp resolution the user requested.
	 */
	p->opt.tstamp_precision = precision;

	/*
	 * Now start looking for an Interface Description Block.
	 */
	for (;;) {
		/*
		 * Read the next block.
		 */
		status = read_block(fp, p, &cursor, errbuf);
		if (status == 0) {
			/* EOF - no IDB in this file */
			snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "the capture file has no Interface Description Blocks");
			goto fail;
		}
		if (status == -1)
			goto fail;	/* error */
		switch (cursor.block_type) {

		case BT_IDB:
			/*
			 * Get a pointer to the fixed-length portion of the
			 * IDB.
			 */
			idbp = get_from_block_data(&cursor, sizeof(*idbp),
			    errbuf);
			if (idbp == NULL)
				goto fail;	/* error */

			/*
			 * Byte-swap it if necessary.
			 */
			if (p->swapped) {
				idbp->linktype = SWAPSHORT((uint16_t)idbp->linktype);
				idbp->snaplen = SWAPLONG(idbp->snaplen);
			}

			/*
			 * Try to add this interface.
			 */
			if (!add_interface(p, &cursor, errbuf))
				goto fail;
			goto done;

		case BT_EPB:
		case BT_SPB:
		case BT_PB:
			/*
			 * Saw a packet before we saw any IDBs.  That's
			 * not valid, as we don't know what link-layer
			 * encapsulation the packet has.
			 */
			snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "the capture file has a packet block before any Interface Description Blocks");
			goto fail;

		default:
			/*
			 * Just ignore it.
			 */
			break;
		}
	}

done:
	p->tzoff = 0;	/* XXX - not used in pcap */
	p->snapshot = idbp->snaplen;
	p->linktype = linktype_to_dlt(idbp->linktype);
	p->linktype_ext = 0;

	p->next_packet_op = pcap_ng_next_packet;
	p->cleanup_op = pcap_ng_cleanup;

	return (p);

fail:
	free(ps->ifaces);
	free(p->buffer);
	free(p);
	*err = 1;
	return (NULL);
}
Ejemplo n.º 22
0
main(int argc, char** argv)
{
  int dum_int, iread, iw, indx, ITStart, namelen, HdrSize,
    NTOT, NSkip, NPtsToRead, SkiByte, log2NTOT, NDM,
    NOPFiles, MAX, NTSampInRead, i, NSampInRead, BytePerFrame,
    NByteInRead, ITOffset, NBitChan, *ibrev, NRead, IFiles, FOld,
    FNew, FSwitch, KeepTrack, NTReadOld, NTReadNew, NReadOld,
    NReadNew, OPFileSize, IOffset, SkipByte, oldper, newper ,NT_Files;

  long long TotalTrack;

  float  FMin, FMax, FCen, *Inbuf, *Outbuf, timecount;

  char  *filename, *filefull, *parfile;

  FILE  *fpar, *Fout[8192], *Fin;

  if (argc <= 1) 
    tree_help();
  else 
    tree_parms(argc, argv);

  filename   = (char *) calloc(120, sizeof(char));
  filefull   = (char *) calloc(120, sizeof(char));
  parfile    = (char *) calloc(120, sizeof(char));

  ITStart = 0; NT_Files = 0;

  /* To read a sample header from first time file */

  strcpy(filename, unfname);
  namelen    = strlen(filename);

  printf("First filename   : %s\n", filename);
  if ((fpar     = fopen(filename, "rb")) == NULL) {
    printf("ERROR opening file %s.\n", filename);
    exit(0);
  }
  HdrSize  = read_header(fpar);
  fclose(fpar);

  FMin     = fch1;
  FMax     = FMin + (float)(nchans - 1) * foff;
  FCen     = (FMin + FMax) / 2.0;

  printf("No. of frequency channels        : %d\n",nchans);
  printf("Beginning radio frequency        : %f MHz\n", FMin - (foff / 2.0));
  printf("Ending radio frequency           : %f MHz\n", FMax + (foff / 2.0));
  printf("Centre frq. of the whole band    : %f MHz\n", FCen);
  printf("\n");
  printf("input sampling interval       : %f sec\n", tsamp);

  NTOT = 0;
  strcpy(filename, unfname);
  namelen    = strlen(filename);
  if ((fpar       = fopen(filename, "rb")) == NULL) {
    printf("ERROR opening %s.\n", filename);
  }

  HdrSize    = read_header(fpar);
  fclose(fpar);

  NT_Files = (int)nsamples(filename, HdrSize, nbits, nifs, nchans);

  NTOT       += NT_Files;
  printf("File %5d   : %s with %d samples\n", i, filename, NT_Files);

  NSkip      = (int)(TSkip / tsamp);
  if (NSkip > NT_Files) {
    printf("Initial Skip-length longer than the first file!\n");
    exit(0);
  }

  NPtsToRead = (int)(TRead / tsamp);
  if (NPtsToRead==0) {
    TRead=NT_Files*tsamp;
    NPtsToRead=NT_Files;
  }
  if (NPtsToRead > NTOT) NPtsToRead = NTOT;
  SkipByte   = (NSkip * nbits * nchans / 8);

  printf("\n");
  printf("This data set contains %d number of samples\n", NTOT);
  printf("Number of bits per sample          : %d\n", nbits);
  printf("Time length to skip at the begin.  : %f\n", TSkip);
  printf("Bytes to skip at the beginning     : %d\n", SkipByte);
  printf("No. of time samples to skip        : %d\n", NSkip);
  printf("Time length to read after skipping : %f\n", TRead);
  printf("Samples to read after skipping     : %d\n", NPtsToRead);

  NTOT  -= NSkip;
  printf("Time samples after initial skip    : %d\n", NTOT);

  if (NPtsToRead > NTOT) {
    NPtsToRead  = NTOT;
    printf("Too many samples to read. Truncated to %d samples\n", NTOT);
  } else
    NTOT  = NPtsToRead;

  log2NTOT = (int)(log((double)NTOT) / log((double)2.0));
  NTOT     = (1 << log2NTOT);
  printf("After truncating to lower 2^n      : %d\n", NTOT);

  if (DMMin < 0) DMMin = 0;
  if (DMMax >= nchans) DMMax = (nchans - 1);
  if ((DMMin == 0) && (DMMax == 0)) {
    DMMin = 0; DMMax = (nchans - 1);
  } else if ((DMMin != 0) && (DMMax == 0)) 
    DMMax = (nchans - 1);
  printf("\n");
  printf("Minimum DM index = %d   Maximum = %d\n", DMMin, DMMax);
  DMMinv=(tsamp/8.3e3)*(double)(DMMin)*pow((FMax+FMin)/2.,3.)/fabs(FMax-FMin);
  DMMaxv=(tsamp/8.3e3)*(double)(DMMax)*pow((FMax+FMin)/2.,3.)/fabs(FMax-FMin);
  printf("Minimum DM value = %f   Maximum = %f\n", DMMinv,DMMaxv);
  NDM        = (DMMax - DMMin + 1);

  NOPFiles   = NDM;
  OPFileSize = sizeof(float) * NTOT;

  printf("\n");
  printf("Total number of output files    : %d\n", NOPFiles);
  printf("Size of each output file        : %d bytes\n", OPFileSize);

  for (i=0; i<NOPFiles; i++) {
    strcpy(filefull,unfname);
    sprintf(&filefull[strlen(filefull)-4], ".DM%.4d.tim", (i+DMMin));
    if((Fout[i] = fopen(filefull,"wb")) == NULL) {
      printf("ERROR opening output file %s.\n", filefull);
      exit(0);
    } else {
      nbands=1;
      nobits=32;
      refdm=(tsamp/8.3e3)*(double)(i+DMMin)*pow((FMax+FMin)/2.,3.)/
	fabs(FMax-FMin);
      output=Fout[i];
      dedisperse_header();
    }
  }
  printf("\n");

  /*
  strcpy(parfile, "\0"); strcpy(parfile, unfname);
  strncat(parfile, ".par", 4);
  fpar = fopen(parfile, "w");
  fprintf(fpar, "%s\n", unfname);
  fprintf(fpar, "%d  %d  %10.8f \n ", NTOT, nchans, tsamp);
  fprintf(fpar, "%d  %d  %d  %d\n", DMMin, DMMax, NDM, NOPFiles);
  fprintf(fpar, "%d\n", nchans);
  fprintf(fpar, "%f %f\n", FMin, FMax);
  fclose(fpar);
  */

  MAX          = (1 << 12);
  if (MAX > NTOT) MAX = NTOT;

  NTSampInRead = (MAX + (2 * nchans));
  NSampInRead  = (nchans * NTSampInRead);
  BytePerFrame = (nchans * nbits / 8);
  NByteInRead  = (NTSampInRead * BytePerFrame);

  ITOffset     = 2 * nchans;
  IOffset      = (-ITOffset * BytePerFrame);

  NBitChan = (int)(log((double)nchans) / 0.6931471);

  printf("Time samples to read in one read      : %d\n", NTSampInRead);
  printf("Bytes to read in one read             : %d bytes\n", NByteInRead);
  printf("Offset counter for each read          : %d time samples\n", IOffset);
  printf("\n");
  printf("No of bits to represent all channels  : %d\n", NBitChan);

  Inbuf    = (float *) calloc(NSampInRead, sizeof(float));
  Outbuf   = (float *) calloc(NSampInRead, sizeof(float));
  ibrev    = (int *)   calloc(nchans, sizeof(int));

  for (i=0; i<nchans; i++) {
    ibrev[i] = bitrev(i, NBitChan);
  }

  NRead    = (int)(NTOT / MAX);
  printf("Total number of reads in the run       : %d\n", NRead);

  IFiles   = 0;
  FOld     = 0;
  FNew     = 0;
  FSwitch  = 1;

  strcpy(filename, unfname);  namelen    = strlen(filename);
  printf("First filename   : %s\n", filename);
  if ((Fin = fopen(filename, "rb")) == NULL) {
    printf("ERROR opening file %s.\n", filename);
    exit(0);
  }
  HdrSize  = read_header(Fin);
  printf("Going into the main loop\n");

  fseek(Fin, SkipByte, SEEK_CUR);
  KeepTrack = (NT_Files - NSkip);
  TotalTrack = 0;

  newper   = 0.0;
  oldper   = newper;
  printf("\n\n");
  for (iread=0; iread<NRead; iread++) {

    newper = 100.0 * (((float)iread / (float)NRead));
    if (newper > oldper) {
      timecount = ((float)iread * NTSampInRead * tsamp);
      printf("\rProcessed : %3d%%    Current file : %s    Time from beg : %8.2f sec", 
	     (int)newper, filename, timecount); fflush(stdout);
      oldper = newper;
    }

    KeepTrack  -= NTSampInRead;
    TotalTrack += NTSampInRead;

    if (KeepTrack > 0) {
      read_block(Fin, nbits, Inbuf, NSampInRead);
      fseek(Fin, IOffset, SEEK_CUR);
      KeepTrack  += ITOffset;
      TotalTrack -= ITOffset;
    }

    if (KeepTrack > 0) FSwitch = 1;

    memset(&Outbuf[0], 0, (sizeof(float) * NSampInRead));

    AxisSwap(Inbuf, Outbuf, nchans, NTSampInRead);

    if (FlipSwitch == 0)  {
      printf("BEFORE..."); fflush(stdout);
      FlipBand(Outbuf, nchans, NTSampInRead);
      printf("AFTER!\n");
    }

    taylor_flt(Outbuf, NSampInRead, nchans);

    for (iw=DMMin; iw<=DMMax; iw++) {
      indx  = (ibrev[iw] * NTSampInRead);
      fwrite(&Outbuf[indx], sizeof(float), MAX, Fout[iw-DMMin]);
    }
  }

  newper    = 100.0 * ((float)iread / NRead);
  timecount = ((float)iread * NTSampInRead * tsamp);
  printf("\rProcessed : %3d%%    Current file : %s    Time from beg : %8.2f sec", 
	 (int)newper, filename, timecount); fflush(stdout);
  printf("\n\n");

  for (i=0; i<NOPFiles; i++) fclose(Fout[i]);

  exit(0);
}
Ejemplo n.º 23
0
//**********************************************************************************************
//**********************************************************************************************
//**********************************************************************************************
int main(void)
{
	volatile unsigned char data, pos;

	WDTCTL = WDTPW + WDTHOLD;             // Stop watchdog timer


	//if (CALBC1_12MHZ==0xFF || CALDCO_12MHZ == 0xFF)
	 //   while(1);                               // If calibration constants erased do not load - trap CPU
	BCSCTL1 = CALBC1_12MHZ;	// Set DCO
	DCOCTL = CALDCO_12MHZ;


	//**********************************************
	//DO NOT CHANGE THE ORDER OF THE FOLLOWING CODE!
	port_init();

	initLCD();
	P4OUT |= BIT1; //LCD backlight on
	writecom(0x01, 0);//mode=0 instruction/command,    mode=1 data  clear display
	writecom(0x02, 0);//mode=0 instruction/command,    mode=1 data  position cursor home home
	write_string_to_LCD("DLP Design      ", 16);
	writecom(0xC0, 0);//mode=0 instruction/command,    mode=1 data  position cursor to start of second row
	write_string_to_LCD("DLP-RFID2 Demo  ", 16);

	keep_local=0;//if high, keep data returned to demo board (from reader) local instead of forwarding to host PC via USB.  if low, send to host PC.
	rx_index=0;//init the receive buffer index
	run_mode=0;//disable inventories
	blkaddr=0;
	currentantswstate = ANTSWINT;//init to internal

	/*	Code showing how to use J2, "SEL"  User Defined Jumper
	//pullup resistor enabled for P6.7 in port_init();
	data = P6IN;	    //read port 6
	data = data & 0x80;	//mask off other port bits
	if(data>0)          //if P6.7 is high (default)
	*/

	uart_init();

	setup_interrupts();
	//ping reader twice to sync and stop transmitting 'D's
	short_dly(50000);//pause for remainder of packet
	ping_reader();//ping for presence of reader and update display
	short_dly(50000);//pause for remainder of packet
	ping_reader();//ping for presence of reader and update display
	short_dly(50000);//pause for remainder of packet
	//**********************************************

	set_output_mode();


	for (;;)
	{
		if(light_pressed())
			P4OUT ^= 0x02; //toggle P4.1
		if(ping_pressed())
			ping_reader();//ping for presence of reader and update display
		if(antsw_pressed())
			toggle_antenna_switch();//toggle between internal and external antenna
		if(run_pressed())
			enter_run_mode();//setup for reading UIDs and set run flag for continuous inventories
		if(stop_pressed())
			exit_run_mode();//setup for reading UIDs and set run flag for continuous inventories
		if(rfoff_pressed())
			turn_rf_off();
		if(rdblk_pressed())
			read_block();

		if(run_mode==1)
			single_slot_inventory();
	}
}
Ejemplo n.º 24
0
int barrett_transaction(RIG *rig, char *cmd, int expected, char **result)
{
    char cmd_buf[MAXCMDLEN];
    int retval, cmd_len;

    rig_debug(RIG_DEBUG_VERBOSE, "%s: cmd=%s\n", __FUNCTION__, cmd);
    struct rig_state *rs = &rig->state;
    struct barrett_priv_data *priv = rig->state.priv;

    cmd_len = snprintf(cmd_buf, sizeof(cmd_buf), "%s%s", cmd, EOM);

    serial_flush(&rs->rigport);
    retval = write_block(&rs->rigport, cmd_buf, cmd_len);

    if (retval < 0) {
        return retval;
    }

    if (expected == 0) {
        // response format is 0x11,data...,0x0d,0x0a,0x13
        retval = read_string(&rs->rigport, priv->ret_data, sizeof(priv->ret_data),
                             "\x11", 1);
        rig_debug(RIG_DEBUG_VERBOSE, "%s: resultlen=%d\n", __FUNCTION__,
                  strlen(priv->ret_data));

        if (retval < 0) {
            return retval;
        }
    } else {
        retval = read_block(&rs->rigport, priv->ret_data, expected);

        if (retval < 0) {
            return retval;
        }
    }

    rig_debug(RIG_DEBUG_VERBOSE, "%s: retval=%d\n", __FUNCTION__, retval);
    dump_hex((const unsigned char *)priv->ret_data, strlen(priv->ret_data));
    char *p = priv->ret_data;
    char xon = p[0];
    char xoff = p[strlen(p) - 1];

    if (xon == 0x13 && xoff == 0x11) {
        rig_debug(RIG_DEBUG_ERR, "%s: removing xoff char\n", __FUNCTION__);
        p[strlen(p) - 1] = 0;
    } else {
        rig_debug(RIG_DEBUG_ERR,
                  "%s: expected XOFF=0x13 as first and XON=0x11 as last byte, got %02x/%02x\n",
                  __FUNCTION__, xon, xoff);
    }

    rig_debug(RIG_DEBUG_ERR, "%s: removing xon char\n", __FUNCTION__);
    // Remove the XON char if there
    p = memchr(priv->ret_data, 0x11, strlen(priv->ret_data));

    if (p) {
        *p = 0;
    }



    rig_debug(RIG_DEBUG_VERBOSE, "%s: result = %04x\n", __FUNCTION__, result);

    if (result != NULL) {
        rig_debug(RIG_DEBUG_VERBOSE, "%s: setting result\n", __FUNCTION__);

        if (priv->ret_data[0] == 0x13) { // we'll return from the 1st good char
            *result = &(priv->ret_data[1]);
        } else { // some commands like IAL don't give XOFF but XON is there -- is this a bug?
            *result = &(priv->ret_data[0]);
        }

        // See how many CR's we have
        int n = 0;

        for (p = *result; *p; ++p) {
            if (*p == 0x0d) {
                ++n;
            }
        }

        // if only 1 CR then we'll truncate string
        // Several commands can return multiline strings and we'll leave them alone
        if (n == 1) {
            strtok(*result, "\r");
        }

        dump_hex((const unsigned char *)*result, strlen(*result));
        rig_debug(RIG_DEBUG_VERBOSE, "%s: returning result=%s\n", __FUNCTION__,
                  *result);
    } else {
        rig_debug(RIG_DEBUG_VERBOSE, "%s: no result requested=%s\n", __FUNCTION__);
    }

    return RIG_OK;
}
Ejemplo n.º 25
0
int read( int fd, void *buffer, size_t nbytes )
{
    if( (stdin_flags&O_NONBLOCK)==O_NONBLOCK )  return read_nonblock(fd, buffer, nbytes);
    else                                        return read_block(fd, buffer, nbytes );
}
Ejemplo n.º 26
0
/*
  read 32 bit value
 */
uint32_t StorageAccess::read_uint32(uint16_t loc) const
{
    uint32_t v;
    read_block(&v, loc, sizeof(v));
    return v;
}
Ejemplo n.º 27
0
int scan_inode_table(int fd, long long start, long long end,
	long long root_inode_start, int root_inode_offset,
	struct squashfs_super_block *sBlk, union squashfs_inode_header
	*dir_inode, unsigned char **inode_table, unsigned int *root_inode_block,
	unsigned int *root_inode_size, long long *uncompressed_file,
	unsigned int *uncompressed_directory, int *file_count, int *sym_count,
	int *dev_count, int *dir_count, int *fifo_count, int *sock_count,
	unsigned int *id_table)
{
	unsigned char *cur_ptr;
	int byte, files = 0;
	unsigned int directory_start_block, bytes = 0, size = 0;
	struct squashfs_base_inode_header base;

	TRACE("scan_inode_table: start 0x%llx, end 0x%llx, root_inode_start "
		"0x%llx\n", start, end, root_inode_start);

	*root_inode_block = UINT_MAX;
	while(start < end) {
		if(start == root_inode_start) {
			TRACE("scan_inode_table: read compressed block 0x%llx "
				"containing root inode\n", start);
			*root_inode_block = bytes;
		}
		if(size - bytes < SQUASHFS_METADATA_SIZE) {
			*inode_table = realloc(*inode_table, size
				+= SQUASHFS_METADATA_SIZE);
			if(*inode_table == NULL)
				MEM_ERROR();
		}
		TRACE("scan_inode_table: reading block 0x%llx\n", start);
		byte = read_block(fd, start, &start, 0, *inode_table + bytes);
		if(byte == 0)
			goto corrupted;

		bytes += byte;

		/* If this is not the last metadata block in the inode table
		 * then it should be SQUASHFS_METADATA_SIZE in size.
		 * Note, we can't use expected in read_block() above for this
		 * because we don't know if this is the last block until
		 * after reading.
		 */
		if(start != end && byte != SQUASHFS_METADATA_SIZE)
			goto corrupted;
	}

	/*
	 * We expect to have found the metadata block containing the
	 * root inode in the above inode_table metadata block scan.  If it
	 * hasn't been found then the filesystem is corrupted
	 */
	if(*root_inode_block == UINT_MAX)
		goto corrupted;

	/*
	 * The number of bytes available after the root inode medata block
	 * should be at least the root inode offset + the size of a
	 * regular directory inode, if not the filesystem is corrupted
	 *
	 *	+-----------------------+-----------------------+
	 *	| 			|        directory	|
	 *	|			|          inode	|
	 *	+-----------------------+-----------------------+
	 *	^			^			^
	 *	*root_inode_block	root_inode_offset	bytes
	 */
	if((bytes - *root_inode_block) < (root_inode_offset +
			sizeof(struct squashfs_dir_inode_header)))
		goto corrupted;

	/*
	 * Read last inode entry which is the root directory inode, and obtain
	 * the last directory start block index.  This is used when calculating
	 * the total uncompressed directory size.  The directory bytes in the
	 * last * block will be counted as normal.
	 *
	 * Note, the previous check ensures the following calculation won't
	 * underflow, and we won't access beyond the buffer
	 */
	*root_inode_size = bytes - (*root_inode_block + root_inode_offset);
	bytes = *root_inode_block + root_inode_offset;
	SQUASHFS_SWAP_DIR_INODE_HEADER(*inode_table + bytes, &dir_inode->dir);
	
	if(dir_inode->base.inode_type == SQUASHFS_DIR_TYPE)
		directory_start_block = dir_inode->dir.start_block;
	else if(dir_inode->base.inode_type == SQUASHFS_LDIR_TYPE) {
		if(*root_inode_size < sizeof(struct squashfs_ldir_inode_header))
			/* corrupted filesystem */
			goto corrupted;
		SQUASHFS_SWAP_LDIR_INODE_HEADER(*inode_table + bytes,
			&dir_inode->ldir);
		directory_start_block = dir_inode->ldir.start_block;
	} else
		/* bad type, corrupted filesystem */
		goto corrupted;

	get_uid(id_table[dir_inode->base.uid]);
	get_guid(id_table[dir_inode->base.guid]);

	/* allocate fragment to file mapping table */
	file_mapping = calloc(sBlk->fragments, sizeof(struct append_file *));
	if(file_mapping == NULL)
		MEM_ERROR();

	for(cur_ptr = *inode_table; cur_ptr < *inode_table + bytes; files ++) {
		if(NO_INODE_BYTES(squashfs_base_inode_header))
			/* corrupted filesystem */
			goto corrupted;

		SQUASHFS_SWAP_BASE_INODE_HEADER(cur_ptr, &base);

		TRACE("scan_inode_table: processing inode @ byte position "
			"0x%x, type 0x%x\n",
			(unsigned int) (cur_ptr - *inode_table),
			base.inode_type);

		get_uid(id_table[base.uid]);
		get_guid(id_table[base.guid]);

		switch(base.inode_type) {
		case SQUASHFS_FILE_TYPE: {
			struct squashfs_reg_inode_header inode;
			int frag_bytes, blocks, i;
			long long start, file_bytes = 0;
			unsigned int *block_list;

			if(NO_INODE_BYTES(squashfs_reg_inode_header))
				/* corrupted filesystem */
				goto corrupted;

			SQUASHFS_SWAP_REG_INODE_HEADER(cur_ptr, &inode);

			frag_bytes = inode.fragment == SQUASHFS_INVALID_FRAG ?
				0 : inode.file_size % sBlk->block_size;
			blocks = inode.fragment == SQUASHFS_INVALID_FRAG ?
				(inode.file_size + sBlk->block_size - 1) >>
				sBlk->block_log : inode.file_size >>
				sBlk->block_log;
			start = inode.start_block;

			TRACE("scan_inode_table: regular file, file_size %d, "
				"blocks %d\n", inode.file_size, blocks);

			if(NO_BYTES(blocks * sizeof(unsigned int)))
				/* corrupted filesystem */
				goto corrupted;

			block_list = malloc(blocks * sizeof(unsigned int));
			if(block_list == NULL)
				MEM_ERROR();

			cur_ptr += sizeof(inode);
			SQUASHFS_SWAP_INTS(cur_ptr, block_list, blocks);

			*uncompressed_file += inode.file_size;
			(*file_count) ++;

			for(i = 0; i < blocks; i++)
				file_bytes +=
					SQUASHFS_COMPRESSED_SIZE_BLOCK
								(block_list[i]);

			if(inode.fragment != SQUASHFS_INVALID_FRAG &&
					inode.fragment >= sBlk->fragments) {
				free(block_list);
				goto corrupted;
			}

			add_file(start, inode.file_size, file_bytes,
				block_list, blocks, inode.fragment,
				inode.offset, frag_bytes);
				
			cur_ptr += blocks * sizeof(unsigned int);
			break;
		}	
		case SQUASHFS_LREG_TYPE: {
			struct squashfs_lreg_inode_header inode;
			int frag_bytes, blocks, i;
			long long start, file_bytes = 0;
			unsigned int *block_list;

			if(NO_INODE_BYTES(squashfs_lreg_inode_header))
				/* corrupted filesystem */
				goto corrupted;

			SQUASHFS_SWAP_LREG_INODE_HEADER(cur_ptr, &inode);

			frag_bytes = inode.fragment == SQUASHFS_INVALID_FRAG ?
				0 : inode.file_size % sBlk->block_size;
			blocks = inode.fragment == SQUASHFS_INVALID_FRAG ?
				(inode.file_size + sBlk->block_size - 1) >>
				sBlk->block_log : inode.file_size >>
				sBlk->block_log;
			start = inode.start_block;

			TRACE("scan_inode_table: extended regular "
				"file, file_size %lld, blocks %d\n",
				inode.file_size, blocks);

			if(NO_BYTES(blocks * sizeof(unsigned int)))
				/* corrupted filesystem */
				goto corrupted;

			block_list = malloc(blocks * sizeof(unsigned int));
			if(block_list == NULL)
				MEM_ERROR();

			cur_ptr += sizeof(inode);
			SQUASHFS_SWAP_INTS(cur_ptr, block_list, blocks);

			*uncompressed_file += inode.file_size;
			(*file_count) ++;

			for(i = 0; i < blocks; i++)
				file_bytes +=
					SQUASHFS_COMPRESSED_SIZE_BLOCK
								(block_list[i]);

			if(inode.fragment != SQUASHFS_INVALID_FRAG &&
					inode.fragment >= sBlk->fragments) {
				free(block_list);
				goto corrupted;
			}

			add_file(start, inode.file_size, file_bytes,
				block_list, blocks, inode.fragment,
				inode.offset, frag_bytes);

			cur_ptr += blocks * sizeof(unsigned int);
			break;
		}	
		case SQUASHFS_SYMLINK_TYPE:
		case SQUASHFS_LSYMLINK_TYPE: {
			struct squashfs_symlink_inode_header inode;

			if(NO_INODE_BYTES(squashfs_symlink_inode_header))
				/* corrupted filesystem */
				goto corrupted;

			SQUASHFS_SWAP_SYMLINK_INODE_HEADER(cur_ptr, &inode);

			(*sym_count) ++;

			if (inode.inode_type == SQUASHFS_LSYMLINK_TYPE) {
				if(NO_BYTES(inode.symlink_size +
							sizeof(unsigned int)))
					/* corrupted filesystem */
					goto corrupted;
				cur_ptr += sizeof(inode) + inode.symlink_size +
							sizeof(unsigned int);
			} else {
				if(NO_BYTES(inode.symlink_size))
					/* corrupted filesystem */
					goto corrupted;
				cur_ptr += sizeof(inode) + inode.symlink_size;
			}
			break;
		}
		case SQUASHFS_DIR_TYPE: {
			struct squashfs_dir_inode_header dir_inode;

			if(NO_INODE_BYTES(squashfs_dir_inode_header))
				/* corrupted filesystem */
				goto corrupted;
				
			SQUASHFS_SWAP_DIR_INODE_HEADER(cur_ptr, &dir_inode);

			if(dir_inode.start_block < directory_start_block)
				*uncompressed_directory += dir_inode.file_size;

			(*dir_count) ++;
			cur_ptr += sizeof(struct squashfs_dir_inode_header);
			break;
		}
		case SQUASHFS_LDIR_TYPE: {
			struct squashfs_ldir_inode_header dir_inode;
			int i;

			if(NO_INODE_BYTES(squashfs_ldir_inode_header))
				/* corrupted filesystem */
				goto corrupted;

			SQUASHFS_SWAP_LDIR_INODE_HEADER(cur_ptr, &dir_inode);

			if(dir_inode.start_block < directory_start_block)
				*uncompressed_directory += dir_inode.file_size;

			(*dir_count) ++;
			cur_ptr += sizeof(struct squashfs_ldir_inode_header);

			for(i = 0; i < dir_inode.i_count; i++) {
				struct squashfs_dir_index index;

				if(NO_BYTES(sizeof(index)))
					/* corrupted filesystem */
					goto corrupted;
			
				SQUASHFS_SWAP_DIR_INDEX(cur_ptr, &index);

				if(NO_BYTES(index.size + 1))
					/* corrupted filesystem */
					goto corrupted;

				cur_ptr += sizeof(index) + index.size + 1;
			}
			break;
		}
	 	case SQUASHFS_BLKDEV_TYPE:
	 	case SQUASHFS_CHRDEV_TYPE:
			if(NO_INODE_BYTES(squashfs_dev_inode_header))
				/* corrupted filesystem */
				goto corrupted;

			(*dev_count) ++;
			cur_ptr += sizeof(struct squashfs_dev_inode_header);
			break;
	 	case SQUASHFS_LBLKDEV_TYPE:
	 	case SQUASHFS_LCHRDEV_TYPE:
			if(NO_INODE_BYTES(squashfs_ldev_inode_header))
				/* corrupted filesystem */
				goto corrupted;

			(*dev_count) ++;
			cur_ptr += sizeof(struct squashfs_ldev_inode_header);
			break;
		case SQUASHFS_FIFO_TYPE:
			if(NO_INODE_BYTES(squashfs_ipc_inode_header))
				/* corrupted filesystem */
				goto corrupted;

			(*fifo_count) ++;
			cur_ptr += sizeof(struct squashfs_ipc_inode_header);
			break;
		case SQUASHFS_LFIFO_TYPE:
			if(NO_INODE_BYTES(squashfs_lipc_inode_header))
				/* corrupted filesystem */
				goto corrupted;

			(*fifo_count) ++;
			cur_ptr += sizeof(struct squashfs_lipc_inode_header);
			break;
		case SQUASHFS_SOCKET_TYPE:
			if(NO_INODE_BYTES(squashfs_ipc_inode_header))
				/* corrupted filesystem */
				goto corrupted;

			(*sock_count) ++;
			cur_ptr += sizeof(struct squashfs_ipc_inode_header);
			break;
		case SQUASHFS_LSOCKET_TYPE:
			if(NO_INODE_BYTES(squashfs_lipc_inode_header))
				/* corrupted filesystem */
				goto corrupted;

			(*sock_count) ++;
			cur_ptr += sizeof(struct squashfs_lipc_inode_header);
			break;
	 	default:
			ERROR("Unknown inode type %d in scan_inode_table!\n",
					base.inode_type);
			goto corrupted;
		}
	}
	
	printf("Read existing filesystem, %d inodes scanned\n", files);
	return TRUE;

corrupted:
	ERROR("scan_inode_table: filesystem corruption detected in "
		"scanning metadata\n");
	free(*inode_table);
	return FALSE;
}
Ejemplo n.º 28
0
int read_fragment_table_3()
{
	int res, i, indexes = SQUASHFS_FRAGMENT_INDEXES_3(sBlk.fragments);
	squashfs_fragment_index fragment_table_index[indexes];

	TRACE("read_fragment_table: %d fragments, reading %d fragment indexes "
		"from 0x%llx\n", sBlk.fragments, indexes,
		sBlk.fragment_table_start);

	if(sBlk.fragments == 0)
		return -1;

	if((fragment_table = malloc(sBlk.fragments *
			sizeof(squashfs_fragment_entry_3))) == NULL)
		EXIT_UNSQUASH("read_fragment_table: failed to allocate "
			"fragment table\n");

	if(swap) {
		squashfs_fragment_index sfragment_table_index[indexes];

		res = read_bytes(sBlk.fragment_table_start,
			SQUASHFS_FRAGMENT_INDEX_BYTES_3(sBlk.fragments),
			(char *) sfragment_table_index);
		if(res == FALSE) {
			ERROR("read_fragment_table: failed to read fragment "
				"table index\n");       
			return FALSE;
		}
		SQUASHFS_SWAP_FRAGMENT_INDEXES_3(fragment_table_index,
			sfragment_table_index, indexes);
	} else {
		res = read_bytes(sBlk.fragment_table_start,
			SQUASHFS_FRAGMENT_INDEX_BYTES_3(sBlk.fragments),
			(char *) fragment_table_index);
		if(res == FALSE) {
			ERROR("read_fragment_table: failed to read fragment "
				"table index\n");       
			return FALSE;
		}
	}

	for(i = 0; i < indexes; i++) {
		int length = read_block(fragment_table_index[i], NULL,
			((char *) fragment_table) + (i *
			SQUASHFS_METADATA_SIZE));
		TRACE("Read fragment table block %d, from 0x%llx, length %d\n",
			i, fragment_table_index[i], length);
		if(length == FALSE) {
			ERROR("read_fragment_table: failed to read fragment "
				"table block\n");       
			return FALSE;
		}
	}

	if(swap) {
		squashfs_fragment_entry_3 sfragment;
		for(i = 0; i < sBlk.fragments; i++) {
			SQUASHFS_SWAP_FRAGMENT_ENTRY_3((&sfragment),
				(&fragment_table[i]));
			memcpy((char *) &fragment_table[i], (char *) &sfragment,
				sizeof(squashfs_fragment_entry_3));
		}
	}

	return TRUE;
}
Ejemplo n.º 29
0
unsigned char *squashfs_readdir(int fd, int root_entries,
	unsigned int directory_start_block, int offset, int size,
	unsigned int *last_directory_block, struct squashfs_super_block *sBlk,
	void (push_directory_entry)(char *, squashfs_inode, int, int))
{
	struct squashfs_dir_header dirh;
	char buffer[sizeof(struct squashfs_dir_entry) + SQUASHFS_NAME_LEN + 1]
		__attribute__ ((aligned));
	struct squashfs_dir_entry *dire = (struct squashfs_dir_entry *) buffer;
	unsigned char *directory_table = NULL;
	int byte, bytes = 0, dir_count;
	long long start = sBlk->directory_table_start + directory_start_block,
		last_start_block = start; 

	size += offset;
	directory_table = malloc((size + SQUASHFS_METADATA_SIZE * 2 - 1) &
		~(SQUASHFS_METADATA_SIZE - 1));
	if(directory_table == NULL)
		MEM_ERROR();

	while(bytes < size) {
		int expected = (size - bytes) >= SQUASHFS_METADATA_SIZE ?
			SQUASHFS_METADATA_SIZE : 0;

		TRACE("squashfs_readdir: reading block 0x%llx, bytes read so "
			"far %d\n", start, bytes);

		last_start_block = start;
		byte = read_block(fd, start, &start, expected, directory_table + bytes);
		if(byte == 0) {
			ERROR("Failed to read directory\n");
			ERROR("Filesystem corrupted?\n");
			free(directory_table);
			return NULL;
		}
		bytes += byte;
	}

	if(!root_entries)
		goto all_done;

	bytes = offset;
 	while(bytes < size) {			
		SQUASHFS_SWAP_DIR_HEADER(directory_table + bytes, &dirh);

		dir_count = dirh.count + 1;
		TRACE("squashfs_readdir: Read directory header @ byte position "
			"0x%x, 0x%x directory entries\n", bytes, dir_count);
		bytes += sizeof(dirh);

		while(dir_count--) {
			SQUASHFS_SWAP_DIR_ENTRY(directory_table + bytes, dire);
			bytes += sizeof(*dire);

			memcpy(dire->name, directory_table + bytes,
				dire->size + 1);
			dire->name[dire->size + 1] = '\0';
			TRACE("squashfs_readdir: pushing directory entry %s, "
				"inode %x:%x, type 0x%x\n", dire->name,
				dirh.start_block, dire->offset, dire->type);
			push_directory_entry(dire->name,
				SQUASHFS_MKINODE(dirh.start_block,
				dire->offset), dirh.inode_number +
				dire->inode_number, dire->type);
			bytes += dire->size + 1;
		}
	}

all_done:
	*last_directory_block = (unsigned int) last_start_block -
		sBlk->directory_table_start;
	return directory_table;
}
Ejemplo n.º 30
0
int order(char * entidad, int (*fp)(struct record_t*, struct record_t*)){
  FILE * block_file;
  FILE * tmp_file;
  struct record_t *record;
  struct block_t * current_block;
  struct file_header * file_header;

  int res, j = 0;
  int current_pos = 0;
  int current_pos_freeze = 0;
  int registros_leidos = 0, bloques_leidos = 0;
  int todo_leido = 1, totales_leidos = 0;
  int registro_en_memoria = 1, registro_procesado = 0;
  int tmp_files = 1;
  char path[30];

  struct record_t buffer[TMP_BUFFER];
  struct record_t freeze_buffer[TMP_BUFFER];

  block_file = open_block_file(entidad, 2);

  if(!block_file){
    return BAD_NAME_FILE;
  }

  /* Allocate buffer and record */
  current_block = (struct block_t *) malloc(sizeof(struct block_t));
  file_header   = (struct file_header*) malloc(sizeof(struct file_header));
  record  = (struct record_t*) malloc(sizeof(struct record_t));

  if(!current_block || !file_header || !record)
    return ALLOCATE_FAIL;

  initialize_block(current_block,1);
  read_header(block_file, file_header);
  initialize_record(record);

  res  = read_from_file(block_file, current_block, file_header, 1);
  bloques_leidos++;

  if(res == RES_OK){

    /* Inicializo el buffer con registros */
    while(j<TMP_BUFFER && (todo_leido == 1)){
      if(registros_leidos < current_block->total_reg){
        current_pos += read_block(current_block, record, current_pos);
        memcpy(&buffer[j],record,sizeof(struct record_t));
        registros_leidos++;
        totales_leidos++;
        j++;
      }else{
        if((bloques_leidos < file_header->total_blocks+1) && (file_header->total_records > totales_leidos)){
          res = read_from_file(block_file, current_block, file_header, current_block->number+1);
          bloques_leidos++;
          registros_leidos = 0;
          current_pos = 0;
        }else{
          todo_leido = 0;
        }
      }
    }

    /* Creo el primer sort file */
    sprintf(path,"tmp/sort%d.txt",tmp_files);
    tmp_file = fopen(path, "w");

    if(file_header->total_records == totales_leidos)
      heapify(buffer,totales_leidos, fp);
    else
      heapify(buffer,TMP_BUFFER, fp);

    while((todo_leido == 1) && tmp_file){
      if(registros_leidos < current_block->total_reg){
        current_pos += read_block(current_block, record, current_pos);
        registros_leidos++;
        registro_en_memoria = 0;
        registro_procesado  = 1;
      }else{
        if(bloques_leidos < file_header->total_blocks){
          res  = read_from_file(block_file, current_block, file_header, current_block->number+1);
          bloques_leidos++;
          current_pos = 0;
          registros_leidos    = 0;
          registro_procesado  = 1;
          registro_en_memoria = 1;
        }else{
          todo_leido = 0;
        }
      }

      while((registro_procesado == 1) && (registro_en_memoria == 0)){
        if(fp(record,&buffer[0]) < 0){
          if(current_pos_freeze<TMP_BUFFER){
            memcpy(&freeze_buffer[current_pos_freeze],record,sizeof(struct record_t));
            current_pos_freeze++;
            registro_procesado  = 0;
            registro_en_memoria = 1;
          }
          else {
            /* vacio el buffer en el archivo */
            print_buffer_to_file(buffer, TMP_BUFFER, tmp_file, fp);
            /* cierro archivo y abro el siguiente */
            tmp_files++;
            fclose(tmp_file);
            sprintf(path,"tmp/sort%d.txt",tmp_files);
            tmp_file = fopen(path, "w");
            /* copio el freeze_buffer al buffer y creo el heap*/
            heapify(freeze_buffer,TMP_BUFFER, fp);
            memcpy(buffer,freeze_buffer,sizeof(struct record_t) * TMP_BUFFER);
            current_pos_freeze = 0;
            registro_procesado = 1;
          }
        }
        else{
          /* Guardo en el archivo, libero memoria */
          print_record_to_file(buffer, tmp_file);
          /* Guardo en el buffer el record leido */
          memcpy(&buffer[0],&buffer[TMP_BUFFER-1],sizeof(struct record_t));
          memcpy(&buffer[TMP_BUFFER-1],record,sizeof(struct record_t));
          registro_procesado  = 0;
          registro_en_memoria = 1;
          update(buffer, TMP_BUFFER, fp);
        }
      }
    }
    /* Guardo en el archivo*/
    if((file_header->total_records == totales_leidos) && (current_block->number == 1))
      print_buffer_to_file(buffer, totales_leidos, tmp_file, fp);
    else
      print_buffer_to_file(buffer, TMP_BUFFER, tmp_file, fp);

    fclose(tmp_file);

    /* Guardo lo que tenia en el freeze_buffer en otro archivo */
    if(current_pos_freeze > 1){
      tmp_files++;
      sprintf(path,"tmp/sort%d.txt",tmp_files);
      tmp_file = fopen(path, "w");
      print_buffer_to_file(freeze_buffer, current_pos_freeze, tmp_file, fp);
      fclose(tmp_file);
    }
  }

  free(record);
  free(current_block);
  free(file_header);
  fclose(block_file);

  /* Merge de los archivos generados */
  res = merge_files(tmp_files, fp);
  printf("\n");

  return res;
}