home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / tar-1.11.8-src.tgz / tar.out / fsf / tar / src / update.c < prev   
C/C++ Source or Header  |  1996-09-28  |  14KB  |  631 lines

  1. /* Update a tar archive.
  2.    Copyright (C) 1988, 1992, 1994 Free Software Foundation, Inc.
  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 2, 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 "system.h"
  27.  
  28. #ifndef    __MSDOS__
  29. # include <pwd.h>
  30. # include <grp.h>
  31. #endif
  32.  
  33. #define STDIN 0
  34. #define STDOUT 1
  35.  
  36. #include "tar.h"
  37. #include "rmt.h"
  38.  
  39. /* We've hit the end of the old stuff, and its time to start writing new
  40.    stuff to the tape.  This involves seeking back one block and
  41.    re-writing the current block (which has been changed).  */
  42. int time_to_start_writing = 0;
  43.  
  44. /* Pointer to where we started to write in the first block we write out.
  45.    This is used if we can't backspace the output and have to null out the
  46.    first part of the block.  */
  47. char *output_start;
  48.  
  49. extern void skip_file ();    /* FIXME */
  50. extern void skip_extended_headers (); /* FIXME */
  51.  
  52. extern union record *head;
  53. extern struct stat hstat;
  54.  
  55. static void append_file __P ((char *));
  56. static int move_arch __P ((int));
  57. static void write_block __P ((int));
  58.  
  59. /*-----------------------------------------------------------------------.
  60. | Implement the 'r' (add files to end of archive), and 'u' (add files to |
  61. | end of archive if they arent there, or are more up to date than the     |
  62. | version in the archive.) commands.                     |
  63. `-----------------------------------------------------------------------*/
  64.  
  65. void
  66. update_archive (void)
  67. {
  68.   int found_end = 0;
  69.   int status = 3;
  70.   int prev_status;
  71.   char *p;
  72.   struct name *name;
  73.  
  74.   name_gather ();
  75.   if (command_mode == COMMAND_UPDATE)
  76.     name_expand ();
  77.   open_archive (2);        /* open for updating */
  78.  
  79.   do
  80.     {
  81.       prev_status = status;
  82.       status = read_header ();
  83.       switch (status)
  84.     {
  85.     case EOF:
  86.       found_end = 1;
  87.       break;
  88.  
  89.     case 0:            /* a bad record */
  90.       userec (head);
  91.       switch (prev_status)
  92.         {
  93.         case 3:
  94.           WARN ((0, 0, _("This does not look like a tar archive")));
  95.           /* Fall through.  */
  96.  
  97.         case 2:
  98.         case 1:
  99.           ERROR ((0, 0, _("Skipping to next header")));
  100.           /* Fall through.  */
  101.  
  102.         case 0:
  103.           break;
  104.         }
  105.       break;
  106.  
  107.     case 1:            /* a good record */
  108. #if 0
  109.       printf (_("File %s\n"), head->header.name);
  110.       head->header.name[NAMSIZ-1] = '\0';
  111. #endif
  112.       if (command_mode == COMMAND_UPDATE
  113.           && (name = name_scan (current_file_name), name))
  114.         {
  115. #if 0
  116.           struct stat hstat;
  117. #endif
  118.           struct stat nstat;
  119.           int unused;
  120.  
  121.           decode_header (head, &hstat, &unused, 0);
  122.           if (stat (current_file_name, &nstat) < 0)
  123.         ERROR ((0, errno, _("Cannot stat %s"), current_file_name));
  124.           else if (hstat.st_mtime >= nstat.st_mtime)
  125.         name->found++;
  126.         }
  127.       userec (head);
  128.       if (head->header.isextended)
  129.         skip_extended_headers ();
  130.       skip_file ((long) hstat.st_size);
  131.       break;
  132.  
  133.     case 2:
  134.       ar_record = head;
  135.       found_end = 1;
  136.       break;
  137.     }
  138.     }
  139.   while (!found_end);
  140.  
  141.   reset_eof ();
  142.   time_to_start_writing = 1;
  143.   output_start = ar_record->charptr;
  144.  
  145.   while (p = name_from_list (), p)
  146.     {
  147.       if (flag_confirm && !confirm ("add", p))
  148.     continue;
  149.       if (command_mode == COMMAND_CAT)
  150.     append_file (p);
  151.       else
  152.     dump_file (p, -1, 1);
  153.     }
  154.  
  155.   write_eot ();
  156.   close_archive ();
  157.   names_notfound ();
  158. }
  159.  
  160. /*-------------------------------------------------------------------------.
  161. | Catenate file p to the archive without creating a header for it.  It had |
  162. | better be a tar file or the archive is screwed.               |
  163. `-------------------------------------------------------------------------*/
  164.  
  165. static void
  166. append_file (char *p)
  167. {
  168.   int fd;
  169.   struct stat statbuf;
  170.   long bytes_left;
  171.   union record *start;
  172.   long bufsiz, count;
  173.  
  174.   if (stat (p, &statbuf) != 0 || (fd = open (p, O_RDONLY | O_BINARY), fd < 0))
  175.     {
  176.       ERROR ((0, errno, _("Cannot open file %s"), p));
  177.       return;
  178.     }
  179.  
  180.   bytes_left = statbuf.st_size;
  181.  
  182.   while (bytes_left > 0)
  183.     {
  184.       start = findrec ();
  185.       bufsiz = endofrecs ()->charptr - start->charptr;
  186.       if (bytes_left < bufsiz)
  187.     {
  188.       bufsiz = bytes_left;
  189.       count = bufsiz % RECORDSIZE;
  190.       if (count)
  191.         memset (start->charptr + bytes_left, 0,
  192.             (size_t) (RECORDSIZE - count));
  193.     }
  194.       count = read (fd, start->charptr, (size_t) bufsiz);
  195.       if (count < 0)
  196.     ERROR ((TAREXIT_FAILURE, errno,
  197.         _("Read error at byte %ld reading %d bytes in file %s"),
  198.         statbuf.st_size - bytes_left, bufsiz, p));
  199.       bytes_left -= count;
  200.       userec (start + (count - 1) / RECORDSIZE);
  201.       if (count != bufsiz)
  202.     ERROR ((TAREXIT_FAILURE, 0, _("%s: File shrunk by %d bytes, (yark!)"),
  203.         p, bytes_left));
  204.     }
  205.   close (fd);
  206. }
  207.  
  208. #ifdef DONTDEF
  209.  
  210. /*---.
  211. | ?  |
  212. `---*/
  213.  
  214. static void
  215. bprint (FILE *fp, char *buf, int num)
  216. {
  217.   int c;
  218.  
  219.   if (num == 0 || num == -1)
  220.     return;
  221.   fputs (" '", fp);
  222.   while (num--)
  223.     {
  224.       c = *buf++;
  225.       if (c == '\\')
  226.     fputs ("\\\\", fp);
  227.       else if (c >= ' ' && c <= '~')
  228.     putc (c, fp);
  229.       else
  230.     switch (c)
  231.       {
  232.       case '\n':
  233.         fputs ("\\n", fp);
  234.         break;
  235.  
  236.       case '\r':
  237.         fputs ("\\r", fp);
  238.         break;
  239.  
  240.       case '\b':
  241.         fputs ("\\b", fp);
  242.         break;
  243.  
  244.       case '\0':
  245. #if 0
  246.         fputs("\\-", fp);
  247. #endif
  248.         break;
  249.  
  250.       default:
  251.         fprintf (fp, "\\%03o", c);
  252.         break;
  253.       }
  254.     }
  255.   fputs ("'\n", fp);
  256. }
  257.  
  258. #endif
  259.  
  260. int number_of_blocks_read = 0;
  261.  
  262. int number_of_new_records = 0;
  263. int number_of_records_needed = 0;
  264.  
  265. union record *new_block = 0;
  266. union record *save_block = 0;
  267.  
  268. /*---.
  269. | ?  |
  270. `---*/
  271.  
  272. void
  273. junk_archive (void)
  274. {
  275.   int found_stuff = 0;
  276.   int status = 3;
  277.   int prev_status;
  278.   struct name *name;
  279.  
  280. #if 0
  281.   int dummy_head;
  282. #endif
  283.   int number_of_records_to_skip = 0;
  284.   int number_of_records_to_keep = 0;
  285.   int number_of_kept_records_in_block;
  286.   int sub_status;
  287.  
  288. #if 0
  289.   fprintf (stderr,_("Junk files\n"));
  290. #endif
  291.   name_gather ();
  292.   open_archive (2);
  293.  
  294.   while (!found_stuff)
  295.     {
  296.       prev_status = status;
  297.       status = read_header ();
  298.       switch (status)
  299.     {
  300.     case EOF:
  301.       found_stuff = 1;
  302.       break;
  303.  
  304.     case 0:
  305.       userec (head);
  306.       switch (prev_status)
  307.         {
  308.         case 3:
  309.           WARN ((0, 0, _("This does not look like a tar archive")));
  310.           /* Fall through.  */
  311.  
  312.         case 2:
  313.         case 1:
  314.           ERROR ((0, 0, _("Skipping to next header")));
  315.           /* Fall through.  */
  316.  
  317.         case 0:
  318.           break;
  319.         }
  320.       break;
  321.  
  322.     case 1:
  323. #if 0
  324.       head->header.name[NAMSIZ-1] = '\0';
  325.       fprintf (stderr, _("file %s\n"), head->header.name);
  326. #endif
  327.       if (name = name_scan (current_file_name), !name)
  328.         {
  329.           userec (head);
  330. #if 0
  331.           fprintf (stderr, _("Skip %ld\n"), (long) (hstat.st_size));
  332. #endif
  333.           if (head->header.isextended)
  334.         skip_extended_headers ();
  335.           skip_file ((long) (hstat.st_size));
  336.           break;
  337.         }
  338.       name->found = 1;
  339.       found_stuff = 2;
  340.       break;
  341.  
  342.     case 2:
  343.       found_stuff = 1;
  344.       break;
  345.     }
  346.     }
  347. #if 0
  348.   fprintf (stderr, _("Out of first loop\n"));
  349. #endif
  350.  
  351.   if (found_stuff != 2)
  352.     {
  353.       write_eot ();
  354.       close_archive ();
  355.       names_notfound ();
  356.       return;
  357.     }
  358.  
  359.   if (write_archive_to_stdout)
  360.     write_archive_to_stdout = 0;
  361.   new_block = (union record *) xmalloc ((size_t) blocksize);
  362.  
  363.   /* Save away records before this one in this block.  */
  364.  
  365.   number_of_new_records = ar_record - ar_block;
  366.   number_of_records_needed = blocking - number_of_new_records;
  367.   if (number_of_new_records)
  368.     memcpy ((void *) new_block, (void *) ar_block,
  369.        (size_t) (number_of_new_records * RECORDSIZE));
  370.  
  371. #if 0
  372.   fprintf (stderr, _("Saved %d recs, need %d more\n"),
  373.        number_of_new_records, number_of_records_needed);
  374. #endif
  375.   userec (head);
  376.   if (head->header.isextended)
  377.     skip_extended_headers ();
  378.   skip_file ((long) (hstat.st_size));
  379.   found_stuff = 0;
  380. #if 0
  381.   goto flush_file;
  382. #endif
  383.  
  384.   while (1)
  385.     {
  386.  
  387.       /* Fill in a block.  */
  388.  
  389.       /* another_file:  */
  390.  
  391.       if (ar_record == ar_last)
  392.     {
  393. #if 0
  394.       fprintf (stderr, _("New block\n"));
  395. #endif
  396.       flush_archive ();
  397.       number_of_blocks_read++;
  398.     }
  399.       sub_status = read_header ();
  400. #if 0
  401.       fprintf (stderr, _("Header type %d\n"), sub_status);
  402. #endif
  403.  
  404.       if (sub_status == 2 && flag_ignorez)
  405.     {
  406.       userec (head);
  407.       continue;
  408.     }
  409.       if (sub_status == EOF || sub_status == 2)
  410.     {
  411.       found_stuff = 1;
  412.       memset (new_block[number_of_new_records].charptr, 0,
  413.          (size_t) (RECORDSIZE * number_of_records_needed));
  414.       number_of_new_records += number_of_records_needed;
  415.       number_of_records_needed = 0;
  416.       write_block (0);
  417.       break;
  418.     }
  419.  
  420.       if (sub_status == 0)
  421.     {
  422.       ERROR ((0, 0, _("Deleting non-header from archive")));
  423.       userec (head);
  424.       continue;
  425.     }
  426.  
  427.       /* Found another header.  Yipee!  */
  428.  
  429. #if 0
  430.       head->header.name[NAMSIZ-1] = '\0';
  431.       fprintf (stderr, _("File %s "), head->header.name);
  432. #endif
  433.       if (name = name_scan (current_file_name), name)
  434.     {
  435.       name->found = 1;
  436. #if 0
  437.       fprintf (stderr, _("Flush it\n"));
  438.       flush_file:
  439.       decode_header (head, &hstat,&dummy_head, 0);
  440. #endif
  441.       userec (head);
  442.       number_of_records_to_skip = (hstat.st_size + RECORDSIZE - 1) / RECORDSIZE;
  443. #if 0
  444.       fprintf (stderr, _("Flushing %d recs from %s\n"),
  445.            number_of_records_to_skip,head->header.name);
  446. #endif
  447.  
  448.       while (ar_last - ar_record <= number_of_records_to_skip)
  449.         {
  450. #if 0
  451.           fprintf (stderr, _("Block: %d <= %d  "),
  452.                ar_last-ar_record, number_of_records_to_skip);
  453. #endif
  454.           number_of_records_to_skip -= (ar_last - ar_record);
  455.           flush_archive ();
  456.           number_of_blocks_read++;
  457. #if 0
  458.           fprintf (stderr, _("Block %d left\n"),
  459.                number_of_records_to_skip);
  460. #endif
  461.         }
  462.       ar_record += number_of_records_to_skip;
  463. #if 0
  464.       fprintf (stderr, _("Final %d\n"), number_of_records_to_skip);
  465. #endif
  466.       number_of_records_to_skip = 0;
  467.       continue;
  468.     }
  469.  
  470.       /* copy_header:  */
  471.  
  472.       new_block[number_of_new_records] = *head;
  473.       number_of_new_records++;
  474.       number_of_records_needed--;
  475.       number_of_records_to_keep
  476.     = (hstat.st_size + RECORDSIZE - 1) / RECORDSIZE;
  477.       userec (head);
  478.       if (number_of_records_needed == 0)
  479.     write_block (1);
  480.  
  481.       /* copy_data: */
  482.  
  483.       number_of_kept_records_in_block = ar_last - ar_record;
  484.       if (number_of_kept_records_in_block > number_of_records_to_keep)
  485.     number_of_kept_records_in_block = number_of_records_to_keep;
  486.  
  487. #if 0
  488.       fprintf (stderr, _("Need %d kept_in %d keep %d\n"),
  489.            blocking, number_of_kept_records_in_block,
  490.            number_of_records_to_keep);
  491. #endif
  492.  
  493.       while (number_of_records_to_keep)
  494.     {
  495.       int n;
  496.  
  497.       if (ar_record == ar_last)
  498.         {
  499. #if 0
  500.           fprintf (stderr, _("Flush...\n"));
  501. #endif
  502.           fl_read ();
  503.           number_of_blocks_read++;
  504.           ar_record = ar_block;
  505.           number_of_kept_records_in_block = blocking;
  506.           if (number_of_kept_records_in_block > number_of_records_to_keep)
  507.         number_of_kept_records_in_block = number_of_records_to_keep;
  508.         }
  509.       n = number_of_kept_records_in_block;
  510.       if (n > number_of_records_needed)
  511.         n = number_of_records_needed;
  512.  
  513. #if 0
  514.       fprintf (stderr, _("Copying %d\n"), n);
  515. #endif
  516.       memcpy ((void *) (new_block + number_of_new_records),
  517.           (void *) ar_record,
  518.           (size_t) (n * RECORDSIZE));
  519.       number_of_new_records += n;
  520.       number_of_records_needed -= n;
  521.       ar_record += n;
  522.       number_of_records_to_keep -= n;
  523.       number_of_kept_records_in_block -= n;
  524. #if 0
  525.       fprintf (stderr,
  526.            _("Now new %d  need %d  keep %d  keep_in %d rec %d/%d\n"),
  527.            number_of_new_records, number_of_records_needed,
  528.            number_of_records_to_keep, number_of_kept_records_in_block,
  529.            ar_record-ar_block, ar_last-ar_block);
  530. #endif
  531.  
  532.       if (number_of_records_needed == 0)
  533.         {
  534.           write_block (1);
  535.         }
  536.     }
  537.     }
  538.  
  539.   write_eot ();
  540.   close_archive ();
  541.   names_notfound ();
  542. }
  543.  
  544. /*---.
  545. | ?  |
  546. `---*/
  547.  
  548. static void
  549. write_block (int f)
  550. {
  551. #if 0
  552.   fprintf (stderr, _("Write block\n"));
  553. #endif
  554.  
  555.   /* We've filled out a block.  Write it out.  Backspace back to where we
  556.      started.  */
  557.  
  558.   if (archive != STDIN)
  559.     move_arch (-(number_of_blocks_read + 1));
  560.  
  561.   save_block = ar_block;
  562.   ar_block = new_block;
  563.  
  564.   if (archive == STDIN)
  565.     archive = STDOUT;
  566.   fl_write ();
  567.  
  568.   if (archive == STDOUT)
  569.     archive = STDIN;
  570.   ar_block = save_block;
  571.  
  572.   if (f)
  573.     {
  574.  
  575.       /* Move the tape head back to where we were.  */
  576.  
  577.       if (archive != STDIN)
  578.     move_arch (number_of_blocks_read);
  579.       number_of_blocks_read--;
  580.     }
  581.  
  582.   number_of_records_needed = blocking;
  583.   number_of_new_records = 0;
  584. }
  585.  
  586. /*-----------------------------------------------------------------------.
  587. | Move archive descriptor by n blocks worth.  If n is positive we move     |
  588. | forward, else we move negative.  If its a tape, MTIOCTOP had better     |
  589. | work.  If its something else, we try to seek on it.  If we can't seek, |
  590. | we lose!                                 |
  591. `-----------------------------------------------------------------------*/
  592.  
  593. static int
  594. move_arch (int n)
  595. {
  596.   off_t cur;
  597.  
  598. #ifdef MTIOCTOP
  599.   struct mtop t;
  600.   int er;
  601.  
  602.   if (n > 0)
  603.     {
  604.       t.mt_op = MTFSR;
  605.       t.mt_count = n;
  606.     }
  607.   else
  608.     {
  609.       t.mt_op = MTBSR;
  610.       t.mt_count = -n;
  611.     }
  612.   if (er = rmtioctl (archive, MTIOCTOP, (char *) &t), er >= 0)
  613.     return 1;
  614.   if (errno == EIO
  615.       && (er = rmtioctl (archive, MTIOCTOP, (char *) &t), er >= 0))
  616.     return 1;
  617. #endif
  618.  
  619.   cur = rmtlseek (archive, 0L, 1);
  620.   cur += blocksize * n;
  621.  
  622. #if 0
  623.   fprintf (stderr, _("Fore to %x\n"), cur);
  624. #endif
  625.   if (rmtlseek (archive, cur, 0) != cur)
  626.     /* Lseek failed.  Try a different method.  */
  627.     ERROR ((TAREXIT_FAILURE, 0, _("Could not re-position archive file")));
  628.  
  629.   return 3;
  630. }
  631.