]> Dogcows Code - chaz/tar/blob - src/update.c
GNU tar 1.12
[chaz/tar] / src / update.c
1 /* Update a tar archive.
2 Copyright (C) 1988, 1992, 1994, 1996, 1997 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 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 "common.h"
25
26 /* FIXME: This module should not directly handle the following variable,
27 instead, this should be done in buffer.c only. */
28 extern union block *current_block;
29
30 /* We've hit the end of the old stuff, and its time to start writing new
31 stuff to the tape. This involves seeking back one record and
32 re-writing the current record (which has been changed). */
33 int time_to_start_writing = 0;
34
35 /* Pointer to where we started to write in the first record we write out.
36 This is used if we can't backspace the output and have to null out the
37 first part of the record. */
38 char *output_start;
39
40 /*------------------------------------------------------------------------.
41 | Catenate file PATH to the archive without creating a header for it. It |
42 | had better be a tar file or the archive is screwed. |
43 `------------------------------------------------------------------------*/
44
45 static void
46 append_file (char *path)
47 {
48 int handle;
49 struct stat stat_data;
50 long bytes_left;
51
52 if (stat (path, &stat_data) != 0
53 || (handle = open (path, O_RDONLY | O_BINARY), handle < 0))
54 {
55 ERROR ((0, errno, _("Cannot open file %s"), path));
56 return;
57 }
58
59 bytes_left = stat_data.st_size;
60
61 while (bytes_left > 0)
62 {
63 union block *start = find_next_block ();
64 long buffer_size = available_space_after (start);
65 int status;
66
67 if (bytes_left < buffer_size)
68 {
69 buffer_size = bytes_left;
70 status = buffer_size % BLOCKSIZE;
71 if (status)
72 memset (start->buffer + bytes_left, 0,
73 (size_t) (BLOCKSIZE - status));
74 }
75
76 status = read (handle, start->buffer, (size_t) buffer_size);
77 if (status < 0)
78 FATAL_ERROR ((0, errno,
79 _("Read error at byte %ld reading %d bytes in file %s"),
80 stat_data.st_size - bytes_left, buffer_size, path));
81 bytes_left -= status;
82
83 set_next_block_after (start + (status - 1) / BLOCKSIZE);
84
85 if (status != buffer_size)
86 FATAL_ERROR ((0, 0, _("%s: File shrunk by %d bytes, (yark!)"),
87 path, bytes_left));
88 }
89
90 close (handle);
91 }
92
93 /*-----------------------------------------------------------------------.
94 | Implement the 'r' (add files to end of archive), and 'u' (add files to |
95 | end of archive if they arent there, or are more up to date than the |
96 | version in the archive.) commands. |
97 `-----------------------------------------------------------------------*/
98
99 void
100 update_archive (void)
101 {
102 enum read_header previous_status = HEADER_STILL_UNREAD;
103 int found_end = 0;
104
105 name_gather ();
106 if (subcommand_option == UPDATE_SUBCOMMAND)
107 name_expand ();
108 open_archive (ACCESS_UPDATE);
109
110 while (!found_end)
111 {
112 enum read_header status = read_header ();
113
114 switch (status)
115 {
116 case HEADER_STILL_UNREAD:
117 abort ();
118
119 case HEADER_SUCCESS:
120 {
121 struct name *name;
122
123 if (subcommand_option == UPDATE_SUBCOMMAND
124 && (name = name_scan (current_file_name), name))
125 {
126 struct stat stat_data;
127 enum archive_format unused;
128
129 decode_header (current_header, &current_stat, &unused, 0);
130 if (stat (current_file_name, &stat_data) < 0)
131 ERROR ((0, errno, _("Cannot stat %s"), current_file_name));
132 else if (current_stat.st_mtime >= stat_data.st_mtime)
133 name->found = 1;
134 }
135 set_next_block_after (current_header);
136 if (current_header->oldgnu_header.isextended)
137 skip_extended_headers ();
138 skip_file ((long) current_stat.st_size);
139 break;
140 }
141
142 case HEADER_ZERO_BLOCK:
143 current_block = current_header;
144 found_end = 1;
145 break;
146
147 case HEADER_END_OF_FILE:
148 found_end = 1;
149 break;
150
151 case HEADER_FAILURE:
152 set_next_block_after (current_header);
153 switch (previous_status)
154 {
155 case HEADER_STILL_UNREAD:
156 WARN ((0, 0, _("This does not look like a tar archive")));
157 /* Fall through. */
158
159 case HEADER_SUCCESS:
160 case HEADER_ZERO_BLOCK:
161 ERROR ((0, 0, _("Skipping to next header")));
162 /* Fall through. */
163
164 case HEADER_FAILURE:
165 break;
166
167 case HEADER_END_OF_FILE:
168 abort ();
169 }
170 break;
171 }
172
173 previous_status = status;
174 }
175
176 reset_eof ();
177 time_to_start_writing = 1;
178 output_start = current_block->buffer;
179
180 {
181 char *path;
182
183 while (path = name_from_list (), path)
184 {
185 if (interactive_option && !confirm ("add", path))
186 continue;
187 if (subcommand_option == CAT_SUBCOMMAND)
188 append_file (path);
189 else
190 dump_file (path, -1, 1);
191 }
192 }
193
194 write_eot ();
195 close_archive ();
196 names_notfound ();
197 }
This page took 0.045674 seconds and 5 git commands to generate.