|
|
1.1 ! root 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 <sys/dir.h> ! 9: ! 10: SCCSID(@(#)purge.c 7.4 7/7/83) ! 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 int (*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 int 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: ! 110: #ifndef DIRBLKSIZ ! 111: typedef DIR FILE; ! 112: #endif ! 113: extern DIR *opendir(); ! 114: extern struct direct *readdir(); ! 115: ! 116: ! 117: purgedb(db) ! 118: register char *db; ! 119: { ! 120: struct relation rel, key; ! 121: TID rtid, rlimtid; ! 122: register int i; ! 123: register char c; ! 124: long l; ! 125: DIR *dirp; ! 126: struct direct *dp; ! 127: int darg[3]; ! 128: PARM pv[2]; ! 129: char pbuff[MAXNAME + 1]; ! 130: char *fname; ! 131: #ifndef DIRBLKSIZ ! 132: char fnambuf[DIRSIZ + 1]; ! 133: #endif ! 134: ! 135: # ifdef xTTR2 ! 136: tTfp(11, 0, "entered purgedb(%s)\n", db); ! 137: # endif ! 138: printf("Database %s", db); ! 139: if (!ask("? ")) ! 140: return; ! 141: if (!Ask) ! 142: printf(":\n"); ! 143: acc_init(); ! 144: ! 145: /* set exclusive lock on data base */ ! 146: # ifdef xTTR2 ! 147: tTfp(11, 1, "calling db_lock(%d)\n", M_EXCL); ! 148: # endif ! 149: db_lock(M_EXCL); ! 150: ! 151: /* open the relation relation for read-write */ ! 152: opencatalog("relation", 2); ! 153: ! 154: if (find(&Reldes, NOKEY, &rtid, &rlimtid)) ! 155: { ! 156: printf("\tcannot find in %s\n", db); ! 157: closecatalog(TRUE); /* really close cache */ ! 158: unldb(); /* unlock the database */ ! 159: acc_close(); ! 160: return; ! 161: } ! 162: ! 163: while (get(&Reldes, &rtid, &rlimtid, &rel, 1) == 0) ! 164: { ! 165: i = 0; ! 166: ! 167: /* check for temp rel */ ! 168: if (bequal(rel.relid, "_SYS", 4)) ! 169: { ! 170: printf("\t%.14s: temporary", rel.relid); ! 171: i++; ! 172: } ! 173: else if (rel.relsave < Today && rel.relsave != 0) ! 174: { ! 175: printf("\t%.14s: expired", rel.relid); ! 176: if (Purge) ! 177: if (ask("\n\t\tPURGE? ")) ! 178: i++; ! 179: } ! 180: else ! 181: i = -1; ! 182: ! 183: /* if this relation should be purged -- call (local) destroy */ ! 184: if (i > 0) ! 185: { ! 186: char *usave; ! 187: printf("\tpurging\n"); ! 188: ! 189: /* allow DBA to purge other users relations */ ! 190: usave = 0; ! 191: if (!bequal(rel.relowner, Usercode, 2)) { ! 192: usave = Usercode; ! 193: Usercode = rel.relowner; ! 194: } ! 195: ! 196: /* set up parameter vector for destroy */ ! 197: bmove(rel.relid, pbuff, MAXNAME); ! 198: pbuff[MAXNAME] = '\0'; ! 199: pv[0].pv_type = PV_STR; ! 200: pv[0].pv_val.pv_str = pbuff; ! 201: pv[1].pv_type = PV_EOF; ! 202: pv[1].pv_val.pv_str = NULL; ! 203: if (destroy(1, pv) != 0) ! 204: syserr("cannot destroy %s\n", pbuff); ! 205: if (usave) ! 206: Usercode = usave; ! 207: closecatalog(FALSE); /* to flush */ ! 208: } ! 209: else if (i == 0) ! 210: printf("\t\t(not purged)\n"); ! 211: } ! 212: ! 213: /* open the directory to check for extra files */ ! 214: if ((dirp = opendir(".")) == NULL) ! 215: { ! 216: printf("\tcannot open .\n"); ! 217: closecatalog(TRUE); /* really */ ! 218: unldb(); /* unlock the database */ ! 219: acc_close(); ! 220: return; ! 221: } ! 222: ! 223: /* scan the directory */ ! 224: for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) { ! 225: ! 226: #ifdef DIRBLKSIZ ! 227: fname = dp->d_name; ! 228: if (dp->d_namlen <= 2) ! 229: continue; ! 230: #else DIRBLKSIZ ! 231: strncpy(fnambuf, dp->d_name, DIRSIZ); ! 232: fnambuf[DIRSIZ] = '\0'; ! 233: fname = fnambuf; ! 234: if (!strcmp(fname, ".") || !strcmp(fname, "..")) ! 235: continue; ! 236: #endif DIRBLKSIZ ! 237: ! 238: /* throw out legitimate files */ ! 239: if (sequal(fname, "admin")) ! 240: continue; ! 241: ! 242: /* always purge _SYS files */ ! 243: if (!bequal(fname, "_SYS", 4)) ! 244: { ! 245: if (fname[13] != 0) ! 246: { ! 247: /* it might be a relation */ ! 248: clearkeys(&Reldes); ! 249: setkey(&Reldes, &key, fname, RELID); ! 250: setkey(&Reldes, &key, &fname[MAXNAME], RELOWNER); ! 251: if (getequal(&Reldes, &key, &rel, &rtid) <= 0) ! 252: { ! 253: /* it is a relation (or should be saved) */ ! 254: continue; ! 255: } ! 256: } ! 257: ! 258: /* it is a funny file!!! */ ! 259: if (!Clean) ! 260: { ! 261: printf("\t%s: file (not unlinked)\n", fname); ! 262: continue; ! 263: } ! 264: } ! 265: ! 266: /* purge the file */ ! 267: printf("\tunlinking %s\n", fname); ! 268: if (unlink(fname)) ! 269: printf("\tcannot unlink\n"); ! 270: } ! 271: closecatalog(TRUE); /* close catalogs */ ! 272: unldb(); /* unlock the database */ ! 273: acc_close(); ! 274: closedir(dirp); ! 275: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.