summaryrefslogtreecommitdiffabout
path: root/src/recover.c
blob: 7cc20a27080ddd2b383196ae38e410c0b52d65cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* This file is part of GDBM, the GNU data base manager.
   Copyright (C) 2016-2018 Free Software Foundation, Inc.

   GDBM is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GDBM is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GDBM. If not, see <http://www.gnu.org/licenses/>.   */

#include "autoconf.h"
#include "gdbmdefs.h"

#define TMPSUF ".XXXXXX"

int
gdbm_copy_meta (GDBM_FILE dst, GDBM_FILE src)
{
  struct stat st;

  if (fstat (src->desc, &st))
    {
      GDBM_SET_ERRNO (src, GDBM_FILE_STAT_ERROR, src->need_recovery);
      return -1;
    }
  if (fchown (dst->desc, st.st_uid, st.st_gid))
    {
      GDBM_SET_ERRNO (dst, GDBM_ERR_FILE_OWNER, dst->need_recovery);
      return -1;
    }
  if (fchmod (dst->desc, st.st_mode & 0777))
    {
      GDBM_SET_ERRNO (dst, GDBM_ERR_FILE_MODE, dst->need_recovery);
      return -1;
    }
  return 0;
}

static char *
backup_name (char const *name)
{
  char *buf;
  size_t len;
  size_t suf_pos;
  size_t suf_len;
  
#define INITIAL_SUFFIX ".~1~"
  
  len = strlen (name) + sizeof (INITIAL_SUFFIX);
  buf = malloc (len);
  if (!buf)
    return NULL;
  strcpy (buf, name);
  suf_pos = strlen (buf) + 2; 
  suf_len = 1;
  strcat (buf, INITIAL_SUFFIX);

  while (access (buf, F_OK) == 0)
    {
      size_t i = suf_len;
      while (buf[suf_pos + i - 1] == '9')
	{
	  buf[suf_pos + i - 1] = '0';
	  i--;
	  if (i == 0)
	    {
	      char *p = realloc (buf, ++len);
	      if (!p)
		{
		  SAVE_ERRNO (free (buf));
		  return NULL;
		}
	      memmove (p + suf_pos + 1, p + suf_pos, suf_len + 2);
	      buf = p;
	      suf_len++;
	      i++;
	    }
	}
      ++buf[suf_pos + i - 1];
    }
  return buf;
}  

static int
_gdbm_finish_transfer (GDBM_FILE dbf, GDBM_FILE new_dbf,
		       gdbm_recovery *rcvr, int flags)
{
  int i;
  
  /* Write everything. */
  if (_gdbm_end_update (new_dbf))
    {
      gdbm_close (new_dbf);
      return -1;
    }
  gdbm_sync (new_dbf);

  if (gdbm_copy_meta (new_dbf, dbf))
    {
      gdbm_close (new_dbf);
      return -1;
    }
  
#if HAVE_MMAP
  _gdbm_mapped_unmap (dbf);
#endif

  if (flags & GDBM_RCVR_BACKUP)
    {
      char *bkname = backup_name (dbf->name);
      if (!bkname)
	{
	  SAVE_ERRNO (gdbm_close (new_dbf));
	  GDBM_SET_ERRNO (NULL, GDBM_BACKUP_FAILED, FALSE);
	  return -1;
	}
      if (rename (dbf->name, bkname) != 0)
	{
	  SAVE_ERRNO (gdbm_close (new_dbf); free (bkname));
	  GDBM_SET_ERRNO (NULL, GDBM_BACKUP_FAILED, FALSE);
	  return -1;
	}
      rcvr->backup_name = bkname;
    }
  
  /* Move the new file to old name. */

  if (rename (new_dbf->name, dbf->name) != 0)
    {
      GDBM_SET_ERRNO (NULL, GDBM_REORGANIZE_FAILED, FALSE);
      gdbm_close (new_dbf);
      return -1;
    }

  /* Fix up DBF to have the correct information for the new file. */
  if (dbf->file_locking)
    _gdbm_unlock_file (dbf);
  close (dbf->desc);
  free (dbf->header);
  free (dbf->dir);

  if (dbf->bucket_cache != NULL)
    {
      for (i = 0; i < dbf->cache_size; i++)
	 {
	   free (dbf->bucket_cache[i].ca_bucket);
	   free (dbf->bucket_cache[i].ca_data.dptr);
	 }
      free (dbf->bucket_cache);
   }

   dbf->desc              = new_dbf->desc;
   dbf->header            = new_dbf->header;
   dbf->dir               = new_dbf->dir;
   dbf->bucket            = new_dbf->bucket;
   dbf->bucket_dir        = new_dbf->bucket_dir;
   dbf->last_read         = new_dbf->last_read;
   dbf->bucket_cache      = new_dbf->bucket_cache;
   dbf->cache_size        = new_dbf->cache_size;
   dbf->header_changed    = new_dbf->header_changed;
   dbf->directory_changed = new_dbf->directory_changed;
   dbf->bucket_changed    = new_dbf->bucket_changed;
   dbf->second_changed    = new_dbf->second_changed;

   free (new_dbf->name);
   free (new_dbf);
   
 #if HAVE_MMAP
   /* Re-initialize mapping if required */
   if (dbf->memory_mapping)
     _gdbm_mapped_init (dbf);
 #endif

   /* Make sure the new database is all on disk. */
   gdbm_file_sync (dbf);

   /* Force the right stuff for a correct bucket cache. */
   dbf->cache_entry    = &dbf->bucket_cache[0];
   return _gdbm_get_bucket (dbf, 0);
 }

int
_gdbm_next_bucket_dir (GDBM_FILE dbf, int bucket_dir)
{
  int dir_count = GDBM_DIR_COUNT (dbf);
  if (bucket_dir < 0 || bucket_dir >= dir_count)
    bucket_dir = dir_count;
  else
    {
      off_t cur = dbf->dir[bucket_dir];
      while (++bucket_dir < dir_count && cur == dbf->dir[bucket_dir])
	;
    }
  return bucket_dir;
}

static int
check_db (GDBM_FILE dbf)
{
  int bucket_dir, i;
  int nbuckets = GDBM_DIR_COUNT (dbf);

  if (_gdbm_validate_header (dbf))
    return 1;
  for (bucket_dir = 0; bucket_dir < nbuckets;
       bucket_dir = _gdbm_next_bucket_dir (dbf, bucket_dir))
    {      
      if (_gdbm_get_bucket (dbf, bucket_dir))
	return 1;
      else
	{
	  if (dbf->bucket->count < 0
	      || dbf->bucket->count > dbf->header->bucket_elems)
	    return 1;
	  for (i = 0; i < dbf->header->bucket_elems; i++)
	    {
	      char *dptr;
	      datum key;
	      int hashval, bucket, off;

	      if (dbf->bucket->h_table[i].hash_value == -1)
		continue;
	      dptr = _gdbm_read_entry (dbf, i);
	      if (!dptr)
		return 1;
	      
	      key.dptr   = dptr;
	      key.dsize  = dbf->bucket->h_table[i].key_size;

	      if (memcmp (dbf->bucket->h_table[i].key_start, key.dptr,
			  (SMALL < key.dsize ? SMALL : key.dsize)))
		return 1;
	      
	      _gdbm_hash_key (dbf, key, &hashval, &bucket, &off);
	      if (bucket >= nbuckets)
		return 1;
	      if (hashval != dbf->bucket->h_table[i].hash_value)
		return 1;
	      if (dbf->dir[bucket] != dbf->dir[bucket_dir])
		return 1;
	    }
	}
    }
  return 0;
}

static int
run_recovery (GDBM_FILE dbf, GDBM_FILE new_dbf, gdbm_recovery *rcvr, int flags)
{
  int bucket_dir, i;
  int nbuckets = GDBM_DIR_COUNT (dbf);

  for (bucket_dir = 0; bucket_dir < nbuckets;
       bucket_dir = _gdbm_next_bucket_dir (dbf, bucket_dir))
    {
      
      if (_gdbm_get_bucket (dbf, bucket_dir))
	{
	  if (flags & GDBM_RCVR_ERRFUN)
	    rcvr->errfun (rcvr->data, _("can't read bucket #%d: %s"),
			  bucket_dir,
			  gdbm_db_strerror (dbf));
	  rcvr->failed_buckets++;
	  if ((flags & GDBM_RCVR_MAX_FAILED_BUCKETS)
	      && rcvr->failed_buckets == rcvr->max_failed_buckets)
	    return -1;
	  if ((flags & GDBM_RCVR_MAX_FAILURES)
	      && (rcvr->failed_buckets + rcvr->failed_keys) == rcvr->max_failures)
	    return -1;
	}
      else
	{
	  rcvr->recovered_buckets++;
	  for (i = 0; i < dbf->header->bucket_elems; i++)
	    {
	      char *dptr;
	      datum key, data;
	    
	      if (dbf->bucket->h_table[i].hash_value == -1)
		continue;
	      dptr = _gdbm_read_entry (dbf, i);
	      if (dptr)
		rcvr->recovered_keys++;
	      else
		{
		  if (flags & GDBM_RCVR_ERRFUN)
		    rcvr->errfun (rcvr->data,
				  _("can't read key pair %d:%d (%lu:%d): %s"),
				  bucket_dir, i,
				  (unsigned long) dbf->bucket->h_table[i].data_pointer,
				  dbf->bucket->h_table[i].key_size
				    + dbf->bucket->h_table[i].data_size,
				  gdbm_db_strerror (dbf));
		  rcvr->failed_keys++;
		  if ((flags & GDBM_RCVR_MAX_FAILED_KEYS)
		      && rcvr->failed_keys == rcvr->max_failed_keys)
		    return -1;
		  if ((flags & GDBM_RCVR_MAX_FAILURES)
		      && (rcvr->failed_buckets + rcvr->failed_keys) == rcvr->max_failures)
		    return -1;
		  continue;
		}

	      key.dptr   = dptr;
	      key.dsize  = dbf->bucket->h_table[i].key_size;

	      data.dptr  = dptr + key.dsize;
	      data.dsize = dbf->bucket->h_table[i].data_size;
	    
	      if (gdbm_store (new_dbf, key, data, GDBM_INSERT) != 0)
		{
		  switch (gdbm_last_errno (new_dbf))
		    {
		    case GDBM_CANNOT_REPLACE:
		      rcvr->duplicate_keys++;
		      if (flags & GDBM_RCVR_ERRFUN)
			rcvr->errfun (rcvr->data,
		          _("ignoring duplicate key %d:%d (%lu:%d)"),
			  bucket_dir, i,
			  (unsigned long) dbf->bucket->h_table[i].data_pointer,
			  dbf->bucket->h_table[i].key_size
				      + dbf->bucket->h_table[i].data_size);
		      break;
		      
		    default:
		      if (flags & GDBM_RCVR_ERRFUN)
			rcvr->errfun (rcvr->data,
			  _("fatal: can't store element %d:%d (%lu:%d): %s"),
			  bucket_dir, i,
			  (unsigned long) dbf->bucket->h_table[i].data_pointer,
			  dbf->bucket->h_table[i].key_size
				    + dbf->bucket->h_table[i].data_size,
			  gdbm_db_strerror (new_dbf));
		      return -1;
		    }
		}	
	    }
	}
    }
  
  return 0;
}

int
gdbm_recover (GDBM_FILE dbf, gdbm_recovery *rcvr, int flags)
{ 
  GDBM_FILE new_dbf;	     /* The new file. */
  char *new_name;	     /* A temporary name. */
  size_t len;
  int fd;
  int rc;
  gdbm_recovery rs;
  
  /* Readers can not reorganize! */
  if (dbf->read_write == GDBM_READER)
    {
      GDBM_SET_ERRNO (dbf, GDBM_READER_CANT_REORGANIZE, dbf->need_recovery);
      return -1;
    }

  /* Initialize gdbm_recovery structure */
  if (!rcvr)
    {
      rcvr  = &rs;
      flags = 0;
    }
  rcvr->recovered_keys = 0;
  rcvr->recovered_buckets = 0;
  rcvr->failed_keys = 0;
  rcvr->failed_buckets = 0;
  rcvr->duplicate_keys = 0;
  rcvr->backup_name = NULL;

  rc = 0;
  if ((flags & GDBM_RCVR_FORCE) || check_db (dbf))
    {
      gdbm_clear_error (dbf);
      len = strlen (dbf->name);
      new_name = malloc (len + sizeof (TMPSUF));
      if (!new_name)
	{
	  GDBM_SET_ERRNO (NULL, GDBM_MALLOC_ERROR, FALSE);
	  return -1;
	}
      strcat (strcpy (new_name, dbf->name), TMPSUF);
  
      fd = mkstemp (new_name);
      if (fd == -1)
	{
	  GDBM_SET_ERRNO (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
	  free (new_name);
	  return -1;
	}
  
      new_dbf = gdbm_fd_open (fd, new_name, dbf->header->block_size,
			      GDBM_WRCREAT
			      | (dbf->cloexec ? GDBM_CLOEXEC : 0)
			      | GDBM_CLOERROR, dbf->fatal_err);
  
      SAVE_ERRNO (free (new_name));
  
      if (new_dbf == NULL)
	{
	  GDBM_SET_ERRNO (NULL, GDBM_REORGANIZE_FAILED, FALSE);
	  return -1;
	}

      rc = run_recovery (dbf, new_dbf, rcvr, flags);
      
      if (rc == 0)
	rc = _gdbm_finish_transfer (dbf, new_dbf, rcvr, flags);
      else
	gdbm_close (new_dbf);
    }

  if (rc == 0)
    {
      gdbm_clear_error (dbf);
      dbf->need_recovery = FALSE;
    }

  return rc;
}

Return to:

Send suggestions and report system problems to the System administrator.