home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 1 / FFMCD01.bin / useful / dist / gnu / gdbm / gdbm-1.6-amiga / update.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-19  |  3.3 KB  |  119 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, 1993  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 2, 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.        
  27. *************************************************************************/
  28.  
  29.  
  30. #include "gdbmdefs.h"
  31.  
  32.  
  33. /* This procedure writes the header back to the file described by DBF. */
  34.  
  35. static 
  36. write_header (dbf)
  37.      gdbm_file_info *dbf;
  38. {
  39.   int  num_bytes;    /* Return value for write. */
  40.   long file_pos;    /* Return value for lseek. */
  41.  
  42.   file_pos = lseek (dbf->desc, 0, L_SET);
  43.   if (file_pos != 0) _gdbm_fatal (dbf, "lseek error");
  44.   num_bytes = write (dbf->desc, dbf->header, dbf->header->block_size);
  45.   if (num_bytes != dbf->header->block_size)
  46.     _gdbm_fatal (dbf, "write error");
  47.  
  48.   /* Wait for all output to be done. */
  49.   if (!dbf->fast_write)
  50.     fsync (dbf->desc);
  51. }
  52.  
  53.  
  54. /* After all changes have been made in memory, we now write them
  55.    all to disk. */
  56. void
  57. _gdbm_end_update (dbf)
  58.      gdbm_file_info *dbf;
  59. {
  60.   int  num_bytes;    /* Return value for write. */
  61.   long file_pos;    /* Return value for lseek. */
  62.   
  63.   
  64.   /* Write the current bucket. */
  65.   if (dbf->bucket_changed)
  66.     {
  67.       _gdbm_write_bucket (dbf, dbf->cache_entry);
  68.       dbf->bucket_changed = FALSE;
  69.     }
  70.  
  71.   /* Write the other changed buckets if there are any. */
  72.   if (dbf->second_changed)
  73.     {
  74.       int index;
  75.  
  76.       for (index = 0; index < CACHE_SIZE; index++)
  77.     if (dbf->bucket_cache[index].ca_changed)
  78.       {
  79.         _gdbm_write_bucket (dbf, &dbf->bucket_cache[index]);
  80.       }
  81.       dbf->second_changed = FALSE;
  82.     }
  83.   
  84.   /* Write the directory. */
  85.   if (dbf->directory_changed)
  86.     {
  87.       file_pos = lseek (dbf->desc, dbf->header->dir, L_SET);
  88.       if (file_pos != dbf->header->dir) _gdbm_fatal (dbf, "lseek error");
  89.       num_bytes = write (dbf->desc, dbf->dir, dbf->header->dir_size);
  90.       if (num_bytes != dbf->header->dir_size)
  91.     _gdbm_fatal (dbf, "write error");
  92.       dbf->directory_changed = FALSE;
  93.       if (!dbf->header_changed && !dbf->fast_write)
  94.     fsync (dbf->desc);
  95.     }
  96.  
  97.   /* Final write of the header. */
  98.   if (dbf->header_changed)
  99.     {
  100.       write_header (dbf);
  101.       dbf->header_changed = FALSE;
  102.     }
  103. }
  104.  
  105.  
  106. /* If a fatal error is detected, come here and exit. VAL tells which fatal
  107.    error occured. */
  108. _gdbm_fatal (dbf, val)
  109.      gdbm_file_info *dbf;
  110.      char *val;
  111. {
  112.   if (dbf->fatal_err != NULL)
  113.     (*dbf->fatal_err) (val);
  114.   else
  115.     fprintf (stderr, "gdbm fatal: %s.\n", val);
  116.   exit (-1);
  117. }
  118.  
  119.