1 /* update.c - The routines for updating the file to a consistent state. */
3 /* This file is part of GDBM, the GNU data base manager.
4 Copyright (C) 1990, 1991, 1993, 2007, 2011 Free Software Foundation,
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)
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.
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/>. */
20 /* Include system configuration before all else. */
25 static void write_header (GDBM_FILE);
27 /* This procedure writes the header back to the file described by DBF. */
30 write_header (GDBM_FILE dbf)
32 off_t file_pos; /* Return value for lseek. */
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);
39 _gdbm_fatal (dbf, gdbm_strerror (rc));
41 /* Sync the file if fast_write is FALSE. */
42 if (dbf->fast_write == FALSE)
47 /* After all changes have been made in memory, we now write them
50 _gdbm_end_update (GDBM_FILE dbf)
52 off_t file_pos; /* Return value for lseek. */
55 /* Write the current bucket. */
56 if (dbf->bucket_changed && (dbf->cache_entry != NULL))
58 _gdbm_write_bucket (dbf, dbf->cache_entry);
59 dbf->bucket_changed = FALSE;
62 /* Write the other changed buckets if there are any. */
63 if (dbf->second_changed)
65 if (dbf->bucket_cache != NULL)
69 for (index = 0; index < dbf->cache_size; index++)
71 if (dbf->bucket_cache[index].ca_changed)
72 _gdbm_write_bucket (dbf, &dbf->bucket_cache[index]);
75 dbf->second_changed = FALSE;
78 /* Write the directory. */
79 if (dbf->directory_changed)
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);
85 _gdbm_fatal (dbf, gdbm_strerror (rc));
86 dbf->directory_changed = FALSE;
87 if (!dbf->header_changed && dbf->fast_write == FALSE)
91 /* Final write of the header. */
92 if (dbf->header_changed)
95 dbf->header_changed = FALSE;
100 /* If a fatal error is detected, come here and exit. VAL tells which fatal
104 _gdbm_fatal (GDBM_FILE dbf, const char *val)
106 if ((dbf != NULL) && (dbf->fatal_err != NULL))
107 (*dbf->fatal_err) (val);
110 fprintf (stderr, _("gdbm fatal: %s\n"), val ? val : "");