]> Dogcows Code - chaz/tar/blob - src/update.c
Update copyright years.
[chaz/tar] / src / update.c
1 /* Update a tar archive.
2
3 Copyright 1988, 1992, 1994, 1996-1997, 1999-2001, 2003-2005, 2007,
4 2010, 2013-2014 Free Software Foundation, Inc.
5
6 This file is part of GNU tar.
7
8 GNU tar is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU tar is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* Implement the 'r', 'u' and 'A' options for tar. 'A' means that the
22 file names are tar files, and they should simply be appended to the end
23 of the archive. No attempt is made to record the reads from the args; if
24 they're on raw tape or something like that, it'll probably lose... */
25
26 #include <system.h>
27 #include <quotearg.h>
28 #include "common.h"
29
30 /* FIXME: This module should not directly handle the following variable,
31 instead, this should be done in buffer.c only. */
32 extern union block *current_block;
33
34 /* We've hit the end of the old stuff, and its time to start writing new
35 stuff to the tape. This involves seeking back one record and
36 re-writing the current record (which has been changed).
37 FIXME: Either eliminate it or move it to common.h.
38 */
39 bool time_to_start_writing;
40
41 /* Pointer to where we started to write in the first record we write out.
42 This is used if we can't backspace the output and have to null out the
43 first part of the record. */
44 char *output_start;
45
46 /* Catenate file FILE_NAME to the archive without creating a header for it.
47 It had better be a tar file or the archive is screwed. */
48 static void
49 append_file (char *file_name)
50 {
51 int handle = openat (chdir_fd, file_name, O_RDONLY | O_BINARY);
52 struct stat stat_data;
53
54 if (handle < 0)
55 {
56 open_error (file_name);
57 return;
58 }
59
60 if (fstat (handle, &stat_data) != 0)
61 stat_error (file_name);
62 else
63 {
64 off_t bytes_left = stat_data.st_size;
65
66 while (bytes_left > 0)
67 {
68 union block *start = find_next_block ();
69 size_t buffer_size = available_space_after (start);
70 size_t status;
71 char buf[UINTMAX_STRSIZE_BOUND];
72
73 if (bytes_left < buffer_size)
74 {
75 buffer_size = bytes_left;
76 status = buffer_size % BLOCKSIZE;
77 if (status)
78 memset (start->buffer + bytes_left, 0, BLOCKSIZE - status);
79 }
80
81 status = safe_read (handle, start->buffer, buffer_size);
82 if (status == SAFE_READ_ERROR)
83 read_fatal_details (file_name, stat_data.st_size - bytes_left,
84 buffer_size);
85 if (status == 0)
86 FATAL_ERROR ((0, 0,
87 ngettext ("%s: File shrank by %s byte",
88 "%s: File shrank by %s bytes",
89 bytes_left),
90 quotearg_colon (file_name),
91 STRINGIFY_BIGINT (bytes_left, buf)));
92
93 bytes_left -= status;
94
95 set_next_block_after (start + (status - 1) / BLOCKSIZE);
96 }
97 }
98
99 if (close (handle) != 0)
100 close_error (file_name);
101 }
102
103 /* Implement the 'r' (add files to end of archive), and 'u' (add files
104 to end of archive if they aren't there, or are more up to date than
105 the version in the archive) commands. */
106 void
107 update_archive (void)
108 {
109 enum read_header previous_status = HEADER_STILL_UNREAD;
110 bool found_end = false;
111
112 name_gather ();
113 open_archive (ACCESS_UPDATE);
114 buffer_write_global_xheader ();
115
116 while (!found_end)
117 {
118 enum read_header status = read_header (&current_header,
119 &current_stat_info,
120 read_header_auto);
121
122 switch (status)
123 {
124 case HEADER_STILL_UNREAD:
125 case HEADER_SUCCESS_EXTENDED:
126 abort ();
127
128 case HEADER_SUCCESS:
129 {
130 struct name *name;
131
132 decode_header (current_header, &current_stat_info,
133 &current_format, 0);
134 transform_stat_info (current_header->header.typeflag,
135 &current_stat_info);
136 archive_format = current_format;
137
138 if (subcommand_option == UPDATE_SUBCOMMAND
139 && (name = name_scan (current_stat_info.file_name)) != NULL)
140 {
141 struct stat s;
142
143 chdir_do (name->change_dir);
144 if (deref_stat (current_stat_info.file_name, &s) == 0)
145 {
146 if (S_ISDIR (s.st_mode))
147 {
148 char *p, *dirp = tar_savedir (name->name, 1);
149 if (dirp)
150 {
151 namebuf_t nbuf = namebuf_create (name->name);
152
153 for (p = dirp; *p; p += strlen (p) + 1)
154 addname (namebuf_name (nbuf, p),
155 0, false, NULL);
156
157 namebuf_free (nbuf);
158 free (dirp);
159
160 remname (name);
161 }
162 }
163 else if (tar_timespec_cmp (get_stat_mtime (&s),
164 current_stat_info.mtime)
165 <= 0)
166 remname (name);
167 }
168 }
169
170 skip_member ();
171 break;
172 }
173
174 case HEADER_ZERO_BLOCK:
175 current_block = current_header;
176 found_end = true;
177 break;
178
179 case HEADER_END_OF_FILE:
180 found_end = true;
181 break;
182
183 case HEADER_FAILURE:
184 set_next_block_after (current_header);
185 switch (previous_status)
186 {
187 case HEADER_STILL_UNREAD:
188 WARN ((0, 0, _("This does not look like a tar archive")));
189 /* Fall through. */
190
191 case HEADER_SUCCESS:
192 case HEADER_ZERO_BLOCK:
193 ERROR ((0, 0, _("Skipping to next header")));
194 /* Fall through. */
195
196 case HEADER_FAILURE:
197 break;
198
199 case HEADER_END_OF_FILE:
200 case HEADER_SUCCESS_EXTENDED:
201 abort ();
202 }
203 break;
204 }
205
206 tar_stat_destroy (&current_stat_info);
207 previous_status = status;
208 }
209
210 reset_eof ();
211 time_to_start_writing = true;
212 output_start = current_block->buffer;
213
214 {
215 struct name const *p;
216 while ((p = name_from_list ()) != NULL)
217 {
218 char *file_name = p->name;
219 if (excluded_name (file_name))
220 continue;
221 if (interactive_option && !confirm ("add", file_name))
222 continue;
223 if (subcommand_option == CAT_SUBCOMMAND)
224 append_file (file_name);
225 else
226 dump_file (0, file_name, file_name);
227 }
228 }
229
230 write_eot ();
231 close_archive ();
232 finish_deferred_unlinks ();
233 names_notfound ();
234 }
This page took 0.038644 seconds and 4 git commands to generate.