home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gtak212.zip / 1.10 / update.c < prev    next >
C/C++ Source or Header  |  1992-09-12  |  15KB  |  577 lines

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