Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / tar / src / update.c
1 /* Update a tar archive.
2
3    Copyright (C) 1988, 1992, 1994, 1996, 1997, 1999, 2000, 2001 Free
4    Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any later
9    version.
10
11    This program is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
14    Public License for more details.
15
16    You should have received a copy of the GNU General Public License along
17    with this program; if not, write to the Free Software Foundation, Inc.,
18    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 /* Implement the 'r', 'u' and 'A' options for tar.  'A' means that the
21    file names are tar files, and they should simply be appended to the end
22    of the archive.  No attempt is made to record the reads from the args; if
23    they're on raw tape or something like that, it'll probably lose...  */
24
25 #include "system.h"
26 #include <quotearg.h>
27 #include "common.h"
28
29 /* FIXME: This module should not directly handle the following variable,
30    instead, this should be done in buffer.c only.  */
31 extern union block *current_block;
32
33 /* We've hit the end of the old stuff, and its time to start writing new
34    stuff to the tape.  This involves seeking back one record and
35    re-writing the current record (which has been changed).  */
36 int time_to_start_writing;
37
38 /* Pointer to where we started to write in the first record we write out.
39    This is used if we can't backspace the output and have to null out the
40    first part of the record.  */
41 char *output_start;
42
43 /* Catenate file PATH to the archive without creating a header for it.
44    It had better be a tar file or the archive is screwed.  */
45 static void
46 append_file (char *path)
47 {
48   int handle = open (path, O_RDONLY | O_BINARY);
49   struct stat stat_data;
50
51   if (handle < 0)
52     {
53       open_error (path);
54       return;
55     }
56
57   if (fstat (handle, &stat_data) != 0)
58     stat_error (path);
59   else
60     {
61       off_t bytes_left = stat_data.st_size;
62
63       while (bytes_left > 0)
64         {
65           union block *start = find_next_block ();
66           size_t buffer_size = available_space_after (start);
67           ssize_t status;
68           char buf[UINTMAX_STRSIZE_BOUND];
69
70           if (bytes_left < buffer_size)
71             {
72               buffer_size = bytes_left;
73               status = buffer_size % BLOCKSIZE;
74               if (status)
75                 memset (start->buffer + bytes_left, 0, BLOCKSIZE - status);
76             }
77
78           status = safe_read (handle, start->buffer, buffer_size);
79           if (status < 0)
80             read_fatal_details (path, stat_data.st_size - bytes_left,
81                                 buffer_size);
82           if (status == 0)
83             FATAL_ERROR ((0, 0, _("%s: File shrank by %s bytes"),
84                           quotearg_colon (path),
85                           STRINGIFY_BIGINT (bytes_left, buf)));
86
87           bytes_left -= status;
88
89           set_next_block_after (start + (status - 1) / BLOCKSIZE);
90         }
91     }
92
93   if (close (handle) != 0)
94     close_error (path);
95 }
96
97 /* Implement the 'r' (add files to end of archive), and 'u' (add files
98    to end of archive if they aren't there, or are more up to date than
99    the version in the archive) commands.  */
100 void
101 update_archive (void)
102 {
103   enum read_header previous_status = HEADER_STILL_UNREAD;
104   int found_end = 0;
105
106   name_gather ();
107   open_archive (ACCESS_UPDATE);
108
109   while (!found_end)
110     {
111       enum read_header status = read_header (0);
112
113       switch (status)
114         {
115         case HEADER_STILL_UNREAD:
116           abort ();
117
118         case HEADER_SUCCESS:
119           {
120             struct name *name;
121
122             if (subcommand_option == UPDATE_SUBCOMMAND
123                 && (name = name_scan (current_file_name), name))
124               {
125                 struct stat s;
126                 enum archive_format unused;
127
128                 decode_header (current_header, &current_stat, &unused, 0);
129                 chdir_do (name->change_dir);
130                 if (deref_stat (dereference_option, current_file_name, &s) == 0
131                     && s.st_mtime <= current_stat.st_mtime)
132                   add_avoided_name (current_file_name);
133               }
134             skip_member ();
135             break;
136           }
137
138         case HEADER_ZERO_BLOCK:
139           current_block = current_header;
140           found_end = 1;
141           break;
142
143         case HEADER_END_OF_FILE:
144           found_end = 1;
145           break;
146
147         case HEADER_FAILURE:
148           set_next_block_after (current_header);
149           switch (previous_status)
150             {
151             case HEADER_STILL_UNREAD:
152               WARN ((0, 0, _("This does not look like a tar archive")));
153               /* Fall through.  */
154
155             case HEADER_SUCCESS:
156             case HEADER_ZERO_BLOCK:
157               ERROR ((0, 0, _("Skipping to next header")));
158               /* Fall through.  */
159
160             case HEADER_FAILURE:
161               break;
162
163             case HEADER_END_OF_FILE:
164               abort ();
165             }
166           break;
167         }
168
169       previous_status = status;
170     }
171
172   reset_eof ();
173   time_to_start_writing = 1;
174   output_start = current_block->buffer;
175
176   {
177     char *path;
178
179     while (path = name_from_list (), path)
180       {
181         if (excluded_name (path))
182           continue;
183         if (interactive_option && !confirm ("add", path))
184           continue;
185         if (subcommand_option == CAT_SUBCOMMAND)
186           append_file (path);
187         else
188           dump_file (path, 1, (dev_t) 0);
189       }
190   }
191
192   write_eot ();
193   close_archive ();
194   names_notfound ();
195 }