home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1950 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  4.3 KB

  1. From: jpr@jpradley.uucp (Jean-Pierre Radley)
  2. Newsgroups: alt.sources
  3. Subject: [comp.unix.misc] Re: Recovering corrupted tar's
  4. Message-ID: <1990Oct14.190247.12336@math.lsa.umich.edu>
  5. Date: 14 Oct 90 19:02:47 GMT
  6.  
  7. Archive-name: tarskip/13-Oct-90
  8. Original-posting-by: jpr@jpradley.uucp (Jean-Pierre Radley)
  9. Original-subject: Re: Recovering corrupted tar's
  10. Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)
  11.  
  12. [Reposted from comp.unix.misc.
  13. Comments on this service to emv@math.lsa.umich.edu (Edward Vielmetti).]
  14.  
  15. In article <ENAG.90Oct4231246@svarte.ifi.uio.no> enag@ifi.uio.no (Erik Naggum) writes:
  16. >In article <4121@neuro.med.umn.edu> alk@neuro.med.umn.edu (Anthony L Kimball) writes:
  17. >
  18. >   So, anyone have a handy-dandy tool to patch a tar archive which
  19. >   has had it's leading N blocks overwritten?  I do so hate to do this
  20. >   by hand.
  21. >
  22. >The files occupying the first n blocks are lost, where n >= N.  The
  23. >problem is to find the value of n.
  24. > ......
  25. >                                  Searching programs shouldn't be that
  26. >hard to write, either, but I don't think anybody has done this, yet.
  27.  
  28.  
  29. Well, this often helps:
  30.  
  31.  
  32. /* tarskip.c
  33.  * taken from volume 2, number 8 of Unix World.
  34.  * Usage: tarskip pathname [tardevice] | tar xvfn -
  35.  * The pathname argument is the first file you can
  36.  * think of after the bad spot on the archive.
  37.  */
  38. #include <stdio.h>
  39.  
  40. #define TARBLKSZ 1024    /* Block size on YOUR tar disk */
  41. #define TRUE     1    /* Boolean true */
  42. #define FALSE     0    /* Boolean false */
  43.  
  44. /* Global variables */
  45.  
  46. FILE *fp, *efopen();    /* file pointer for tar device file */
  47. char fd;        /* file descriptor for tar device file */
  48. char buf[TARBLKSZ];    /* buffer for "tar blocks" */
  49. char *progname;
  50. char *tardev = "/dev/rfd0"; /* Substitute YOUR default device name !! */
  51.  
  52. main(argc, argv)
  53. int argc;
  54. char *argv[];
  55. {
  56.     char *pathname;
  57.     int match = FALSE;        /*Initialize to zero */
  58.     progname = argv[0];
  59.  
  60. /* Process command line arguments */
  61.  
  62.     if (argc == 1 || argc > 3) {    /* Correct # of args? */
  63.          error("Usage: %s pathname [tardevice] | tar xvfn -", progname); }
  64.     pathname = argv[1];        /* The pattern string to search for */
  65.     if (argc == 3)
  66.         tardev= argv[2];    /* Use device other than default */
  67.     
  68.         fp= efopen(tardev,"r");    /* Open for reading */
  69.         fd= fileno(fp);        /* File descriptor */
  70.  
  71. /* Find block containing desired pathname */
  72.  
  73.     do {    getblk(tardev);
  74.         match = strncmp(pathname, buf, strlen(pathname));
  75.     } while (match);
  76.  
  77. /* Then read and process disk blocks "forever" */
  78.  
  79.     while (TRUE) { write(1, buf, sizeof(buf));
  80.                getblk(tardev); }
  81. }
  82.  
  83. getblk(device)        /* Read the next block into the buffer */
  84. char *device;        /* The tar disk device name */
  85. {
  86.     int c, done, n;
  87. begin:    n = (read(fd, buf, sizeof(buf)));    /* Read a block */
  88.  
  89.     if (n ==0) {                 /* End of disk */
  90.         done = query("\nEnd of disk, read another (y/n)? ");
  91.         if (!done) {            /* Not done */
  92.             close(fd);        /* Close previous device */
  93.             fprintf(stderr, "Insert next disk and");
  94.             fprintf(stderr, " press <ENTER> when ready\n");
  95.             while ((c=getchar()) != '\n' && c != EOF)
  96.                 ;         /* Wait for ENTER */
  97.             fp= efopen(device,"r");    /* Open for reading */
  98.             fd= fileno(fp);        /* File descriptor */
  99.             goto begin; /* Get first block from next disk */
  100.         } else                /* Done */
  101.             exit (0);
  102.     } else if (n < 0) {            /* Tape read error */
  103.         done = query("\nRead error, ignore (y/n)? ");
  104.         if (done) {
  105.             close(fd);
  106.             fprintf(stderr, "Goodbye ... \n");
  107.             exit(2);
  108.         }
  109.     }
  110. }
  111.  
  112. query(msg)        /* Display message and prompt for continuance */
  113. char *msg;
  114. {
  115.     int c, c2;
  116.     fprintf(stderr, "%s", msg);
  117.     c = c2 = getchar();
  118.     while (c2 != '\n' && c2 != EOF)
  119.         c2 = getchar();     /* Ignore remaining input */
  120.     if (c == 'y' || c == 'Y')     /* First char was y or Y */
  121.     return(0);             /* Not done */
  122.     else
  123.     return(1);             /* Done */
  124. }
  125.  
  126. error(s1, s2)        /* print error message and die */
  127. char    *s1, *s2;
  128. {
  129.     extern    int    errno, sys_nerr;
  130.     extern    char    *sys_errlist[], *progname;
  131.  
  132.     if (progname)
  133.         fprintf(stderr, "%s: ", progname);
  134.     fprintf(stderr, s1, s2);
  135.     if (errno >0 && errno < sys_nerr)
  136.             fprintf(stderr, "    (%s)", sys_errlist[errno]);
  137.     fprintf(stderr, "\n");
  138.     exit(1);
  139. }
  140.  
  141. FILE *efopen(file, mode)    /* fopen file, call error() & die if can't */
  142. char *file, *mode;
  143. {
  144.     FILE    *f;
  145.     extern char *progname;
  146.     char    str[80], *s=str;
  147.  
  148.     if    ((f = fopen(file,mode)) != (FILE *)0)
  149.         return f;
  150.     sprintf(s, "can't open file %s mode %s\n", file, mode);
  151.     error(s);
  152. }
  153. -- 
  154.  Jean-Pierre Radley          HIGH-Q         jpr@jpradley    CIS: 72160,1341
  155.