1 /* gdbmstore.c - Add a new key/data pair to the database. */
3 /* This file is part of GDBM, the GNU data base manager.
4 Copyright (C) 1990, 1991, 1993, 2007, 2011, 2013 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. */
26 /* Add a new element to the database. CONTENT is keyed by KEY. The
27 file on disk is updated to reflect the structure of the new database
28 before returning from this procedure. The FLAGS define the action to
29 take when the KEY is already in the database. The value GDBM_REPLACE
30 asks that the old data be replaced by the new CONTENT. The value
31 GDBM_INSERT asks that an error be returned and no action taken. A
32 return value of 0 means no errors. A return value of -1 means that
33 the item was not stored in the data base because the caller was not an
34 official writer. A return value of 0 means that the item was not stored
35 because the argument FLAGS was GDBM_INSERT and the KEY was already in
39 gdbm_store (GDBM_FILE dbf, datum key, datum content, int flags)
41 int new_hash_val; /* The new hash value. */
42 int elem_loc; /* The location in hash bucket. */
43 off_t file_adr; /* The address of new space in the file. */
44 off_t file_pos; /* The position after a lseek. */
45 off_t free_adr; /* For keeping track of a freed section. */
47 int new_size; /* Used in allocating space. */
48 char *temp; /* Used in _gdbm_findkey call. */
51 /* First check to make sure this guy is a writer. */
52 if (dbf->read_write == GDBM_READER)
54 gdbm_errno = GDBM_READER_CANT_STORE;
58 /* Check for illegal data values. A NULL dptr field is illegal because
59 NULL dptr returned by a lookup procedure indicates an error. */
60 if ((key.dptr == NULL) || (content.dptr == NULL))
62 gdbm_errno = GDBM_ILLEGAL_DATA;
66 /* Initialize the gdbm_errno variable. */
67 gdbm_errno = GDBM_NO_ERROR;
69 /* Look for the key in the file.
70 A side effect loads the correct bucket and calculates the hash value. */
71 elem_loc = _gdbm_findkey (dbf, key, &temp, &new_hash_val);
73 /* Initialize these. */
75 new_size = key.dsize + content.dsize;
77 /* Did we find the item? */
80 if (flags == GDBM_REPLACE)
82 /* Just replace the data. */
83 free_adr = dbf->bucket->h_table[elem_loc].data_pointer;
84 free_size = dbf->bucket->h_table[elem_loc].key_size
85 + dbf->bucket->h_table[elem_loc].data_size;
86 if (free_size != new_size)
88 _gdbm_free (dbf, free_adr, free_size);
92 /* Just reuse the same address! */
98 gdbm_errno = GDBM_CANNOT_REPLACE;
104 /* Get the file address for the new space.
105 (Current bucket's free space is first place to look.) */
107 file_adr = _gdbm_alloc (dbf, new_size);
109 /* If this is a new entry in the bucket, we need to do special things. */
112 if (dbf->bucket->count == dbf->header->bucket_elems)
114 /* Split the current bucket. */
115 _gdbm_split_bucket (dbf, new_hash_val);
118 /* Find space to insert into bucket and set elem_loc to that place. */
119 elem_loc = new_hash_val % dbf->header->bucket_elems;
120 while (dbf->bucket->h_table[elem_loc].hash_value != -1)
121 elem_loc = (elem_loc + 1) % dbf->header->bucket_elems;
123 /* We now have another element in the bucket. Add the new information.*/
124 dbf->bucket->count++;
125 dbf->bucket->h_table[elem_loc].hash_value = new_hash_val;
126 memcpy (dbf->bucket->h_table[elem_loc].key_start, key.dptr,
127 (SMALL < key.dsize ? SMALL : key.dsize));
131 /* Update current bucket data pointer and sizes. */
132 dbf->bucket->h_table[elem_loc].data_pointer = file_adr;
133 dbf->bucket->h_table[elem_loc].key_size = key.dsize;
134 dbf->bucket->h_table[elem_loc].data_size = content.dsize;
136 /* Write the data to the file. */
137 file_pos = __lseek (dbf, file_adr, SEEK_SET);
138 if (file_pos != file_adr)
139 _gdbm_fatal (dbf, _("lseek error"));
140 rc = _gdbm_full_write (dbf, key.dptr, key.dsize);
142 _gdbm_fatal (dbf, gdbm_strerror (rc));
143 rc = _gdbm_full_write (dbf, content.dptr, content.dsize);
145 _gdbm_fatal (dbf, gdbm_strerror (rc));
147 /* Current bucket has changed. */
148 dbf->cache_entry->ca_changed = TRUE;
149 dbf->bucket_changed = TRUE;
151 /* Write everything that is needed to the disk. */
152 _gdbm_end_update (dbf);