revert-voldump-v-tag-generation-20090629
[openafs.git] / src / volser / restorevol.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  * Read a vos dump and recreate the tree.
12  *
13  * restorevol [-file <dump file>]
14  *            [-dir <restore dir>]
15  *            [-extension <name extension>]
16  *            [-mountpoint <mount point root>]
17  *            [-umask <mode mask>]
18  *
19  * 1. The dump file will be restored within the current or that specified with -dir.
20  * 2. Within this dir, a subdir is created. It's name is the RW volume name
21  *    that was dumped. An extension can be appended to this directory name
22  *    with -extension.
23  * 3. All mountpoints will appear as symbolic links to the volume. The
24  *    pathname to the volume will be either that in -mountpoint, or -dir.
25  *    Symbolic links remain untouched.
26  * 4. You can change your umask during the restore with -umask. Otherwise, it
27  *    uses your current umask. Mode bits for directories are 0777 (then
28  *    AND'ed with the umask). Mode bits for files are the owner mode bits
29  *    duplicated accross group and user (then AND'ed with the umask).
30  * 5. For restores of full dumps, if a directory says it has a file and 
31  *    the file is not found, then a symbolic link "AFSFile-<#>" will 
32  *    appear in that restored tree. Restores of incremental dumps remove 
33  *    all these files at the end (expensive because it is a tree search).  
34  * 6. If a file or directory was found in the dump but found not to be 
35  *    connected to the hierarchical tree, then the file or directory 
36  *    will be connected at the root of the tree as "__ORPHANEDIR__.<#>" 
37  *    or "__ORPHANFILE__.<#>".  
38  * 7. ACLs are not restored.
39  *
40  */
41
42 #include <afsconfig.h>
43 #include <afs/param.h>
44
45 RCSID
46     ("$Header$");
47
48 #include <afs/afsint.h>
49 #include <afs/nfs.h>
50 #include <lock.h>
51 #include <afs/ihandle.h>
52 #include <afs/vnode.h>
53 #include <afs/volume.h>
54 #include "volint.h"
55 #include "dump.h"
56 #include <afs/cmd.h>
57
58 #include <sys/param.h>
59 #include <sys/types.h>
60 #include <sys/uio.h>
61 #include <stdio.h>
62 #include <errno.h>
63 #include <netinet/in.h>
64 #include <sys/stat.h>
65 #include <fcntl.h>
66 #include <dirent.h>
67 #include <string.h>
68
69
70 char rootdir[MAXPATHLEN];
71 char mntroot[MAXPATHLEN];
72 #define ADIR  "AFSDir-"
73 #define AFILE "AFSFile-"
74 #define ODIR  "__ORPHANEDIR__."
75 #define OFILE "__ORPHANFILE__."
76
77 int inc_dump = 0;
78 FILE *dumpfile;
79
80 afs_int32
81 readvalue(size)
82 {
83     afs_int32 value, s;
84     int code;
85     char *ptr;
86
87     value = 0;
88     ptr = (char *)&value;
89
90     s = sizeof(value) - size;
91     if (size < 0) {
92         fprintf(stderr, "Too much data in afs_int32\n");
93         return 0;
94     }
95
96     code = fread(&ptr[s], 1, size, dumpfile);
97     if (code != size)
98         fprintf(stderr, "Code = %d; Errno = %d\n", code, errno);
99
100     return (value);
101 }
102
103 char
104 readchar()
105 {
106     char value;
107     int code;
108     char *ptr;
109
110     value = '\0';
111     code = fread(&value, 1, 1, dumpfile);
112     if (code != 1)
113         fprintf(stderr, "Code = %d; Errno = %d\n", code, errno);
114
115     return (value);
116 }
117
118 #define BUFSIZE 16384
119 char buf[BUFSIZE];
120
121 void
122 readdata(buffer, size)
123      char *buffer;
124      afs_sfsize_t size;
125 {
126     int code;
127     afs_int32 s;
128
129     if (!buffer) {
130         while (size > 0) {
131             s = (afs_int32) ((size > BUFSIZE) ? BUFSIZE : size);
132             code = fread(buf, 1, s, dumpfile);
133             if (code != s)
134                 fprintf(stderr, "Code = %d; Errno = %d\n", code, errno);
135             size -= s;
136         }
137     } else {
138         code = fread(buffer, 1, size, dumpfile);
139         if (code != size) {
140             if (code < 0)
141                 fprintf(stderr, "Code = %d; Errno = %d\n", code, errno);
142             else
143                 fprintf(stderr, "Read %d bytes out of %" AFS_INT64_FMT "\n", code, (afs_uintmax_t)size);
144         }
145         if ((code >= 0) && (code < BUFSIZE))
146             buffer[size] = 0;   /* Add null char at end */
147     }
148 }
149
150 afs_int32
151 ReadDumpHeader(dh)
152      struct DumpHeader *dh;     /* Defined in dump.h */
153 {
154     int code, i, done;
155     char tag, c;
156     afs_int32 magic;
157
158 /*  memset(&dh, 0, sizeof(dh)); */
159
160     magic = ntohl(readvalue(4));
161     dh->version = ntohl(readvalue(4));
162
163     done = 0;
164     while (!done) {
165         tag = readchar();
166         switch (tag) {
167         case 'v':
168             dh->volumeId = ntohl(readvalue(4));
169             break;
170
171         case 'n':
172             for (i = 0, c = 'a'; c != '\0'; i++) {
173                 dh->volumeName[i] = c = readchar();
174             }
175             dh->volumeName[i] = c;
176             break;
177
178         case 't':
179             dh->nDumpTimes = ntohl(readvalue(2)) >> 1;
180             for (i = 0; i < dh->nDumpTimes; i++) {
181                 dh->dumpTimes[i].from = ntohl(readvalue(4));
182                 dh->dumpTimes[i].to = ntohl(readvalue(4));
183             }
184             break;
185
186         default:
187             done = 1;
188             break;
189         }
190     }
191
192     return ((afs_int32) tag);
193 }
194
195 struct volumeHeader {
196     afs_int32 volumeId;
197     char volumeName[100];
198     afs_int32 volType;
199     afs_int32 uniquifier;
200     afs_int32 parentVol;
201     afs_int32 cloneId;
202     afs_int32 maxQuota;
203     afs_int32 minQuota;
204     afs_int32 diskUsed;
205     afs_int32 fileCount;
206     afs_int32 accountNumber;
207     afs_int32 owner;
208     afs_int32 creationDate;
209     afs_int32 accessDate;
210     afs_int32 updateDate;
211     afs_int32 expirationDate;
212     afs_int32 backupDate;
213     afs_int32 dayUseDate;
214     afs_int32 dayUse;
215     afs_int32 weekCount;
216     afs_int32 weekUse[100];     /* weekCount of these */
217     char motd[1024];
218     int inService;
219     int blessed;
220     char message[1024];
221     afs_int32 volUpdateCounter;
222 };
223
224 afs_int32
225 ReadVolumeHeader(count)
226      afs_int32 count;
227 {
228     struct volumeHeader vh;
229     int code, i, done, entries;
230     char tag, c;
231
232 /*  memset(&vh, 0, sizeof(vh)); */
233
234     done = 0;
235     while (!done) {
236         tag = readchar();
237         switch (tag) {
238         case 'i':
239             vh.volumeId = ntohl(readvalue(4));
240             break;
241
242         case 'v':
243             ntohl(readvalue(4));        /* version stamp - ignore */
244             break;
245
246         case 'n':
247             for (i = 0, c = 'a'; c != '\0'; i++) {
248                 vh.volumeName[i] = c = readchar();
249             }
250             vh.volumeName[i] = c;
251             break;
252
253         case 's':
254             vh.inService = ntohl(readvalue(1));
255             break;
256
257         case 'b':
258             vh.blessed = ntohl(readvalue(1));
259             break;
260
261         case 'u':
262             vh.uniquifier = ntohl(readvalue(4));
263             break;
264
265         case 't':
266             vh.volType = ntohl(readvalue(1));
267             break;
268
269         case 'p':
270             vh.parentVol = ntohl(readvalue(4));
271             break;
272
273         case 'c':
274             vh.cloneId = ntohl(readvalue(4));
275             break;
276
277         case 'q':
278             vh.maxQuota = ntohl(readvalue(4));
279             break;
280
281         case 'm':
282             vh.minQuota = ntohl(readvalue(4));
283             break;
284
285         case 'd':
286             vh.diskUsed = ntohl(readvalue(4));
287             break;
288
289         case 'f':
290             vh.fileCount = ntohl(readvalue(4));
291             break;
292
293         case 'a':
294             vh.accountNumber = ntohl(readvalue(4));
295             break;
296
297         case 'o':
298             vh.owner = ntohl(readvalue(4));
299             break;
300
301         case 'C':
302             vh.creationDate = ntohl(readvalue(4));
303             break;
304
305         case 'A':
306             vh.accessDate = ntohl(readvalue(4));
307             break;
308
309         case 'U':
310             vh.updateDate = ntohl(readvalue(4));
311             break;
312
313         case 'E':
314             vh.expirationDate = ntohl(readvalue(4));
315             break;
316
317         case 'B':
318             vh.backupDate = ntohl(readvalue(4));
319             break;
320
321         case 'O':
322             for (i = 0, c = 'a'; c != '\0'; i++) {
323                 vh.message[i] = c = readchar();
324             }
325             vh.volumeName[i] = c;
326             break;
327
328         case 'W':
329             vh.weekCount = ntohl(readvalue(2));
330             for (i = 0; i < vh.weekCount; i++) {
331                 vh.weekUse[i] = ntohl(readvalue(4));
332             }
333             break;
334
335         case 'M':
336             for (i = 0, c = 'a'; c != '\0'; i++) {
337                 vh.motd[i] = c = readchar();
338             }
339             break;
340
341         case 'D':
342             vh.dayUseDate = ntohl(readvalue(4));
343             break;
344
345         case 'Z':
346             vh.dayUse = ntohl(readvalue(4));
347             break;
348
349         case 'V':
350             readvalue(4); /*volUpCounter*/
351             break;
352
353         default:
354             done = 1;
355             break;
356         }
357     }
358
359     return ((afs_int32) tag);
360 }
361
362 struct vNode {
363     afs_int32 vnode;
364     afs_int32 uniquifier;
365     afs_int32 type;
366     afs_int32 linkCount;
367     afs_int32 dataVersion;
368     afs_int32 unixModTime;
369     afs_int32 servModTime;
370     afs_int32 author;
371     afs_int32 owner;
372     afs_int32 group;
373     afs_int32 modebits;
374     afs_int32 parent;
375     char acl[192];
376 #ifdef notdef
377     struct acl_accessList {
378         int size;               /*size of this access list in bytes, including MySize itself */
379         int version;            /*to deal with upward compatibility ; <= ACL_ACLVERSION */
380         int total;
381         int positive;           /* number of positive entries */
382         int negative;           /* number of minus entries */
383         struct acl_accessEntry {
384             int id;             /*internally-used ID of user or group */
385             int rights;         /*mask */
386         } entries[100];
387     } acl;
388 #endif
389     afs_sfsize_t dataSize;
390 };
391
392 #define MAXNAMELEN 256
393
394 afs_int32
395 ReadVNode(count)
396      afs_int32 count;
397 {
398     struct vNode vn;
399     int code, i, done, entries;
400     char tag, c;
401     char dirname[MAXNAMELEN], linkname[MAXNAMELEN], lname[MAXNAMELEN];
402     char parentdir[MAXNAMELEN], vflink[MAXNAMELEN];
403     char filename[MAXNAMELEN], fname[MAXNAMELEN];
404     int len;
405     afs_int32 vnode;
406     afs_int32 mode = 0;
407
408 /*  memset(&vn, 0, sizeof(vn)); */
409     vn.dataSize = 0;
410     vn.vnode = 0;
411     vn.parent = 0;
412     vn.type = 0;
413
414     vn.vnode = ntohl(readvalue(4));
415     vn.uniquifier = ntohl(readvalue(4));
416
417     done = 0;
418     while (!done) {
419         tag = readchar();
420         switch (tag) {
421         case 't':
422             vn.type = ntohl(readvalue(1));
423             break;
424
425         case 'l':
426             vn.linkCount = ntohl(readvalue(2));
427             break;
428
429         case 'v':
430             vn.dataVersion = ntohl(readvalue(4));
431             break;
432
433         case 'm':
434             vn.unixModTime = ntohl(readvalue(4));
435             break;
436
437         case 's':
438             vn.servModTime = ntohl(readvalue(4));
439             break;
440
441         case 'a':
442             vn.author = ntohl(readvalue(4));
443             break;
444
445         case 'o':
446             vn.owner = ntohl(readvalue(4));
447             break;
448
449         case 'g':
450             vn.group = ntohl(readvalue(4));
451             break;
452
453         case 'b':
454             vn.modebits = ntohl(readvalue(2));
455             break;
456
457         case 'p':
458             vn.parent = ntohl(readvalue(4));
459             break;
460
461         case 'A':
462             readdata(vn.acl, 192);      /* Skip ACL data */
463             break;
464
465 #ifdef AFS_LARGEFILE_ENV
466         case 'h':
467             {
468                 afs_uint32 hi, lo;
469                 hi = ntohl(readvalue(4));
470                 lo = ntohl(readvalue(4));
471                 FillInt64(vn.dataSize, hi, lo);
472             }
473             goto common_vnode;
474 #endif /* !AFS_LARGEFILE_ENV */
475
476         case 'f':
477             vn.dataSize = ntohl(readvalue(4));
478
479           common_vnode:
480             /* parentdir is the name of this dir's vnode-file-link
481              * or this file's parent vnode-file-link.
482              * "./AFSDir-<#>". It's a symbolic link to its real dir.
483              * The parent dir and symbolic link to it must exist.
484              */
485             vnode = ((vn.type == 2) ? vn.vnode : vn.parent);
486             if (vnode == 1)
487                 strncpy(parentdir, rootdir, sizeof parentdir);
488             else {
489                 afs_snprintf(parentdir, sizeof parentdir, "%s/%s%d", rootdir,
490                              ADIR, vnode);
491
492                 len = readlink(parentdir, linkname, MAXNAMELEN);
493                 if (len < 0) {
494                     /* parentdir does not exist. So create an orphan dir.
495                      * and then link the parentdir to the orphaned dir.
496                      */
497                     afs_snprintf(linkname, sizeof linkname, "%s/%s%d",
498                                  rootdir, ODIR, vnode);
499                     code = mkdir(linkname, 0777);
500                     if ((code < 0) && (errno != EEXIST)) {
501                         fprintf(stderr,
502                                 "Error creating directory %s  code=%d;%d\n",
503                                 linkname, code, errno);
504                     }
505
506                     /* Link the parentdir to it - now parentdir exists */
507                     afs_snprintf(linkname, sizeof linkname, "%s%d/", ODIR,
508                                  vnode);
509                     code = symlink(linkname, parentdir);
510                     if (code) {
511                         fprintf(stderr,
512                                 "Error creating symlink %s -> %s  code=%d;%d\n",
513                                 parentdir, linkname, code, errno);
514                     }
515                 }
516             }
517
518             if (vn.type == 2) {
519                  /*ITSADIR*/
520                     /* We read the directory entries. If the entry is a
521                      * directory, the subdir is created and the root dir
522                      * will contain a link to it. If its a file, we only
523                      * create a symlink in the dir to the file name.
524                      */
525                 char *buffer;
526                 unsigned short j;
527                 afs_int32 this_vn;
528                 char *this_name;
529
530                 struct DirEntry {
531                     char flag;
532                     char length;
533                     unsigned short next;
534                     struct MKFid {
535                         afs_int32 vnode;
536                         afs_int32 vunique;
537                     } fid;
538                     char name[20];
539                 };
540
541                 struct Pageheader {
542                     unsigned short pgcount;
543                     unsigned short tag;
544                     char freecount;
545                     char freebitmap[8];
546                     char padding[19];
547                 };
548
549                 struct DirHeader {
550                     struct Pageheader header;
551                     char alloMap[128];
552                     unsigned short hashTable[128];
553                 };
554
555                 struct Page0 {
556                     struct DirHeader header;
557                     struct DirEntry entry[1];
558                 } *page0;
559
560
561                 buffer = NULL;
562                 buffer = (char *)malloc(vn.dataSize);
563
564                 readdata(buffer, vn.dataSize);
565                 page0 = (struct Page0 *)buffer;
566
567                 /* Step through each bucket in the hash table, i,
568                  * and follow each element in the hash chain, j.
569                  * This gives us each entry of the dir.
570                  */
571                 for (i = 0; i < 128; i++) {
572                     for (j = ntohs(page0->header.hashTable[i]); j;
573                          j = ntohs(page0->entry[j].next)) {
574                         j -= 13;
575                         this_vn = ntohl(page0->entry[j].fid.vnode);
576                         this_name = page0->entry[j].name;
577
578                         if ((strcmp(this_name, ".") == 0)
579                             || (strcmp(this_name, "..") == 0))
580                             continue;   /* Skip these */
581
582                         /* For a directory entry, create it. Then create the
583                          * link (from the rootdir) to this directory.
584                          */
585                         if (this_vn & 1) {
586                              /*ADIRENTRY*/
587                                 /* dirname is the directory to create.
588                                  * vflink is what will link to it. 
589                                  */
590                                 afs_snprintf(dirname, sizeof dirname, "%s/%s",
591                                              parentdir, this_name);
592                             afs_snprintf(vflink, sizeof vflink, "%s/%s%d",
593                                          rootdir, ADIR, this_vn);
594
595                             /* The link and directory may already exist */
596                             len = readlink(vflink, linkname, MAXNAMELEN);
597                             if (len < 0) {
598                                 /* Doesn't already exist - so create the directory.
599                                  * umask will pare the mode bits down.
600                                  */
601                                 code = mkdir(dirname, 0777);
602                                 if ((code < 0) && (errno != EEXIST)) {
603                                     fprintf(stderr,
604                                             "Error creating directory %s  code=%d;%d\n",
605                                             dirname, code, errno);
606                                 }
607                             } else {
608                                 /* Does already exist - so move the directory.
609                                  * It was created originally as orphaned.
610                                  */
611                                 linkname[len - 1] = '\0';       /* remove '/' at end */
612                                 afs_snprintf(lname, sizeof lname, "%s/%s",
613                                              rootdir, linkname);
614                                 code = rename(lname, dirname);
615                                 if (code) {
616                                     fprintf(stderr,
617                                             "Error renaming %s to %s  code=%d;%d\n",
618                                             lname, dirname, code, errno);
619                                 }
620                             }
621
622                             /* Now create/update the link to the new/moved directory */
623                             if (vn.vnode == 1)
624                                 afs_snprintf(dirname, sizeof dirname, "%s/",
625                                              this_name);
626                             else
627                                 afs_snprintf(dirname, sizeof dirname,
628                                              "%s%d/%s/", ADIR, vn.vnode,
629                                              this_name);
630                             unlink(vflink);
631                             code = symlink(dirname, vflink);
632                             if (code) {
633                                 fprintf(stderr,
634                                         "Error creating symlink %s -> %s  code=%d;%d\n",
635                                         vflink, dirname, code, errno);
636                             }
637                         }
638                         /*ADIRENTRY*/
639                             /* For a file entry, we remember the name of the file
640                              * by creating a link within the directory. Restoring
641                              * the file will later remove the link.
642                              */
643                             else {
644                              /*AFILEENTRY*/ afs_snprintf(vflink,
645                                                          sizeof vflink,
646                                                          "%s/%s%d", parentdir,
647                                                          AFILE, this_vn);
648
649                             code = symlink(this_name, vflink);
650                             if ((code < 0) && (errno != EEXIST)) {
651                                 fprintf(stderr,
652                                         "Error creating symlink %s -> %s  code=%d;%d\n",
653                                         vflink, page0->entry[j].name, code,
654                                         errno);
655                             }
656                         }
657                      /*AFILEENTRY*/}
658                 }
659                 free(buffer);
660             }
661             /*ITSADIR*/
662             else if (vn.type == 1) {
663                  /*ITSAFILE*/
664                     /* A file vnode. So create it into the desired directory. A
665                      * link should exist in the directory naming the file.
666                      */
667                 int fid;
668                 int lfile;
669                 afs_sfsize_t size, s;
670
671                 /* Check if its vnode-file-link exists. If not,
672                  * then the file will be an orphaned file.
673                  */
674                 lfile = 1;
675                 afs_snprintf(filename, sizeof filename, "%s/%s%d", parentdir,
676                              AFILE, vn.vnode);
677                 len = readlink(filename, fname, MAXNAMELEN);
678                 if (len < 0) {
679                     afs_snprintf(filename, sizeof filename, "%s/%s%d",
680                                  rootdir, OFILE, vn.vnode);
681                     lfile = 0;  /* no longer a linked file; a direct path */
682                 }
683
684                 /* Create a mode for the file. Use the owner bits and
685                  * duplicate them across group and other. The umask
686                  * will remove what we don't want.
687                  */
688                 mode = (vn.modebits >> 6) & 0x7;
689                 mode |= (mode << 6) | (mode << 3);
690
691                 /* Write the file out */
692                 fid = open(filename, (O_CREAT | O_WRONLY | O_TRUNC), mode);
693                 size = vn.dataSize;
694                 while (size > 0) {
695                     s = (afs_int32) ((size > BUFSIZE) ? BUFSIZE : size);
696                     code = fread(buf, 1, s, dumpfile);
697                     if (code > 0) {
698                         (void)write(fid, buf, code);
699                         size -= code;
700                     }
701                     if (code != s) {
702                         if (code < 0)
703                             fprintf(stderr, "Code = %d; Errno = %d\n", code,
704                                     errno);
705                         else {
706                             char tmp[100];
707                             (void)afs_snprintf(tmp, sizeof tmp,
708                                                "Read %llu bytes out of %llu",
709                                                (afs_uintmax_t) (vn.dataSize -
710                                                                 size),
711                                                (afs_uintmax_t) vn.dataSize);
712                             fprintf(stderr, "%s\n", tmp);
713                         }
714                         break;
715                     }
716                 }
717                 close(fid);
718                 if (size != 0) {
719                     fprintf(stderr, "   File %s (%s) is incomplete\n",
720                             filename, fname);
721                 }
722
723                 /* Remove the link to the file */
724                 if (lfile) {
725                     unlink(filename);
726                 }
727             }
728             /*ITSAFILE*/
729             else if (vn.type == 3) {
730                  /*ITSASYMLINK*/
731                     /* A symlink vnode. So read it into the desired directory. This could
732                      * also be a mount point. If the volume is being restored to AFS, this
733                      * will become a mountpoint. If not, it becomes a symlink to no-where.
734                      */
735                 int fid;
736                 afs_int32 size, s;
737
738                 /* Check if its vnode-file-link exists and create pathname
739                  * of the symbolic link. If it doesn't exist,
740                  * then the link will be an orphaned link.
741                  */
742                 afs_snprintf(linkname, sizeof linkname, "%s/%s%d", parentdir,
743                              AFILE, vn.vnode);
744                 len = readlink(linkname, fname, MAXNAMELEN);
745                 if (len < 0) {
746                     afs_snprintf(filename, sizeof filename, "%s/%s%d",
747                                  rootdir, OFILE, vn.vnode);
748                 } else {
749                     fname[len] = '\0';
750                     afs_snprintf(filename, sizeof filename, "%s/%s",
751                                  parentdir, fname);
752                 }
753
754                 /* Read the link in, delete it, and then create it */
755                 readdata(buf, vn.dataSize);
756
757                 /* If a mountpoint, change its link path to mountroot */
758                 s = strlen(buf);
759                 if (((buf[0] == '%') || (buf[0] == '#'))
760                     && (buf[s - 1] == '.')) {
761                     /* This is a symbolic link */
762                     buf[s - 1] = 0;     /* Remove prefix '.' */
763                     strcpy(lname, &buf[1]);     /* Remove postfix '#' or '%' */
764                     strcpy(buf, mntroot);
765                     strcat(buf, lname);
766                 }
767
768                 unlink(filename);
769                 code = symlink(buf, filename);
770                 if (code) {
771                     fprintf(stderr,
772                             "Error creating symlink %s -> %s  code=%d;%d\n",
773                             filename, buf, code, errno);
774                 }
775
776                 /* Remove the symbolic link */
777                 unlink(linkname);
778             }
779             /*ITSASYMLINK*/
780             else {
781                 fprintf(stderr, "Unknown Vnode block\n");
782             }
783             break;
784
785         default:
786             done = 1;
787             break;
788         }
789     }
790     if (vn.type == 0)
791         inc_dump = 1;
792
793     return ((afs_int32) tag);
794 }
795
796 static int
797 WorkerBee(struct cmd_syndesc *as, void *arock)
798 {
799     int code = 0, c, len;
800     afs_int32 type, count, vcount;
801     DIR *dirP, *dirQ;
802     struct dirent *dirE, *dirF;
803     char fname[MAXNAMELEN], name[MAXNAMELEN], lname[MAXNAMELEN],
804         mname[MAXNAMELEN];
805     char thisdir[MAXPATHLEN], *t;
806     struct DumpHeader dh;       /* Defined in dump.h */
807 #if 0/*ndef HAVE_GETCWD*/       /* XXX enable when autoconf happens */
808     extern char *getwd();
809 #define getcwd(x,y) getwd(x)
810 #endif
811
812     if (as->parms[0].items) {   /* -file <dumpfile> */
813         dumpfile = fopen(as->parms[0].items->data, "r");
814         if (!dumpfile) {
815             fprintf(stderr, "Cannot open '%s'. Code = %d\n",
816                     as->parms[0].items->data, errno);
817             goto cleanup;
818         }
819     } else {
820         dumpfile = (FILE *) stdin;      /* use stdin */
821     }
822
823     /* Read the dump header. From it we get the volume name */
824     type = ntohl(readvalue(1));
825     if (type != 1) {
826         fprintf(stderr, "Expected DumpHeader\n");
827         code = -1;
828         goto cleanup;
829     }
830     type = ReadDumpHeader(&dh);
831
832     /* Get the root directory we restore to */
833     if (as->parms[1].items) {   /* -dir <rootdir> */
834         strcpy(rootdir, as->parms[1].items->data);
835     } else {
836         strcpy(rootdir, ".");
837     }
838     strcat(rootdir, "/");
839
840     /* Append the RW volume name to the root directory */
841     strcat(rootdir, dh.volumeName);
842     len = strlen(rootdir);
843     if (strcmp(".backup", rootdir + len - 7) == 0) {
844         rootdir[len - 7] = 0;
845     } else if (strcmp(".readonly", rootdir + len - 9) == 0) {
846         rootdir[len - 9] = 0;
847     }
848
849     /* Append the extension we asked for */
850     if (as->parms[2].items) {
851         strcat(rootdir, as->parms[2].items->data);      /* -extension <ext> */
852     }
853
854     /* The mountpoint root is either specifid in -mountpoint
855      * or -dir or the current working dir.
856      */
857     if ((as->parms[3].items) || (as->parms[1].items)) { /* -mountpoint  or -dir */
858         t = (char *)getcwd(thisdir, MAXPATHLEN);        /* remember current dir */
859         if (!t) {
860             fprintf(stderr,
861                     "Cannot get pathname of current working directory: %s\n",
862                     thisdir);
863             code = -1;
864             goto cleanup;
865         }
866         /* Change to the mount point dir */
867         code =
868             chdir((as->parms[3].items ? as->parms[3].items->data : as->
869                    parms[1].items->data));
870         if (code) {
871             fprintf(stderr, "Mount point directory not found: Error = %d\n",
872                     errno);
873             goto cleanup;
874         }
875         t = (char *)getcwd(mntroot, MAXPATHLEN);        /* get its full pathname */
876         if (!t) {
877             fprintf(stderr,
878                     "Cannot determine pathname of mount point root directory: %s\n",
879                     mntroot);
880             code = -1;
881             goto cleanup;
882         }
883         strcat(mntroot, "/");   /* append '/' to end of it */
884         code = chdir(thisdir);  /* return to original working dir */
885         if (code) {
886             fprintf(stderr, "Cannot find working directory: Error = %d\n",
887                     errno);
888             goto cleanup;
889         }
890     } else {                    /* use current directory */
891         t = (char *)getcwd(mntroot, MAXPATHLEN);        /* get full pathname of current dir */
892         if (!t) {
893             fprintf(stderr,
894                     "Cannot determine pathname of current working directory: %s\n",
895                     mntroot);
896             code = -1;
897             goto cleanup;
898         }
899     }
900     strcat(mntroot, "/");       /* append '/' to end of it */
901
902     /* Set the umask for the restore */
903     if (as->parms[4].items) {   /* -umask */
904         afs_int32 mask;
905         mask = strtol(as->parms[4].items->data, 0, 8);
906         fprintf(stderr, "Umask set to 0%03o\n", mask);
907         umask(mask);
908     }
909
910     fprintf(stderr, "Restoring volume dump of '%s' to directory '%s'.\n",
911             dh.volumeName, rootdir);
912     code = mkdir(rootdir, 0777);
913     if ((code < 0) && (errno != EEXIST)) {
914         fprintf(stderr, "Error creating directory %s  code=%d;%d\n", rootdir,
915                 code, errno);
916     }
917
918     for (count = 1; type == 2; count++) {
919         type = ReadVolumeHeader(count);
920         for (vcount = 1; type == 3; vcount++)
921             type = ReadVNode(vcount);
922     }
923
924     if (type != 4) {
925         fprintf(stderr, "Expected End-of-Dump\n");
926         code = -1;
927         goto cleanup;
928     }
929
930   cleanup:
931     /* For incremental restores, Follow each directory link and
932      * remove an "AFSFile" links.
933      */
934     if (inc_dump) {
935         fprintf(stderr, "An incremental dump.\n");
936         dirP = opendir(rootdir);
937         while (dirP && (dirE = readdir(dirP))) {
938             if (strncmp(dirE->d_name, ADIR, strlen(ADIR)) == 0) {
939                 afs_snprintf(name, sizeof name, "%s/%s", rootdir,
940                              dirE->d_name);
941                 dirQ = opendir(name);
942                 while (dirQ && (dirF = readdir(dirQ))) {
943                     if (strncmp(dirF->d_name, AFILE, strlen(AFILE)) == 0) {
944                         afs_snprintf(name, sizeof name, "%s/%s/%s", rootdir,
945                                      dirE->d_name, dirF->d_name);
946                         unlink(name);
947                     }
948                 }
949                 closedir(dirQ);
950             } else if (strncmp(dirE->d_name, AFILE, strlen(AFILE)) == 0) {
951                 afs_snprintf(name, sizeof name, "%s/%s", rootdir,
952                              dirE->d_name);
953                 unlink(name);
954             }
955         }
956         closedir(dirP);
957     }
958
959     /* Now go through and remove all the directory links */
960     dirP = opendir(rootdir);
961     while (dirP && (dirE = readdir(dirP))) {
962         if (strncmp(dirE->d_name, ADIR, strlen(ADIR)) == 0) {
963             afs_snprintf(name, sizeof name, "%s/%s", rootdir, dirE->d_name);
964             unlink(name);
965         }
966     }
967     closedir(dirP);
968
969     return (code);
970 }
971
972 main(argc, argv)
973      int argc;
974      char **argv;
975 {
976     struct cmd_syndesc *ts;
977     struct cmd_item *ti;
978
979     setlinebuf(stdout);
980
981     ts = cmd_CreateSyntax(NULL, WorkerBee, NULL, "vldb check");
982     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
983     cmd_AddParm(ts, "-dir", CMD_SINGLE, CMD_OPTIONAL, "restore dir");
984     cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL, "name extension");
985     cmd_AddParm(ts, "-mountpoint", CMD_SINGLE, CMD_OPTIONAL,
986                 "mount point root");
987     cmd_AddParm(ts, "-umask", CMD_SINGLE, CMD_OPTIONAL, "mode mask");
988
989     return cmd_Dispatch(argc, argv);
990 }