Exclude VCS directory with writing from an archive
[tar.git] / src / update.c
blob7b148b8077896d77418f6edd310733f2bd0058d2
1 /* Update a tar archive.
3 Copyright 1988-2023 Free Software Foundation, Inc.
5 This file is part of GNU tar.
7 GNU tar is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GNU tar is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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... */
25 #include <system.h>
26 #include <quotearg.h>
27 #include "common.h"
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;
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 FIXME: Either eliminate it or move it to common.h.
38 bool time_to_start_writing;
40 /* Pointer to where we started to write in the first record we write out.
41 This is used if we can't backspace the output and have to null out the
42 first part of the record. */
43 char *output_start;
45 static bool acting_as_filter;
47 /* Catenate file FILE_NAME to the archive without creating a header for it.
48 It had better be a tar file or the archive is screwed. */
49 static void
50 append_file (char *file_name)
52 int handle = openat (chdir_fd, file_name, O_RDONLY | O_BINARY);
54 if (handle < 0)
56 open_error (file_name);
57 return;
60 while (true)
62 union block *start = find_next_block ();
63 size_t status = safe_read (handle, start->buffer,
64 available_space_after (start));
65 if (status == 0)
66 break;
67 if (status == SAFE_READ_ERROR)
68 read_fatal (file_name);
69 if (status % BLOCKSIZE)
70 memset (start->buffer + status - status % BLOCKSIZE, 0,
71 BLOCKSIZE - status % BLOCKSIZE);
72 set_next_block_after (start + (status - 1) / BLOCKSIZE);
75 if (close (handle) != 0)
76 close_error (file_name);
79 /* Implement the 'r' (add files to end of archive), and 'u' (add files
80 to end of archive if they aren't there, or are more up to date than
81 the version in the archive) commands. */
82 void
83 update_archive (void)
85 enum read_header previous_status = HEADER_STILL_UNREAD;
86 bool found_end = false;
88 name_gather ();
89 open_archive (ACCESS_UPDATE);
90 acting_as_filter = strcmp (archive_name_array[0], "-") == 0;
91 xheader_forbid_global ();
93 while (!found_end)
95 enum read_header status = read_header (&current_header,
96 &current_stat_info,
97 read_header_auto);
99 switch (status)
101 case HEADER_STILL_UNREAD:
102 case HEADER_SUCCESS_EXTENDED:
103 abort ();
105 case HEADER_SUCCESS:
107 struct name *name;
109 decode_header (current_header, &current_stat_info,
110 &current_format, 0);
111 transform_stat_info (current_header->header.typeflag,
112 &current_stat_info);
113 archive_format = current_format;
115 if (subcommand_option == UPDATE_SUBCOMMAND
116 && (name = name_scan (current_stat_info.file_name)) != NULL)
118 struct stat s;
120 chdir_do (name->change_dir);
121 if (deref_stat (current_stat_info.file_name, &s) == 0)
123 if (S_ISDIR (s.st_mode))
125 char *p, *dirp = tar_savedir (name->name, 1);
126 if (dirp)
128 namebuf_t nbuf = namebuf_create (name->name);
130 for (p = dirp; *p; p += strlen (p) + 1)
131 addname (namebuf_name (nbuf, p),
132 name->change_dir, false, NULL);
134 namebuf_free (nbuf);
135 free (dirp);
137 remname (name);
140 else if (tar_timespec_cmp (get_stat_mtime (&s),
141 current_stat_info.mtime)
142 <= 0)
143 remname (name);
147 skim_member (acting_as_filter);
148 break;
151 case HEADER_ZERO_BLOCK:
152 current_block = current_header;
153 found_end = true;
154 break;
156 case HEADER_END_OF_FILE:
157 found_end = true;
158 break;
160 case HEADER_FAILURE:
161 set_next_block_after (current_header);
162 switch (previous_status)
164 case HEADER_STILL_UNREAD:
165 WARN ((0, 0, _("This does not look like a tar archive")));
166 FALLTHROUGH;
167 case HEADER_SUCCESS:
168 case HEADER_ZERO_BLOCK:
169 ERROR ((0, 0, _("Skipping to next header")));
170 FALLTHROUGH;
171 case HEADER_FAILURE:
172 break;
174 case HEADER_END_OF_FILE:
175 case HEADER_SUCCESS_EXTENDED:
176 abort ();
178 break;
181 tar_stat_destroy (&current_stat_info);
182 previous_status = status;
185 reset_eof ();
186 time_to_start_writing = true;
187 output_start = current_block->buffer;
190 struct name const *p;
191 while ((p = name_from_list ()) != NULL)
193 char *file_name = p->name;
194 if (excluded_name (file_name, NULL))
195 continue;
196 if (interactive_option && !confirm ("add", file_name))
197 continue;
198 if (subcommand_option == CAT_SUBCOMMAND)
199 append_file (file_name);
200 else
201 dump_file (0, file_name, file_name);
205 write_eot ();
206 close_archive ();
207 finish_deferred_unlinks ();
208 names_notfound ();