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 <afsconfig.h>
11 #include <afs/param.h>
18 #include <afs/tcdata.h>
20 #include <sys/types.h>
21 #include <netinet/in.h>
31 afs_int32 nrestore, nskip;
32 int ask, printlabels, printheaders, verbose;
36 #define TAPE_MAGIC 1100000009 /* Defined in file_tm.c */
37 #define BLOCK_MAGIC 1100000005
38 #define FILE_MAGIC 1000000007
43 struct tapeLabel { /* also in file_tm.c */
45 struct butm_tapeLabel label;
47 struct fileMark { /* also in file_tm.c */
52 /* Read a tape block of size 16K */
58 u_int nread, total = 0;
61 while (total < BUTM_BLOCKSIZE) {
62 rc = USD_READ(fd, buffer + total, BUTM_BLOCKSIZE - total, &nread);
65 } else if ((nread == 0) && (total == 0)) {
67 fprintf(stderr, "Hardware file mark\n");
71 "Greater than 3 hardware file marks in a row - done\n");
74 } else if (nread == 0) {
75 fprintf(stderr, "Reached unexpected end of dump file\n");
84 printLabel(tapeLabelPtr)
85 struct tapeLabel *tapeLabelPtr;
87 tapeLabelPtr->label.dumpid = ntohl(tapeLabelPtr->label.dumpid);
88 tapeLabelPtr->label.creationTime =
89 ntohl(tapeLabelPtr->label.creationTime);
90 tapeLabelPtr->label.expirationDate =
91 ntohl(tapeLabelPtr->label.expirationDate);
92 tapeLabelPtr->label.structVersion =
93 ntohl(tapeLabelPtr->label.structVersion);
94 tapeLabelPtr->label.useCount = ntohl(tapeLabelPtr->label.useCount);
95 tapeLabelPtr->label.size = ntohl(tapeLabelPtr->label.size);
97 fprintf(stderr, "\nDUMP %u\n", tapeLabelPtr->label.dumpid);
99 fprintf(stderr, " AFS Tape Name : %s\n",
100 tapeLabelPtr->label.AFSName);
101 fprintf(stderr, " Permanent Name : %s\n",
102 tapeLabelPtr->label.pName);
103 fprintf(stderr, " Dump Id : %u\n",
104 tapeLabelPtr->label.dumpid);
105 fprintf(stderr, " Dump Id Time : %.24s\n",
106 ctime(&(tapeLabelPtr->label.dumpid)));
107 fprintf(stderr, " Date Created : %.24s\n",
108 ctime(&(tapeLabelPtr->label.creationTime)));
109 fprintf(stderr, " Date Expires : %.24s\n",
110 ctime(&(tapeLabelPtr->label.expirationDate)));
111 fprintf(stderr, " Version Number : %d\n",
112 tapeLabelPtr->label.structVersion);
113 fprintf(stderr, " Tape Use Count : %d\n",
114 tapeLabelPtr->label.useCount);
115 fprintf(stderr, " Tape Size : %u\n",
116 tapeLabelPtr->label.size);
117 fprintf(stderr, " Comment : %s\n",
118 tapeLabelPtr->label.comment);
119 fprintf(stderr, " Dump Path : %s\n",
120 tapeLabelPtr->label.dumpPath);
121 fprintf(stderr, " Cell Name : %s\n",
122 tapeLabelPtr->label.cell);
123 fprintf(stderr, " Creator Name : %s\n",
124 tapeLabelPtr->label.creator.name);
125 fprintf(stderr, " Creator Instance : %s\n",
126 tapeLabelPtr->label.creator.instance);
127 fprintf(stderr, " Creator Cell : %s\n",
128 tapeLabelPtr->label.creator.cell);
132 printHeader(headerPtr, isvolheader)
133 struct volumeHeader *headerPtr;
134 afs_int32 *isvolheader;
139 headerPtr->volumeID = ntohl(headerPtr->volumeID);
140 headerPtr->server = ntohl(headerPtr->server);
141 headerPtr->part = ntohl(headerPtr->part);
142 headerPtr->from = ntohl(headerPtr->from);
143 headerPtr->frag = ntohl(headerPtr->frag);
144 headerPtr->magic = ntohl(headerPtr->magic);
145 headerPtr->contd = ntohl(headerPtr->contd);
146 headerPtr->dumpID = ntohl(headerPtr->dumpID);
147 headerPtr->level = ntohl(headerPtr->level);
148 headerPtr->parentID = ntohl(headerPtr->parentID);
149 headerPtr->endTime = ntohl(headerPtr->endTime);
150 headerPtr->versionflags = ntohl(headerPtr->versionflags);
151 headerPtr->cloneDate = ntohl(headerPtr->cloneDate);
153 if (headerPtr->magic == TC_VOLBEGINMAGIC) {
156 fprintf(stderr, "Volume header\n");
158 "VOLUME %3d %s (%u) - %s dump from %.24s till %.24s\n",
159 ++volcount, headerPtr->volumeName, headerPtr->volumeID,
160 (headerPtr->level ? "Incr" : "Full"),
161 ((headerPtr->from) ? (char *)ctime(&headerPtr->from) : "0"),
162 ctime(&(headerPtr->cloneDate)));
164 fprintf(stderr, " Volume Name = %s\n",
165 headerPtr->volumeName);
166 fprintf(stderr, " Volume ID = %u\n", headerPtr->volumeID);
167 fprintf(stderr, " Clone Date = %.24s\n",
168 ctime(&headerPtr->cloneDate));
169 fprintf(stderr, " Vol Fragment = %d\n", headerPtr->frag);
170 fprintf(stderr, " Vol Continued = 0x%x\n", headerPtr->contd);
171 fprintf(stderr, " DumpSet Name = %s\n",
172 headerPtr->dumpSetName);
173 fprintf(stderr, " Dump ID = %u\n", headerPtr->dumpID);
174 fprintf(stderr, " Dump Level = %d\n", headerPtr->level);
175 fprintf(stderr, " Dump Since = %.24s\n",
176 ctime(&headerPtr->from));
177 fprintf(stderr, " parent Dump ID = %u\n", headerPtr->parentID);
179 } else if (headerPtr->magic == TC_VOLENDMAGIC) {
181 fprintf(stderr, "Volume Trailer\n");
183 fprintf(stderr, "Unrecognized Volume Header/Trailer\n");
188 openOutFile(headerPtr)
189 struct volumeHeader *headerPtr;
199 /* If we were asked to skip this volume, then skip it */
204 /* Skip if we are not to restore any */
208 /* Get the volume name and strip off the BK or RO extension */
210 strcpy(filename, outfile);
212 strcpy(filename, headerPtr->volumeName);
213 len = strlen(filename);
214 if ((len > 7) && (strcmp(".backup", filename + len - 7) == 0)) {
215 filename[len - 7] = 0;
217 && (strcmp(".readonly", filename + len - 9) == 0)) {
218 filename[len - 9] = 0;
225 printf("Press return to retreive volume %s (%u) to file %s; " "s"
226 " to skip\n", headerPtr->volumeName, headerPtr->volumeID,
230 if ((first == 1) && (ch == 's'))
232 if ((first == 1) && (ch == 'q'))
235 } while (ch != '\n');
237 printf("Will not restore volume %s\n", headerPtr->volumeName,
238 headerPtr->volumeID);
242 printf("Retreive volume %s (%u) to file %s\n", headerPtr->volumeName,
243 headerPtr->volumeID, filename);
246 /* Should I append the date onto the end of the name? */
248 /* Open the file to write to */
249 if (headerPtr->contd == TC_VOLCONTD) {
250 /* Continuation of dump */
251 oflag = USD_OPEN_RDWR;
253 /* An all new dump */
254 oflag = USD_OPEN_RDWR | USD_OPEN_CREATE;
256 rc = usd_Open(filename, oflag, 0664, &ofd);
258 fprintf(stderr, "Unable to open file %s. Skipping. Code = %d\n",
263 if (headerPtr->contd != TC_VOLCONTD) {
265 rc = USD_IOCTL(ofd, USD_IOCTL_SETSIZE, &size);
267 fprintf(stderr, "Unable to open file %s. Skipping. Code = %d\n",
279 writeLastBlocks(char *lastblock, char *lastblock2)
282 struct volumeHeader vh;
284 struct blockMark *bmark, *bmark2;
292 bmark = (struct blockMark *)lastblock;
293 data = &lastblock[sizeof(struct blockMark)];
294 count = ntohl(bmark->count);
296 bmark2 = (struct blockMark *)lastblock2;
297 data2 = &lastblock2[sizeof(struct blockMark)];
298 count2 = ntohl(bmark2->count);
304 * Strip off all but the last twelve bytes of the volume trailer
306 skip = sizeof(struct volumeHeader) - 12;
308 count = count - skip;
309 } else if (count + count2 >= skip) {
310 count2 = count2 - (skip - count);
313 fprintf(stderr, "Failed to strip off volume trailer (1).\n");
317 /* volume trailer is somewhere in the last 12 bytes of the tape file.
318 * The volume trailer may span tape blocks. */
321 memcpy(trailer, data + (count - 12), 12);
324 memcpy(trailer, data2 + (count2 - tlen), tlen);
326 memcpy(trailer + tlen, data, count);
330 for (pos = 0; pos <= 2; pos++) {
331 if (strncmp(&trailer[pos], "H++NAME#", 8) == 0) {
340 fprintf(stderr, "Failed to strip off volume trailer (2).\n");
342 if (count2 - tlen > 0) {
343 rc = writeData(data2, count2 - tlen);
345 if ((tlen == 0) && (count > 12 - pos)) {
346 rc = writeData(data, count - (12 - pos));
361 /* Decrement the number of volumes to restore */
367 writeData(data, size)
376 rc = USD_WRITE(ofd, data, (u_int) size, &nwritten);
378 fprintf(stderr, "Unable to write volume data to file. Code = %d\n",
385 struct cmd_syndesc *as;
389 struct tapeLabel *label;
390 struct fileMark *fmark;
392 struct blockMark *bmark;
393 afs_int32 isheader, isdatablock;
397 struct volumeHeader *volheaderPtr;
400 char *nextblock; /* We cycle through three tape blocks so we */
401 char *lastblock; /* can trim off the volume trailer from the */
402 char *lastblock2; /* end of each volume without having to back */
403 char *tapeblock1; /* up the output stream. */
407 tapedev = as->parms[0].items->data; /* -tape */
409 (as->parms[1].items ? atol(as->parms[1].items->data) : 0x7fffffff);
410 nskip = (as->parms[2].items ? atol(as->parms[2].items->data) : 0);
411 if (as->parms[4].items)
412 nskip = 0x7fffffff; /* -scan */
413 outfile = (as->parms[3].items ? as->parms[3].items->data : 0);
414 ask = (as->parms[5].items ? 0 : 1); /* -noask */
415 printlabels = (as->parms[6].items ? 1 : 0); /* -label */
416 printheaders = (as->parms[7].items ? 1 : 0); /* -vheaders */
417 verbose = (as->parms[8].items ? 1 : 0); /* -verbose */
419 /* Open the tape device */
420 rc = usd_Open(tapedev, USD_OPEN_RDONLY, 0, &fd);
422 printf("Failed to open tape device %s. Code = %d\n", tapedev, rc);
427 * Initialize the tape block buffers
429 tapeblock1 = (char *)malloc(3 * 16384);
430 if (tapeblock1 == NULL) {
431 printf("Failed to allocate I/O buffers.\n");
434 tapeblock2 = tapeblock1 + 16384;
435 tapeblock3 = tapeblock2 + 16384;
437 nextblock = tapeblock1;
441 /* Read each tape block deciding what to do with it */
442 do { /* while ((nskip!=0) && (nrestore!=0)) */
443 code = readblock(nextblock);
446 fprintf(stderr, "Tape device read error: %d\n", code);
451 /* A data block can be either a volume header, volume trailer,
452 * or actual data from a dump.
454 bmark = (struct blockMark *)nextblock;
455 label = (struct tapeLabel *)nextblock;
456 fmark = (struct fileMark *)nextblock;
457 if (ntohl(bmark->magic) == BLOCK_MAGIC) {
459 printf("Data block\n");
462 data = &nextblock[sizeof(struct blockMark)];
463 if (strncmp(data, "H++NAME#", 8) == 0) {
464 volheaderPtr = (struct volumeHeader *)data;
465 printHeader(volheaderPtr, &isheader);
468 code = openOutFile(volheaderPtr);
469 nextblock = tapeblock1;
473 if (lastblock2 != NULL) {
474 data = &lastblock2[sizeof(struct blockMark)];
475 bmark = (struct blockMark *)lastblock2;
476 code = writeData(data, ntohl(bmark->count));
478 } else if (lastblock != NULL) {
483 lastblock2 = lastblock;
484 lastblock = nextblock;
489 /* Filemarks come in 3 forms: BEGIN, END, and EOD.
490 * There is no information stored in filemarks.
492 else if (ntohl(fmark->magic) == FILE_MAGIC) {
493 fmtype = ntohl(fmark->nBytes);
494 if (fmtype == FILE_BEGIN) {
496 fprintf(stderr, "File mark volume begin\n");
497 } else if (fmtype == FILE_END) {
499 fprintf(stderr, "File mark volume end\n");
500 } else if (fmtype == FILE_EOD) {
502 fprintf(stderr, "File mark end-of-dump\n");
508 else if (ntohl(label->magic) == TAPE_MAGIC) {
510 fprintf(stderr, "Dump label\n");
517 fprintf(stderr, "Unrecognized tape block - end\n");
521 /* Anything other than a data block closes the out file.
522 * At this point nextblock contains the end of tape file mark,
523 * lastblock contains the last data block for the current volume,
524 * and lastblock2 contains the second to last block for the current
525 * volume. If the volume fits in a single block, lastblock2 will
526 * be NULL. Call writeLastBlocks to strip off the dump trailer before
527 * writing the last of the volume data to the dump file. The dump
528 * trailer may span block boundaries.
530 if (!isdatablock && lastblock) {
531 writeLastBlocks(lastblock, lastblock2);
533 nextblock = tapeblock1;
537 } while ((nskip != 0) || (nrestore != 0));
548 struct cmd_syndesc *ts;
553 ts = cmd_CreateSyntax(NULL, WorkerBee, NULL,
554 "Restore volumes from backup tape");
555 cmd_AddParm(ts, "-tape", CMD_SINGLE, CMD_REQUIRED, "tape device");
556 cmd_AddParm(ts, "-restore", CMD_SINGLE, CMD_OPTIONAL,
557 "# volumes to restore");
558 cmd_AddParm(ts, "-skip", CMD_SINGLE, CMD_OPTIONAL, "# volumes to skip");
559 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "filename");
560 cmd_AddParm(ts, "-scan", CMD_FLAG, CMD_OPTIONAL, "Scan the tape");
561 cmd_AddParm(ts, "-noask", CMD_FLAG, CMD_OPTIONAL,
562 "Prompt for each volume");
563 cmd_AddParm(ts, "-label", CMD_FLAG, CMD_OPTIONAL, "Display dump label");
564 cmd_AddParm(ts, "-vheaders", CMD_FLAG, CMD_OPTIONAL,
565 "Display volume headers");
566 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "verbose");
568 return cmd_Dispatch(argc, argv);