Unused variable cleanup
[openafs.git] / src / bucoord / dsstub.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  * ALL RIGHTS RESERVED
12  */
13
14 #include <afsconfig.h>
15 #include <afs/param.h>
16
17
18 #include <sys/types.h>
19 #include <afs/cmd.h>
20 #ifdef AFS_NT40_ENV
21 #include <winsock2.h>
22 #else
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <netdb.h>
26 #endif
27 #include <stdio.h>
28 #include <string.h>
29 #include <dirent.h>
30 #include <afs/afsutil.h>
31 #include <afs/budb.h>
32 #include <afs/bubasics.h>
33 #include <afs/volser.h>
34 #include "bc.h"
35
36 /* protos */
37
38 static char * TapeName(register char *);
39 static char * DumpName(register afs_int32 adumpID);
40 static FILE * OpenDump(afs_int32 , char * );
41 FILE * OpenTape(char * , char * );
42 static afs_int32 ScanForChildren(afs_int32 );
43 static afs_int32 DeleteDump(afs_int32 );
44 char * tailCompPtr(char *);
45 afs_int32 ScanDumpHdr(register FILE *, char *, char *, afs_int32 *, afs_int32 *,
46   afs_int32 *, afs_int32 *);
47 afs_int32 ScanTapeVolume(FILE *, char *, afs_int32 *, char *, afs_int32 *, afs_int32 *,
48    afs_int32 *, afs_int32 *);
49 afs_int32 ScanVolClone(FILE *, char *, afs_int32 *);
50
51
52 /* basic format of a tape file is a file, whose name is "T<tapename>.db", and
53  * which contains the fields
54  * (afs_int32) dumpID, (afs_int32) tape-sequence-within-dump, (afs_int32) damage_flag
55  * all as space-separated integers.
56  */
57
58 /* The format of a dump file is:
59  * a file whose name is "D<dump#>.db"
60  * and whose contents are a header line:
61  * (string) dumpName, (long) parent-id, (long) incTime, (long) dumpEndTime, (long) level
62  * and a bunch of bcdb_volumeEntries with this format:
63  * (string) volume name, (long) volume ID, (string) tape name, (long) position-on-tape,
64  *     (long) sequence-in-volume-dump, (long) is-this-the-last-vol-frag, (long) incTime
65  * again, all space-separated.
66  * Note that dumpEndTime is stored and returned in the dump creation time field.
67  */
68
69 /* return the tape file name corresponding to a particular tape */
70
71 static char * TapeName(register char *atapeName)
72 {
73     static char tbuffer[AFSDIR_PATH_MAX];
74
75     /* construct the backup dir path */
76     strcpy(tbuffer, AFSDIR_SERVER_BACKUP_DIRPATH);
77     strcat(tbuffer, "/T");
78     strcat(tbuffer + 1, atapeName);
79     strcat(tbuffer, ".db");
80     return tbuffer;
81 }
82
83 /* return the dump file name corresponding to a particular dump ID */
84
85 static char * DumpName(register afs_int32 adumpID)
86 {
87     static char tbuffer[AFSDIR_PATH_MAX];
88     char buf[AFSDIR_PATH_MAX];
89
90     /* construct the backup dir path */
91     strcpy(buf, AFSDIR_SERVER_BACKUP_DIRPATH);
92     strcat(buf, "/D%d.db");
93     sprintf(tbuffer, buf, adumpID);
94     return tbuffer;
95 }
96
97 static FILE * OpenDump(afs_int32 adumpID, char * awrite)
98 {
99     register char *tp;
100     register FILE *tfile;
101
102     tp = DumpName(adumpID);
103     tfile = fopen(tp, awrite);
104     return tfile;
105 }
106
107 /* OpenTape
108  * notes: 
109  *      non-static for recoverDB
110  */
111
112 FILE * OpenTape(char * atapeName, char * awrite)
113 {
114     register char *tp;
115     register FILE *tfile;
116     tp = TapeName(atapeName);
117     tfile = fopen(tp, awrite);
118     return tfile;
119 }
120
121 /* scan for, and delete, all dumps whose parent dump ID is aparentID */
122
123 static afs_int32 ScanForChildren(afs_int32 aparentID)
124 {
125     DIR *tdir;
126     register struct dirent *tde;
127     afs_int32 dumpID, parent;
128     register FILE *tfile;
129     register afs_int32 code;
130     afs_int32 j2, j3, j4;
131     char dname[256];
132     char dumpName[1024];
133
134     tdir = opendir(AFSDIR_SERVER_BACKUP_DIRPATH);
135     if (!tdir)
136         return -1;
137
138     for (tde = readdir(tdir); tde; tde = readdir(tdir)) {
139         code = sscanf(tde->d_name, "D%ld.db", (long int *) &dumpID);
140         if (code != 1)
141             continue;
142
143         tfile = OpenDump(dumpID, "r");
144         if (!tfile)
145             continue;           /* shouldn't happen, but should continue anyway */
146
147         code = ScanDumpHdr(tfile, dname, dumpName, &parent, &j2, &j3, &j4);
148         fclose(tfile);
149         if (code) {
150             printf("backup:dsstub: bad dump header for dump %d\n", dumpID);
151             continue;
152         }
153
154         /* if this guy's parent is the ID we're scanning for, delete it */
155         if (aparentID == parent) {
156             code = DeleteDump(dumpID);
157             if (code)
158                 printf("backup:dsstub: failed to delete child dump %d\n",
159                        dumpID);
160         }
161     }
162     closedir(tdir);
163     return 0;
164 }
165
166 static afs_int32 DeleteDump(afs_int32 adumpID)
167 {
168     register char *tp;
169     register afs_int32 code;
170     tp = DumpName(adumpID);
171     code = unlink(tp);
172     if (code)
173         return code;
174     code = ScanForChildren(adumpID);
175     return code;
176 }
177
178 #if 0
179 static afs_int32 DeleteTape(char * atapeName)
180 {
181     register char *tp;
182     register afs_int32 code;
183     tp = TapeName(atapeName);
184     code = unlink(tp);
185     return code;
186 }
187 #endif
188
189 /* tailCompPtr
190  *      name is a pathname style name, determine trailing name and return
191  *      pointer to it
192  */
193
194 char *
195 tailCompPtr(char *pathNamePtr)
196 {
197     char *ptr;
198     ptr = strrchr(pathNamePtr, '/');
199     if (ptr == 0) {
200         /* this should never happen */
201         printf("tailCompPtr: could not find / in name(%s)\n", pathNamePtr);
202         return (pathNamePtr);
203     } else
204         ptr++;                  /* skip the / */
205     return (ptr);
206 }
207
208 /* ScanDumpHdr
209  *      scan a dump header out of a dump file, leaving the file ptr set after
210  *      the header. 
211  * entry:
212  *      afile - ptr to file, for reading.
213  *      various - ptrs for return values
214  * exit:
215  *      aname - string of form volume_set.dump_level
216  *      dumpName - pathname of dump schedule node
217  *      aparent - id of parent
218  *      aincTime
219  *      acreateTime - time at which dump was created
220  *      alevel - level of dump (0 = full, 1+ are incrementals)
221  */
222 afs_int32
223 ScanDumpHdr(register FILE *afile, char *aname, char *dumpName, afs_int32 *aparent, afs_int32 *aincTime, afs_int32 *acreateTime, afs_int32 *alevel)
224 {
225     char tbuffer[256];
226     char *tp;
227     afs_int32 dbmagic, dbversion;
228     register afs_int32 code;
229
230     tp = fgets(tbuffer, sizeof(tbuffer), afile);
231     if (!tp)
232         return -1;
233     code =
234         sscanf(tbuffer, "%d %d %s %s %ld %ld %ld %ld", &dbmagic, &dbversion,
235                aname, dumpName, (long int *) aparent, (long int *) aincTime, 
236                (long int *) acreateTime, (long int *) alevel);
237     if (code != 8)
238         return -1;
239
240     /* now check the magic and version numbers */
241     if ((dbmagic != BC_DUMPDB_MAGIC) || (dbversion != BC_DUMPDB_VERSION))
242         return (-1);
243
244     return 0;
245 }
246
247 #if 0
248 /* scan a tape header out of a tape file, leaving the file ptr positioned just past the header */
249 static afs_int32 ScanTapeHdr(register FILE *afile, afs_int32 *adumpID, afs_int32 *aseq, afs_int32 *adamage)
250 {
251     char tbuffer[256];
252     char *tp;
253     register afs_int32 code;
254
255     tp = fgets(tbuffer, sizeof(tbuffer), afile);
256     if (!tp)
257         return -1;
258     code = sscanf(tbuffer, "%ld %ld %ld", (long int *)adumpID, 
259                   (long int *)aseq, (long int *)adamage);
260     if (code != 3)
261         return -1;
262     return 0;
263 }
264 #endif
265
266 /* ScanTapeVolume
267  *      scan a tape volume record from a dump file, leaving the file ptr
268  *      positioned past the just-scanned record.
269  * exit:
270  *      0 - success
271  *      1 - EOF
272  *      -1 for error
273  */
274
275 afs_int32 ScanTapeVolume(FILE *afile, char *avolName, afs_int32 *avolID, char *atapeName, afs_int32 *apos, afs_int32 *aseq, afs_int32 *alastp, afs_int32 *cloneTime)
276 {
277     char tbuffer[256];
278     register afs_int32 code;
279     register char *tp;
280
281     tp = fgets(tbuffer, sizeof(tbuffer), afile);
282     if (!tp) {                  /* something went wrong, or eof hit */
283         if (ferror(afile))
284             return -1;          /* error occurred */
285         else
286             return 1;           /* eof */
287     }
288     code =
289         sscanf(tbuffer, "%s %ld %s %ld %ld %ld %ld", avolName, 
290                (long int *) avolID, atapeName, (long int *)apos, 
291                (long int *) aseq, (long int *) alastp, 
292                (long int *) cloneTime);
293     if (code != 7)
294         return -1;              /* bad input line */
295     return 0;
296 }
297
298 /* ScanVolClone
299  *      Search the dump for the volume with name volName, and return it's
300  *      clone time.
301  * exit:
302  *      0 - clonetime set.
303  *      -1 - volume with volName not found
304  */
305
306 afs_int32 ScanVolClone(FILE *tdump, char *volName, afs_int32 *cloneTime)
307 {
308     char avolName[256], atapeName[256];
309     afs_int32 retval, avolID, apos, aseq, alastp;
310
311     retval =
312         ScanTapeVolume(tdump, &avolName[0], &avolID, &atapeName[0], &apos,
313                        &aseq, &alastp, cloneTime);
314     while (retval == 0) {
315         if (strcmp(avolName, volName) == 0)
316             return (0);
317         retval =
318             ScanTapeVolume(tdump, &avolName[0], &avolID, &atapeName[0], &apos,
319                            &aseq, &alastp, cloneTime);
320     }
321     return (-1);
322 }
323
324 #if 0
325 /* seek a dump file (after a header scan has been done) to position apos */
326 static int SeekDump(register FILE *afile, afs_int32 apos)
327 {
328     register afs_int32 i;
329     register char *tp;
330     char tbuffer[256];
331
332     /* now skip to appropriate position */
333     for (i = 0; i < apos; i++) {
334         tp = fgets(tbuffer, sizeof(tbuffer), afile);
335         if (!tp) {
336             fclose(afile);
337             return -1;
338         }
339     }
340     return 0;
341 }
342 #endif