add changelog
[platform/upstream/gdbm.git] / src / gdbmstore.c
1 /* gdbmstore.c - Add a new key/data pair to the database. */
2
3 /* This file is part of GDBM, the GNU data base manager.
4    Copyright (C) 1990, 1991, 1993, 2007, 2011, 2013 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
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
36    the database. */
37
38 int
39 gdbm_store (GDBM_FILE dbf, datum key, datum content, int flags)
40 {
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. */
46   int  free_size;
47   int   new_size;               /* Used in allocating space. */
48   char *temp;                   /* Used in _gdbm_findkey call. */
49   int rc;
50
51   /* First check to make sure this guy is a writer. */
52   if (dbf->read_write == GDBM_READER)
53     {
54       gdbm_errno = GDBM_READER_CANT_STORE;
55       return -1;
56     }
57
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))
61     {
62       gdbm_errno = GDBM_ILLEGAL_DATA;
63       return -1;
64     }
65
66   /* Initialize the gdbm_errno variable. */
67   gdbm_errno = GDBM_NO_ERROR;
68
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);
72
73   /* Initialize these. */
74   file_adr = 0;
75   new_size = key.dsize + content.dsize;
76
77   /* Did we find the item? */
78   if (elem_loc != -1)
79     {
80       if (flags == GDBM_REPLACE)
81         {
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)
87             {
88               _gdbm_free (dbf, free_adr, free_size);
89             }
90           else
91             {
92               /* Just reuse the same address! */
93               file_adr = free_adr;
94             }
95         }
96       else
97         {
98           gdbm_errno = GDBM_CANNOT_REPLACE;
99           return 1;
100         }
101     }
102
103
104   /* Get the file address for the new space.
105      (Current bucket's free space is first place to look.) */
106   if (file_adr == 0)
107     file_adr = _gdbm_alloc (dbf, new_size);
108
109   /* If this is a new entry in the bucket, we need to do special things. */
110   if (elem_loc == -1)
111     {
112       if (dbf->bucket->count == dbf->header->bucket_elems)
113         {
114           /* Split the current bucket. */
115           _gdbm_split_bucket (dbf, new_hash_val);
116         }
117       
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;
122
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));
128     }
129
130
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;
135
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);
141   if (rc)
142     _gdbm_fatal (dbf, gdbm_strerror (rc));
143   rc = _gdbm_full_write (dbf, content.dptr, content.dsize);
144   if (rc)
145     _gdbm_fatal (dbf, gdbm_strerror (rc));
146
147   /* Current bucket has changed. */
148   dbf->cache_entry->ca_changed = TRUE;
149   dbf->bucket_changed = TRUE;
150
151   /* Write everything that is needed to the disk. */
152   _gdbm_end_update (dbf);
153   return 0;
154   
155 }