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
10 #include <afs/param.h>
11 #include <afsconfig.h>
17 #include <afs/tcdata.h>
19 #include <sys/types.h>
20 #include <netinet/in.h>
30 afs_int32 nrestore, nskip;
31 int ask, printlabels, printheaders, verbose;
35 #define TAPE_MAGIC 1100000009 /* Defined in file_tm.c */
36 #define BLOCK_MAGIC 1100000005
37 #define FILE_MAGIC 1000000007
42 struct tapeLabel { /* also in file_tm.c */
44 struct butm_tapeLabel label;
46 struct fileMark { /* also in file_tm.c */
51 /* Read a tape block of size 16K */
52 afs_int32 readblock(buffer)
56 u_int nread, total = 0;
59 while (total < BUTM_BLOCKSIZE) {
60 rc = USD_READ(fd, buffer+total, BUTM_BLOCKSIZE-total, &nread);
64 else if ((nread == 0) && (total == 0)) {
65 if (verbose) fprintf(stderr, "Hardware file mark\n");
68 fprintf(stderr, "Greater than 3 hardware file marks in a row - done\n");
72 else if (nread == 0) {
73 fprintf(stderr, "Reached unexpected end of dump file\n");
82 printLabel(tapeLabelPtr)
83 struct tapeLabel *tapeLabelPtr;
85 tapeLabelPtr->label.dumpid = ntohl(tapeLabelPtr->label.dumpid);
86 tapeLabelPtr->label.creationTime = ntohl(tapeLabelPtr->label.creationTime);
87 tapeLabelPtr->label.expirationDate = ntohl(tapeLabelPtr->label.expirationDate);
88 tapeLabelPtr->label.structVersion = ntohl(tapeLabelPtr->label.structVersion);
89 tapeLabelPtr->label.useCount = ntohl(tapeLabelPtr->label.useCount);
90 tapeLabelPtr->label.size = ntohl(tapeLabelPtr->label.size);
92 fprintf(stderr, "\nDUMP %u\n", tapeLabelPtr->label.dumpid);
94 fprintf(stderr, " AFS Tape Name : %s\n", tapeLabelPtr->label.AFSName);
95 fprintf(stderr, " Permanent Name : %s\n", tapeLabelPtr->label.pName);
96 fprintf(stderr, " Dump Id : %u\n", tapeLabelPtr->label.dumpid);
97 fprintf(stderr, " Dump Id Time : %.24s\n", ctime(&(tapeLabelPtr->label.dumpid)));
98 fprintf(stderr, " Date Created : %.24s\n", ctime(&(tapeLabelPtr->label.creationTime)));
99 fprintf(stderr, " Date Expires : %.24s\n", ctime(&(tapeLabelPtr->label.expirationDate)));
100 fprintf(stderr, " Version Number : %d\n", tapeLabelPtr->label.structVersion);
101 fprintf(stderr, " Tape Use Count : %d\n", tapeLabelPtr->label.useCount);
102 fprintf(stderr, " Tape Size : %u\n", tapeLabelPtr->label.size);
103 fprintf(stderr, " Comment : %s\n", tapeLabelPtr->label.comment);
104 fprintf(stderr, " Dump Path : %s\n", tapeLabelPtr->label.dumpPath);
105 fprintf(stderr, " Cell Name : %s\n", tapeLabelPtr->label.cell);
106 fprintf(stderr, " Creator Name : %s\n", tapeLabelPtr->label.creator.name);
107 fprintf(stderr, " Creator Instance : %s\n", tapeLabelPtr->label.creator.instance);
108 fprintf(stderr, " Creator Cell : %s\n", tapeLabelPtr->label.creator.cell);
112 printHeader(headerPtr, isvolheader)
113 struct volumeHeader *headerPtr;
114 afs_int32 *isvolheader;
119 headerPtr->volumeID = ntohl(headerPtr->volumeID);
120 headerPtr->server = ntohl(headerPtr->server);
121 headerPtr->part = ntohl(headerPtr->part);
122 headerPtr->from = ntohl(headerPtr->from);
123 headerPtr->frag = ntohl(headerPtr->frag);
124 headerPtr->magic = ntohl(headerPtr->magic);
125 headerPtr->contd = ntohl(headerPtr->contd);
126 headerPtr->dumpID = ntohl(headerPtr->dumpID);
127 headerPtr->level = ntohl(headerPtr->level);
128 headerPtr->parentID = ntohl(headerPtr->parentID);
129 headerPtr->endTime = ntohl(headerPtr->endTime);
130 headerPtr->versionflags = ntohl(headerPtr->versionflags);
131 headerPtr->cloneDate = ntohl(headerPtr->cloneDate);
133 if (headerPtr->magic == TC_VOLBEGINMAGIC) {
135 if (verbose) fprintf(stderr, "Volume header\n");
136 fprintf(stderr, "VOLUME %3d %s (%u) - %s dump from %.24s till %.24s\n",
137 ++volcount, headerPtr->volumeName, headerPtr->volumeID,
138 (headerPtr->level?"Incr":"Full"),
139 ((headerPtr->from)?(char *)ctime(&headerPtr->from):"0"),
140 ctime(&(headerPtr->cloneDate)));
142 fprintf(stderr, " Volume Name = %s\n", headerPtr->volumeName);
143 fprintf(stderr, " Volume ID = %u\n", headerPtr->volumeID);
144 fprintf(stderr, " Clone Date = %.24s\n", ctime(&headerPtr->cloneDate));
145 fprintf(stderr, " Vol Fragment = %d\n", headerPtr->frag);
146 fprintf(stderr, " Vol Continued = 0x%x\n", headerPtr->contd);
147 fprintf(stderr, " DumpSet Name = %s\n", headerPtr->dumpSetName);
148 fprintf(stderr, " Dump ID = %u\n", headerPtr->dumpID);
149 fprintf(stderr, " Dump Level = %d\n", headerPtr->level);
150 fprintf(stderr, " Dump Since = %.24s\n", ctime(&headerPtr->from));
151 fprintf(stderr, " parent Dump ID = %u\n", headerPtr->parentID);
154 else if (headerPtr->magic == TC_VOLENDMAGIC) {
155 if (verbose) fprintf(stderr, "Volume Trailer\n");
158 fprintf(stderr, "Unrecognized Volume Header/Trailer\n");
162 int openOutFile(headerPtr)
163 struct volumeHeader *headerPtr;
173 /* If we were asked to skip this volume, then skip it */
178 /* Skip if we are not to restore any */
182 /* Get the volume name and strip off the BK or RO extension */
184 strcpy(filename, outfile);
186 strcpy(filename, headerPtr->volumeName);
187 len = strlen(filename);
188 if ((len>7) && (strcmp(".backup",filename+len-7)==0)) {
190 } else if ((len>9) && (strcmp(".readonly",filename+len-9)==0)) {
198 printf("Press return to retreive volume %s (%u) to file %s; ""s"" to skip\n",
199 headerPtr->volumeName, headerPtr->volumeID, filename);
202 if ((first == 1) && (ch == 's'))
204 if ((first == 1) && (ch == 'q'))
207 } while (ch != '\n');
209 printf("Will not restore volume %s\n",
210 headerPtr->volumeName, headerPtr->volumeID);
214 printf("Retreive volume %s (%u) to file %s\n",
215 headerPtr->volumeName, headerPtr->volumeID, filename);
218 /* Should I append the date onto the end of the name? */
220 /* Open the file to write to */
221 if (headerPtr->contd == TC_VOLCONTD) {
222 /* Continuation of dump */
223 oflag = USD_OPEN_RDWR;
225 /* An all new dump */
226 oflag = USD_OPEN_RDWR | USD_OPEN_CREATE;
228 rc = usd_Open(filename, oflag, 0664, &ofd);
230 fprintf(stderr, "Unable to open file %s. Skipping. Code = %d\n",
235 if (headerPtr->contd != TC_VOLCONTD) {
237 rc = USD_IOCTL(ofd, USD_IOCTL_SETSIZE, &size);
239 fprintf(stderr, "Unable to open file %s. Skipping. Code = %d\n",
250 int writeLastBlocks(char *lastblock, char *lastblock2)
253 struct volumeHeader vh;
255 struct blockMark *bmark, *bmark2;
260 if (!ofdIsOpen) return 0;
262 bmark = (struct blockMark *)lastblock;
263 data = &lastblock[sizeof(struct blockMark)];
264 count = ntohl(bmark->count);
266 bmark2 = (struct blockMark *)lastblock2;
267 data2 = &lastblock2[sizeof(struct blockMark)];
268 count2 = ntohl(bmark2->count);
274 * Strip off all but the last twelve bytes of the volume trailer
276 skip = sizeof(struct volumeHeader)-12;
278 count = count - skip;
279 } else if (count + count2 >= skip) {
280 count2 = count2 - (skip - count);
283 fprintf(stderr, "Failed to strip off volume trailer (1).\n");
287 /* volume trailer is somewhere in the last 12 bytes of the tape file.
288 * The volume trailer may span tape blocks. */
291 memcpy(trailer, data + (count - 12), 12);
294 memcpy(trailer, data2 + (count2 - tlen), tlen);
296 memcpy(trailer + tlen, data, count);
300 for (pos = 0 ; pos <= 2 ; pos++) {
301 if (strncmp(&trailer[pos], "H++NAME#", 8) == 0) {
310 fprintf(stderr, "Failed to strip off volume trailer (2).\n");
312 if (count2 - tlen > 0) {
313 rc = writeData(data2, count2 - tlen);
315 if ((tlen == 0) && (count > 12 - pos)) {
316 rc = writeData(data, count - (12 - pos));
324 if (!ofdIsOpen) return 0;
329 /* Decrement the number of volumes to restore */
334 int writeData(data, size)
343 rc = USD_WRITE(ofd, data, (u_int)size, &nwritten);
345 fprintf(stderr, "Unable to write volume data to file. Code = %d\n", rc);
351 struct cmd_syndesc *as;
355 struct tapeLabel *label;
356 struct fileMark *fmark;
358 struct blockMark *bmark;
359 afs_int32 isheader, isdatablock;
363 struct volumeHeader *volheaderPtr;
366 char *nextblock; /* We cycle through three tape blocks so we */
367 char *lastblock; /* can trim off the volume trailer from the */
368 char *lastblock2; /* end of each volume without having to back */
369 char *tapeblock1; /* up the output stream. */
373 tapedev = as->parms[0].items->data; /* -tape */
374 nrestore = (as->parms[1].items ? atol(as->parms[1].items->data) : 0x7fffffff);
375 nskip = (as->parms[2].items ? atol(as->parms[2].items->data) : 0);
376 if (as->parms[4].items) nskip = 0x7fffffff; /* -scan */
377 outfile = (as->parms[3].items ? as->parms[3].items->data : 0);
378 ask = (as->parms[5].items ? 0 : 1); /* -noask */
379 printlabels = (as->parms[6].items ? 1 : 0); /* -label */
380 printheaders = (as->parms[7].items ? 1 : 0); /* -vheaders */
381 verbose = (as->parms[8].items ? 1 : 0); /* -verbose */
383 /* Open the tape device */
384 rc = usd_Open(tapedev, USD_OPEN_RDONLY, 0, &fd);
386 printf("Failed to open tape device %s. Code = %d\n", tapedev, rc);
391 * Initialize the tape block buffers
393 tapeblock1 = (char *)malloc(3 * 16384);
394 if (tapeblock1 == NULL) {
395 printf("Failed to allocate I/O buffers.\n");
398 tapeblock2 = tapeblock1 + 16384;
399 tapeblock3 = tapeblock2 + 16384;
401 nextblock = tapeblock1;
405 /* Read each tape block deciding what to do with it */
406 do { /* while ((nskip!=0) && (nrestore!=0)) */
407 code = readblock(nextblock);
410 fprintf(stderr, "Tape device read error: %d\n", code);
415 /* A data block can be either a volume header, volume trailer,
416 * or actual data from a dump.
418 bmark = (struct blockMark *)nextblock;
419 label = (struct tapeLabel *)nextblock;
420 fmark = (struct fileMark *)nextblock;
421 if (ntohl(bmark->magic) == BLOCK_MAGIC) {
422 if (verbose) printf("Data block\n");
425 data = &nextblock[sizeof(struct blockMark)];
426 if (strncmp(data, "H++NAME#", 8) == 0) {
427 volheaderPtr = (struct volumeHeader *)data;
428 printHeader(volheaderPtr, &isheader);
431 code = openOutFile(volheaderPtr);
432 nextblock = tapeblock1;
436 if (lastblock2 != NULL) {
437 data = &lastblock2[sizeof(struct blockMark)];
438 bmark = (struct blockMark *)lastblock2;
439 code = writeData(data, ntohl(bmark->count));
441 } else if (lastblock != NULL) {
446 lastblock2 = lastblock;
447 lastblock = nextblock;
452 /* Filemarks come in 3 forms: BEGIN, END, and EOD.
453 * There is no information stored in filemarks.
455 else if (ntohl(fmark->magic) == FILE_MAGIC) {
456 fmtype = ntohl(fmark->nBytes);
457 if (fmtype == FILE_BEGIN) {
458 if (verbose) fprintf(stderr, "File mark volume begin\n");
459 } else if (fmtype == FILE_END) {
460 if (verbose) fprintf(stderr, "File mark volume end\n");
461 } else if (fmtype == FILE_EOD) {
462 if (verbose) fprintf(stderr, "File mark end-of-dump\n");
468 else if (ntohl(label->magic) == TAPE_MAGIC) {
469 if (verbose) fprintf(stderr, "Dump label\n");
476 fprintf(stderr, "Unrecognized tape block - end\n");
480 /* Anything other than a data block closes the out file.
481 * At this point nextblock contains the end of tape file mark,
482 * lastblock contains the last data block for the current volume,
483 * and lastblock2 contains the second to last block for the current
484 * volume. If the volume fits in a single block, lastblock2 will
485 * be NULL. Call writeLastBlocks to strip off the dump trailer before
486 * writing the last of the volume data to the dump file. The dump
487 * trailer may span block boundaries.
489 if (!isdatablock && lastblock) {
490 writeLastBlocks(lastblock, lastblock2);
492 nextblock = tapeblock1;
496 } while ((nskip!=0) || (nrestore!=0));
507 struct cmd_syndesc *ts;
512 ts=cmd_CreateSyntax((char *)0, WorkerBee, (char *) 0, "Restore volumes from backup tape");
513 cmd_AddParm(ts, "-tape", CMD_SINGLE, CMD_REQUIRED, "tape device");
514 cmd_AddParm(ts, "-restore", CMD_SINGLE, CMD_OPTIONAL, "# volumes to restore");
515 cmd_AddParm(ts, "-skip", CMD_SINGLE, CMD_OPTIONAL, "# volumes to skip");
516 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "filename");
517 cmd_AddParm(ts, "-scan", CMD_FLAG, CMD_OPTIONAL, "Scan the tape");
518 cmd_AddParm(ts, "-noask", CMD_FLAG, CMD_OPTIONAL, "Prompt for each volume");
519 cmd_AddParm(ts, "-label", CMD_FLAG, CMD_OPTIONAL, "Display dump label");
520 cmd_AddParm(ts, "-vheaders",CMD_FLAG, CMD_OPTIONAL, "Display volume headers");
521 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "verbose");
523 return cmd_Dispatch(argc, argv);