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... */
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. */
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. */
50 append_file (char *file_name
)
52 int handle
= openat (chdir_fd
, file_name
, O_RDONLY
| O_BINARY
);
56 open_error (file_name
);
62 union block
*start
= find_next_block ();
63 size_t status
= safe_read (handle
, start
->buffer
,
64 available_space_after (start
));
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. */
85 enum read_header previous_status
= HEADER_STILL_UNREAD
;
86 bool found_end
= false;
89 open_archive (ACCESS_UPDATE
);
90 acting_as_filter
= strcmp (archive_name_array
[0], "-") == 0;
91 xheader_forbid_global ();
95 enum read_header status
= read_header (¤t_header
,
101 case HEADER_STILL_UNREAD
:
102 case HEADER_SUCCESS_EXTENDED
:
109 decode_header (current_header
, ¤t_stat_info
,
111 transform_stat_info (current_header
->header
.typeflag
,
113 archive_format
= current_format
;
115 if (subcommand_option
== UPDATE_SUBCOMMAND
116 && (name
= name_scan (current_stat_info
.file_name
)) != NULL
)
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);
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
);
140 else if (tar_timespec_cmp (get_stat_mtime (&s
),
141 current_stat_info
.mtime
)
147 skim_member (acting_as_filter
);
151 case HEADER_ZERO_BLOCK
:
152 current_block
= current_header
;
156 case HEADER_END_OF_FILE
:
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")));
168 case HEADER_ZERO_BLOCK
:
169 ERROR ((0, 0, _("Skipping to next header")));
174 case HEADER_END_OF_FILE
:
175 case HEADER_SUCCESS_EXTENDED
:
181 tar_stat_destroy (¤t_stat_info
);
182 previous_status
= status
;
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
))
196 if (interactive_option
&& !confirm ("add", file_name
))
198 if (subcommand_option
== CAT_SUBCOMMAND
)
199 append_file (file_name
);
201 dump_file (0, file_name
, file_name
);
207 finish_deferred_unlinks ();