1 /* Update a tar archive.
3 Copyright 1988, 1992, 1994, 1996-1997, 1999-2001, 2003-2005, 2007, 2010,
4 2013 Free Software Foundation, Inc.
6 This file is part of GNU tar.
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.
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.
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/>. */
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... */
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
;
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.
39 bool time_to_start_writing
;
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. */
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. */
49 append_file (char *file_name
)
51 int handle
= openat (chdir_fd
, file_name
, O_RDONLY
| O_BINARY
);
52 struct stat stat_data
;
56 open_error (file_name
);
60 if (fstat (handle
, &stat_data
) != 0)
61 stat_error (file_name
);
64 off_t bytes_left
= stat_data
.st_size
;
66 while (bytes_left
> 0)
68 union block
*start
= find_next_block ();
69 size_t buffer_size
= available_space_after (start
);
71 char buf
[UINTMAX_STRSIZE_BOUND
];
73 if (bytes_left
< buffer_size
)
75 buffer_size
= bytes_left
;
76 status
= buffer_size
% BLOCKSIZE
;
78 memset (start
->buffer
+ bytes_left
, 0, BLOCKSIZE
- status
);
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
,
87 ngettext ("%s: File shrank by %s byte",
88 "%s: File shrank by %s bytes",
90 quotearg_colon (file_name
),
91 STRINGIFY_BIGINT (bytes_left
, buf
)));
95 set_next_block_after (start
+ (status
- 1) / BLOCKSIZE
);
99 if (close (handle
) != 0)
100 close_error (file_name
);
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. */
107 update_archive (void)
109 enum read_header previous_status
= HEADER_STILL_UNREAD
;
110 bool found_end
= false;
113 open_archive (ACCESS_UPDATE
);
114 buffer_write_global_xheader ();
118 enum read_header status
= read_header (¤t_header
,
124 case HEADER_STILL_UNREAD
:
125 case HEADER_SUCCESS_EXTENDED
:
132 decode_header (current_header
, ¤t_stat_info
,
134 transform_stat_info (current_header
->header
.typeflag
,
136 archive_format
= current_format
;
138 if (subcommand_option
== UPDATE_SUBCOMMAND
139 && (name
= name_scan (current_stat_info
.file_name
)) != NULL
)
143 chdir_do (name
->change_dir
);
144 if (deref_stat (current_stat_info
.file_name
, &s
) == 0)
146 if (S_ISDIR (s
.st_mode
))
150 int fd
= openat (chdir_fd
, name
->name
,
151 open_read_flags
| O_DIRECTORY
);
153 open_error (name
->name
);
154 else if (! ((stream
= fdopendir (fd
))
155 && (dirp
= streamsavedir (stream
))))
156 savedir_error (name
->name
);
159 namebuf_t nbuf
= namebuf_create (name
->name
);
161 for (p
= dirp
; *p
; p
+= strlen (p
) + 1)
162 addname (namebuf_name (nbuf
, p
),
172 ? closedir (stream
) != 0
173 : 0 <= fd
&& close (fd
) != 0)
174 savedir_error (name
->name
);
176 else if (tar_timespec_cmp (get_stat_mtime (&s
),
177 current_stat_info
.mtime
)
187 case HEADER_ZERO_BLOCK
:
188 current_block
= current_header
;
192 case HEADER_END_OF_FILE
:
197 set_next_block_after (current_header
);
198 switch (previous_status
)
200 case HEADER_STILL_UNREAD
:
201 WARN ((0, 0, _("This does not look like a tar archive")));
205 case HEADER_ZERO_BLOCK
:
206 ERROR ((0, 0, _("Skipping to next header")));
212 case HEADER_END_OF_FILE
:
213 case HEADER_SUCCESS_EXTENDED
:
219 tar_stat_destroy (¤t_stat_info
);
220 previous_status
= status
;
224 time_to_start_writing
= true;
225 output_start
= current_block
->buffer
;
228 struct name
const *p
;
229 while ((p
= name_from_list ()) != NULL
)
231 char *file_name
= p
->name
;
232 if (excluded_name (file_name
))
234 if (interactive_option
&& !confirm ("add", file_name
))
236 if (subcommand_option
== CAT_SUBCOMMAND
)
237 append_file (file_name
);
239 dump_file (0, file_name
, file_name
);
245 finish_deferred_unlinks ();