home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / jove / part07 / recover.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-02-03  |  13.7 KB  |  726 lines

  1. /************************************************************************
  2.  * This program is Copyright (C) 1986 by Jonathan Payne.  JOVE is       *
  3.  * provided to you without charge, and with no warranty.  You may give  *
  4.  * away copies of JOVE, including sources, provided that this notice is *
  5.  * included in all the files.                                           *
  6.  ************************************************************************/
  7.  
  8. /* Recovers JOVE files after a system/editor crash.
  9.    Usage: recover [-d directory] [-syscrash]
  10.    The -syscrash option is specified in /etc/rc and what it does it
  11.    move all the jove tmp files from TMP_DIR to REC_DIR.
  12.  
  13.    The -d option lets you specify the directory to search for tmp files when
  14.    the default isn't the right one.
  15.  
  16.    Look in Makefile to change the default directories. */
  17.  
  18. #include <stdio.h>    /* Do stdio first so it doesn't override OUR
  19.                definitions. */
  20. #undef EOF
  21. #undef BUFSIZ
  22. #undef putchar
  23.  
  24. #include "jove.h"
  25. #include "temp.h"
  26. #include "rec.h"
  27. #include <signal.h>
  28. #include <sys/file.h>
  29. #include <sys/stat.h>
  30. #include <sys/dir.h>
  31.  
  32. #ifndef L_SET
  33. #    define L_SET    0
  34. #    define L_INCR    1
  35. #endif
  36.  
  37. char    blk_buf[BUFSIZ];
  38. int    nleft;
  39. FILE    *ptrs_fp;
  40. int    data_fd;
  41. struct rec_head    Header;
  42. char    datafile[40],
  43.     pntrfile[40];
  44. long    Nchars,
  45.     Nlines;
  46. char    tty[] = "/dev/tty";
  47. int    UserID,
  48.     Verbose = 0;
  49. char    *Directory = 0;        /* the directory we're looking in */
  50.  
  51. struct file_pair {
  52.     char    *file_data,
  53.         *file_rec;
  54. #define INSPECTED    01
  55.     int    file_flags;
  56.     struct file_pair    *file_next;
  57. } *First = 0,
  58.   *Last = 0;
  59.  
  60. struct rec_entry    *buflist[100] = {0};
  61.  
  62. #ifndef BSD4_2
  63.  
  64. typedef struct {
  65.     int    d_fd;        /* File descriptor for this directory */
  66. } DIR;
  67.  
  68. DIR *
  69. opendir(dir)
  70. char    *dir;
  71. {
  72.     DIR    *dp = (DIR *) malloc(sizeof *dp);
  73.  
  74.     if ((dp->d_fd = open(dir, 0)) == -1)
  75.         return NULL;
  76.     return dp;
  77. }
  78.  
  79. closedir(dp)
  80. DIR    *dp;
  81. {
  82.     (void) close(dp->d_fd);
  83.     free(dp);
  84. }
  85.  
  86. struct direct *
  87. readdir(dp)
  88. DIR    *dp;
  89. {
  90.     static struct direct    dir;
  91.  
  92.     do
  93.         if (read(dp->d_fd, &dir, sizeof dir) != sizeof dir)
  94.             return NULL;
  95.     while (dir.d_ino == 0);
  96.  
  97.     return &dir;
  98. }
  99.  
  100. #endif BSD4_2
  101.  
  102. /* Get a line at `tl' in the tmp file into `buf' which should be LBSIZE
  103.    long. */
  104.  
  105. getline(tl, buf)
  106. disk_line    tl;
  107. char    *buf;
  108. {
  109.     register char    *bp,
  110.             *lp;
  111.     register int    nl;
  112.     char    *getblock();
  113.  
  114.     lp = buf;
  115.     bp = getblock(tl >> 1);
  116.     nl = nleft;
  117.     tl = blk_round(tl);
  118.  
  119.     while (*lp++ = *bp++) {
  120.         if (--nl == 0) {
  121.             tl = forward_block(tl);
  122.             bp = getblock(tl >> 1);
  123.             nl = nleft;
  124.         }
  125.     }
  126. }
  127.  
  128. char *
  129. getblock(atl)
  130. disk_line    atl;
  131. {
  132.     int    bno,
  133.         off;
  134.     static int    curblock = -1;
  135.  
  136.     bno = daddr_to_bno(atl);
  137.     off = daddr_to_off(atl);
  138.     nleft = BUFSIZ - off;
  139.  
  140.     if (bno != curblock) {
  141.         lseek(data_fd, (long) bno * BUFSIZ, L_SET);
  142.         read(data_fd, blk_buf, BUFSIZ);
  143.         curblock = bno;
  144.     }
  145.     return blk_buf + off;
  146. }
  147.  
  148. char *
  149. copystr(s)
  150. char    *s;
  151. {
  152.     char    *str;
  153.  
  154.     str = malloc(strlen(s) + 1);
  155.     strcpy(str, s);
  156.  
  157.     return str;
  158. }
  159.  
  160. /* Scandir returns the number of entries or -1 if the directory cannoot
  161.    be opened or malloc fails. */
  162.  
  163. scandir(dir, nmptr, qualify, sorter)
  164. char    *dir;
  165. struct direct    ***nmptr;
  166. int    (*qualify)();
  167. struct direct    *(*sorter)();
  168. {
  169.     DIR    *dirp;
  170.     struct direct    *entry,
  171.             **ourarray;
  172.     int    nalloc = 10,
  173.         nentries = 0;
  174.  
  175.     if ((dirp = opendir(dir)) == NULL)
  176.         return -1;
  177.     ourarray = (struct direct **) malloc(nalloc * sizeof (struct direct *));
  178.     while ((entry = readdir(dirp)) != NULL) {
  179.         if (qualify != 0 && (*qualify)(entry) == 0)
  180.             continue;
  181.         if (nentries == nalloc) {
  182.             ourarray = (struct direct **) realloc(ourarray, (nalloc += 10) * sizeof (struct direct));
  183.             if (ourarray == NULL)
  184.                 return -1;
  185.         }
  186.         ourarray[nentries] = (struct direct *) malloc(sizeof *entry);
  187.         *ourarray[nentries] = *entry;
  188.         nentries++;
  189.     }
  190.     closedir(dirp);
  191.     if (nentries != nalloc)
  192.         ourarray = (struct direct **) realloc(ourarray,
  193.                     (nentries * sizeof (struct direct)));
  194.     if (sorter != 0)
  195.         qsort(ourarray, nentries, sizeof (struct direct **), sorter);
  196.     *nmptr = ourarray;
  197.  
  198.     return nentries;
  199. }
  200.  
  201. alphacomp(a, b)
  202. struct direct    **a,
  203.         **b;
  204. {
  205.     return strcmp((*a)->d_name, (*b)->d_name);
  206. }
  207.  
  208. char    *CurDir;
  209.  
  210. /* Scan the DIRNAME directory for jove tmp files, and make a linked list
  211.    out of them. */
  212.  
  213. get_files(dirname)
  214. char    *dirname;
  215. {
  216.     int    add_name();
  217.     struct direct    **nmptr;
  218.  
  219.     CurDir = dirname;
  220.     scandir(dirname, &nmptr, add_name, (int (*)())0);
  221. }
  222.  
  223. add_name(dp)
  224. struct direct    *dp;
  225. {
  226.     char    dfile[128],
  227.         rfile[128];
  228.     struct file_pair    *fp;
  229.     struct rec_head        header;
  230.     int    fd;
  231.  
  232.     if (strncmp(dp->d_name, "jrec", 4) != 0)
  233.         return 0;
  234.     /* If we get here, we found a "recover" tmp file, so now
  235.        we look for the corresponding "data" tmp file.  First,
  236.        though, we check to see whether there is anything in
  237.        the "recover" file.  If it's 0 length, there's no point
  238.        in saving its name. */
  239.     (void) sprintf(rfile, "%s/%s", CurDir, dp->d_name);
  240.     (void) sprintf(dfile, "%s/jove%s", CurDir, dp->d_name + 4);
  241.     if ((fd = open(rfile, 0)) != -1) {
  242.         if ((read(fd, (char *) &header, sizeof header) != sizeof header)) {
  243.             close(fd);
  244.                 return 0;
  245.         } else
  246.             close(fd);
  247.     }
  248.     if (access(dfile, 0) != 0) {
  249.         fprintf(stderr, "recover: can't find the data file for %s/%s\n", Directory, dp->d_name);
  250.         fprintf(stderr, "so deleting...\n");
  251.         (void) unlink(rfile);
  252.         (void) unlink(dfile);
  253.         return 0;
  254.     }
  255.     /* If we get here, we've found both files, so we put them
  256.        in the list. */
  257.     fp = (struct file_pair *) malloc (sizeof *fp);
  258.     if ((char *) fp == 0) {
  259.         fprintf(stderr, "recover: cannot malloc for file_pair.\n");
  260.         exit(-1);
  261.     }
  262.     fp->file_data = copystr(dfile);
  263.     fp->file_rec = copystr(rfile);
  264.     fp->file_flags = 0;
  265.     fp->file_next = First;
  266.     First = fp;
  267.  
  268.     return 1;
  269. }
  270.  
  271. options()
  272. {
  273.     printf("Options are:\n");
  274.     printf("    ?        list options.\n");
  275.     printf("    get        get a buffer to a file.\n");
  276.     printf("    list        list known buffers.\n");
  277.     printf("    print        print a buffer to terminal.\n");
  278.     printf("    quit        quit and delete jove tmp files.\n");
  279.     printf("    restore        restore all buffers.\n");
  280. }
  281.  
  282. /* Returns a legitimate buffer # */
  283.  
  284. struct rec_entry **
  285. getsrc()
  286. {
  287.     char    name[128];
  288.     int    number;
  289.  
  290.     for (;;) {
  291.         tellme("Which buffer ('?' for list)? ", name);
  292.         if (name[0] == '?')
  293.             list();
  294.         else if (name[0] == '\0')
  295.             return 0;
  296.         else if ((number = atoi(name)) > 0 && number <= Header.Nbuffers)
  297.             return &buflist[number];
  298.         else {
  299.             int    i;
  300.  
  301.             for (i = 1; i <= Header.Nbuffers; i++)
  302.                 if (strcmp(buflist[i]->r_bname, name) == 0)
  303.                     return &buflist[i];
  304.             printf("%s: unknown buffer.\n", name);
  305.         }
  306.     }
  307. }
  308.  
  309. /* Get a destination file name. */
  310.  
  311. static char *
  312. getdest()
  313. {
  314.     static char    filebuf[256];
  315.  
  316.     tellme("Output file: ", filebuf);
  317.     if (filebuf[0] == '\0')
  318.         return 0;
  319.     return filebuf;
  320. }
  321.  
  322. #include "ctype.h"
  323.  
  324. char *
  325. readword(buf)
  326. char    *buf;
  327. {
  328.     int    c;
  329.     char    *bp = buf;
  330.  
  331.     while (index(" \t\n", c = getchar()))
  332.         ;
  333.  
  334.     do {
  335.         if (index(" \t\n", c))
  336.             break;
  337.         *bp++ = c;
  338.     } while ((c = getchar()) != EOF);
  339.     *bp = 0;
  340.  
  341.     return buf;
  342. }
  343.  
  344. tellme(quest, answer)
  345. char    *quest,
  346.     *answer;
  347. {
  348.     if (stdin->_cnt <= 0) {
  349.         printf("%s", quest);
  350.         fflush(stdout);
  351.     }
  352.     readword(answer);
  353. }
  354.  
  355. /* Print the specified file to strandard output. */
  356.  
  357. jmp_buf    int_env;
  358.  
  359. catch()
  360. {
  361.     longjmp(int_env, 1);
  362. }
  363.  
  364. restore()
  365. {
  366.     register int    i;
  367.     char    tofile[100],
  368.         answer[30];
  369.     int    nrecovered = 0;
  370.  
  371.     for (i = 1; i <= Header.Nbuffers; i++) {
  372.         (void) sprintf(tofile, "#%s", buflist[i]->r_bname);
  373. tryagain:
  374.         printf("Restoring %s to %s, okay?", buflist[i]->r_bname,
  375.                              tofile);
  376.         tellme(" ", answer);
  377.         switch (answer[0]) {
  378.         case 'y':
  379.             break;
  380.  
  381.         case 'n':
  382.             continue;
  383.  
  384.         default:
  385.             tellme("What file should I use instead? ", tofile);
  386.             goto tryagain;
  387.         }
  388.         get(&buflist[i], tofile);
  389.         nrecovered++;
  390.     }
  391.     printf("Recovered %d buffers.\n", nrecovered);
  392. }
  393.  
  394. get(src, dest)
  395. struct rec_entry    **src;
  396. char    *dest;
  397. {
  398.     FILE    *outfile;
  399.  
  400.     if (src == 0 || dest == 0)
  401.         return;
  402.     (void) signal(SIGINT, catch);
  403.     if (setjmp(int_env) == 0) {
  404.         if ((outfile = fopen(dest, "w")) == NULL) {
  405.             printf("recover: cannot create %s.\n", dest);
  406.             return;
  407.         }
  408.         seekto(src - buflist);
  409.         if (dest != tty)
  410.             printf("\"%s\"", dest);
  411.         dump_file(outfile);
  412.     } else
  413.         printf("\nAborted!\n");
  414.     fclose(outfile);
  415.     if (dest != tty)
  416.         printf(" %ld lines, %ld characters.\n", Nlines, Nchars);
  417.     (void) signal(SIGINT, SIG_DFL);
  418. }
  419.  
  420. char **
  421. scanvec(args, str)
  422. register char    **args,
  423.         *str;
  424. {
  425.     while (*args) {
  426.         if (strcmp(*args, str) == 0)
  427.             return args;
  428.         args++;
  429.     }
  430.     return 0;
  431. }
  432.  
  433. read_rec(recptr)
  434. struct rec_entry    *recptr;
  435. {
  436.     if (fread((char *) recptr, sizeof *recptr, 1, ptrs_fp) != 1)
  437.         fprintf(stderr, "recover: cannot read record.\n");
  438. }
  439.  
  440. seekto(which)
  441. {
  442.     struct rec_entry    rec;
  443.  
  444.     fseek(ptrs_fp, (long) (sizeof Header), L_SET);
  445.     
  446.     while (which-- > 1) {
  447.         read_rec(&rec);
  448.         if (fseek(ptrs_fp, (long) rec.r_nlines * sizeof (disk_line),
  449.             L_INCR) == -1)
  450.             printf("recover: improper fseek!\n");
  451.     }
  452. }
  453.  
  454. makblist()
  455. {
  456.     int    i;
  457.  
  458.     for (i = 1; i <= Header.Nbuffers; i++) {
  459.         seekto(i);
  460.         if (buflist[i] == 0)
  461.             buflist[i] = (struct rec_entry *) malloc (sizeof (struct rec_entry));
  462.         read_rec(buflist[i]);
  463.     }
  464.     if (buflist[i]) {
  465.         free((char *) buflist[i]);
  466.         buflist[i] = 0;
  467.     }
  468. }
  469.  
  470. disk_line
  471. getaddr(fp)
  472. register FILE    *fp;
  473. {
  474.     register int    nchars = sizeof (disk_line);
  475.     disk_line    addr;
  476.     register char    *cp = (char *) &addr;
  477.  
  478.     while (--nchars >= 0)
  479.         *cp++ = getc(fp);
  480.  
  481.     return addr;
  482. }
  483.  
  484. dump_file(out)
  485. FILE    *out;
  486. {
  487.     struct rec_entry    record;
  488.     register int    nlines;
  489.     register disk_line    daddr;
  490.     char    buf[BUFSIZ];
  491.  
  492.     read_rec(&record);
  493.     nlines = record.r_nlines;
  494.     Nchars = Nlines = 0L;
  495.     while (--nlines >= 0) {
  496.         daddr = getaddr(ptrs_fp);
  497.         getline(daddr, buf);
  498.         Nlines++;
  499.         Nchars += 1 + strlen(buf);
  500.         fputs(buf, out);
  501.         if (nlines > 0)
  502.             fputc('\n', out);
  503.     }
  504.     if (out != stdout)
  505.         fclose(out);
  506. }
  507.  
  508. /* List all the buffers. */
  509.  
  510. list()
  511. {
  512.     int    i;
  513.  
  514.     for (i = 1; i <= Header.Nbuffers; i++)
  515.         printf("%d) buffer %s  \"%s\" (%d lines)\n", i,
  516.             buflist[i]->r_bname,
  517.             buflist[i]->r_fname,
  518.             buflist[i]->r_nlines);
  519. }
  520.  
  521. doit(fp)
  522. struct file_pair    *fp;
  523. {
  524.     char    answer[30];
  525.     char    *datafile = fp->file_data,
  526.         *pntrfile = fp->file_rec;
  527.  
  528.     ptrs_fp = fopen(pntrfile, "r");
  529.     if (ptrs_fp == NULL) {
  530.         if (Verbose)
  531.             fprintf(stderr, "recover: cannot read rec file (%s).\n", pntrfile);
  532.         return 0;
  533.     }
  534.     fread((char *) &Header, sizeof Header, 1, ptrs_fp);
  535.     if (Header.Uid != UserID)
  536.         return 0;
  537.  
  538.     /* Don't ask about JOVE's that are still running ... */
  539. #ifdef KILL0
  540.     if (kill(Header.Pid, 0) == 0)
  541.         return 0;
  542. #else
  543. #ifdef LSRHS
  544.     if (pexist(Header.Pid))
  545.         return 0;
  546. #endif LSRHS
  547. #endif KILL0
  548.  
  549.     if (Header.Nbuffers == 0) {
  550.         printf("There are no modified buffers in %s; should I delete the tmp file?", pntrfile);
  551.         ask_del(" ", fp);
  552.         return 1;
  553.     }
  554.         
  555.     if (Header.Nbuffers < 0) {
  556.         fprintf(stderr, "recover: %s doesn't look like a jove file.\n", pntrfile);
  557.         ask_del("Should I delete it? ", fp);
  558.         return 1;    /* We'll, we sort of found something. */
  559.     }
  560.     printf("Found %d buffer%s last updated: %s",
  561.         Header.Nbuffers,
  562.         Header.Nbuffers != 1 ? "s" : "",
  563.         ctime(&Header.UpdTime));
  564.     data_fd = open(datafile, 0);
  565.     if (data_fd == -1) {
  566.         fprintf(stderr, "recover: but I can't read the data file (%s).\n", datafile);
  567.         ask_del("Should I delete the tmp files? ", fp);
  568.         return 1;
  569.     }
  570.     makblist();
  571.  
  572.     for (;;) {
  573.         tellme("(Type '?' for options): ", answer);
  574.         switch (answer[0]) {
  575.         case '\0':
  576.             continue;
  577.  
  578.         case '?':
  579.             options();
  580.             break;
  581.  
  582.         case 'l':
  583.             list();
  584.             break;
  585.  
  586.         case 'p':
  587.             get(getsrc(), tty);
  588.             break;
  589.  
  590.         case 'q':
  591.             ask_del("Shall I delete the tmp files? ", fp);
  592.             return 1;
  593.  
  594.         case 'g':
  595.             {    /* So it asks for src first. */
  596.                 char    *dest;
  597.                 struct rec_entry    **src;
  598.  
  599.                 if ((src = getsrc()) == 0)
  600.                     break;
  601.                 dest = getdest();
  602.             get(src, dest);
  603.             break;
  604.             }
  605.  
  606.         case 'r':
  607.             restore();
  608.             break;
  609.  
  610.         default:
  611.             printf("I don't know how to \"%s\"!\n", answer);
  612.             break;
  613.         }
  614.     }
  615. }
  616.  
  617. ask_del(prompt, fp)
  618. char    *prompt;
  619. struct file_pair    *fp;
  620. {
  621.     char    yorn[20];
  622.  
  623.     tellme(prompt, yorn);
  624.     if (yorn[0] == 'y')
  625.         del_files(fp);
  626. }
  627.  
  628. del_files(fp)
  629. struct file_pair    *fp;
  630. {
  631.     (void) unlink(fp->file_data);
  632.     (void) unlink(fp->file_rec);
  633. }
  634.  
  635. #ifdef notdef
  636. savetmps()
  637. {
  638.     struct file_pair    *fp;
  639.     int    status,
  640.         pid;
  641.  
  642.     if (strcmp(TMP_DIR, REC_DIR) == 0)
  643.         return;        /* Files are moved to the same place. */
  644.     get_files(TMP_DIR);
  645.     for (fp = First; fp != 0; fp = fp->file_next) {
  646.         switch (pid = fork()) {
  647.         case -1:
  648.             fprintf(stderr, "recover: can't fork\n!");
  649.             exit(-1);
  650.  
  651.         case 0:
  652.             execl("/bin/cp", "cp", fp->file_data, fp->file_rec, 
  653.                   REC_DIR, (char *)0);
  654.             fprintf(stderr, "recover: cannot execl /bin/cp.\n");
  655.             exit(-1);
  656.  
  657.         default:
  658.             while (wait(&status) != pid)
  659.                 ;
  660.             if (status != 0)
  661.                 fprintf(stderr, "recover: non-zero status (%d) returned from copy.\n", status);
  662.         }
  663.     }
  664. }
  665. #endif
  666.  
  667. lookup(dir)
  668. char    *dir;
  669. {
  670.     struct file_pair    *fp;
  671.     struct rec_head        header;
  672.     char    yorn[20];
  673.     int    nfound = 0,
  674.         this_one;
  675.  
  676.     printf("Checking %s ...\n", dir);
  677.     Directory = dir;
  678.     get_files(dir);
  679.     for (fp = First; fp != 0; fp = fp->file_next) {
  680.         nfound += doit(fp);
  681.         if (ptrs_fp)
  682.             (void) fclose(ptrs_fp);
  683.         if (data_fd > 0)
  684.             (void) close(data_fd);
  685.     }
  686.     return nfound;
  687. }
  688.  
  689. main(argc, argv)
  690. int    argc;
  691. char    *argv[];
  692. {
  693.     int    nfound;
  694.     char    **argvp;
  695.  
  696.     UserID = getuid();
  697.  
  698.     if (scanvec(argv, "-help")) {
  699.         printf("recover: usage: recover [-d directory]\n");
  700.         printf("Use \"recover\" after JOVE has died for some\n");
  701.         printf("unknown reason.\n\n");
  702.         printf("Use \"recover -syscrash\" when the system is in the process\n");
  703.         printf("of rebooting.  This is done automatically at reboot time\n");
  704.         printf("and so most of you don't have to worry about that.\n\n");
  705.         printf("Use \"recover -d directory\" when the tmp files are store\n");
  706.         printf("in DIRECTORY instead of the default one (/tmp).\n");
  707.         exit(0);
  708.     }
  709.     if (scanvec(argv, "-v"))
  710.         Verbose++;
  711. /*    if (scanvec(argv, "-syscrash")) {
  712.         printf("Recovering jove files ... ");
  713.         savetmps();
  714.         printf("Done.\n");
  715.         exit(0);
  716.     } */
  717.     if (argvp = scanvec(argv, "-uid"))
  718.         UserID = atoi(argvp[1]);
  719.     if (argvp = scanvec(argv, "-d"))
  720.         nfound = lookup(argvp[1]);
  721.     else
  722.         nfound = lookup(TmpFilePath);
  723.     if (nfound == 0)
  724.         printf("There's nothing to recover.\n");
  725. }
  726.