示例#1
0
static void
diff_sparse_files (int size_of_file)
{
  int remaining_size = size_of_file;
  char *buffer = (char *) xmalloc (BLOCKSIZE * sizeof (char));
  int buffer_size = BLOCKSIZE;
  union block *data_block = NULL;
  int counter = 0;
  int different = 0;

  fill_in_sparse_array ();

  while (remaining_size > 0)
    {
      int status;
      long chunk_size;
#if 0
      int amount_read = 0;
#endif

      data_block = find_next_block ();
      chunk_size = sparsearray[counter].numbytes;
      if (!chunk_size)
	break;

      lseek (diff_handle, sparsearray[counter].offset, 0);

      /* Take care to not run out of room in our buffer.  */

      while (buffer_size < chunk_size)
	{
	  buffer_size *= 2;
	  buffer = (char *) xrealloc (buffer, buffer_size * sizeof (char));
	}

      while (chunk_size > BLOCKSIZE)
	{
	  if (status = read (diff_handle, buffer, BLOCKSIZE),
	      status != BLOCKSIZE)
	    {
	      if (status < 0)
		{
		  WARN ((0, errno, _("Cannot read %s"), current_file_name));
		  report_difference (NULL);
		}
	      else
		{
		  char message[MESSAGE_BUFFER_SIZE];

		  sprintf (message, _("Could only read %d of %ld bytes"),
			   status, chunk_size);
		  report_difference (message);
		}
	      break;
	    }

	  if (memcmp (buffer, data_block->buffer, BLOCKSIZE))
	    {
	      different = 1;
	      break;
	    }

	  chunk_size -= status;
	  remaining_size -= status;
	  set_next_block_after (data_block);
	  data_block = find_next_block ();
	}
      if (status = read (diff_handle, buffer, (size_t) chunk_size),
	  status != chunk_size)
	{
	  if (status < 0)
	    {
	      WARN ((0, errno, _("Cannot read %s"), current_file_name));
	      report_difference (NULL);
	    }
	  else
	    {
	      char message[MESSAGE_BUFFER_SIZE];

	      sprintf (message, _("Could only read %d of %ld bytes"),
		       status, chunk_size);
	      report_difference (message);
	    }
	  break;
	}

      if (memcmp (buffer, data_block->buffer, (size_t) chunk_size))
	{
	  different = 1;
	  break;
	}
#if 0
      amount_read += chunk_size;
      if (amount_read >= BLOCKSIZE)
	{
	  amount_read = 0;
	  set_next_block_after (data_block);
	  data_block = find_next_block ();
	}
#endif
      set_next_block_after (data_block);
      counter++;
      remaining_size -= chunk_size;
    }

#if 0
  /* If the number of bytes read isn't the number of bytes supposedly in
     the file, they're different.  */

  if (amount_read != size_of_file)
    different = 1;
#endif

  set_next_block_after (data_block);
  free (sparsearray);

  if (different)
    report_difference (_("Data differs"));
}
示例#2
0
static void
diff_sparse_files (int filesize)
{
  int sparse_ind = 0;
  char *buf;
  int buf_size = RECORDSIZE;
  union record *datarec;
  int err;
  long numbytes;
#if 0
  int amt_read = 0;
#endif
  int size = filesize;

  /* FIXME: `datarec' might be used uninitialized in this function.
     Reported by Bruno Haible.  */

  buf = (char *) tar_xmalloc (buf_size * sizeof (char));

  fill_in_sparse_array ();


  while (size > 0)
    {
      datarec = findrec ();
      if (!sparsearray[sparse_ind].numbytes)
	break;

      /* `numbytes' is nicer to write than
	 `sparsearray[sparse_ind].numbytes' all the time...  */

      numbytes = sparsearray[sparse_ind].numbytes;

      lseek (diff_fd, sparsearray[sparse_ind].offset, 0);

      /* Take care to not run out of room in our buffer.  */

      while (buf_size < numbytes)
	{
	  buf_size *= 2;
	  buf = (char *) tar_realloc (buf, buf_size * sizeof (char));
	}
      while (numbytes > RECORDSIZE)
	{
	  if (err = read (diff_fd, buf, RECORDSIZE), err != RECORDSIZE)
	    {
	      if (err < 0)
		WARN ((0, errno, _("Cannot read %s"), current_file_name));
	      else
		fprintf (stdlis, _("%s: Could only read %d of %ld bytes\n"),
			 current_file_name, err, numbytes);
	      break;
	    }
	  if (memcmp (buf, datarec->charptr, RECORDSIZE))
	    {
	      different++;
	      break;
	    }
	  numbytes -= err;
	  size -= err;
	  userec (datarec);
	  datarec = findrec ();
	}
      if (err = read (diff_fd, buf, (size_t) numbytes), err != numbytes)
	{
	  if (err < 0)
	    WARN ((0, errno, _("Cannot read %s"), current_file_name));
	  else
	    fprintf (stdlis, _("%s: Could only read %d of %ld bytes\n"),
		     current_file_name, err, numbytes);
	  break;
	}

      if (memcmp (buf, datarec->charptr, (size_t) numbytes))
	{
	  different++;
	  break;
	}
#if 0
      amt_read += numbytes;
      if (amt_read >= RECORDSIZE)
	{
	  amt_read = 0;
	  userec (datarec);
	  datarec = findrec ();
	}
#endif
      userec (datarec);
      sparse_ind++;
      size -= numbytes;
    }

  /* If the number of bytes read isn't the number of bytes supposedly in
     the file, they're different.  */

#if 0
  if (amt_read != filesize)
    different++;
#endif

  userec (datarec);
  free (sparsearray);
  if (different)
    {
      fprintf (stdlis, _("%s: Data differs\n"), current_file_name);
      if (exit_status == TAREXIT_SUCCESS)
	exit_status = TAREXIT_DIFFERS;
    }
}