home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / GDBM14AS.ZIP / GDBMREOR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-22  |  6.4 KB  |  225 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  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@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.  * MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  32.  *
  33.  * To this port, the same copying conditions apply as to the
  34.  * original release.
  35.  *
  36.  * IMPORTANT:
  37.  * This file is not identical to the original GNU release!
  38.  * You should have received this code as patch to the official
  39.  * GNU release.
  40.  *
  41.  * MORE IMPORTANT:
  42.  * This port comes with ABSOLUTELY NO WARRANTY.
  43.  *
  44.  * $Header: e:/gnu/gdbm/RCS/gdbmreor.c'v 1.4.0.1 90/08/16 09:22:31 tho Exp $
  45.  */
  46.  
  47. #include <stdio.h>
  48. #include <sys/types.h>
  49. #ifndef MSDOS
  50. #include <sys/file.h>
  51. #endif /* not MSDOS */
  52. #include <sys/stat.h>
  53. #include "gdbmdefs.h"
  54. #include "systems.h"
  55. #include "gdbmerrno.h"
  56. extern gdbm_error gdbm_errno;
  57. #include "extern.h"
  58.  
  59. #ifdef NEED_RENAME
  60.  
  61. /* Rename takes OLD_NAME and renames it as NEW_NAME.  If it can not rename
  62.    the file a non-zero value is returned.  OLD_NAME is guaranteed to
  63.    remain if it can't be renamed. It assumes NEW_NAME always exists (due
  64.    to being used in gdbm). */
  65.  
  66. static int
  67. rename (old_name, new_name)
  68.      char* old_name;
  69.      char* new_name;
  70. {
  71.   if (unlink (new_name) != 0)   
  72.     return -1;
  73.  
  74.   if (link (old_name, new_name) != 0)
  75.     return -1;
  76.   
  77.   unlink (old_name);
  78.   return 0;
  79.  
  80. }
  81. #endif
  82.  
  83.  
  84.  
  85. /* Reorganize the database.  This requires creating a new file and inserting
  86.    all the elements in the old file DBF into the new file.  The new file
  87.    is then renamed to the same name as the old file and DBF is updated to
  88.    contain all the correct information about the new file.  If an error
  89.    is detected, the return value is negative.  The value zero is returned
  90.    after a successful reorganization. */
  91.  
  92. int
  93. gdbm_reorganize (dbf)
  94.      gdbm_file_info *dbf;
  95.  
  96. {
  97.   gdbm_file_info *new_dbf;        /* The new file. */
  98.   char *new_name;            /* A temporary name. */
  99.   int  len;                /* Used in new_name construction. */
  100.   datum key, nextkey, data;        /* For the sequential sweep. */
  101.   struct stat fileinfo;            /* Information about the file. */
  102.   int  index;                /* Use in moving the bucket cache. */
  103.  
  104.  
  105.   /* Readers can not reorganize! */
  106.   if (dbf->read_write == GDBM_READER)
  107.     {
  108.       gdbm_errno = GDBM_READER_CANT_REORGANIZE;
  109.       return -1;
  110.     }
  111.  
  112.   /* Construct new name for temporary file. */
  113.   len = strlen (dbf->name);
  114.   new_name = (char *) malloc (len + 3);
  115.   if (new_name == NULL)
  116.     {
  117.       gdbm_errno = GDBM_MALLOC_ERROR;
  118.       return -1;
  119.     }
  120.   strcpy (&new_name[0], dbf->name);
  121.   new_name[len+2] = 0;
  122.   new_name[len+1] = '#';
  123.   while ( (len > 0) && new_name[len-1] != '/')
  124.     {
  125.       new_name[len] = new_name[len-1];
  126.       len -= 1;
  127.     }
  128.   new_name[len] = '#';
  129.  
  130.   /* Get the mode for the old file and open the new database. */
  131.   fstat (dbf->desc, &fileinfo);
  132.   new_dbf = gdbm_open (new_name, dbf->header->block_size, GDBM_WRCREAT,
  133.                fileinfo.st_mode, dbf->fatal_err);
  134.  
  135.   if (new_dbf == NULL)
  136.     {
  137.       gdbm_errno = GDBM_REORGANIZE_FAILED;
  138.       free (new_name);
  139.       return -1;
  140.     }
  141.  
  142.   
  143.   /* For each item in the old database, add an entry in the new. */
  144.   key = gdbm_firstkey (dbf);
  145.  
  146.   while (key.dptr != NULL)
  147.     {
  148.       data = gdbm_fetch (dbf, key);
  149.       if (data.dptr != NULL)
  150.      {
  151.       /* Add the data to the new file. */
  152.       if (gdbm_store (new_dbf, key, data, GDBM_INSERT) != 0)
  153.         {
  154.           gdbm_close (new_dbf);
  155.           gdbm_errno = GDBM_REORGANIZE_FAILED;
  156.           unlink (new_name);
  157.           return -1;
  158.         };
  159.      }
  160.       else
  161.      {
  162.       /* ERROR! Abort and don't finish reorganize. */
  163.       gdbm_close (new_dbf);
  164.       gdbm_errno = GDBM_REORGANIZE_FAILED;
  165.       unlink (new_name);
  166.       return -1;
  167.      }
  168.       nextkey = gdbm_nextkey (dbf, key);
  169.       free (key.dptr);
  170.       key = nextkey;
  171.     }
  172.  
  173.     /* Move the new file to old name. */
  174.  
  175. #ifdef MSDOS
  176.   if (close (new_dbf->desc)
  177.       || unlink (dbf->name)
  178.       || rename (new_name, dbf->name)
  179.       || (new_dbf->desc = open (dbf->name, O_RDWR|O_BINARY)) < 0)
  180. #else /* not MSDOS */
  181.   if (rename (new_name, dbf->name) != 0)
  182. #endif /* not MSDOS */
  183.     {
  184.       gdbm_errno = GDBM_REORGANIZE_FAILED;
  185.       gdbm_close (new_dbf);
  186.       return -1;
  187.     }
  188.  
  189.   /* Fix up DBF to have the correct information for the new file. */
  190.   UNLOCK_FILE(dbf);
  191.   close (dbf->desc);
  192.   free (dbf->header);
  193.   free (dbf->dir);
  194.   for (index = 0; index < CACHE_SIZE; index++)
  195.     {
  196.       if (dbf->bucket_cache[index].ca_bucket != NULL)
  197.     free (dbf->bucket_cache[index].ca_bucket);
  198.       if (dbf->bucket_cache[index].ca_data.dptr != NULL)
  199.     free (dbf->bucket_cache[index].ca_data.dptr);
  200.     }
  201.  
  202.   dbf->desc           = new_dbf->desc;
  203.   dbf->header         = new_dbf->header;
  204.   dbf->dir            = new_dbf->dir;
  205.   dbf->bucket         = new_dbf->bucket;
  206.   dbf->bucket_dir     = new_dbf->bucket_dir;
  207.   dbf->cache_entry    = new_dbf->cache_entry;
  208.   dbf->last_read      = new_dbf->last_read;
  209.   for (index = 0; index < CACHE_SIZE; index++)
  210.     dbf->bucket_cache[index] = new_dbf->bucket_cache[index];
  211.   dbf->header_changed    = new_dbf->header_changed;
  212.   dbf->directory_changed = new_dbf->directory_changed;
  213.   dbf->bucket_changed    = new_dbf->bucket_changed;
  214.   dbf->second_changed    = new_dbf->second_changed;
  215.       
  216.   free (new_dbf);
  217.  
  218.   /* Make sure the new database is all on disk. */
  219.   fsync (dbf->desc);
  220.  
  221.   return 0;
  222. }
  223.  
  224.  
  225.