home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / support / purge.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-18  |  5.4 KB  |  261 lines

  1. # include    <stdio.h>
  2. # include    <ingres.h>
  3. # include    <aux.h>
  4. # include    <lock.h>
  5. # include    <pv.h>
  6. # include    <sccs.h>
  7. # include    <opsys.h>
  8. # include    <dirent.h> 
  9.  
  10. SCCSID(@(#)purge.c    8.3    1/31/86)
  11.  
  12. /*
  13. **  PURGE DATABASE
  14. **
  15. **    This stand-alone routine cleans up a database.  This includes:
  16. **
  17. **    - Destroy temporary relations, i.e., relations with names
  18. **        beginning with "_SYS".
  19. **    - Destroy expired relations
  20. **    - Clean out junk files, such as core, etc.
  21. **    - As a suggested future expansion, reformat relations which
  22. **        have many overflow pages, or heaps with lots of old
  23. **        deleted tuples, etc.
  24. **
  25. **    It may be called by the ingres superuser or by the dba of
  26. **    a database.  There are two modes.  The first is where databases
  27. **    to be purged are explicitly named.  If none are named, then
  28. **    all databases owned by the particular user (or all databases
  29. **    if the INGRES superuser) are purged.
  30. **
  31. **    Flags:
  32. **    -p    enable the purge feature, i.e., clean out expired
  33. **        relations as well as temporary relations.
  34. **    -s    attempt to run in superuser mode.  The user must be
  35. **        login "ingres" for this to succeed.
  36. **    -a    ask the user before each database.
  37. **    -f    clean out rather than report junk files.
  38. **
  39. **    (8/2/82 peter@lbl-unix)
  40. **        allow dba to destroy user relations by suitable manipulation
  41. **        of Usercode
  42. **        added ExitFn = nullfn() to avoid untimely aborts on syserrs.
  43. **    (5/13/83 peter@lbl-csam)
  44. **        folded in new directory rootines for 4.1c-bsd
  45. */
  46.  
  47.  
  48. char        All;
  49. char        Superuser;
  50. char        Ask;
  51. char        Purge;
  52. char        Clean;
  53. extern int    Wait_action;
  54. extern int    Status;
  55. extern char    *Usercode;
  56. long        Today;
  57. short        tTdbu[100];
  58. extern void    (*ExitFn)();
  59.  
  60. main(argc, argv)
  61. int    argc;
  62. char    *argv[];
  63. {
  64.     register char    *db;
  65.     register int    i;
  66.     char        fake[256];
  67.     extern char    *getnxtdb();
  68.     extern void    null_fn();
  69.  
  70.     argv[argc] = NULL;
  71. #    ifdef xTTR1
  72.     tTrace(argv, 'T', tTdbu, 100);
  73. #    endif
  74.  
  75.     /* set up arguments and operating modes */
  76.     initialize(argc, argv);
  77.     time(&Today);
  78. #    ifdef    xTTR2
  79.     tTfp(10, 2, "Usercode: %.2s\n", Usercode);
  80. #    endif
  81.  
  82.     /* Assign ExitFn so syserr (e.g. from destroy) does not cause aborts */
  83.     ExitFn = null_fn;
  84.  
  85.     while (db = getnxtdb())
  86.     {
  87.         purgedb(db);
  88.     }
  89.     printf("\npurge completed\n");
  90. }
  91.  
  92.  
  93. rubproc()
  94. {
  95.     unldb();
  96.     exit(-1);
  97. }
  98.  
  99.  
  100.  
  101. /*
  102. **  PURGE DATABASE
  103. **
  104. **    The database is purged of temporaries, expired relations, and
  105. **    junk.
  106. */
  107.  
  108. extern DESC    Reldes;
  109. DESC        Btreesec;
  110. int        Btree_fd;
  111. char        *Fileset;
  112.  
  113. purgedb(db)
  114. register char    *db;
  115. {
  116.     struct relation        rel, key;
  117.     TID            rtid, rlimtid;
  118.     register int        i;
  119.     register char        c;
  120.     long            l;
  121.     DIR            *dirp;
  122.     struct dirent        *dp;
  123.     int            darg[3];
  124.     PARM            pv[2];
  125.     char            pbuff[MAXNAME + 1];
  126.     char            *fname;
  127.  
  128. #    ifdef    xTTR2
  129.     tTfp(11, 0, "entered purgedb(%s)\n", db);
  130. #    endif
  131.     printf("Database %s", db);
  132.     if (!ask("? "))
  133.         return;
  134.     if (!Ask)
  135.         printf(":\n");
  136.     acc_init();
  137.  
  138.     /* set exclusive lock on data base */
  139. #    ifdef    xTTR2
  140.     tTfp(11, 1, "calling db_lock(%d)\n", M_EXCL);
  141. #    endif
  142.     db_lock(M_EXCL);
  143.  
  144.     /* open the relation relation for read-write */
  145.     opencatalog("relation", OR_WRITE);
  146.  
  147.     if (find(&Reldes, NOKEY, &rtid, &rlimtid))
  148.     {
  149.         printf("\tcannot find in %s\n", db);
  150.         closecatalog(TRUE);    /* really close cache */
  151.         unldb();        /* unlock the database */
  152.         acc_close();
  153.         return;
  154.     }
  155.  
  156.     while (get(&Reldes, &rtid, &rlimtid, &rel, 1) == 0)
  157.     {
  158.         i = 0;
  159.  
  160.         /* check for temp rel */
  161.         if (bequal(rel.relid, "_SYS", 4))
  162.         {
  163.             printf("\t%.14s: temporary", rel.relid);
  164.             i++;
  165.         }
  166.         else if (rel.relsave < Today && rel.relsave != 0)
  167.         {
  168.             printf("\t%.14s: expired", rel.relid);
  169.             if (Purge)
  170.                 if (ask("\n\t\tPURGE? "))
  171.                     i++;
  172.         }
  173.         else
  174.             i = -1;
  175.  
  176.         /* if this relation should be purged -- call (local) destroy */
  177.         if (i > 0)
  178.         {
  179.             char    *usave;
  180.             printf("\tpurging\n");
  181.  
  182.             /* allow DBA to purge other users relations */
  183.             usave = 0;
  184.             if (!bequal(rel.relowner, Usercode, 2)) {
  185.                 usave = Usercode;
  186.                 Usercode = rel.relowner;
  187.             }
  188.  
  189.             /* set up parameter vector for destroy */
  190.             bmove(rel.relid, pbuff, MAXNAME);
  191.             pbuff[MAXNAME] = '\0';
  192.             pv[0].pv_type = PV_STR;
  193.             pv[0].pv_val.pv_str = pbuff;
  194.             pv[1].pv_type = PV_EOF;
  195.             pv[1].pv_val.pv_str = NULL;
  196.             if (destroy(1, pv) != 0)
  197.                 syserr("cannot destroy %s\n", pbuff);
  198.             if (usave)
  199.                 Usercode = usave;
  200.             closecatalog(FALSE);    /* to flush */
  201.         }
  202.         else if (i == 0)
  203.             printf("\t\t(not purged)\n");
  204.     }
  205.  
  206.     /* open the directory to check for extra files */
  207.     if ((dirp = opendir(".")) == NULL)
  208.     {
  209.         printf("\tcannot open .\n");
  210.         closecatalog(TRUE);        /* really */
  211.         unldb();        /* unlock the database */
  212.         acc_close();
  213.         return;
  214.     }
  215.  
  216.     /* scan the directory */
  217.     for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
  218.  
  219.         fname = dp->d_name;
  220.         if (strlen(dp->d_name) <= 2)
  221.             continue;
  222.  
  223.         /* throw out legitimate files */
  224.         if (sequal(fname, "admin"))
  225.             continue;
  226.  
  227.         /* always purge _SYS files */
  228.         if (!bequal(fname, "_SYS", 4))
  229.         {
  230.             if (fname[13] != 0)
  231.             {
  232.                 /* it might be a relation */
  233.                 clearkeys(&Reldes);
  234.                 setkey(&Reldes, &key, fname, RELID);
  235.                 setkey(&Reldes, &key, &fname[MAXNAME], RELOWNER);
  236.                 if (getequal(&Reldes, &key, &rel, &rtid) <= 0)
  237.                 {
  238.                     /* it is a relation (or should be saved) */
  239.                     continue;
  240.                 }
  241.             }
  242.  
  243.             /* it is a funny file!!! */
  244.             if (!Clean)
  245.             {
  246.                 printf("\t%s: file (not unlinked)\n", fname);
  247.                 continue;
  248.             }
  249.         }
  250.  
  251.         /* purge the file */
  252.         printf("\tunlinking %s\n", fname);
  253.         if (unlink(fname))
  254.             printf("\tcannot unlink\n");
  255.     }
  256.     closecatalog(TRUE);    /* close catalogs */
  257.     unldb();        /* unlock the database */
  258.     acc_close();
  259.     closedir(dirp);
  260. }
  261.