faefe4f9c7d971f589685bec827510c3cf1c9375
[platform/upstream/gdbm.git] / src / update.c
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.
4    Copyright (C) 1990, 1991, 1993, 2007, 2011 Free Software Foundation,
5    Inc.
6
7    GDBM is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11
12    GDBM is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GDBM. If not, see <http://www.gnu.org/licenses/>.   */
19
20 /* Include system configuration before all else. */
21 #include "autoconf.h"
22
23 #include "gdbmdefs.h"
24
25 static void write_header (GDBM_FILE);
26
27 /* This procedure writes the header back to the file described by DBF. */
28
29 static void
30 write_header (GDBM_FILE dbf)
31 {
32   off_t file_pos;       /* Return value for lseek. */
33   int rc;
34   
35   file_pos = __lseek (dbf, 0L, SEEK_SET);
36   if (file_pos != 0) _gdbm_fatal (dbf, _("lseek error"));
37   rc = _gdbm_full_write (dbf, dbf->header, dbf->header->block_size);
38   if (rc)
39     _gdbm_fatal (dbf, gdbm_strerror (rc));
40
41   /* Sync the file if fast_write is FALSE. */
42   if (dbf->fast_write == FALSE)
43     __fsync (dbf);
44 }
45
46
47 /* After all changes have been made in memory, we now write them
48    all to disk. */
49 void
50 _gdbm_end_update (GDBM_FILE dbf)
51 {
52   off_t file_pos;       /* Return value for lseek. */
53   int rc;
54   
55   /* Write the current bucket. */
56   if (dbf->bucket_changed && (dbf->cache_entry != NULL))
57     {
58       _gdbm_write_bucket (dbf, dbf->cache_entry);
59       dbf->bucket_changed = FALSE;
60     }
61
62   /* Write the other changed buckets if there are any. */
63   if (dbf->second_changed)
64     {
65       if (dbf->bucket_cache != NULL)
66         {
67           int index;
68
69           for (index = 0; index < dbf->cache_size; index++)
70             {
71               if (dbf->bucket_cache[index].ca_changed)
72                 _gdbm_write_bucket (dbf, &dbf->bucket_cache[index]);
73             }
74         }
75       dbf->second_changed = FALSE;
76     }
77   
78   /* Write the directory. */
79   if (dbf->directory_changed)
80     {
81       file_pos = __lseek (dbf, dbf->header->dir, SEEK_SET);
82       if (file_pos != dbf->header->dir) _gdbm_fatal (dbf, _("lseek error"));
83       rc = _gdbm_full_write (dbf, dbf->dir, dbf->header->dir_size);
84       if (rc)
85         _gdbm_fatal (dbf, gdbm_strerror (rc));
86       dbf->directory_changed = FALSE;
87       if (!dbf->header_changed && dbf->fast_write == FALSE)
88         __fsync (dbf);
89     }
90
91   /* Final write of the header. */
92   if (dbf->header_changed)
93     {
94       write_header (dbf);
95       dbf->header_changed = FALSE;
96     }
97 }
98
99
100 /* If a fatal error is detected, come here and exit. VAL tells which fatal
101    error occured. */
102
103 void
104 _gdbm_fatal (GDBM_FILE dbf, const char *val)
105 {
106   if ((dbf != NULL) && (dbf->fatal_err != NULL))
107     (*dbf->fatal_err) (val);
108   else
109     {
110       fprintf (stderr, _("gdbm fatal: %s\n"), val ? val : "");
111     }
112   exit (1);
113   /* NOTREACHED */
114 }