home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd2.bin / dosutils / gtar109 / update.c < prev    next >
C/C++ Source or Header  |  1991-01-14  |  13KB  |  539 lines

  1. /* Update a tar archive.
  2.    Copyright (C) 1988 Free Software Foundation
  3.  
  4. This file is part of GNU Tar.
  5.  
  6. GNU Tar is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Tar is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Tar; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* JF implement the 'r' 'u' and 'A' options for tar. */
  21. /* The 'A' option is my own invention:  It means that the file-names are
  22.    tar files, and they should simply be appended to the end of the archive.
  23.    No attempt is made to block the reads from the args; if they're on raw
  24.    tape or something like that, it'll probably lose. . . */
  25.  
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <stdio.h>
  29. #include <errno.h>
  30.  
  31. /* JF these includes are copied from create.c  I'm not sure if they're right
  32.    or not.  */
  33. #ifndef V7
  34. #include <fcntl.h>
  35. #endif
  36.  
  37. #ifdef    MSDOS
  38. #include <io.h>
  39. #else
  40. #include <pwd.h>
  41. #include <grp.h>
  42. #endif
  43.  
  44. #ifdef USG
  45. #include <sys/sysmacros.h>    /* major() and minor() defined here */
  46. #endif
  47.  
  48. /*
  49.  * V7 doesn't have a #define for this.
  50.  */
  51. #ifndef O_RDONLY
  52. #define    O_RDONLY    0
  53. #endif
  54.  
  55. /*
  56.  * Most people don't have a #define for this.
  57.  */
  58. #ifndef    O_BINARY
  59. #define    O_BINARY    0
  60. #endif
  61.  
  62. #define STDIN 0
  63. #define STDOUT 1
  64.  
  65. #include "tar.h"
  66. #include "port.h"
  67. #include "rmt.h"
  68.  
  69. int time_to_start_writing = 0;    /* We've hit the end of the old stuff,
  70.                    and its time to start writing new stuff
  71.                    to the tape.  This involves seeking
  72.                    back one block and re-writing the current
  73.                    block (which has been changed). */
  74.  
  75. char *output_start;        /* Pointer to where we started to write in
  76.                    the first block we write out.  This is used
  77.                    if we can't backspace the output and have
  78.                    to null out the first part of the block */
  79.  
  80. extern void skip_file();
  81. extern void skip_extended_headers();
  82.  
  83. extern union record *head;
  84. extern struct stat hstat;
  85.  
  86. struct name *name_scan();
  87. char    *name_from_list();
  88.  
  89. /* Implement the 'r' (add files to end of archive), and 'u' (add files to
  90.    end of archive if they arent there, or are more up to date than the
  91.    version in the archive.) commands.*/
  92. void
  93. update_archive()
  94. {
  95.     int    found_end = 0;
  96.     int    status = 3;
  97.     int    prev_status;
  98.     char    *p;
  99.     struct name *name;
  100.     extern void dump_file();
  101.  
  102.     name_gather();
  103.     if(cmd_mode==CMD_UPDATE)
  104.         name_expand();
  105.     open_archive(2);    /* Open for updating */
  106.  
  107.     do {
  108.         prev_status=status;
  109.         status=read_header();
  110.         switch(status) {
  111.         case EOF:
  112.             found_end=1;
  113.             break;
  114.  
  115.         case 0:        /* A bad record */
  116.             userec(head);
  117.             switch(prev_status) {
  118.             case 3:
  119.                 msg("This doesn't look like a tar archive.\n");
  120.                 /* FALL THROUGH */
  121.             case 2:
  122.             case 1:
  123.                 msg("Skipping to next header\n");
  124.             case 0:
  125.                 break;
  126.             }
  127.             break;
  128.  
  129.             /* A good record */
  130.         case 1:
  131.  /* printf("File %s\n",head->header.name); */
  132.             head->header.name[NAMSIZ-1]='\0';
  133.             if(cmd_mode==CMD_UPDATE && (name=name_scan(head->header.name))) {
  134.                 /* struct stat hstat; */
  135.                 struct stat nstat;
  136.                 int head_standard;
  137.  
  138.                 decode_header(head,&hstat,&head_standard,0);
  139.                 if(stat(head->header.name,&nstat)<0) {
  140.                     msg_perror("can't stat %s:",head->header.name);
  141.                 } else {
  142.                     if(hstat.st_mtime>=nstat.st_mtime)
  143.                         name->found++;
  144.                 }
  145.             }
  146.             userec(head);
  147.             if (head->header.isextended)
  148.                 skip_extended_headers();
  149.             skip_file((long)hstat.st_size);
  150.             break;
  151.  
  152.         case 2:
  153.             ar_record=head;
  154.             found_end = 1;
  155.             break;
  156.         }
  157.     } while(!found_end);
  158.  
  159.     reset_eof();
  160.     time_to_start_writing = 1;
  161.     output_start=ar_record->charptr;
  162.  
  163.     while(p=name_from_list()) {
  164.         if(f_confirm && !confirm("add", p))
  165.             continue;
  166.         if(cmd_mode==CMD_CAT)
  167.             append_file(p);
  168.         else
  169.             dump_file(p,-1);
  170.     }
  171.  
  172.     write_eot();
  173.     close_archive();
  174.     names_notfound();
  175. }
  176.  
  177. /* Catenate file p to the archive without creating a header for it.  It had
  178.    better be a tar file or the archive is screwed */
  179.  
  180. append_file(p)
  181. char *p;
  182. {
  183.     int    fd;
  184.     struct stat statbuf;
  185.     long    bytes_left;
  186.     union record *start;
  187.     long    bufsiz,count;
  188.  
  189.     if(0 != stat(p,&statbuf) || (fd=open(p,O_RDONLY|O_BINARY))<0) {
  190.         msg_perror("can't open file %s",p);
  191.         errors++;
  192.         return;
  193.     }
  194.  
  195.     bytes_left = statbuf.st_size;
  196.  
  197.     while(bytes_left>0) {
  198.         start=findrec();
  199.         bufsiz=endofrecs()->charptr - start->charptr;
  200.         if(bytes_left < bufsiz) {
  201.             bufsiz = bytes_left;
  202.             count = bufsiz % RECORDSIZE;
  203.             if(count)
  204.                 bzero(start->charptr + bytes_left,(int)(RECORDSIZE-count));
  205.         }
  206.         count=read(fd,start->charptr,bufsiz);
  207.         if(count<0) {
  208.             msg_perror("read error at byte %ld reading %d bytes in file %s",statbuf.st_size-bytes_left,bufsiz,p);
  209.             exit(EX_ARGSBAD);        /* FOO */
  210.         }
  211.         bytes_left-=count;
  212.         userec(start+(count-1)/RECORDSIZE);
  213.         if(count!=bufsiz) {
  214.             msg("%s: file shrunk by %d bytes, yark!\n",p,bytes_left);
  215.             abort();
  216.         }
  217.     }
  218.     (void)close(fd);
  219. }
  220.  
  221. #ifdef DONTDEF
  222. bprint(fp,buf,num)
  223. FILE *fp;
  224. char *buf;
  225. {
  226.     int    c;
  227.  
  228.     if(num==0 || num==-1)
  229.         return;
  230.     fputs(" '",fp);
  231.     while(num--) {
  232.         c= *buf++;
  233.         if(c=='\\') fputs("\\\\",fp);
  234.         else if(c>=' ' && c<='~')
  235.             putc(c,fp);
  236.         else switch(c) {
  237.         case '\n':
  238.             fputs("\\n",fp);
  239.             break;
  240.         case '\r':
  241.             fputs("\\r",fp);
  242.             break;
  243.         case '\b':
  244.             fputs("\\b",fp);
  245.             break;
  246.         case '\0':
  247.             /* fputs("\\-",fp); */
  248.             break;
  249.         default:
  250.             fprintf(fp,"\\%03o",c);
  251.             break;
  252.         }
  253.     }
  254.     fputs("'\n",fp);
  255. }
  256. #endif
  257.  
  258. int number_of_blocks_read = 0;
  259.  
  260. int number_of_new_records = 0;
  261. int number_of_records_needed = 0;
  262.  
  263. union record *new_block = 0;
  264. union record *save_block = 0;
  265.  
  266. void
  267. junk_archive()
  268. {
  269.     int    found_stuff = 0;
  270.     int    status = 3;
  271.     int    prev_status;
  272.     struct name *name;
  273.  
  274.     /* int dummy_head; */
  275.     int number_of_records_to_skip = 0;
  276.     int number_of_records_to_keep = 0;
  277.     int number_of_kept_records_in_block;
  278.     int sub_status;
  279.     extern int write_archive_to_stdout;
  280.  
  281. /* fprintf(stderr,"Junk files\n"); */
  282.     name_gather();
  283.     open_archive(2);
  284.  
  285.     while(!found_stuff) {
  286.         prev_status=status;
  287.         status=read_header();
  288.         switch(status) {
  289.         case EOF:
  290.             found_stuff = 1;
  291.             break;
  292.  
  293.         case 0:
  294.             userec(head);
  295.             switch(prev_status) {
  296.             case 3:
  297.                 msg("This doesn't look like a tar archive.\n");
  298.                 /* FALL THROUGH */
  299.             case 2:
  300.             case 1:
  301.                 msg("Skipping to next header\n");
  302.                 /* FALL THROUGH */
  303.             case 0:
  304.                 break;
  305.             }
  306.             break;
  307.  
  308.         case 1:
  309.             head->header.name[NAMSIZ-1] = '\0';
  310.  /* fprintf(stderr,"file %s\n",head->header.name); */
  311.             if((name=name_scan(head->header.name))==(struct name *)0) {
  312.                 userec(head);
  313.  /* fprintf(stderr,"Skip %ld\n",(long)(hstat.st_size)); */
  314.                  if (head->header.isextended)
  315.                     skip_extended_headers();
  316.                 skip_file((long)(hstat.st_size));
  317.                 break;
  318.             }
  319.             name->found = 1;
  320.             found_stuff = 2;
  321.             break;
  322.  
  323.         case 2:
  324.             found_stuff = 1;
  325.             break;
  326.         }
  327.     }
  328.  /* fprintf(stderr,"Out of first loop\n"); */
  329.  
  330.     if(found_stuff!=2) {
  331.         write_eot();
  332.         close_archive();
  333.         names_notfound();
  334.         return;
  335.     }
  336.  
  337.     if(write_archive_to_stdout)
  338.         write_archive_to_stdout = 0;
  339.     new_block = (union record *)malloc(blocksize);
  340.     if(new_block==0) {
  341.         msg("Can't allocate secondary block of %d bytes",blocksize);
  342.         exit(EX_SYSTEM);
  343.     }
  344.  
  345.         /* Save away records before this one in this block */
  346.     number_of_new_records=ar_record-ar_block;
  347.     number_of_records_needed = blocking - number_of_new_records;
  348.     if(number_of_new_records)
  349.         bcopy((void *)ar_block,(void *)new_block,(number_of_new_records)*RECORDSIZE);
  350.  
  351.  /* fprintf(stderr,"Saved %d recs, need %d more\n",number_of_new_records,number_of_records_needed); */
  352.     userec(head);
  353.     if (head->header.isextended)
  354.         skip_extended_headers();
  355.     skip_file((long)(hstat.st_size));
  356.     found_stuff=0;
  357.     /* goto flush_file; */
  358.  
  359.     for(;;) {
  360.             /* Fill in a block */
  361.     /* another_file: */
  362.         if(ar_record==ar_last) {
  363.  /* fprintf(stderr,"New block\n"); */
  364.             flush_archive();
  365.             number_of_blocks_read++;
  366.         }
  367.         sub_status = read_header();
  368.  /* fprintf(stderr,"Header type %d\n",sub_status); */
  369.  
  370.         if(sub_status==2 && f_ignorez) {
  371.             userec(head);
  372.             continue;
  373.         }
  374.         if(sub_status==EOF || sub_status==2) {
  375.             found_stuff = 1;
  376.             bzero(new_block[number_of_new_records].charptr,RECORDSIZE*number_of_records_needed);
  377.             number_of_new_records+=number_of_records_needed;
  378.             number_of_records_needed = 0;
  379.             write_block(0);
  380.             break;
  381.         }
  382.  
  383.         if(sub_status==0) {
  384.             msg("Deleting non-header from archive.");
  385.             userec(head);
  386.             continue;
  387.         }
  388.  
  389.         /* Found another header.  Yipee! */
  390.         head->header.name[NAMSIZ-1] = '\0';
  391.  /* fprintf(stderr,"File %s ",head->header.name); */
  392.         if(name=name_scan(head->header.name)) {
  393.             name->found = 1;
  394.  /* fprintf(stderr,"Flush it\n"); */
  395.         /* flush_file: */
  396.             /* decode_header(head,&hstat,&dummy_head,0); */
  397.             userec(head);
  398.             number_of_records_to_skip=(hstat.st_size+RECORDSIZE-1)/RECORDSIZE;
  399.  /* fprintf(stderr,"Flushing %d recs from %s\n",number_of_records_to_skip,head->header.name); */
  400.  
  401.             while(ar_last-ar_record<=number_of_records_to_skip) {
  402.  
  403.  /* fprintf(stderr,"Block: %d <= %d  ",ar_last-ar_record,number_of_records_to_skip); */
  404.                 number_of_records_to_skip -= (ar_last - ar_record);
  405.                 flush_archive();
  406.                 number_of_blocks_read++;
  407.  /* fprintf(stderr,"Block %d left\n",number_of_records_to_skip); */
  408.             }
  409.             ar_record+=number_of_records_to_skip;
  410.  /* fprintf(stderr,"Final %d\n",number_of_records_to_skip); */
  411.             number_of_records_to_skip = 0;
  412.             continue;
  413.         }
  414.  
  415.     /* copy_header: */
  416.         new_block[number_of_new_records]= *head;
  417.         number_of_new_records++;
  418.         number_of_records_needed--;
  419.         number_of_records_to_keep=(hstat.st_size+RECORDSIZE-1)/RECORDSIZE;
  420.         userec(head);
  421.         if(number_of_records_needed==0)
  422.             write_block(1);
  423.     /* copy_data: */
  424.         number_of_kept_records_in_block = ar_last - ar_record;
  425.         if(number_of_kept_records_in_block > number_of_records_to_keep)
  426.             number_of_kept_records_in_block = number_of_records_to_keep;
  427.  
  428.  /* fprintf(stderr,"Need %d kept_in %d keep %d\n",blocking,number_of_kept_records_in_block,number_of_records_to_keep); */
  429.  
  430.         while(number_of_records_to_keep) {
  431.             int n;
  432.  
  433.             if(ar_record==ar_last) {
  434.  /* fprintf(stderr,"Flush. . .\n"); */
  435.                 fl_read();
  436.                 number_of_blocks_read++;
  437.                 ar_record=ar_block;
  438.                 number_of_kept_records_in_block = blocking;
  439.                 if(number_of_kept_records_in_block > number_of_records_to_keep)
  440.                     number_of_kept_records_in_block = number_of_records_to_keep;
  441.             }
  442.             n = number_of_kept_records_in_block;
  443.             if(n>number_of_records_needed)
  444.                 n = number_of_records_needed;
  445.  
  446.  /* fprintf(stderr,"Copying %d\n",n); */
  447.             bcopy((void *)ar_record, (void *)(new_block+number_of_new_records), n*RECORDSIZE);
  448.             number_of_new_records           += n;
  449.             number_of_records_needed        -= n;
  450.             ar_record                       += n;
  451.             number_of_records_to_keep       -= n;
  452.             number_of_kept_records_in_block -= n;
  453.  /* fprintf(stderr,"Now new %d  need %d  keep %d  keep_in %d rec %d/%d\n",
  454.  number_of_new_records,number_of_records_needed,number_of_records_to_keep,
  455.  number_of_kept_records_in_block,ar_record-ar_block,ar_last-ar_block); */
  456.  
  457.             if(number_of_records_needed == 0) {
  458.                 write_block(1);
  459.             }
  460.         }
  461.     }
  462.  
  463.     write_eot();
  464.     close_archive();
  465.     names_notfound();
  466. }
  467.  
  468. write_block(f)
  469. {
  470.  /* fprintf(stderr,"Write block\n"); */
  471.     /* We've filled out a block.  Write it out. */
  472.  
  473.     /* Backspace back to where we started. . . */
  474.     if(archive!=STDIN)
  475.         (void)move_arch(-(number_of_blocks_read+1));
  476.  
  477.     save_block = ar_block;
  478.     ar_block = new_block;
  479.  
  480.     if(archive==STDIN)
  481.         archive=STDOUT;
  482.     fl_write();
  483.  
  484.     if(archive==STDOUT)
  485.         archive=STDIN;
  486.     ar_block = save_block;
  487.  
  488.     if(f) {
  489.         /* Move the tape head back to where we were */
  490.         if(archive!=STDIN)
  491.             (void)move_arch(number_of_blocks_read);
  492.         number_of_blocks_read--;
  493.     }
  494.  
  495.     number_of_records_needed = blocking;
  496.     number_of_new_records = 0;
  497. }
  498.  
  499. /* Move archive descriptor by n blocks worth.  If n is positive we move
  500.    forward, else we move negative.   If its a tape, MTIOCTOP had better
  501.    work.  If its something else, we try to seek on it.  If we can't
  502.    seek, we lose! */
  503. move_arch(n)
  504. {
  505.     long cur;
  506. #ifndef MSDOS
  507.     extern int errno;
  508. #endif
  509.  
  510. #ifdef MTIOCTOP
  511.     struct mtop t;
  512.     int er;
  513.  
  514.     if(n>0) {
  515.         t.mt_op = MTFSR;
  516.         t.mt_count = n;
  517.     } else {
  518.         t.mt_op = MTBSR;
  519.         t.mt_count = -n;
  520.     }
  521.     if((er=rmtioctl(archive,MTIOCTOP,&t))>=0)
  522.         return 1;
  523.     if(errno==EIO && (er=rmtioctl(archive,MTIOCTOP,&t))>=0)
  524.         return 1;
  525. #endif
  526.  
  527.     cur=rmtlseek(archive,0L,1);
  528.     cur+=blocksize*n;
  529.  
  530.  /* fprintf(stderr,"Fore to %x\n",cur); */
  531.     if(rmtlseek(archive,cur,0)!=cur) {
  532.         /* Lseek failed.  Try a different method */
  533.         msg("Couldn't re-position archive file.");
  534.         exit(EX_BADARCH);
  535.     }
  536.     return 3;
  537. }
  538.  
  539.