2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
14 #include <afsconfig.h>
15 #include <afs/param.h>
19 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
31 #include <afs/afsutil.h>
33 #include <afs/bubasics.h>
34 #include <afs/volser.h>
39 static char * TapeName(char *);
40 static char * DumpName(afs_int32 adumpID);
41 static FILE * OpenDump(afs_int32 , char * );
42 FILE * OpenTape(char * , char * );
43 static afs_int32 ScanForChildren(afs_int32 );
44 static afs_int32 DeleteDump(afs_int32 );
45 char * tailCompPtr(char *);
46 afs_int32 ScanDumpHdr(FILE *, char *, char *, afs_int32 *, afs_int32 *,
47 afs_int32 *, afs_int32 *);
48 afs_int32 ScanTapeVolume(FILE *, char *, afs_int32 *, char *, afs_int32 *, afs_int32 *,
49 afs_int32 *, afs_int32 *);
50 afs_int32 ScanVolClone(FILE *, char *, afs_int32 *);
53 /* basic format of a tape file is a file, whose name is "T<tapename>.db", and
54 * which contains the fields
55 * (afs_int32) dumpID, (afs_int32) tape-sequence-within-dump, (afs_int32) damage_flag
56 * all as space-separated integers.
59 /* The format of a dump file is:
60 * a file whose name is "D<dump#>.db"
61 * and whose contents are a header line:
62 * (string) dumpName, (long) parent-id, (long) incTime, (long) dumpEndTime, (long) level
63 * and a bunch of bcdb_volumeEntries with this format:
64 * (string) volume name, (long) volume ID, (string) tape name, (long) position-on-tape,
65 * (long) sequence-in-volume-dump, (long) is-this-the-last-vol-frag, (long) incTime
66 * again, all space-separated.
67 * Note that dumpEndTime is stored and returned in the dump creation time field.
70 /* return the tape file name corresponding to a particular tape */
72 static char * TapeName(char *atapeName)
74 static char tbuffer[AFSDIR_PATH_MAX];
76 /* construct the backup dir path */
77 strcpy(tbuffer, AFSDIR_SERVER_BACKUP_DIRPATH);
78 strcat(tbuffer, "/T");
79 strcat(tbuffer + 1, atapeName);
80 strcat(tbuffer, ".db");
84 /* return the dump file name corresponding to a particular dump ID */
86 static char * DumpName(afs_int32 adumpID)
88 static char tbuffer[AFSDIR_PATH_MAX];
89 char buf[AFSDIR_PATH_MAX];
91 /* construct the backup dir path */
92 strcpy(buf, AFSDIR_SERVER_BACKUP_DIRPATH);
93 strcat(buf, "/D%d.db");
94 sprintf(tbuffer, buf, adumpID);
98 static FILE * OpenDump(afs_int32 adumpID, char * awrite)
103 tp = DumpName(adumpID);
104 tfile = fopen(tp, awrite);
110 * non-static for recoverDB
113 FILE * OpenTape(char * atapeName, char * awrite)
117 tp = TapeName(atapeName);
118 tfile = fopen(tp, awrite);
122 /* scan for, and delete, all dumps whose parent dump ID is aparentID */
124 static afs_int32 ScanForChildren(afs_int32 aparentID)
128 afs_int32 dumpID, parent;
131 afs_int32 j2, j3, j4;
135 tdir = opendir(AFSDIR_SERVER_BACKUP_DIRPATH);
139 for (tde = readdir(tdir); tde; tde = readdir(tdir)) {
140 code = sscanf(tde->d_name, "D%ld.db", (long int *) &dumpID);
144 tfile = OpenDump(dumpID, "r");
146 continue; /* shouldn't happen, but should continue anyway */
148 code = ScanDumpHdr(tfile, dname, dumpName, &parent, &j2, &j3, &j4);
151 printf("backup:dsstub: bad dump header for dump %d\n", dumpID);
155 /* if this guy's parent is the ID we're scanning for, delete it */
156 if (aparentID == parent) {
157 code = DeleteDump(dumpID);
159 printf("backup:dsstub: failed to delete child dump %d\n",
167 static afs_int32 DeleteDump(afs_int32 adumpID)
171 tp = DumpName(adumpID);
175 code = ScanForChildren(adumpID);
180 static afs_int32 DeleteTape(char * atapeName)
184 tp = TapeName(atapeName);
191 * name is a pathname style name, determine trailing name and return
196 tailCompPtr(char *pathNamePtr)
199 ptr = strrchr(pathNamePtr, '/');
201 /* this should never happen */
202 printf("tailCompPtr: could not find / in name(%s)\n", pathNamePtr);
203 return (pathNamePtr);
205 ptr++; /* skip the / */
210 * scan a dump header out of a dump file, leaving the file ptr set after
213 * afile - ptr to file, for reading.
214 * various - ptrs for return values
216 * aname - string of form volume_set.dump_level
217 * dumpName - pathname of dump schedule node
218 * aparent - id of parent
220 * acreateTime - time at which dump was created
221 * alevel - level of dump (0 = full, 1+ are incrementals)
224 ScanDumpHdr(FILE *afile, char *aname, char *dumpName, afs_int32 *aparent, afs_int32 *aincTime, afs_int32 *acreateTime, afs_int32 *alevel)
228 afs_int32 dbmagic, dbversion;
231 tp = fgets(tbuffer, sizeof(tbuffer), afile);
235 sscanf(tbuffer, "%d %d %s %s %ld %ld %ld %ld", &dbmagic, &dbversion,
236 aname, dumpName, (long int *) aparent, (long int *) aincTime,
237 (long int *) acreateTime, (long int *) alevel);
241 /* now check the magic and version numbers */
242 if ((dbmagic != BC_DUMPDB_MAGIC) || (dbversion != BC_DUMPDB_VERSION))
249 /* scan a tape header out of a tape file, leaving the file ptr positioned just past the header */
250 static afs_int32 ScanTapeHdr(FILE *afile, afs_int32 *adumpID, afs_int32 *aseq, afs_int32 *adamage)
256 tp = fgets(tbuffer, sizeof(tbuffer), afile);
259 code = sscanf(tbuffer, "%ld %ld %ld", (long int *)adumpID,
260 (long int *)aseq, (long int *)adamage);
268 * scan a tape volume record from a dump file, leaving the file ptr
269 * positioned past the just-scanned record.
276 afs_int32 ScanTapeVolume(FILE *afile, char *avolName, afs_int32 *avolID, char *atapeName, afs_int32 *apos, afs_int32 *aseq, afs_int32 *alastp, afs_int32 *cloneTime)
282 tp = fgets(tbuffer, sizeof(tbuffer), afile);
283 if (!tp) { /* something went wrong, or eof hit */
285 return -1; /* error occurred */
290 sscanf(tbuffer, "%s %ld %s %ld %ld %ld %ld", avolName,
291 (long int *) avolID, atapeName, (long int *)apos,
292 (long int *) aseq, (long int *) alastp,
293 (long int *) cloneTime);
295 return -1; /* bad input line */
300 * Search the dump for the volume with name volName, and return it's
304 * -1 - volume with volName not found
307 afs_int32 ScanVolClone(FILE *tdump, char *volName, afs_int32 *cloneTime)
309 char avolName[256], atapeName[256];
310 afs_int32 retval, avolID, apos, aseq, alastp;
313 ScanTapeVolume(tdump, &avolName[0], &avolID, &atapeName[0], &apos,
314 &aseq, &alastp, cloneTime);
315 while (retval == 0) {
316 if (strcmp(avolName, volName) == 0)
319 ScanTapeVolume(tdump, &avolName[0], &avolID, &atapeName[0], &apos,
320 &aseq, &alastp, cloneTime);
326 /* seek a dump file (after a header scan has been done) to position apos */
327 static int SeekDump(FILE *afile, afs_int32 apos)
333 /* now skip to appropriate position */
334 for (i = 0; i < apos; i++) {
335 tp = fgets(tbuffer, sizeof(tbuffer), afile);