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