home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / update.c < prev    next >
C/C++ Source or Header  |  1992-01-20  |  4KB  |  123 lines

  1. /* update.c - The routines for updating the file to a consistent state. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990, 1991  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 1, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@cs.wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.         phone:  (206) 676-3035
  27.  
  28. *************************************************************************/
  29.  
  30.  
  31. #include "gdbmdefs.h"
  32.  
  33.  
  34. #ifdef __STDC__
  35. static void write_header (gdbm_file_info *dbf);
  36. #endif /* __STDC__ */
  37.  
  38.  
  39. /* This procedure writes the header back to the file described by DBF. */
  40.  
  41. static void
  42. write_header (dbf)
  43.      gdbm_file_info *dbf;
  44. {
  45.   int  num_bytes;    /* Return value for write. */
  46.   long file_pos;    /* Return value for lseek. */
  47.  
  48.   file_pos = lseek (dbf->desc, 0, L_SET);
  49.   if (file_pos != 0) _gdbm_fatal (dbf, "lseek error");
  50.   num_bytes = write (dbf->desc, dbf->header, dbf->header->block_size);
  51.   if (num_bytes != dbf->header->block_size)
  52.     _gdbm_fatal (dbf, "write error");
  53.  
  54.   /* Wait for all output to be done. */
  55.   fsync (dbf->desc);
  56. }
  57.  
  58.  
  59. /* After all changes have been made in memory, we now write them
  60.    all to disk. */
  61. void
  62. _gdbm_end_update (dbf)
  63.      gdbm_file_info *dbf;
  64. {
  65.   int  num_bytes;    /* Return value for write. */
  66.   long file_pos;    /* Return value for lseek. */
  67.  
  68.  
  69.   /* Write the current bucket. */
  70.   if (dbf->bucket_changed)
  71.     {
  72.       _gdbm_write_bucket (dbf, dbf->cache_entry);
  73.       dbf->bucket_changed = FALSE;
  74.     }
  75.  
  76.   /* Write the other changed buckets if there are any. */
  77.   if (dbf->second_changed)
  78.     {
  79.       int index;
  80.  
  81.       for (index = 0; index < CACHE_SIZE; index++)
  82.     if (dbf->bucket_cache[index].ca_changed)
  83.       {
  84.         _gdbm_write_bucket (dbf, &dbf->bucket_cache[index]);
  85.       }
  86.       dbf->second_changed = FALSE;
  87.     }
  88.  
  89.   /* Write the directory. */
  90.   if (dbf->directory_changed)
  91.     {
  92.       file_pos = lseek (dbf->desc, dbf->header->dir, L_SET);
  93.       if (file_pos != dbf->header->dir) _gdbm_fatal (dbf, "lseek error");
  94.       num_bytes = write (dbf->desc, dbf->dir, dbf->header->dir_size);
  95.       if (num_bytes != dbf->header->dir_size)
  96.     _gdbm_fatal (dbf, "write error");
  97.       dbf->directory_changed = FALSE;
  98.       if (!dbf->header_changed) fsync (dbf->desc);
  99.     }
  100.  
  101.   /* Final write of the header. */
  102.   if (dbf->header_changed)
  103.     {
  104.       write_header (dbf);
  105.       dbf->header_changed = FALSE;
  106.     }
  107. }
  108.  
  109.  
  110. /* If a fatal error is detected, come here and exit. VAL tells which fatal
  111.    error occured. */
  112. void
  113. _gdbm_fatal (dbf, val)
  114.      gdbm_file_info *dbf;
  115.      char *val;
  116. {
  117.   if (dbf->fatal_err != NULL)
  118.     (*dbf->fatal_err) (val);
  119.   else
  120.     fprintf (stderr, "gdbm fatal: %s.\n", val);
  121.   exit (-1);
  122. }
  123.