source: main/trunk/greenstone2/common-src/packages/gdbm/gdbm-1.8.3/gdbmreorg.c@ 22259

Last change on this file since 22259 was 22259, checked in by davidb, 14 years ago

Tie Makefile and source code in with -DMSDOS so MinGW/MSys compilation works with GDBM

File size: 6.5 KB
Line 
1/* gdbmreorg.c - Reorganize the database file. */
2
3/* This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
4 Copyright (C) 1990, 1991, 1993 Free Software Foundation, Inc.
5
6 GDBM is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GDBM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GDBM; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 You may contact the author by:
21 e-mail: [email protected]
22 us-mail: Philip A. Nelson
23 Computer Science Department
24 Western Washington University
25 Bellingham, WA 98226
26
27************************************************************************/
28
29
30/* include system configuration before all else. */
31#include "autoconf.h"
32
33#include "gdbmdefs.h"
34#include "gdbmerrno.h"
35#include "extern.h"
36
37#if !HAVE_RENAME
38
39/* Rename takes OLD_NAME and renames it as NEW_NAME. If it can not rename
40 the file a non-zero value is returned. OLD_NAME is guaranteed to
41 remain if it can't be renamed. It assumes NEW_NAME always exists (due
42 to being used in gdbm). */
43
44static int
45_gdbm_rename (old_name, new_name)
46 char* old_name;
47 char* new_name;
48{
49 if (unlink (new_name) != 0)
50 return -1;
51
52 if (link (old_name, new_name) != 0)
53 return -1;
54
55 unlink (old_name);
56 return 0;
57
58}
59
60#define rename _gdbm_rename
61#endif
62
63
64
65/* Reorganize the database. This requires creating a new file and inserting
66 all the elements in the old file DBF into the new file. The new file
67 is then renamed to the same name as the old file and DBF is updated to
68 contain all the correct information about the new file. If an error
69 is detected, the return value is negative. The value zero is returned
70 after a successful reorganization. */
71
72int
73gdbm_reorganize (dbf)
74 gdbm_file_info *dbf;
75
76{
77 gdbm_file_info *new_dbf; /* The new file. */
78 char *new_name; /* A temporary name. */
79 int len; /* Used in new_name construction. */
80 datum key, nextkey, data; /* For the sequential sweep. */
81 struct stat fileinfo; /* Information about the file. */
82 int index; /* Use in moving the bucket cache. */
83
84
85 /* Readers can not reorganize! */
86 if (dbf->read_write == GDBM_READER)
87 {
88 gdbm_errno = GDBM_READER_CANT_REORGANIZE;
89 return -1;
90 }
91
92 /* Initialize the gdbm_errno variable. */
93 gdbm_errno = GDBM_NO_ERROR;
94
95 /* Construct new name for temporary file. */
96 len = strlen (dbf->name);
97 new_name = (char *) malloc (len + 3);
98 if (new_name == NULL)
99 {
100 gdbm_errno = GDBM_MALLOC_ERROR;
101 return -1;
102 }
103 strcpy (&new_name[0], dbf->name);
104 new_name[len+2] = 0;
105 new_name[len+1] = '#';
106 while ( (len > 0) && new_name[len-1] != '/')
107 {
108 new_name[len] = new_name[len-1];
109 len -= 1;
110 }
111 new_name[len] = '#';
112
113 /* Get the mode for the old file and open the new database. */
114 fstat (dbf->desc, &fileinfo);
115#ifdef MSDOS
116 /* Not clear if final argument for Windows should be to obtain a
117 file lock or not. Play it safe for now and ask for a lock
118 */
119 new_dbf = gdbm_open (new_name, dbf->header->block_size, GDBM_WRCREAT,
120 fileinfo.st_mode, dbf->fatal_err,dbf->need_filelock);
121#else
122 new_dbf = gdbm_open (new_name, dbf->header->block_size, GDBM_WRCREAT,
123 fileinfo.st_mode, dbf->fatal_err);
124#endif
125
126 if (new_dbf == NULL)
127 {
128 free (new_name);
129 gdbm_errno = GDBM_REORGANIZE_FAILED;
130 return -1;
131 }
132
133
134 /* For each item in the old database, add an entry in the new. */
135 key = gdbm_firstkey (dbf);
136
137 while (key.dptr != NULL)
138 {
139 data = gdbm_fetch (dbf, key);
140 if (data.dptr != NULL)
141 {
142 /* Add the data to the new file. */
143 if (gdbm_store (new_dbf, key, data, GDBM_INSERT) != 0)
144 {
145 gdbm_close (new_dbf);
146 gdbm_errno = GDBM_REORGANIZE_FAILED;
147 unlink (new_name);
148 free (new_name);
149 return -1;
150 }
151 }
152 else
153 {
154 /* ERROR! Abort and don't finish reorganize. */
155 gdbm_close (new_dbf);
156 gdbm_errno = GDBM_REORGANIZE_FAILED;
157 unlink (new_name);
158 free (new_name);
159 return -1;
160 }
161 nextkey = gdbm_nextkey (dbf, key);
162 free (key.dptr);
163 free (data.dptr);
164 key = nextkey;
165 }
166
167 /* Write everything. */
168 _gdbm_end_update (new_dbf);
169 gdbm_sync (new_dbf);
170
171
172 /* Move the new file to old name. */
173
174#ifdef MSDOS
175 if (close (new_dbf->desc)
176 || unlink (dbf->name)
177 || rename (new_name, dbf->name)
178 || (new_dbf->desc = open (dbf->name, O_RDWR|O_BINARY)) < 0)
179#else /* not MSDOS */
180 if (rename (new_name, dbf->name) != 0)
181#endif /* not MSDOS */
182 {
183 gdbm_errno = GDBM_REORGANIZE_FAILED;
184 gdbm_close (new_dbf);
185 free (new_name);
186 return -1;
187 }
188
189 /* Fix up DBF to have the correct information for the new file. */
190 if (dbf->file_locking)
191 {
192#ifdef MSDOS
193 if (dbf->need_filelock) {
194 UNLOCK_FILE(dbf);
195 }
196#else
197 UNLOCK_FILE(dbf);
198#endif
199 }
200 close (dbf->desc);
201 free (dbf->header);
202 free (dbf->dir);
203
204 if (dbf->bucket_cache != NULL) {
205 for (index = 0; index < dbf->cache_size; index++) {
206 if (dbf->bucket_cache[index].ca_bucket != NULL)
207 free (dbf->bucket_cache[index].ca_bucket);
208 if (dbf->bucket_cache[index].ca_data.dptr != NULL)
209 free (dbf->bucket_cache[index].ca_data.dptr);
210 }
211 free (dbf->bucket_cache);
212 }
213
214 dbf->desc = new_dbf->desc;
215 dbf->header = new_dbf->header;
216 dbf->dir = new_dbf->dir;
217 dbf->bucket = new_dbf->bucket;
218 dbf->bucket_dir = new_dbf->bucket_dir;
219 dbf->last_read = new_dbf->last_read;
220 dbf->bucket_cache = new_dbf->bucket_cache;
221 dbf->cache_size = new_dbf->cache_size;
222 dbf->header_changed = new_dbf->header_changed;
223 dbf->directory_changed = new_dbf->directory_changed;
224 dbf->bucket_changed = new_dbf->bucket_changed;
225 dbf->second_changed = new_dbf->second_changed;
226
227 free (new_dbf->name);
228 free (new_dbf);
229 free (new_name);
230
231 /* Make sure the new database is all on disk. */
232 fsync (dbf->desc);
233
234 /* Force the right stuff for a correct bucket cache. */
235 dbf->cache_entry = &dbf->bucket_cache[0];
236 _gdbm_get_bucket (dbf, 0);
237
238 return 0;
239}
Note: See TracBrowser for help on using the repository browser.