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>
32 afs_int32 nrestore, nskip;
33 int ask, printlabels, printheaders, verbose;
37 #define TAPE_MAGIC 1100000009 /* Defined in file_tm.c */
38 #define BLOCK_MAGIC 1100000005
39 #define FILE_MAGIC 1000000007
44 struct tapeLabel { /* also in file_tm.c */
46 struct butm_tapeLabel label;
48 struct fileMark { /* also in file_tm.c */
53 /* Read a tape block of size 16K */
59 u_int nread, total = 0;
62 while (total < BUTM_BLOCKSIZE) {
63 rc = USD_READ(fd, buffer + total, BUTM_BLOCKSIZE - total, &nread);
66 } else if ((nread == 0) && (total == 0)) {
68 fprintf(stderr, "Hardware file mark\n");
72 "Greater than 3 hardware file marks in a row - done\n");
75 } else if (nread == 0) {
76 fprintf(stderr, "Reached unexpected end of dump file\n");
85 printLabel(tapeLabelPtr)
86 struct tapeLabel *tapeLabelPtr;
88 tapeLabelPtr->label.dumpid = ntohl(tapeLabelPtr->label.dumpid);
89 tapeLabelPtr->label.creationTime =
90 ntohl(tapeLabelPtr->label.creationTime);
91 tapeLabelPtr->label.expirationDate =
92 ntohl(tapeLabelPtr->label.expirationDate);
93 tapeLabelPtr->label.structVersion =
94 ntohl(tapeLabelPtr->label.structVersion);
95 tapeLabelPtr->label.useCount = ntohl(tapeLabelPtr->label.useCount);
96 tapeLabelPtr->label.size = ntohl(tapeLabelPtr->label.size);
98 fprintf(stderr, "\nDUMP %u\n", tapeLabelPtr->label.dumpid);
100 fprintf(stderr, " AFS Tape Name : %s\n",
101 tapeLabelPtr->label.AFSName);
102 fprintf(stderr, " Permanent Name : %s\n",
103 tapeLabelPtr->label.pName);
104 fprintf(stderr, " Dump Id : %u\n",
105 tapeLabelPtr->label.dumpid);
106 fprintf(stderr, " Dump Id Time : %.24s\n",
107 ctime(&(tapeLabelPtr->label.dumpid)));
108 fprintf(stderr, " Date Created : %.24s\n",
109 ctime(&(tapeLabelPtr->label.creationTime)));
110 fprintf(stderr, " Date Expires : %.24s\n",
111 ctime(&(tapeLabelPtr->label.expirationDate)));
112 fprintf(stderr, " Version Number : %d\n",
113 tapeLabelPtr->label.structVersion);
114 fprintf(stderr, " Tape Use Count : %d\n",
115 tapeLabelPtr->label.useCount);
116 fprintf(stderr, " Tape Size : %u\n",
117 tapeLabelPtr->label.size);
118 fprintf(stderr, " Comment : %s\n",
119 tapeLabelPtr->label.comment);
120 fprintf(stderr, " Dump Path : %s\n",
121 tapeLabelPtr->label.dumpPath);
122 fprintf(stderr, " Cell Name : %s\n",
123 tapeLabelPtr->label.cell);
124 fprintf(stderr, " Creator Name : %s\n",
125 tapeLabelPtr->label.creator.name);
126 fprintf(stderr, " Creator Instance : %s\n",
127 tapeLabelPtr->label.creator.instance);
128 fprintf(stderr, " Creator Cell : %s\n",
129 tapeLabelPtr->label.creator.cell);
133 printHeader(headerPtr, isvolheader)
134 struct volumeHeader *headerPtr;
135 afs_int32 *isvolheader;
140 headerPtr->volumeID = ntohl(headerPtr->volumeID);
141 headerPtr->server = ntohl(headerPtr->server);
142 headerPtr->part = ntohl(headerPtr->part);
143 headerPtr->from = ntohl(headerPtr->from);
144 headerPtr->frag = ntohl(headerPtr->frag);
145 headerPtr->magic = ntohl(headerPtr->magic);
146 headerPtr->contd = ntohl(headerPtr->contd);
147 headerPtr->dumpID = ntohl(headerPtr->dumpID);
148 headerPtr->level = ntohl(headerPtr->level);
149 headerPtr->parentID = ntohl(headerPtr->parentID);
150 headerPtr->endTime = ntohl(headerPtr->endTime);
151 headerPtr->versionflags = ntohl(headerPtr->versionflags);
152 headerPtr->cloneDate = ntohl(headerPtr->cloneDate);
154 if (headerPtr->magic == TC_VOLBEGINMAGIC) {
157 fprintf(stderr, "Volume header\n");
159 "VOLUME %3d %s (%u) - %s dump from %.24s till %.24s\n",
160 ++volcount, headerPtr->volumeName, headerPtr->volumeID,
161 (headerPtr->level ? "Incr" : "Full"),
162 ((headerPtr->from) ? (char *)ctime(&headerPtr->from) : "0"),
163 ctime(&(headerPtr->cloneDate)));
165 fprintf(stderr, " Volume Name = %s\n",
166 headerPtr->volumeName);
167 fprintf(stderr, " Volume ID = %u\n", headerPtr->volumeID);
168 fprintf(stderr, " Clone Date = %.24s\n",
169 ctime(&headerPtr->cloneDate));
170 fprintf(stderr, " Vol Fragment = %d\n", headerPtr->frag);
171 fprintf(stderr, " Vol Continued = 0x%x\n", headerPtr->contd);
172 fprintf(stderr, " DumpSet Name = %s\n",
173 headerPtr->dumpSetName);
174 fprintf(stderr, " Dump ID = %u\n", headerPtr->dumpID);
175 fprintf(stderr, " Dump Level = %d\n", headerPtr->level);
176 fprintf(stderr, " Dump Since = %.24s\n",
177 ctime(&headerPtr->from));
178 fprintf(stderr, " parent Dump ID = %u\n", headerPtr->parentID);
180 } else if (headerPtr->magic == TC_VOLENDMAGIC) {
182 fprintf(stderr, "Volume Trailer\n");
184 fprintf(stderr, "Unrecognized Volume Header/Trailer\n");
189 openOutFile(headerPtr)
190 struct volumeHeader *headerPtr;
200 /* If we were asked to skip this volume, then skip it */
205 /* Skip if we are not to restore any */
209 /* Get the volume name and strip off the BK or RO extension */
211 strcpy(filename, outfile);
213 strcpy(filename, headerPtr->volumeName);
214 len = strlen(filename);
215 if ((len > 7) && (strcmp(".backup", filename + len - 7) == 0)) {
216 filename[len - 7] = 0;
218 && (strcmp(".readonly", filename + len - 9) == 0)) {
219 filename[len - 9] = 0;
226 printf("Press return to retreive volume %s (%u) to file %s; " "s"
227 " to skip\n", headerPtr->volumeName, headerPtr->volumeID,
231 if ((first == 1) && (ch == 's'))
233 if ((first == 1) && (ch == 'q'))
236 } while (ch != '\n');
238 printf("Will not restore volume %s\n", headerPtr->volumeName,
239 headerPtr->volumeID);
243 printf("Retreive volume %s (%u) to file %s\n", headerPtr->volumeName,
244 headerPtr->volumeID, filename);
247 /* Should I append the date onto the end of the name? */
249 /* Open the file to write to */
250 if (headerPtr->contd == TC_VOLCONTD) {
251 /* Continuation of dump */
252 oflag = USD_OPEN_RDWR;
254 /* An all new dump */
255 oflag = USD_OPEN_RDWR | USD_OPEN_CREATE;
257 rc = usd_Open(filename, oflag, 0664, &ofd);
259 fprintf(stderr, "Unable to open file %s. Skipping. Code = %d\n",
264 if (headerPtr->contd != TC_VOLCONTD) {
266 rc = USD_IOCTL(ofd, USD_IOCTL_SETSIZE, &size);
268 fprintf(stderr, "Unable to open file %s. Skipping. Code = %d\n",
280 writeLastBlocks(char *lastblock, char *lastblock2)
283 struct volumeHeader vh;
285 struct blockMark *bmark, *bmark2;
293 bmark = (struct blockMark *)lastblock;
294 data = &lastblock[sizeof(struct blockMark)];
295 count = ntohl(bmark->count);
297 bmark2 = (struct blockMark *)lastblock2;
298 data2 = &lastblock2[sizeof(struct blockMark)];
299 count2 = ntohl(bmark2->count);
305 * Strip off all but the last twelve bytes of the volume trailer
307 skip = sizeof(struct volumeHeader) - 12;
309 count = count - skip;
310 } else if (count + count2 >= skip) {
311 count2 = count2 - (skip - count);
314 fprintf(stderr, "Failed to strip off volume trailer (1).\n");
318 /* volume trailer is somewhere in the last 12 bytes of the tape file.
319 * The volume trailer may span tape blocks. */
322 memcpy(trailer, data + (count - 12), 12);
325 memcpy(trailer, data2 + (count2 - tlen), tlen);
327 memcpy(trailer + tlen, data, count);
331 for (pos = 0; pos <= 2; pos++) {
332 if (strncmp(&trailer[pos], "H++NAME#", 8) == 0) {
341 fprintf(stderr, "Failed to strip off volume trailer (2).\n");
343 if (count2 - tlen > 0) {
344 rc = writeData(data2, count2 - tlen);
346 if ((tlen == 0) && (count > 12 - pos)) {
347 rc = writeData(data, count - (12 - pos));
362 /* Decrement the number of volumes to restore */
368 writeData(data, size)
377 rc = USD_WRITE(ofd, data, (u_int) size, &nwritten);
379 fprintf(stderr, "Unable to write volume data to file. Code = %d\n",
386 struct cmd_syndesc *as;
390 struct tapeLabel *label;
391 struct fileMark *fmark;
393 struct blockMark *bmark;
394 afs_int32 isheader, isdatablock;
398 struct volumeHeader *volheaderPtr;
401 char *nextblock; /* We cycle through three tape blocks so we */
402 char *lastblock; /* can trim off the volume trailer from the */
403 char *lastblock2; /* end of each volume without having to back */
404 char *tapeblock1; /* up the output stream. */
408 tapedev = as->parms[0].items->data; /* -tape */
410 (as->parms[1].items ? atol(as->parms[1].items->data) : 0x7fffffff);
411 nskip = (as->parms[2].items ? atol(as->parms[2].items->data) : 0);
412 if (as->parms[4].items)
413 nskip = 0x7fffffff; /* -scan */
414 outfile = (as->parms[3].items ? as->parms[3].items->data : 0);
415 ask = (as->parms[5].items ? 0 : 1); /* -noask */
416 printlabels = (as->parms[6].items ? 1 : 0); /* -label */
417 printheaders = (as->parms[7].items ? 1 : 0); /* -vheaders */
418 verbose = (as->parms[8].items ? 1 : 0); /* -verbose */
420 /* Open the tape device */
421 rc = usd_Open(tapedev, USD_OPEN_RDONLY, 0, &fd);
423 printf("Failed to open tape device %s. Code = %d\n", tapedev, rc);
428 * Initialize the tape block buffers
430 tapeblock1 = (char *)malloc(3 * 16384);
431 if (tapeblock1 == NULL) {
432 printf("Failed to allocate I/O buffers.\n");
435 tapeblock2 = tapeblock1 + 16384;
436 tapeblock3 = tapeblock2 + 16384;
438 nextblock = tapeblock1;
442 /* Read each tape block deciding what to do with it */
443 do { /* while ((nskip!=0) && (nrestore!=0)) */
444 code = readblock(nextblock);
447 fprintf(stderr, "Tape device read error: %d\n", code);
452 /* A data block can be either a volume header, volume trailer,
453 * or actual data from a dump.
455 bmark = (struct blockMark *)nextblock;
456 label = (struct tapeLabel *)nextblock;
457 fmark = (struct fileMark *)nextblock;
458 if (ntohl(bmark->magic) == BLOCK_MAGIC) {
460 printf("Data block\n");
463 data = &nextblock[sizeof(struct blockMark)];
464 if (strncmp(data, "H++NAME#", 8) == 0) {
465 volheaderPtr = (struct volumeHeader *)data;
466 printHeader(volheaderPtr, &isheader);
469 code = openOutFile(volheaderPtr);
470 nextblock = tapeblock1;
474 if (lastblock2 != NULL) {
475 data = &lastblock2[sizeof(struct blockMark)];
476 bmark = (struct blockMark *)lastblock2;
477 code = writeData(data, ntohl(bmark->count));
479 } else if (lastblock != NULL) {
484 lastblock2 = lastblock;
485 lastblock = nextblock;
490 /* Filemarks come in 3 forms: BEGIN, END, and EOD.
491 * There is no information stored in filemarks.
493 else if (ntohl(fmark->magic) == FILE_MAGIC) {
494 fmtype = ntohl(fmark->nBytes);
495 if (fmtype == FILE_BEGIN) {
497 fprintf(stderr, "File mark volume begin\n");
498 } else if (fmtype == FILE_END) {
500 fprintf(stderr, "File mark volume end\n");
501 } else if (fmtype == FILE_EOD) {
503 fprintf(stderr, "File mark end-of-dump\n");
509 else if (ntohl(label->magic) == TAPE_MAGIC) {
511 fprintf(stderr, "Dump label\n");
518 fprintf(stderr, "Unrecognized tape block - end\n");
522 /* Anything other than a data block closes the out file.
523 * At this point nextblock contains the end of tape file mark,
524 * lastblock contains the last data block for the current volume,
525 * and lastblock2 contains the second to last block for the current
526 * volume. If the volume fits in a single block, lastblock2 will
527 * be NULL. Call writeLastBlocks to strip off the dump trailer before
528 * writing the last of the volume data to the dump file. The dump
529 * trailer may span block boundaries.
531 if (!isdatablock && lastblock) {
532 writeLastBlocks(lastblock, lastblock2);
534 nextblock = tapeblock1;
538 } while ((nskip != 0) || (nrestore != 0));
549 struct cmd_syndesc *ts;
554 ts = cmd_CreateSyntax(NULL, WorkerBee, NULL,
555 "Restore volumes from backup tape");
556 cmd_AddParm(ts, "-tape", CMD_SINGLE, CMD_REQUIRED, "tape device");
557 cmd_AddParm(ts, "-restore", CMD_SINGLE, CMD_OPTIONAL,
558 "# volumes to restore");
559 cmd_AddParm(ts, "-skip", CMD_SINGLE, CMD_OPTIONAL, "# volumes to skip");
560 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "filename");
561 cmd_AddParm(ts, "-scan", CMD_FLAG, CMD_OPTIONAL, "Scan the tape");
562 cmd_AddParm(ts, "-noask", CMD_FLAG, CMD_OPTIONAL,
563 "Prompt for each volume");
564 cmd_AddParm(ts, "-label", CMD_FLAG, CMD_OPTIONAL, "Display dump label");
565 cmd_AddParm(ts, "-vheaders", CMD_FLAG, CMD_OPTIONAL,
566 "Display volume headers");
567 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "verbose");
569 return cmd_Dispatch(argc, argv);