/*
 * IREWRITE.C - update a record
 *
 *                      Copyright (c) 1987, Jim Mischel
 * Modifications:
 *
 * 08/13/87 - jim - original coding
 * 08/21/87 - jim - copy source to data buffer before writing
 */

#include "inxdefs.h"

/*
 * irewrite() - update the current data record with the data at 'source'.
 * Returns 0 if successful, error status otherwise.  If the key at 'source'
 * is not the same as the key in the current data record, I_INVKEY is
 * returned and no update is performed.  Since the only integrity check
 * made is a simple key comparison, if duplicate keys are permitted, it
 * is possible to write a record where it doesn't belong.
 */
int irewrite(void *d, void *src)
{
  df_rec *db_control = (df_rec *)d;
  char *source = (char *)src;

  /* check delete flag */
  if (db_control->df_flags & DF_DELETE)
    return(ierror(I_INVKEY));

  /* see if keys are equal */
  if ((*db_control->df_cmp)((source+db_control->df_key_offset),
                            db_control->df_key_ptr))
    return(ierror(I_INVKEY));           /* error: keys not equal */

  /* ok, write the record */
  memcpy(db_control->df_dat_buff,source,db_control->df_rec_size);
  if (iwrite_dat(db_control,source,db_control->df_dat_ptr))
    return(ierrno);                     /* write error */

  return(ierror(0));
} /* irewrite */
