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 <afs/param.h>
43 #include <afs/afsint.h>
46 #include <afs/ihandle.h>
47 #include <afs/vnode.h>
48 #include <afs/volume.h>
53 #include <sys/param.h>
54 #include <sys/types.h>
58 #include <netinet/in.h>
63 char rootdir[MAXPATHLEN];
64 char mntroot[MAXPATHLEN];
65 #define ADIR "AFSDir-"
66 #define AFILE "AFSFile-"
67 #define ODIR "__ORPHANEDIR__."
68 #define OFILE "__ORPHANFILE__."
73 afs_int32 readvalue(size)
80 ptr = (char *) &value;
82 s = sizeof(value) - size;
84 fprintf (stderr, "Too much data in afs_int32\n");
88 code = fread (&ptr[s], 1, size, dumpfile);
89 if (code != size) fprintf (stderr, "Code = %d; Errno = %d\n", code, errno);
101 code = fread (&value, 1, 1, dumpfile);
102 if (code != 1) fprintf (stderr, "Code = %d; Errno = %d\n", code, errno);
107 #define BUFSIZE 16384
110 char readdata(buffer, size)
119 s = ((size > BUFSIZE) ? BUFSIZE : size);
120 code = fread(buf, 1, s, dumpfile);
121 if (code != s) fprintf (stderr, "Code = %d; Errno = %d\n", code, errno);
126 code = fread (buffer, 1, size, dumpfile);
129 fprintf (stderr, "Code = %d; Errno = %d\n", code, errno);
131 fprintf (stderr, "Read %d bytes out of %d\n", code, size);
133 if ((code >= 0) && (code < BUFSIZE))
134 buffer[size] = 0; /* Add null char at end */
138 afs_int32 ReadDumpHeader(dh)
139 struct DumpHeader *dh; /* Defined in dump.h */
145 /* bzero(&dh, sizeof(dh)); */
147 magic = ntohl(readvalue(4));
148 dh->version = ntohl(readvalue(4));
155 dh->volumeId = ntohl(readvalue(4));
159 for (i=0, c='a'; c!='\0'; i++) {
160 dh->volumeName[i] = c = readchar();
162 dh->volumeName[i] = c;
166 dh->nDumpTimes = ntohl(readvalue(2)) >> 1;
167 for (i=0; i<dh->nDumpTimes; i++) {
168 dh->dumpTimes[i].from = ntohl(readvalue(4));
169 dh->dumpTimes[i].to = ntohl(readvalue(4));
179 return((afs_int32)tag);
185 char volumeName[100];
187 afs_int32 uniquifier;
194 afs_int32 accountNumber;
196 afs_int32 creationDate;
197 afs_int32 accessDate;
198 afs_int32 updateDate;
199 afs_int32 expirationDate;
200 afs_int32 backupDate;
201 afs_int32 dayUseDate;
204 afs_int32 weekUse[100]; /* weekCount of these */
211 afs_int32 ReadVolumeHeader(count)
214 struct volumeHeader vh;
215 int code, i, done, entries;
218 /* bzero(&vh, sizeof(vh)); */
225 vh.volumeId = ntohl(readvalue(4));
229 ntohl(readvalue(4)); /* version stamp - ignore */
233 for (i=0, c='a'; c!='\0'; i++) {
234 vh.volumeName[i] = c = readchar();
236 vh.volumeName[i] = c;
240 vh.inService = ntohl(readvalue(1));
244 vh.blessed = ntohl(readvalue(1));
248 vh.uniquifier = ntohl(readvalue(4));
252 vh.volType = ntohl(readvalue(1));
256 vh.parentVol = ntohl(readvalue(4));
260 vh.cloneId = ntohl(readvalue(4));
264 vh.maxQuota = ntohl(readvalue(4));
268 vh.minQuota = ntohl(readvalue(4));
272 vh.diskUsed = ntohl(readvalue(4));
276 vh.fileCount = ntohl(readvalue(4));
280 vh.accountNumber = ntohl(readvalue(4));
284 vh.owner = ntohl(readvalue(4));
288 vh.creationDate = ntohl(readvalue(4));
292 vh.accessDate = ntohl(readvalue(4));
296 vh.updateDate = ntohl(readvalue(4));
300 vh.expirationDate = ntohl(readvalue(4));
304 vh.backupDate = ntohl(readvalue(4));
308 for (i=0, c='a'; c!='\0'; i++) {
309 vh.message[i] = c = readchar();
311 vh.volumeName[i] = c;
315 vh.weekCount = ntohl(readvalue(2));
316 for (i=0; i<vh.weekCount; i++) {
317 vh.weekUse[i] = ntohl(readvalue(4));
322 for (i=0, c='a'; c!='\0'; i++) {
323 vh.motd[i] = c = readchar();
328 vh.dayUseDate = ntohl(readvalue(4));
332 vh.dayUse = ntohl(readvalue(4));
341 return((afs_int32)tag);
347 afs_int32 uniquifier;
350 afs_int32 dataVersion;
351 afs_int32 unixModTime;
352 afs_int32 servModTime;
360 struct acl_accessList
362 int size; /*size of this access list in bytes, including MySize itself*/
363 int version; /*to deal with upward compatibility ; <= ACL_ACLVERSION*/
365 int positive; /* number of positive entries */
366 int negative; /* number of minus entries */
367 struct acl_accessEntry
369 int id; /*internally-used ID of user or group*/
377 #define MAXNAMELEN 256
379 afs_int32 ReadVNode(count)
383 int code, i, done, entries;
385 char dirname[MAXNAMELEN], linkname[MAXNAMELEN], lname[MAXNAMELEN];
386 char parentdir[MAXNAMELEN], vflink[MAXNAMELEN];
387 char filename[MAXNAMELEN], fname[MAXNAMELEN];
392 /* bzero(&vn, sizeof(vn)); */
398 vn.vnode = ntohl(readvalue(4));
399 vn.uniquifier = ntohl(readvalue(4));
406 vn.type = ntohl(readvalue(1));
410 vn.linkCount = ntohl(readvalue(2));
414 vn.dataVersion = ntohl(readvalue(4));
418 vn.unixModTime = ntohl(readvalue(4));
422 vn.servModTime = ntohl(readvalue(4));
426 vn.author = ntohl(readvalue(4));
430 vn.owner = ntohl(readvalue(4));
434 vn.group = ntohl(readvalue(4));
438 vn.modebits = ntohl(readvalue(2));
442 vn.parent = ntohl(readvalue(4));
446 readdata(vn.acl, 192); /* Skip ACL data */
450 vn.dataSize = ntohl(readvalue(4));
452 /* parentdir is the name of this dir's vnode-file-link
453 * or this file's parent vnode-file-link.
454 * "./AFSDir-<#>". It's a symbolic link to its real dir.
455 * The parent dir and symbolic link to it must exist.
457 vnode = ((vn.type == 2) ? vn.vnode : vn.parent);
459 sprintf(parentdir, "%s", rootdir);
461 sprintf(parentdir, "%s/%s%d", rootdir, ADIR, vnode);
463 len = readlink(parentdir, linkname, MAXNAMELEN);
465 /* parentdir does not exist. So create an orphan dir.
466 * and then link the parentdir to the orphaned dir.
468 sprintf(linkname, "%s/%s%d", rootdir, ODIR, vnode);
469 code = mkdir(linkname, 0777);
470 if ((code < 0) && (errno != EEXIST)) {
471 fprintf(stderr, "Error creating directory %s code=%d;%d\n",
472 linkname, code, errno);
475 /* Link the parentdir to it - now parentdir exists */
476 sprintf(linkname, "%s%d/", ODIR, vnode);
477 code = symlink(linkname, parentdir);
479 fprintf(stderr, "Error creating symlink %s -> %s code=%d;%d\n",
480 parentdir, linkname, code, errno);
487 /* We read the directory entries. If the entry is a
488 * directory, the subdir is created and the root dir
489 * will contain a link to it. If its a file, we only
490 * create a symlink in the dir to the file name.
512 unsigned short pgcount;
521 struct Pageheader header;
523 unsigned short hashTable[128];
528 struct DirHeader header;
529 struct DirEntry entry[1];
534 buffer = (char *)malloc(vn.dataSize);
536 readdata(buffer, vn.dataSize);
537 page0 = (struct Page0 *)buffer;
539 /* Step through each bucket in the hash table, i,
540 * and follow each element in the hash chain, j.
541 * This gives us each entry of the dir.
543 for (i=0; i<128; i++) {
544 for (j=ntohs(page0->header.hashTable[i]); j;
545 j=ntohs(page0->entry[j].next)) {
547 this_vn = ntohl(page0->entry[j].fid.vnode);
548 this_name = page0->entry[j].name;
550 if ((strcmp(this_name,"." ) == 0) ||
551 (strcmp(this_name,"..") == 0))
552 continue; /* Skip these */
554 /* For a directory entry, create it. Then create the
555 * link (from the rootdir) to this directory.
557 if (this_vn & 1) { /*ADIRENTRY*/
558 /* dirname is the directory to create.
559 * vflink is what will link to it.
561 sprintf(dirname, "%s/%s", parentdir, this_name);
562 sprintf(vflink, "%s/%s%d", rootdir, ADIR, this_vn);
564 /* The link and directory may already exist */
565 len = readlink(vflink, linkname, MAXNAMELEN);
567 /* Doesn't already exist - so create the directory.
568 * umask will pare the mode bits down.
570 code = mkdir(dirname, 0777);
571 if ((code < 0) && (errno != EEXIST)) {
572 fprintf(stderr, "Error creating directory %s code=%d;%d\n",
573 dirname, code, errno);
576 /* Does already exist - so move the directory.
577 * It was created originally as orphaned.
579 linkname[len-1] = '\0'; /* remove '/' at end */
580 sprintf(lname, "%s/%s", rootdir, linkname);
581 code = rename(lname, dirname);
583 fprintf(stderr, "Error renaming %s to %s code=%d;%d\n",
584 lname, dirname, code, errno);
588 /* Now create/update the link to the new/moved directory */
590 sprintf(dirname, "%s/", this_name);
592 sprintf(dirname, "%s%d/%s/", ADIR, vn.vnode, this_name);
594 code = symlink(dirname, vflink);
596 fprintf(stderr, "Error creating symlink %s -> %s code=%d;%d\n",
597 vflink, dirname, code, errno);
601 /* For a file entry, we remember the name of the file
602 * by creating a link within the directory. Restoring
603 * the file will later remove the link.
607 sprintf(vflink, "%s/%s%d", parentdir, AFILE, this_vn);
609 code = symlink(this_name, vflink);
610 if ((code < 0) && (errno != EEXIST)) {
611 fprintf(stderr, "Error creating symlink %s -> %s code=%d;%d\n",
612 vflink, page0->entry[j].name, code, errno);
620 else if (vn.type == 1) {
622 /* A file vnode. So create it into the desired directory. A
623 * link should exist in the directory naming the file.
629 /* Check if its vnode-file-link exists. If not,
630 * then the file will be an orphaned file.
633 sprintf(filename, "%s/%s%d", parentdir, AFILE, vn.vnode);
634 len = readlink(filename, fname, MAXNAMELEN);
636 sprintf(filename, "%s/%s%d", rootdir, OFILE, vn.vnode);
637 lfile = 0; /* no longer a linked file; a direct path */
640 /* Create a mode for the file. Use the owner bits and
641 * duplicate them across group and other. The umask
642 * will remove what we don't want.
644 mode = (vn.modebits >> 6) & 0x7;
645 mode |= (mode << 6) | (mode << 3);
647 /* Write the file out */
648 fid = open(filename, (O_CREAT | O_WRONLY | O_TRUNC), mode);
651 s = ((size > BUFSIZE) ? BUFSIZE : size);
652 code = fread(buf, 1, s, dumpfile);
654 write(fid, buf, code);
659 fprintf (stderr, "Code = %d; Errno = %d\n", code, errno);
661 fprintf (stderr, "Read %d bytes out of %d\n",
662 (vn.dataSize - size), vn.dataSize);
668 fprintf(stderr, " File %s (%s) is incomplete\n", filename, fname);
671 /* Remove the link to the file */
677 else if (vn.type == 3) {
679 /* A symlink vnode. So read it into the desired directory. This could
680 * also be a mount point. If the volume is being restored to AFS, this
681 * will become a mountpoint. If not, it becomes a symlink to no-where.
686 /* Check if its vnode-file-link exists and create pathname
687 * of the symbolic link. If it doesn't exist,
688 * then the link will be an orphaned link.
690 sprintf(linkname, "%s/%s%d", parentdir, AFILE, vn.vnode);
691 len = readlink(linkname, fname, MAXNAMELEN);
693 sprintf(filename, "%s/%s%d", rootdir, OFILE, vn.vnode);
696 sprintf(filename, "%s/%s", parentdir, fname);
699 /* Read the link in, delete it, and then create it */
700 readdata(buf, vn.dataSize);
702 /* If a mountpoint, change its link path to mountroot */
704 if ( ((buf[0] == '%') || (buf[0] == '#')) && (buf[s-1] == '.') ) {
705 /* This is a symbolic link */
706 buf[s-1] = 0; /* Remove prefix '.' */
707 strcpy(lname, &buf[1]); /* Remove postfix '#' or '%' */
708 strcpy(buf, mntroot);
713 code = symlink(buf, filename);
715 fprintf(stderr, "Error creating symlink %s -> %s code=%d;%d\n",
716 filename, buf, code, errno);
719 /* Remove the symbolic link */
723 fprintf (stderr, "Unknown Vnode block\n");
735 return((afs_int32)tag);
739 struct cmd_syndesc *as;
743 afs_int32 type, count, vcount;
745 struct dirent *dirE, *dirF;
746 char fname[MAXNAMELEN], name[MAXNAMELEN], lname[MAXNAMELEN], mname[MAXNAMELEN];
747 char thisdir[MAXPATHLEN], *t;
748 struct DumpHeader dh; /* Defined in dump.h */
749 #if 0/*ndef HAVE_GETCWD*/ /* XXX enable when autoconf happens */
750 extern char *getwd();
751 #define getcwd(x,y) getwd(x)
754 if (as->parms[0].items) { /* -file <dumpfile> */
755 dumpfile = fopen(as->parms[0].items->data, "r");
757 fprintf(stderr, "Cannot open '%s'. Code = %d\n",
758 as->parms[0].items->data, errno);
762 dumpfile = (FILE *)stdin; /* use stdin */
765 /* Read the dump header. From it we get the volume name */
766 type = ntohl(readvalue(1));
768 fprintf(stderr, "Expected DumpHeader\n");
772 type = ReadDumpHeader(&dh);
774 /* Get the root directory we restore to */
775 if (as->parms[1].items) { /* -dir <rootdir> */
776 strcpy(rootdir, as->parms[1].items->data);
782 /* Append the RW volume name to the root directory */
783 strcat(rootdir, dh.volumeName);
784 len = strlen(rootdir);
785 if (strcmp(".backup",rootdir+len-7) == 0) {
787 } else if (strcmp(".readonly",rootdir+len-9) == 0) {
791 /* Append the extension we asked for */
792 if (as->parms[2].items) {
793 strcat(rootdir, as->parms[2].items->data); /* -extension <ext> */
796 /* The mountpoint root is either specifid in -mountpoint
797 * or -dir or the current working dir.
799 if ((as->parms[3].items) || (as->parms[1].items)) { /* -mountpoint or -dir*/
800 t = (char *)getcwd(thisdir, MAXPATHLEN); /* remember current dir */
802 fprintf(stderr, "Cannot get pathname of current working directory: %s\n",
807 /* Change to the mount point dir */
808 code = chdir((as->parms[3].items ? as->parms[3].items->data :
809 as->parms[1].items->data));
811 fprintf(stderr, "Mount point directory not found: Error = %d\n", stderr);
814 t = (char *)getcwd(mntroot, MAXPATHLEN); /* get its full pathname */
816 fprintf(stderr, "Cannot determine pathname of mount point root directory: %s\n",
821 strcat(mntroot, "/"); /* append '/' to end of it */
822 chdir(thisdir); /* return to original working dir */
824 fprintf(stderr, "Cannot find working directory: Error = %d\n", stderr);
827 } else { /* use current directory */
828 t = (char *)getcwd(mntroot, MAXPATHLEN); /* get full pathname of current dir */
830 fprintf(stderr, "Cannot determine pathname of current working directory: %s\n",
836 strcat(mntroot, "/"); /* append '/' to end of it */
838 /* Set the umask for the restore */
839 if (as->parms[4].items) { /* -umask */
841 mask = strtol(as->parms[4].items->data, 0, 8);
842 fprintf(stderr, "Umask set to 0%03o\n", mask);
846 fprintf(stderr, "Restoring volume dump of '%s' to directory '%s'.\n",
847 dh.volumeName, rootdir);
848 code = mkdir(rootdir, 0777);
849 if ((code < 0) && (errno != EEXIST)) {
850 fprintf(stderr, "Error creating directory %s code=%d;%d\n",
851 rootdir, code, errno);
854 for (count=1; type==2; count++) {
855 type = ReadVolumeHeader(count);
856 for (vcount=1; type==3; vcount++)
857 type = ReadVNode(vcount);
861 fprintf(stderr, "Expected End-of-Dump\n");
867 /* For incremental restores, Follow each directory link and
868 * remove an "AFSFile" links.
871 fprintf(stderr, "An incremental dump.\n");
872 dirP = opendir(rootdir);
873 while (dirP && (dirE = readdir(dirP))) {
874 if (strncmp(dirE->d_name, ADIR, strlen(ADIR)) == 0) {
875 sprintf(name, "%s/%s", rootdir, dirE->d_name);
876 dirQ = opendir(name);
877 while (dirQ && (dirF = readdir(dirQ))) {
878 if (strncmp(dirF->d_name, AFILE, strlen(AFILE)) == 0) {
879 sprintf(name, "%s/%s/%s", rootdir, dirE->d_name, dirF->d_name);
884 } else if (strncmp(dirE->d_name, AFILE, strlen(AFILE)) == 0) {
885 sprintf(name, "%s/%s", rootdir, dirE->d_name);
892 /* Now go through and remove all the directory links */
893 dirP = opendir(rootdir);
894 while (dirP && (dirE = readdir(dirP))) {
895 if (strncmp(dirE->d_name, ADIR, strlen(ADIR)) == 0) {
896 sprintf(name, "%s/%s", rootdir, dirE->d_name);
909 struct cmd_syndesc *ts;
914 ts=cmd_CreateSyntax((char *)0, WorkerBee, (char *) 0, "vldb check");
915 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
916 cmd_AddParm(ts, "-dir", CMD_SINGLE, CMD_OPTIONAL, "restore dir");
917 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL, "name extension");
918 cmd_AddParm(ts, "-mountpoint", CMD_SINGLE, CMD_OPTIONAL, "mount point root");
919 cmd_AddParm(ts, "-umask", CMD_SINGLE, CMD_OPTIONAL, "mode mask");
921 return cmd_Dispatch(argc, argv);