File: update.c

package info (click to toggle)
gdbm 1.7.3-25
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 588 kB
  • ctags: 348
  • sloc: ansic: 3,527; makefile: 330; sh: 186; cpp: 25
file content (126 lines) | stat: -rw-r--r-- 3,709 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* update.c - The routines for updating the file to a consistent state. */

/*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
    Copyright (C) 1990, 1991, 1993  Free Software Foundation, Inc.

    GDBM is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2, or (at your option)
    any later version.

    GDBM is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GDBM; see the file COPYING.  If not, write to
    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

    You may contact the author by:
       e-mail:  phil@cs.wwu.edu
      us-mail:  Philip A. Nelson
                Computer Science Department
                Western Washington University
                Bellingham, WA 98226
       
*************************************************************************/


/* include system configuration before all else. */
#include "autoconf.h"

#include "gdbmdefs.h"

static void write_header _ARGS((gdbm_file_info *));

/* This procedure writes the header back to the file described by DBF. */

static void
write_header (dbf)
     gdbm_file_info *dbf;
{
  int  num_bytes;	/* Return value for write. */
  off_t file_pos;	/* Return value for lseek. */

  file_pos = lseek (dbf->desc, 0L, L_SET);
  if (file_pos != 0) _gdbm_fatal (dbf, "lseek error");
  num_bytes = write (dbf->desc, dbf->header, dbf->header->block_size);
  if (num_bytes != dbf->header->block_size)
    _gdbm_fatal (dbf, "write error");

  /* Wait for all output to be done. */
  if (!dbf->fast_write)
    fsync (dbf->desc);
}


/* After all changes have been made in memory, we now write them
   all to disk. */
void
_gdbm_end_update (dbf)
     gdbm_file_info *dbf;
{
  int  num_bytes;	/* Return value for write. */
  off_t file_pos;	/* Return value for lseek. */
  
  
  /* Write the current bucket. */
  if (dbf->bucket_changed && (dbf->cache_entry != NULL))
    {
      _gdbm_write_bucket (dbf, dbf->cache_entry);
      dbf->bucket_changed = FALSE;
    }

  /* Write the other changed buckets if there are any. */
  if (dbf->second_changed)
    {
      if(dbf->bucket_cache != NULL)
        {
          register int index;

          for (index = 0; index < dbf->cache_size; index++)
	    {
	      if (dbf->bucket_cache[index].ca_changed)
	        _gdbm_write_bucket (dbf, &dbf->bucket_cache[index]);
            }
        }
      dbf->second_changed = FALSE;
    }
  
  /* Write the directory. */
  if (dbf->directory_changed)
    {
      file_pos = lseek (dbf->desc, dbf->header->dir, L_SET);
      if (file_pos != dbf->header->dir) _gdbm_fatal (dbf, "lseek error");
      num_bytes = write (dbf->desc, dbf->dir, dbf->header->dir_size);
      if (num_bytes != dbf->header->dir_size)
	_gdbm_fatal (dbf, "write error");
      dbf->directory_changed = FALSE;
      if (!dbf->header_changed && !dbf->fast_write)
	fsync (dbf->desc);
    }

  /* Final write of the header. */
  if (dbf->header_changed)
    {
      write_header (dbf);
      dbf->header_changed = FALSE;
    }
}


/* If a fatal error is detected, come here and exit. VAL tells which fatal
   error occured. */
int
_gdbm_fatal (dbf, val)
     gdbm_file_info *dbf;
     char *val;
{
  if (dbf->fatal_err != NULL)
    (*dbf->fatal_err) (val);
  else
    fprintf (stderr, "gdbm fatal: %s.\n", val);
  exit (-1);
  /* NOTREACHED */
}