]> Dogcows Code - chaz/tar/blob - src/update.c
Revamp initial name collection functions to ensure proper argument ordering.
[chaz/tar] / src / update.c
1 /* Update a tar archive.
2
3 Copyright 1988, 1992, 1994, 1996-1997, 1999-2001, 2003-2005, 2007, 2010,
4 2013 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;
149 DIR *stream = NULL;
150 int fd = openat (chdir_fd, name->name,
151 open_read_flags | O_DIRECTORY);
152 if (fd < 0)
153 open_error (name->name);
154 else if (! ((stream = fdopendir (fd))
155 && (dirp = streamsavedir (stream))))
156 savedir_error (name->name);
157 else
158 {
159 namebuf_t nbuf = namebuf_create (name->name);
160
161 for (p = dirp; *p; p += strlen (p) + 1)
162 addname (namebuf_name (nbuf, p),
163 0, false, NULL);
164
165 namebuf_free (nbuf);
166 free (dirp);
167
168 remname (name);
169 }
170
171 if (stream
172 ? closedir (stream) != 0
173 : 0 <= fd && close (fd) != 0)
174 savedir_error (name->name);
175 }
176 else if (tar_timespec_cmp (get_stat_mtime (&s),
177 current_stat_info.mtime)
178 <= 0)
179 remname (name);
180 }
181 }
182
183 skip_member ();
184 break;
185 }
186
187 case HEADER_ZERO_BLOCK:
188 current_block = current_header;
189 found_end = true;
190 break;
191
192 case HEADER_END_OF_FILE:
193 found_end = true;
194 break;
195
196 case HEADER_FAILURE:
197 set_next_block_after (current_header);
198 switch (previous_status)
199 {
200 case HEADER_STILL_UNREAD:
201 WARN ((0, 0, _("This does not look like a tar archive")));
202 /* Fall through. */
203
204 case HEADER_SUCCESS:
205 case HEADER_ZERO_BLOCK:
206 ERROR ((0, 0, _("Skipping to next header")));
207 /* Fall through. */
208
209 case HEADER_FAILURE:
210 break;
211
212 case HEADER_END_OF_FILE:
213 case HEADER_SUCCESS_EXTENDED:
214 abort ();
215 }
216 break;
217 }
218
219 tar_stat_destroy (&current_stat_info);
220 previous_status = status;
221 }
222
223 reset_eof ();
224 time_to_start_writing = true;
225 output_start = current_block->buffer;
226
227 {
228 struct name const *p;
229 while ((p = name_from_list ()) != NULL)
230 {
231 char *file_name = p->name;
232 if (excluded_name (file_name))
233 continue;
234 if (interactive_option && !confirm ("add", file_name))
235 continue;
236 if (subcommand_option == CAT_SUBCOMMAND)
237 append_file (file_name);
238 else
239 dump_file (0, file_name, file_name);
240 }
241 }
242
243 write_eot ();
244 close_archive ();
245 finish_deferred_unlinks ();
246 names_notfound ();
247 }
This page took 0.046192 seconds and 4 git commands to generate.