1: # include <stdio.h> 2: # include <sys/types.h> 3: # include <sys/dir.h> 4: # include <ingres.h> 5: # include <aux.h> 6: # include <access.h> 7: # include <sccs.h> 8: 9: SCCSID(@(#)destroydb.c 8.3 1/31/86) 10: 11: /* 12: ** DESTROY DATA BASE 13: ** 14: ** This program destroys an existing database. To be able 15: ** to wield this awesome power, you must be the dba for 16: ** the database. Also, anyone has this power if the admin 17: ** the database, or the ingres superuser, and have the "-s" 18: ** flag requested. If admin is trashed, the INGRES superuser 19: ** must either destroy the database or recover it. 20: ** 21: ** If -m is specified, then the directory is not removed. 22: ** This is useful if the directory is a mounted file system. 23: */ 24: 25: extern char *Usercode; 26: extern int Status; 27: extern char *Pathname; 28: extern char *Parmvect[]; 29: extern char *Flagvect[]; 30: extern char *Dbpath; 31: struct admin Admin; 32: short tTdbu[100]; 33: 34: main(argc, argv) 35: int argc; 36: char *argv[]; 37: { 38: register int i; 39: register char *dbase; 40: int superuser, mounted; 41: char **av; 42: register char *p; 43: char *q; 44: # ifdef rewinddir 45: DIR *dirp; 46: # else rewinddir 47: FILE *iop; 48: # endif rewinddir 49: 50: argv[argc] = NULL; 51: # ifdef xSTR1 52: tTrace(argv, 'T', tTdbu, 100); 53: # endif 54: 55: i = initucode(argc, argv, TRUE, NULL, -1); 56: dbase = Parmvect[0]; 57: # ifdef xSTR1 58: if (tTf(1, 0)) 59: { 60: printf("after initcode %d: ", i); 61: prargs(argc, argv); 62: } 63: # endif 64: switch (i) 65: { 66: case 0: 67: case 5: 68: break; 69: 70: case 1: 71: case 6: 72: printf("Database %s does not exist\n", dbase); 73: exit(-1); 74: 75: case 2: 76: printf("You are not authorized to access database %s\n", dbase); 77: exit(-1); 78: 79: case 3: 80: printf("You are not an authorized INGRES user\n"); 81: exit(-1); 82: 83: case 4: 84: printf("No database name specified\n"); 85: usage: 86: printf("Usage: destroydb [-s] [-m] dbname\n"); 87: exit(-1); 88: 89: default: 90: syserr("initucode %d", i); 91: } 92: 93: mounted = superuser = 0; 94: for (av = Flagvect; (p = *av) != NULL; av++) 95: { 96: # ifdef xSTR1 97: if (tTf(1, 1)) 98: printf("p = *av (\"%s\")\n", p); 99: # endif 100: if (p[0] != '-') 101: { 102: badflag: 103: printf("Bad flag %s\n", p); 104: goto usage; 105: } 106: switch (p[1]) 107: { 108: 109: case 's': 110: superuser++; 111: break; 112: 113: case 'm': 114: mounted++; 115: break; 116: 117: default: 118: goto badflag; 119: } 120: } 121: 122: if (Parmvect[1] != NULL) 123: { 124: printf("Too many parameters to destroydb\n"); 125: goto usage; 126: } 127: if (length(dbase) > 14) 128: syserr(0, "invalid dbname %s", dbase); 129: if (superuser && (Status & U_SUPER) == 0) 130: syserr(0, "you may not use the -s flag"); 131: 132: if (!superuser) 133: { 134: if (!bequal(Admin.adhdr.adowner, Usercode, UCODE_SZ)) 135: { 136: printf("You are not the DBA for %s\n", dbase); 137: exit(-1); 138: } 139: } 140: 141: if (chdir(Dbpath) < 0) 142: syserr("chdir %s", Dbpath); 143: 144: # ifdef rewinddir 145: if ( (dirp = opendir(".")) == NULL ) 146: syserr("Can't open . in %s",Dbpath); 147: clean(dirp); 148: closedir(dirp); 149: # else rewinddir 150: iop = fopen(".", "r"); 151: if (iop == NULL) 152: syserr("Cannot open dot in %s", Dbpath); 153: clean(iop); 154: fclose(iop); 155: # endif rewinddir 156: 157: if (!mounted) 158: { 159: /* find end of Dbpath and trim it off. */ 160: for (p = q = Dbpath; *p != '\0'; p++) 161: if (*p == '/') 162: q = p; 163: *q++ = '\0'; 164: if (chdir(Dbpath) < 0) 165: syserr("chdir(%s)", Dbpath); 166: if ( i == 5 ) 167: if ( unlink(ztack(ztack(Pathname,"/data/base/"),dbase)) == -1 ) 168: syserr("Can't unlink the indirect file %s",dbase); 169: execl("/bin/rmdir", "/bin/rmdir", q, 0); 170: perror("/bin/rmdir"); 171: } 172: } 173: 174: 175: 176: # ifdef rewinddir 177: clean(dirp) 178: register DIR *dirp; 179: { 180: struct direct *dp; 181: 182: # ifdef xSTR1 183: if (tTf(2, 0)) 184: printf("clean: "); 185: # endif 186: 187: for ( dp = readdir(dirp) ; dp != NULL ; dp = readdir(dirp) ) 188: { 189: if ( !strcmp(".",dp->d_name) || !strcmp("..",dp->d_name) ) 190: continue; 191: # ifdef xSTR1 192: if (tTf(2, 1)) 193: printf("unlinking %s\n", dp->d_name); 194: # endif 195: unlink(dp->d_name); 196: } 197: }/* clean */ 198: 199: # else rewinddir 200: 201: clean(f) 202: register FILE *f; 203: { 204: struct direc 205: { 206: struct direct d; 207: char null; 208: }; 209: struct direc cur; 210: 211: # ifdef xSTR1 212: if (tTf(2, 0)) 213: printf("clean: "); 214: # endif 215: 216: cur.null = 0; 217: 218: /* skip "." and ".." entries */ 219: fread(&cur, sizeof cur.d, 1, f); 220: fread(&cur, sizeof cur.d, 1, f); 221: 222: /* scan directory */ 223: while (fread(&cur, sizeof cur.d, 1, f) > 0) 224: { 225: /* skip null entries */ 226: if (cur.d.d_ino == 0) 227: continue; 228: 229: # ifdef xSTR1 230: if (tTf(2, 1)) 231: printf("unlinking %s\n", cur.d.d_name); 232: # endif 233: unlink(cur.d.d_name); 234: } 235: } 236: # endif rewinddir 237: 238: 239: 240: /* 241: ** Rubout processing. 242: */ 243: 244: rubproc() 245: { 246: exit(-2); 247: }