cleanup spec
[platform/upstream/gdbm.git] / src / gdbmdelete.c
1 /* gdbmdelete.c - Remove the key and its associated data from 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 /* Remove the KEYed item and the KEY from the database DBF.  The file on disk
26    is updated to reflect the structure of the new database before returning
27    from this procedure.  */
28
29 int
30 gdbm_delete (GDBM_FILE dbf, datum key)
31 {
32   int elem_loc;         /* The location in the current hash bucket. */
33   int last_loc;         /* Last location emptied by the delete.  */
34   int home;             /* Home position of an item. */
35   bucket_element elem;  /* The element to be deleted. */
36   char *find_data;      /* Return pointer from findkey. */
37   int   hash_val;       /* Returned by findkey. */
38   off_t free_adr;       /* Temporary stroage for address and size. */
39   int   free_size;
40
41   /* First check to make sure this guy is a writer. */
42   if (dbf->read_write == GDBM_READER)
43     {
44       gdbm_errno = GDBM_READER_CANT_DELETE;
45       return -1;
46     }
47
48   /* Initialize the gdbm_errno variable. */
49   gdbm_errno = GDBM_NO_ERROR;
50
51   /* Find the item. */
52   elem_loc = _gdbm_findkey (dbf, key, &find_data, &hash_val);
53   if (elem_loc == -1)
54     {
55       gdbm_errno = GDBM_ITEM_NOT_FOUND;
56       return -1;
57     }
58
59   /* Save the element.  */
60   elem = dbf->bucket->h_table[elem_loc];
61
62   /* Delete the element.  */
63   dbf->bucket->h_table[elem_loc].hash_value = -1;
64   dbf->bucket->count--;
65
66   /* Move other elements to guarantee that they can be found. */
67   last_loc = elem_loc;
68   elem_loc = (elem_loc + 1) % dbf->header->bucket_elems;
69   while (elem_loc != last_loc
70          && dbf->bucket->h_table[elem_loc].hash_value != -1)
71     {
72       home = dbf->bucket->h_table[elem_loc].hash_value
73              % dbf->header->bucket_elems;
74       if ( (last_loc < elem_loc && (home <= last_loc || home > elem_loc))
75           || (last_loc > elem_loc && home <= last_loc && home > elem_loc))
76         
77         {
78           dbf->bucket->h_table[last_loc] = dbf->bucket->h_table[elem_loc];
79           dbf->bucket->h_table[elem_loc].hash_value = -1;
80           last_loc = elem_loc;
81         }
82       elem_loc = (elem_loc + 1) % dbf->header->bucket_elems;
83     }
84
85   /* Free the file space. */
86   free_adr = elem.data_pointer;
87   free_size = elem.key_size + elem.data_size;
88   _gdbm_free (dbf, free_adr, free_size);
89
90   /* Set the flags. */
91   dbf->bucket_changed = TRUE;
92
93   /* Clear out the data cache for the current bucket. */
94   if (dbf->cache_entry->ca_data.dptr != NULL)
95     {
96       free (dbf->cache_entry->ca_data.dptr);
97       dbf->cache_entry->ca_data.dptr = NULL;
98     }
99   dbf->cache_entry->ca_data.hash_val = -1;
100   dbf->cache_entry->ca_data.key_size = 0;
101   dbf->cache_entry->ca_data.elem_loc = -1;
102
103   /* Do the writes. */
104   _gdbm_end_update (dbf);
105   return 0;  
106 }