home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / gdbmreor.c < prev    next >
C/C++ Source or Header  |  1992-01-19  |  6KB  |  218 lines

  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  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 1, 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:  phil@cs.wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.         phone:  (206) 676-3035
  27.  
  28. ************************************************************************/
  29.  
  30.  
  31. #include "gdbmdefs.h"
  32. #include "gdbmerrno.h"
  33. extern gdbm_error gdbm_errno;
  34. #include "extern.h"
  35.  
  36. #ifdef NEED_RENAME
  37.  
  38. /* Rename takes OLD_NAME and renames it as NEW_NAME.  If it can not rename
  39.    the file a non-zero value is returned.  OLD_NAME is guaranteed to
  40.    remain if it can't be renamed. It assumes NEW_NAME always exists (due
  41.    to being used in gdbm). */
  42.  
  43. static int
  44. rename (old_name, new_name)
  45.      char* old_name;
  46.      char* new_name;
  47. {
  48.   if (unlink (new_name) != 0)
  49.     return -1;
  50.  
  51.   if (link (old_name, new_name) != 0)
  52.     return -1;
  53.  
  54.   unlink (old_name);
  55.   return 0;
  56.  
  57. }
  58. #endif
  59.  
  60.  
  61.  
  62. /* Reorganize the database.  This requires creating a new file and inserting
  63.    all the elements in the old file DBF into the new file.  The new file
  64.    is then renamed to the same name as the old file and DBF is updated to
  65.    contain all the correct information about the new file.  If an error
  66.    is detected, the return value is negative.  The value zero is returned
  67.    after a successful reorganization. */
  68.  
  69. int
  70. gdbm_reorganize (dbf)
  71.      gdbm_file_info *dbf;
  72.  
  73. {
  74.   gdbm_file_info *new_dbf;        /* The new file. */
  75.   char *new_name;            /* A temporary name. */
  76.   int  len;                /* Used in new_name construction. */
  77.   int new_desc;                /* Used to avoid walking file desc */
  78.   datum key, nextkey, data;        /* For the sequential sweep. */
  79.   struct stat fileinfo;            /* Information about the file. */
  80.   int  index;                /* Use in moving the bucket cache. */
  81.  
  82.  
  83.   /* Readers can not reorganize! */
  84.   if (dbf->read_write == GDBM_READER)
  85.     {
  86.       gdbm_errno = GDBM_READER_CANT_REORGANIZE;
  87.       return -1;
  88.     }
  89.  
  90.   /* Construct new name for temporary file. */
  91.   len = strlen (dbf->name);
  92.   new_name = (char *) malloc (len + 3);
  93.   if (new_name == NULL)
  94.     {
  95.       gdbm_errno = GDBM_MALLOC_ERROR;
  96.       return -1;
  97.     }
  98.   strcpy (&new_name[0], dbf->name);
  99.   new_name[len+2] = 0;
  100.   new_name[len+1] = '#';
  101.   while ( (len > 0) && new_name[len-1] != '/')
  102.     {
  103.       new_name[len] = new_name[len-1];
  104.       len -= 1;
  105.     }
  106.   new_name[len] = '#';
  107.  
  108.   /* Get the mode for the old file and open the new database. */
  109.   fstat (dbf->desc, &fileinfo);
  110.   new_dbf = gdbm_open (new_name, dbf->header->block_size, GDBM_WRCREAT,
  111.                fileinfo.st_mode, dbf->fatal_err);
  112.  
  113.   if (new_dbf == NULL)
  114.     {
  115.       gdbm_errno = GDBM_REORGANIZE_FAILED;
  116.       free (new_name);
  117.       return -1;
  118.     }
  119.  
  120.  
  121.   /* For each item in the old database, add an entry in the new. */
  122.   key = gdbm_firstkey (dbf);
  123.  
  124.   while (key.dptr != NULL)
  125.     {
  126.       data = gdbm_fetch (dbf, key);
  127.       if (data.dptr != NULL)
  128.      {
  129.       /* Add the data to the new file. */
  130.       if (gdbm_store (new_dbf, key, data, GDBM_INSERT) != 0)
  131.         {
  132.           gdbm_close (new_dbf);
  133.           gdbm_errno = GDBM_REORGANIZE_FAILED;
  134.           unlink (new_name);
  135.           free(new_name);
  136.           return -1;
  137.         }
  138.      }
  139.       else
  140.      {
  141.       /* ERROR! Abort and don't finish reorganize. */
  142.       gdbm_close (new_dbf);
  143.       gdbm_errno = GDBM_REORGANIZE_FAILED;
  144.       unlink (new_name);
  145.       free(new_name);
  146.       return -1;
  147.      }
  148.       nextkey = gdbm_nextkey (dbf, key);
  149.       free (key.dptr);
  150.       free (data.dptr);
  151.       key = nextkey;
  152.     }
  153.  
  154.     /* Move the new file to old name. */
  155.  
  156. #ifdef OS2
  157.   if (close (new_dbf->desc)
  158.       || unlink (dbf->name)
  159.       || rename (new_name, dbf->name)
  160.       || (new_dbf->desc = open (dbf->name, O_RDWR|O_BINARY)) < 0)
  161. #else /* not OS2 */
  162.   if (rename (new_name, dbf->name) != 0)
  163. #endif /* not OS2 */
  164.     {
  165.       gdbm_errno = GDBM_REORGANIZE_FAILED;
  166.       gdbm_close (new_dbf);
  167.       unlink (new_name);
  168.       free(new_name);
  169.       return -1;
  170.     }
  171.  
  172.   /* Fix up DBF to have the correct information for the new file. */
  173.   UNLOCK_FILE(dbf);
  174.   /* close (dbf->desc); */
  175.   if ((new_desc = dup2(new_dbf->desc, dbf->desc)) < 0)
  176.     {
  177.       gdbm_close(new_dbf);
  178.       gdbm_errno = GDBM_REORGANIZE_FAILED;
  179.       unlink (new_name);
  180.       free(new_name);
  181.       return -1;
  182.     }
  183.   dbf->desc = new_desc;
  184.   free (dbf->header);
  185.   free (dbf->dir);
  186.   for (index = 0; index < CACHE_SIZE; index++)
  187.     {
  188.       if (dbf->bucket_cache[index].ca_bucket != NULL)
  189.     free (dbf->bucket_cache[index].ca_bucket);
  190.       if (dbf->bucket_cache[index].ca_data.dptr != NULL)
  191.     free (dbf->bucket_cache[index].ca_data.dptr);
  192.     }
  193.  
  194.   /* dbf->desc           = new_dbf->desc; */
  195.   dbf->header         = new_dbf->header;
  196.   dbf->dir            = new_dbf->dir;
  197.   dbf->bucket         = new_dbf->bucket;
  198.   dbf->bucket_dir     = new_dbf->bucket_dir;
  199.   /* dbf->cache_entry    = new_dbf->cache_entry; */
  200.   dbf->cache_entry    = &dbf->bucket_cache[new_dbf->last_read];
  201.   dbf->last_read      = new_dbf->last_read;
  202.   for (index = 0; index < CACHE_SIZE; index++)
  203.     dbf->bucket_cache[index] = new_dbf->bucket_cache[index];
  204.   dbf->header_changed    = new_dbf->header_changed;
  205.   dbf->directory_changed = new_dbf->directory_changed;
  206.   dbf->bucket_changed    = new_dbf->bucket_changed;
  207.   dbf->second_changed    = new_dbf->second_changed;
  208.  
  209.   free (new_dbf);
  210.  
  211.   /* Make sure the new database is all on disk. */
  212.   fsync (dbf->desc);
  213.  
  214.   free(new_name);
  215.  
  216.   return 0;
  217. }
  218.