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