2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * Read a vos dump and recreate the tree.
13 * restorevol [-file <dump file>]
14 * [-dir <restore dir>]
15 * [-extension <name extension>]
16 * [-mountpoint <mount point root>]
17 * [-umask <mode mask>]
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
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.
42 #include <afsconfig.h>
43 #include <afs/param.h>
47 #include <afs/afsint.h>
50 #include <afs/ihandle.h>
51 #include <afs/vnode.h>
52 #include <afs/volume.h>
57 #include <sys/param.h>
58 #include <sys/types.h>
62 #include <netinet/in.h>
76 char rootdir[MAXPATHLEN];
77 char mntroot[MAXPATHLEN];
78 #define ADIR "AFSDir-"
79 #define AFILE "AFSFile-"
80 #define ODIR "__ORPHANEDIR__."
81 #define OFILE "__ORPHANFILE__."
86 afs_int32 readvalue(size)
93 ptr = (char *) &value;
95 s = sizeof(value) - size;
97 fprintf (stderr, "Too much data in afs_int32\n");
101 code = fread (&ptr[s], 1, size, dumpfile);
102 if (code != size) fprintf (stderr, "Code = %d; Errno = %d\n", code, errno);
114 code = fread (&value, 1, 1, dumpfile);
115 if (code != 1) fprintf (stderr, "Code = %d; Errno = %d\n", code, errno);
120 #define BUFSIZE 16384
123 char readdata(buffer, size)
132 s = ((size > BUFSIZE) ? BUFSIZE : size);
133 code = fread(buf, 1, s, dumpfile);
134 if (code != s) fprintf (stderr, "Code = %d; Errno = %d\n", code, errno);
139 code = fread (buffer, 1, size, dumpfile);
142 fprintf (stderr, "Code = %d; Errno = %d\n", code, errno);
144 fprintf (stderr, "Read %d bytes out of %d\n", code, size);
146 if ((code >= 0) && (code < BUFSIZE))
147 buffer[size] = 0; /* Add null char at end */
151 afs_int32 ReadDumpHeader(dh)
152 struct DumpHeader *dh; /* Defined in dump.h */
158 /* memset(&dh, 0, sizeof(dh)); */
160 magic = ntohl(readvalue(4));
161 dh->version = ntohl(readvalue(4));
168 dh->volumeId = ntohl(readvalue(4));
172 for (i=0, c='a'; c!='\0'; i++) {
173 dh->volumeName[i] = c = readchar();
175 dh->volumeName[i] = c;
179 dh->nDumpTimes = ntohl(readvalue(2)) >> 1;
180 for (i=0; i<dh->nDumpTimes; i++) {
181 dh->dumpTimes[i].from = ntohl(readvalue(4));
182 dh->dumpTimes[i].to = ntohl(readvalue(4));
192 return((afs_int32)tag);
198 char volumeName[100];
200 afs_int32 uniquifier;
207 afs_int32 accountNumber;
209 afs_int32 creationDate;
210 afs_int32 accessDate;
211 afs_int32 updateDate;
212 afs_int32 expirationDate;
213 afs_int32 backupDate;
214 afs_int32 dayUseDate;
217 afs_int32 weekUse[100]; /* weekCount of these */
224 afs_int32 ReadVolumeHeader(count)
227 struct volumeHeader vh;
228 int code, i, done, entries;
231 /* memset(&vh, 0, sizeof(vh)); */
238 vh.volumeId = ntohl(readvalue(4));
242 ntohl(readvalue(4)); /* version stamp - ignore */
246 for (i=0, c='a'; c!='\0'; i++) {
247 vh.volumeName[i] = c = readchar();
249 vh.volumeName[i] = c;
253 vh.inService = ntohl(readvalue(1));
257 vh.blessed = ntohl(readvalue(1));
261 vh.uniquifier = ntohl(readvalue(4));
265 vh.volType = ntohl(readvalue(1));
269 vh.parentVol = ntohl(readvalue(4));
273 vh.cloneId = ntohl(readvalue(4));
277 vh.maxQuota = ntohl(readvalue(4));
281 vh.minQuota = ntohl(readvalue(4));
285 vh.diskUsed = ntohl(readvalue(4));
289 vh.fileCount = ntohl(readvalue(4));
293 vh.accountNumber = ntohl(readvalue(4));
297 vh.owner = ntohl(readvalue(4));
301 vh.creationDate = ntohl(readvalue(4));
305 vh.accessDate = ntohl(readvalue(4));
309 vh.updateDate = ntohl(readvalue(4));
313 vh.expirationDate = ntohl(readvalue(4));
317 vh.backupDate = ntohl(readvalue(4));
321 for (i=0, c='a'; c!='\0'; i++) {
322 vh.message[i] = c = readchar();
324 vh.volumeName[i] = c;
328 vh.weekCount = ntohl(readvalue(2));
329 for (i=0; i<vh.weekCount; i++) {
330 vh.weekUse[i] = ntohl(readvalue(4));
335 for (i=0, c='a'; c!='\0'; i++) {
336 vh.motd[i] = c = readchar();
341 vh.dayUseDate = ntohl(readvalue(4));
345 vh.dayUse = ntohl(readvalue(4));
354 return((afs_int32)tag);
360 afs_int32 uniquifier;
363 afs_int32 dataVersion;
364 afs_int32 unixModTime;
365 afs_int32 servModTime;
373 struct acl_accessList
375 int size; /*size of this access list in bytes, including MySize itself*/
376 int version; /*to deal with upward compatibility ; <= ACL_ACLVERSION*/
378 int positive; /* number of positive entries */
379 int negative; /* number of minus entries */
380 struct acl_accessEntry
382 int id; /*internally-used ID of user or group*/
390 #define MAXNAMELEN 256
392 afs_int32 ReadVNode(count)
396 int code, i, done, entries;
398 char dirname[MAXNAMELEN], linkname[MAXNAMELEN], lname[MAXNAMELEN];
399 char parentdir[MAXNAMELEN], vflink[MAXNAMELEN];
400 char filename[MAXNAMELEN], fname[MAXNAMELEN];
405 /* memset(&vn, 0, sizeof(vn)); */
411 vn.vnode = ntohl(readvalue(4));
412 vn.uniquifier = ntohl(readvalue(4));
419 vn.type = ntohl(readvalue(1));
423 vn.linkCount = ntohl(readvalue(2));
427 vn.dataVersion = ntohl(readvalue(4));
431 vn.unixModTime = ntohl(readvalue(4));
435 vn.servModTime = ntohl(readvalue(4));
439 vn.author = ntohl(readvalue(4));
443 vn.owner = ntohl(readvalue(4));
447 vn.group = ntohl(readvalue(4));
451 vn.modebits = ntohl(readvalue(2));
455 vn.parent = ntohl(readvalue(4));
459 readdata(vn.acl, 192); /* Skip ACL data */
463 vn.dataSize = ntohl(readvalue(4));
465 /* parentdir is the name of this dir's vnode-file-link
466 * or this file's parent vnode-file-link.
467 * "./AFSDir-<#>". It's a symbolic link to its real dir.
468 * The parent dir and symbolic link to it must exist.
470 vnode = ((vn.type == 2) ? vn.vnode : vn.parent);
472 sprintf(parentdir, "%s", rootdir);
474 sprintf(parentdir, "%s/%s%d", rootdir, ADIR, vnode);
476 len = readlink(parentdir, linkname, MAXNAMELEN);
478 /* parentdir does not exist. So create an orphan dir.
479 * and then link the parentdir to the orphaned dir.
481 sprintf(linkname, "%s/%s%d", rootdir, ODIR, vnode);
482 code = mkdir(linkname, 0777);
483 if ((code < 0) && (errno != EEXIST)) {
484 fprintf(stderr, "Error creating directory %s code=%d;%d\n",
485 linkname, code, errno);
488 /* Link the parentdir to it - now parentdir exists */
489 sprintf(linkname, "%s%d/", ODIR, vnode);
490 code = symlink(linkname, parentdir);
492 fprintf(stderr, "Error creating symlink %s -> %s code=%d;%d\n",
493 parentdir, linkname, code, errno);
500 /* We read the directory entries. If the entry is a
501 * directory, the subdir is created and the root dir
502 * will contain a link to it. If its a file, we only
503 * create a symlink in the dir to the file name.
525 unsigned short pgcount;
534 struct Pageheader header;
536 unsigned short hashTable[128];
541 struct DirHeader header;
542 struct DirEntry entry[1];
547 buffer = (char *)malloc(vn.dataSize);
549 readdata(buffer, vn.dataSize);
550 page0 = (struct Page0 *)buffer;
552 /* Step through each bucket in the hash table, i,
553 * and follow each element in the hash chain, j.
554 * This gives us each entry of the dir.
556 for (i=0; i<128; i++) {
557 for (j=ntohs(page0->header.hashTable[i]); j;
558 j=ntohs(page0->entry[j].next)) {
560 this_vn = ntohl(page0->entry[j].fid.vnode);
561 this_name = page0->entry[j].name;
563 if ((strcmp(this_name,"." ) == 0) ||
564 (strcmp(this_name,"..") == 0))
565 continue; /* Skip these */
567 /* For a directory entry, create it. Then create the
568 * link (from the rootdir) to this directory.
570 if (this_vn & 1) { /*ADIRENTRY*/
571 /* dirname is the directory to create.
572 * vflink is what will link to it.
574 sprintf(dirname, "%s/%s", parentdir, this_name);
575 sprintf(vflink, "%s/%s%d", rootdir, ADIR, this_vn);
577 /* The link and directory may already exist */
578 len = readlink(vflink, linkname, MAXNAMELEN);
580 /* Doesn't already exist - so create the directory.
581 * umask will pare the mode bits down.
583 code = mkdir(dirname, 0777);
584 if ((code < 0) && (errno != EEXIST)) {
585 fprintf(stderr, "Error creating directory %s code=%d;%d\n",
586 dirname, code, errno);
589 /* Does already exist - so move the directory.
590 * It was created originally as orphaned.
592 linkname[len-1] = '\0'; /* remove '/' at end */
593 sprintf(lname, "%s/%s", rootdir, linkname);
594 code = rename(lname, dirname);
596 fprintf(stderr, "Error renaming %s to %s code=%d;%d\n",
597 lname, dirname, code, errno);
601 /* Now create/update the link to the new/moved directory */
603 sprintf(dirname, "%s/", this_name);
605 sprintf(dirname, "%s%d/%s/", ADIR, vn.vnode, this_name);
607 code = symlink(dirname, vflink);
609 fprintf(stderr, "Error creating symlink %s -> %s code=%d;%d\n",
610 vflink, dirname, code, errno);
614 /* For a file entry, we remember the name of the file
615 * by creating a link within the directory. Restoring
616 * the file will later remove the link.
620 sprintf(vflink, "%s/%s%d", parentdir, AFILE, this_vn);
622 code = symlink(this_name, vflink);
623 if ((code < 0) && (errno != EEXIST)) {
624 fprintf(stderr, "Error creating symlink %s -> %s code=%d;%d\n",
625 vflink, page0->entry[j].name, code, errno);
633 else if (vn.type == 1) {
635 /* A file vnode. So create it into the desired directory. A
636 * link should exist in the directory naming the file.
642 /* Check if its vnode-file-link exists. If not,
643 * then the file will be an orphaned file.
646 sprintf(filename, "%s/%s%d", parentdir, AFILE, vn.vnode);
647 len = readlink(filename, fname, MAXNAMELEN);
649 sprintf(filename, "%s/%s%d", rootdir, OFILE, vn.vnode);
650 lfile = 0; /* no longer a linked file; a direct path */
653 /* Create a mode for the file. Use the owner bits and
654 * duplicate them across group and other. The umask
655 * will remove what we don't want.
657 mode = (vn.modebits >> 6) & 0x7;
658 mode |= (mode << 6) | (mode << 3);
660 /* Write the file out */
661 fid = open(filename, (O_CREAT | O_WRONLY | O_TRUNC), mode);
664 s = ((size > BUFSIZE) ? BUFSIZE : size);
665 code = fread(buf, 1, s, dumpfile);
667 write(fid, buf, code);
672 fprintf (stderr, "Code = %d; Errno = %d\n", code, errno);
674 fprintf (stderr, "Read %d bytes out of %d\n",
675 (vn.dataSize - size), vn.dataSize);
681 fprintf(stderr, " File %s (%s) is incomplete\n", filename, fname);
684 /* Remove the link to the file */
690 else if (vn.type == 3) {
692 /* A symlink vnode. So read it into the desired directory. This could
693 * also be a mount point. If the volume is being restored to AFS, this
694 * will become a mountpoint. If not, it becomes a symlink to no-where.
699 /* Check if its vnode-file-link exists and create pathname
700 * of the symbolic link. If it doesn't exist,
701 * then the link will be an orphaned link.
703 sprintf(linkname, "%s/%s%d", parentdir, AFILE, vn.vnode);
704 len = readlink(linkname, fname, MAXNAMELEN);
706 sprintf(filename, "%s/%s%d", rootdir, OFILE, vn.vnode);
709 sprintf(filename, "%s/%s", parentdir, fname);
712 /* Read the link in, delete it, and then create it */
713 readdata(buf, vn.dataSize);
715 /* If a mountpoint, change its link path to mountroot */
717 if ( ((buf[0] == '%') || (buf[0] == '#')) && (buf[s-1] == '.') ) {
718 /* This is a symbolic link */
719 buf[s-1] = 0; /* Remove prefix '.' */
720 strcpy(lname, &buf[1]); /* Remove postfix '#' or '%' */
721 strcpy(buf, mntroot);
726 code = symlink(buf, filename);
728 fprintf(stderr, "Error creating symlink %s -> %s code=%d;%d\n",
729 filename, buf, code, errno);
732 /* Remove the symbolic link */
736 fprintf (stderr, "Unknown Vnode block\n");
748 return((afs_int32)tag);
752 struct cmd_syndesc *as;
756 afs_int32 type, count, vcount;
758 struct dirent *dirE, *dirF;
759 char fname[MAXNAMELEN], name[MAXNAMELEN], lname[MAXNAMELEN], mname[MAXNAMELEN];
760 char thisdir[MAXPATHLEN], *t;
761 struct DumpHeader dh; /* Defined in dump.h */
762 #if 0/*ndef HAVE_GETCWD*/ /* XXX enable when autoconf happens */
763 extern char *getwd();
764 #define getcwd(x,y) getwd(x)
767 if (as->parms[0].items) { /* -file <dumpfile> */
768 dumpfile = fopen(as->parms[0].items->data, "r");
770 fprintf(stderr, "Cannot open '%s'. Code = %d\n",
771 as->parms[0].items->data, errno);
775 dumpfile = (FILE *)stdin; /* use stdin */
778 /* Read the dump header. From it we get the volume name */
779 type = ntohl(readvalue(1));
781 fprintf(stderr, "Expected DumpHeader\n");
785 type = ReadDumpHeader(&dh);
787 /* Get the root directory we restore to */
788 if (as->parms[1].items) { /* -dir <rootdir> */
789 strcpy(rootdir, as->parms[1].items->data);
795 /* Append the RW volume name to the root directory */
796 strcat(rootdir, dh.volumeName);
797 len = strlen(rootdir);
798 if (strcmp(".backup",rootdir+len-7) == 0) {
800 } else if (strcmp(".readonly",rootdir+len-9) == 0) {
804 /* Append the extension we asked for */
805 if (as->parms[2].items) {
806 strcat(rootdir, as->parms[2].items->data); /* -extension <ext> */
809 /* The mountpoint root is either specifid in -mountpoint
810 * or -dir or the current working dir.
812 if ((as->parms[3].items) || (as->parms[1].items)) { /* -mountpoint or -dir*/
813 t = (char *)getcwd(thisdir, MAXPATHLEN); /* remember current dir */
815 fprintf(stderr, "Cannot get pathname of current working directory: %s\n",
820 /* Change to the mount point dir */
821 code = chdir((as->parms[3].items ? as->parms[3].items->data :
822 as->parms[1].items->data));
824 fprintf(stderr, "Mount point directory not found: Error = %d\n", stderr);
827 t = (char *)getcwd(mntroot, MAXPATHLEN); /* get its full pathname */
829 fprintf(stderr, "Cannot determine pathname of mount point root directory: %s\n",
834 strcat(mntroot, "/"); /* append '/' to end of it */
835 chdir(thisdir); /* return to original working dir */
837 fprintf(stderr, "Cannot find working directory: Error = %d\n", stderr);
840 } else { /* use current directory */
841 t = (char *)getcwd(mntroot, MAXPATHLEN); /* get full pathname of current dir */
843 fprintf(stderr, "Cannot determine pathname of current working directory: %s\n",
849 strcat(mntroot, "/"); /* append '/' to end of it */
851 /* Set the umask for the restore */
852 if (as->parms[4].items) { /* -umask */
854 mask = strtol(as->parms[4].items->data, 0, 8);
855 fprintf(stderr, "Umask set to 0%03o\n", mask);
859 fprintf(stderr, "Restoring volume dump of '%s' to directory '%s'.\n",
860 dh.volumeName, rootdir);
861 code = mkdir(rootdir, 0777);
862 if ((code < 0) && (errno != EEXIST)) {
863 fprintf(stderr, "Error creating directory %s code=%d;%d\n",
864 rootdir, code, errno);
867 for (count=1; type==2; count++) {
868 type = ReadVolumeHeader(count);
869 for (vcount=1; type==3; vcount++)
870 type = ReadVNode(vcount);
874 fprintf(stderr, "Expected End-of-Dump\n");
880 /* For incremental restores, Follow each directory link and
881 * remove an "AFSFile" links.
884 fprintf(stderr, "An incremental dump.\n");
885 dirP = opendir(rootdir);
886 while (dirP && (dirE = readdir(dirP))) {
887 if (strncmp(dirE->d_name, ADIR, strlen(ADIR)) == 0) {
888 sprintf(name, "%s/%s", rootdir, dirE->d_name);
889 dirQ = opendir(name);
890 while (dirQ && (dirF = readdir(dirQ))) {
891 if (strncmp(dirF->d_name, AFILE, strlen(AFILE)) == 0) {
892 sprintf(name, "%s/%s/%s", rootdir, dirE->d_name, dirF->d_name);
897 } else if (strncmp(dirE->d_name, AFILE, strlen(AFILE)) == 0) {
898 sprintf(name, "%s/%s", rootdir, dirE->d_name);
905 /* Now go through and remove all the directory links */
906 dirP = opendir(rootdir);
907 while (dirP && (dirE = readdir(dirP))) {
908 if (strncmp(dirE->d_name, ADIR, strlen(ADIR)) == 0) {
909 sprintf(name, "%s/%s", rootdir, dirE->d_name);
922 struct cmd_syndesc *ts;
927 ts=cmd_CreateSyntax(NULL, WorkerBee, NULL, "vldb check");
928 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
929 cmd_AddParm(ts, "-dir", CMD_SINGLE, CMD_OPTIONAL, "restore dir");
930 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL, "name extension");
931 cmd_AddParm(ts, "-mountpoint", CMD_SINGLE, CMD_OPTIONAL, "mount point root");
932 cmd_AddParm(ts, "-umask", CMD_SINGLE, CMD_OPTIONAL, "mode mask");
934 return cmd_Dispatch(argc, argv);