reindent-20030715
[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_int32 size;
132 {
133     int code;
134     afs_int32 s;
135
136     if (!buffer) {
137         while (size > 0) {
138             s = ((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_int32 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         case 'f':
468             vn.dataSize = ntohl(readvalue(4));
469
470             /* parentdir is the name of this dir's vnode-file-link
471              * or this file's parent vnode-file-link.
472              * "./AFSDir-<#>". It's a symbolic link to its real dir.
473              * The parent dir and symbolic link to it must exist.
474              */
475             vnode = ((vn.type == 2) ? vn.vnode : vn.parent);
476             if (vnode == 1)
477                 strncpy(parentdir, rootdir, sizeof parentdir);
478             else {
479                 afs_snprintf(parentdir, sizeof parentdir, "%s/%s%d", rootdir,
480                              ADIR, vnode);
481
482                 len = readlink(parentdir, linkname, MAXNAMELEN);
483                 if (len < 0) {
484                     /* parentdir does not exist. So create an orphan dir.
485                      * and then link the parentdir to the orphaned dir.
486                      */
487                     afs_snprintf(linkname, sizeof linkname, "%s/%s%d",
488                                  rootdir, ODIR, vnode);
489                     code = mkdir(linkname, 0777);
490                     if ((code < 0) && (errno != EEXIST)) {
491                         fprintf(stderr,
492                                 "Error creating directory %s  code=%d;%d\n",
493                                 linkname, code, errno);
494                     }
495
496                     /* Link the parentdir to it - now parentdir exists */
497                     afs_snprintf(linkname, sizeof linkname, "%s%d/", ODIR,
498                                  vnode);
499                     code = symlink(linkname, parentdir);
500                     if (code) {
501                         fprintf(stderr,
502                                 "Error creating symlink %s -> %s  code=%d;%d\n",
503                                 parentdir, linkname, code, errno);
504                     }
505                 }
506             }
507
508             if (vn.type == 2) {
509                  /*ITSADIR*/
510                     /* We read the directory entries. If the entry is a
511                      * directory, the subdir is created and the root dir
512                      * will contain a link to it. If its a file, we only
513                      * create a symlink in the dir to the file name.
514                      */
515                 char *buffer;
516                 unsigned short j;
517                 afs_int32 this_vn;
518                 char *this_name;
519
520                 struct DirEntry {
521                     char flag;
522                     char length;
523                     unsigned short next;
524                     struct MKFid {
525                         afs_int32 vnode;
526                         afs_int32 vunique;
527                     } fid;
528                     char name[20];
529                 };
530
531                 struct Pageheader {
532                     unsigned short pgcount;
533                     unsigned short tag;
534                     char freecount;
535                     char freebitmap[8];
536                     char padding[19];
537                 };
538
539                 struct DirHeader {
540                     struct Pageheader header;
541                     char alloMap[128];
542                     unsigned short hashTable[128];
543                 };
544
545                 struct Page0 {
546                     struct DirHeader header;
547                     struct DirEntry entry[1];
548                 } *page0;
549
550
551                 buffer = NULL;
552                 buffer = (char *)malloc(vn.dataSize);
553
554                 readdata(buffer, vn.dataSize);
555                 page0 = (struct Page0 *)buffer;
556
557                 /* Step through each bucket in the hash table, i,
558                  * and follow each element in the hash chain, j.
559                  * This gives us each entry of the dir.
560                  */
561                 for (i = 0; i < 128; i++) {
562                     for (j = ntohs(page0->header.hashTable[i]); j;
563                          j = ntohs(page0->entry[j].next)) {
564                         j -= 13;
565                         this_vn = ntohl(page0->entry[j].fid.vnode);
566                         this_name = page0->entry[j].name;
567
568                         if ((strcmp(this_name, ".") == 0)
569                             || (strcmp(this_name, "..") == 0))
570                             continue;   /* Skip these */
571
572                         /* For a directory entry, create it. Then create the
573                          * link (from the rootdir) to this directory.
574                          */
575                         if (this_vn & 1) {
576                              /*ADIRENTRY*/
577                                 /* dirname is the directory to create.
578                                  * vflink is what will link to it. 
579                                  */
580                                 afs_snprintf(dirname, sizeof dirname, "%s/%s",
581                                              parentdir, this_name);
582                             afs_snprintf(vflink, sizeof vflink, "%s/%s%d",
583                                          rootdir, ADIR, this_vn);
584
585                             /* The link and directory may already exist */
586                             len = readlink(vflink, linkname, MAXNAMELEN);
587                             if (len < 0) {
588                                 /* Doesn't already exist - so create the directory.
589                                  * umask will pare the mode bits down.
590                                  */
591                                 code = mkdir(dirname, 0777);
592                                 if ((code < 0) && (errno != EEXIST)) {
593                                     fprintf(stderr,
594                                             "Error creating directory %s  code=%d;%d\n",
595                                             dirname, code, errno);
596                                 }
597                             } else {
598                                 /* Does already exist - so move the directory.
599                                  * It was created originally as orphaned.
600                                  */
601                                 linkname[len - 1] = '\0';       /* remove '/' at end */
602                                 afs_snprintf(lname, sizeof lname, "%s/%s",
603                                              rootdir, linkname);
604                                 code = rename(lname, dirname);
605                                 if (code) {
606                                     fprintf(stderr,
607                                             "Error renaming %s to %s  code=%d;%d\n",
608                                             lname, dirname, code, errno);
609                                 }
610                             }
611
612                             /* Now create/update the link to the new/moved directory */
613                             if (vn.vnode == 1)
614                                 afs_snprintf(dirname, sizeof dirname, "%s/",
615                                              this_name);
616                             else
617                                 afs_snprintf(dirname, sizeof dirname,
618                                              "%s%d/%s/", ADIR, vn.vnode,
619                                              this_name);
620                             unlink(vflink);
621                             code = symlink(dirname, vflink);
622                             if (code) {
623                                 fprintf(stderr,
624                                         "Error creating symlink %s -> %s  code=%d;%d\n",
625                                         vflink, dirname, code, errno);
626                             }
627                         }
628                         /*ADIRENTRY*/
629                             /* For a file entry, we remember the name of the file
630                              * by creating a link within the directory. Restoring
631                              * the file will later remove the link.
632                              */
633                             else {
634                              /*AFILEENTRY*/ afs_snprintf(vflink,
635                                                          sizeof vflink,
636                                                          "%s/%s%d", parentdir,
637                                                          AFILE, this_vn);
638
639                             code = symlink(this_name, vflink);
640                             if ((code < 0) && (errno != EEXIST)) {
641                                 fprintf(stderr,
642                                         "Error creating symlink %s -> %s  code=%d;%d\n",
643                                         vflink, page0->entry[j].name, code,
644                                         errno);
645                             }
646                         }
647                      /*AFILEENTRY*/}
648                 }
649                 free(buffer);
650             }
651             /*ITSADIR*/
652             else if (vn.type == 1) {
653                  /*ITSAFILE*/
654                     /* A file vnode. So create it into the desired directory. A
655                      * link should exist in the directory naming the file.
656                      */
657                 int fid;
658                 int lfile;
659                 afs_int32 size, s;
660
661                 /* Check if its vnode-file-link exists. If not,
662                  * then the file will be an orphaned file.
663                  */
664                 lfile = 1;
665                 afs_snprintf(filename, sizeof filename, "%s/%s%d", parentdir,
666                              AFILE, vn.vnode);
667                 len = readlink(filename, fname, MAXNAMELEN);
668                 if (len < 0) {
669                     afs_snprintf(filename, sizeof filename, "%s/%s%d",
670                                  rootdir, OFILE, vn.vnode);
671                     lfile = 0;  /* no longer a linked file; a direct path */
672                 }
673
674                 /* Create a mode for the file. Use the owner bits and
675                  * duplicate them across group and other. The umask
676                  * will remove what we don't want.
677                  */
678                 mode = (vn.modebits >> 6) & 0x7;
679                 mode |= (mode << 6) | (mode << 3);
680
681                 /* Write the file out */
682                 fid = open(filename, (O_CREAT | O_WRONLY | O_TRUNC), mode);
683                 size = vn.dataSize;
684                 while (size > 0) {
685                     s = ((size > BUFSIZE) ? BUFSIZE : size);
686                     code = fread(buf, 1, s, dumpfile);
687                     if (code > 0) {
688                         (void)write(fid, buf, code);
689                         size -= code;
690                     }
691                     if (code != s) {
692                         if (code < 0)
693                             fprintf(stderr, "Code = %d; Errno = %d\n", code,
694                                     errno);
695                         else {
696                             char tmp[100];
697                             (void)afs_snprintf(tmp, sizeof tmp,
698                                                "Read %llu bytes out of %llu",
699                                                (afs_uintmax_t) (vn.dataSize -
700                                                                 size),
701                                                (afs_uintmax_t) vn.dataSize);
702                             fprintf(stderr, "%s\n", tmp);
703                         }
704                         break;
705                     }
706                 }
707                 close(fid);
708                 if (size != 0) {
709                     fprintf(stderr, "   File %s (%s) is incomplete\n",
710                             filename, fname);
711                 }
712
713                 /* Remove the link to the file */
714                 if (lfile) {
715                     unlink(filename);
716                 }
717             }
718             /*ITSAFILE*/
719             else if (vn.type == 3) {
720                  /*ITSASYMLINK*/
721                     /* A symlink vnode. So read it into the desired directory. This could
722                      * also be a mount point. If the volume is being restored to AFS, this
723                      * will become a mountpoint. If not, it becomes a symlink to no-where.
724                      */
725                 int fid;
726                 afs_int32 size, s;
727
728                 /* Check if its vnode-file-link exists and create pathname
729                  * of the symbolic link. If it doesn't exist,
730                  * then the link will be an orphaned link.
731                  */
732                 afs_snprintf(linkname, sizeof linkname, "%s/%s%d", parentdir,
733                              AFILE, vn.vnode);
734                 len = readlink(linkname, fname, MAXNAMELEN);
735                 if (len < 0) {
736                     afs_snprintf(filename, sizeof filename, "%s/%s%d",
737                                  rootdir, OFILE, vn.vnode);
738                 } else {
739                     fname[len] = '\0';
740                     afs_snprintf(filename, sizeof filename, "%s/%s",
741                                  parentdir, fname);
742                 }
743
744                 /* Read the link in, delete it, and then create it */
745                 readdata(buf, vn.dataSize);
746
747                 /* If a mountpoint, change its link path to mountroot */
748                 s = strlen(buf);
749                 if (((buf[0] == '%') || (buf[0] == '#'))
750                     && (buf[s - 1] == '.')) {
751                     /* This is a symbolic link */
752                     buf[s - 1] = 0;     /* Remove prefix '.' */
753                     strcpy(lname, &buf[1]);     /* Remove postfix '#' or '%' */
754                     strcpy(buf, mntroot);
755                     strcat(buf, lname);
756                 }
757
758                 unlink(filename);
759                 code = symlink(buf, filename);
760                 if (code) {
761                     fprintf(stderr,
762                             "Error creating symlink %s -> %s  code=%d;%d\n",
763                             filename, buf, code, errno);
764                 }
765
766                 /* Remove the symbolic link */
767                 unlink(linkname);
768             }
769             /*ITSASYMLINK*/
770             else {
771                 fprintf(stderr, "Unknown Vnode block\n");
772             }
773             break;
774
775         default:
776             done = 1;
777             break;
778         }
779     }
780     if (vn.type == 0)
781         inc_dump = 1;
782
783     return ((afs_int32) tag);
784 }
785
786 WorkerBee(as, arock)
787      struct cmd_syndesc *as;
788      char *arock;
789 {
790     int code = 0, c, len;
791     afs_int32 type, count, vcount;
792     DIR *dirP, *dirQ;
793     struct dirent *dirE, *dirF;
794     char fname[MAXNAMELEN], name[MAXNAMELEN], lname[MAXNAMELEN],
795         mname[MAXNAMELEN];
796     char thisdir[MAXPATHLEN], *t;
797     struct DumpHeader dh;       /* Defined in dump.h */
798 #if 0/*ndef HAVE_GETCWD*/       /* XXX enable when autoconf happens */
799     extern char *getwd();
800 #define getcwd(x,y) getwd(x)
801 #endif
802
803     if (as->parms[0].items) {   /* -file <dumpfile> */
804         dumpfile = fopen(as->parms[0].items->data, "r");
805         if (!dumpfile) {
806             fprintf(stderr, "Cannot open '%s'. Code = %d\n",
807                     as->parms[0].items->data, errno);
808             goto cleanup;
809         }
810     } else {
811         dumpfile = (FILE *) stdin;      /* use stdin */
812     }
813
814     /* Read the dump header. From it we get the volume name */
815     type = ntohl(readvalue(1));
816     if (type != 1) {
817         fprintf(stderr, "Expected DumpHeader\n");
818         code = -1;
819         goto cleanup;
820     }
821     type = ReadDumpHeader(&dh);
822
823     /* Get the root directory we restore to */
824     if (as->parms[1].items) {   /* -dir <rootdir> */
825         strcpy(rootdir, as->parms[1].items->data);
826     } else {
827         strcpy(rootdir, ".");
828     }
829     strcat(rootdir, "/");
830
831     /* Append the RW volume name to the root directory */
832     strcat(rootdir, dh.volumeName);
833     len = strlen(rootdir);
834     if (strcmp(".backup", rootdir + len - 7) == 0) {
835         rootdir[len - 7] = 0;
836     } else if (strcmp(".readonly", rootdir + len - 9) == 0) {
837         rootdir[len - 9] = 0;
838     }
839
840     /* Append the extension we asked for */
841     if (as->parms[2].items) {
842         strcat(rootdir, as->parms[2].items->data);      /* -extension <ext> */
843     }
844
845     /* The mountpoint root is either specifid in -mountpoint
846      * or -dir or the current working dir.
847      */
848     if ((as->parms[3].items) || (as->parms[1].items)) { /* -mountpoint  or -dir */
849         t = (char *)getcwd(thisdir, MAXPATHLEN);        /* remember current dir */
850         if (!t) {
851             fprintf(stderr,
852                     "Cannot get pathname of current working directory: %s\n",
853                     thisdir);
854             code = -1;
855             goto cleanup;
856         }
857         /* Change to the mount point dir */
858         code =
859             chdir((as->parms[3].items ? as->parms[3].items->data : as->
860                    parms[1].items->data));
861         if (code) {
862             fprintf(stderr, "Mount point directory not found: Error = %d\n",
863                     errno);
864             goto cleanup;
865         }
866         t = (char *)getcwd(mntroot, MAXPATHLEN);        /* get its full pathname */
867         if (!t) {
868             fprintf(stderr,
869                     "Cannot determine pathname of mount point root directory: %s\n",
870                     mntroot);
871             code = -1;
872             goto cleanup;
873         }
874         strcat(mntroot, "/");   /* append '/' to end of it */
875         code = chdir(thisdir);  /* return to original working dir */
876         if (code) {
877             fprintf(stderr, "Cannot find working directory: Error = %d\n",
878                     errno);
879             goto cleanup;
880         }
881     } else {                    /* use current directory */
882         t = (char *)getcwd(mntroot, MAXPATHLEN);        /* get full pathname of current dir */
883         if (!t) {
884             fprintf(stderr,
885                     "Cannot determine pathname of current working directory: %s\n",
886                     mntroot);
887             code = -1;
888             goto cleanup;
889         }
890     }
891     strcat(mntroot, "/");       /* append '/' to end of it */
892
893     /* Set the umask for the restore */
894     if (as->parms[4].items) {   /* -umask */
895         afs_int32 mask;
896         mask = strtol(as->parms[4].items->data, 0, 8);
897         fprintf(stderr, "Umask set to 0%03o\n", mask);
898         umask(mask);
899     }
900
901     fprintf(stderr, "Restoring volume dump of '%s' to directory '%s'.\n",
902             dh.volumeName, rootdir);
903     code = mkdir(rootdir, 0777);
904     if ((code < 0) && (errno != EEXIST)) {
905         fprintf(stderr, "Error creating directory %s  code=%d;%d\n", rootdir,
906                 code, errno);
907     }
908
909     for (count = 1; type == 2; count++) {
910         type = ReadVolumeHeader(count);
911         for (vcount = 1; type == 3; vcount++)
912             type = ReadVNode(vcount);
913     }
914
915     if (type != 4) {
916         fprintf(stderr, "Expected End-of-Dump\n");
917         code = -1;
918         goto cleanup;
919     }
920
921   cleanup:
922     /* For incremental restores, Follow each directory link and
923      * remove an "AFSFile" links.
924      */
925     if (inc_dump) {
926         fprintf(stderr, "An incremental dump.\n");
927         dirP = opendir(rootdir);
928         while (dirP && (dirE = readdir(dirP))) {
929             if (strncmp(dirE->d_name, ADIR, strlen(ADIR)) == 0) {
930                 afs_snprintf(name, sizeof name, "%s/%s", rootdir,
931                              dirE->d_name);
932                 dirQ = opendir(name);
933                 while (dirQ && (dirF = readdir(dirQ))) {
934                     if (strncmp(dirF->d_name, AFILE, strlen(AFILE)) == 0) {
935                         afs_snprintf(name, sizeof name, "%s/%s/%s", rootdir,
936                                      dirE->d_name, dirF->d_name);
937                         unlink(name);
938                     }
939                 }
940                 closedir(dirQ);
941             } else if (strncmp(dirE->d_name, AFILE, strlen(AFILE)) == 0) {
942                 afs_snprintf(name, sizeof name, "%s/%s", rootdir,
943                              dirE->d_name);
944                 unlink(name);
945             }
946         }
947         closedir(dirP);
948     }
949
950     /* Now go through and remove all the directory links */
951     dirP = opendir(rootdir);
952     while (dirP && (dirE = readdir(dirP))) {
953         if (strncmp(dirE->d_name, ADIR, strlen(ADIR)) == 0) {
954             afs_snprintf(name, sizeof name, "%s/%s", rootdir, dirE->d_name);
955             unlink(name);
956         }
957     }
958     closedir(dirP);
959
960     return (code);
961 }
962
963 main(argc, argv)
964      int argc;
965      char **argv;
966 {
967     struct cmd_syndesc *ts;
968     struct cmd_item *ti;
969
970     setlinebuf(stdout);
971
972     ts = cmd_CreateSyntax(NULL, WorkerBee, NULL, "vldb check");
973     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
974     cmd_AddParm(ts, "-dir", CMD_SINGLE, CMD_OPTIONAL, "restore dir");
975     cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL, "name extension");
976     cmd_AddParm(ts, "-mountpoint", CMD_SINGLE, CMD_OPTIONAL,
977                 "mount point root");
978     cmd_AddParm(ts, "-umask", CMD_SINGLE, CMD_OPTIONAL, "mode mask");
979
980     return cmd_Dispatch(argc, argv);
981 }