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