| 1 | /* Update a tar archive.
|
|---|
| 2 |
|
|---|
| 3 | Copyright (C) 1988, 1992, 1994, 1996, 1997, 1999, 2000, 2001, 2003,
|
|---|
| 4 | 2004, 2005 Free Software Foundation, Inc.
|
|---|
| 5 |
|
|---|
| 6 | This program is free software; you can redistribute it and/or modify it
|
|---|
| 7 | under the terms of the GNU General Public License as published by the
|
|---|
| 8 | Free Software Foundation; either version 2, or (at your option) any later
|
|---|
| 9 | version.
|
|---|
| 10 |
|
|---|
| 11 | This program is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|---|
| 14 | Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License along
|
|---|
| 17 | with this program; if not, write to the Free Software Foundation, Inc.,
|
|---|
| 18 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
|---|
| 19 |
|
|---|
| 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... */
|
|---|
| 24 |
|
|---|
| 25 | #include <system.h>
|
|---|
| 26 | #include <quotearg.h>
|
|---|
| 27 | #include "common.h"
|
|---|
| 28 |
|
|---|
| 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;
|
|---|
| 32 |
|
|---|
| 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.
|
|---|
| 37 | */
|
|---|
| 38 | bool time_to_start_writing;
|
|---|
| 39 |
|
|---|
| 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;
|
|---|
| 44 |
|
|---|
| 45 | /* Catenate file FILE_NAME to the archive without creating a header for it.
|
|---|
| 46 | It had better be a tar file or the archive is screwed. */
|
|---|
| 47 | static void
|
|---|
| 48 | append_file (char *file_name)
|
|---|
| 49 | {
|
|---|
| 50 | int handle = open (file_name, O_RDONLY | O_BINARY);
|
|---|
| 51 | struct stat stat_data;
|
|---|
| 52 |
|
|---|
| 53 | if (handle < 0)
|
|---|
| 54 | {
|
|---|
| 55 | open_error (file_name);
|
|---|
| 56 | return;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | if (fstat (handle, &stat_data) != 0)
|
|---|
| 60 | stat_error (file_name);
|
|---|
| 61 | else
|
|---|
| 62 | {
|
|---|
| 63 | off_t bytes_left = stat_data.st_size;
|
|---|
| 64 |
|
|---|
| 65 | while (bytes_left > 0)
|
|---|
| 66 | {
|
|---|
| 67 | union block *start = find_next_block ();
|
|---|
| 68 | size_t buffer_size = available_space_after (start);
|
|---|
| 69 | size_t status;
|
|---|
| 70 | char buf[UINTMAX_STRSIZE_BOUND];
|
|---|
| 71 |
|
|---|
| 72 | if (bytes_left < buffer_size)
|
|---|
| 73 | {
|
|---|
| 74 | buffer_size = bytes_left;
|
|---|
| 75 | status = buffer_size % BLOCKSIZE;
|
|---|
| 76 | if (status)
|
|---|
| 77 | memset (start->buffer + bytes_left, 0, BLOCKSIZE - status);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | status = safe_read (handle, start->buffer, buffer_size);
|
|---|
| 81 | if (status == SAFE_READ_ERROR)
|
|---|
| 82 | read_fatal_details (file_name, stat_data.st_size - bytes_left,
|
|---|
| 83 | buffer_size);
|
|---|
| 84 | if (status == 0)
|
|---|
| 85 | FATAL_ERROR ((0, 0,
|
|---|
| 86 | ngettext ("%s: File shrank by %s byte",
|
|---|
| 87 | "%s: File shrank by %s bytes",
|
|---|
| 88 | bytes_left),
|
|---|
| 89 | quotearg_colon (file_name),
|
|---|
| 90 | STRINGIFY_BIGINT (bytes_left, buf)));
|
|---|
| 91 |
|
|---|
| 92 | bytes_left -= status;
|
|---|
| 93 |
|
|---|
| 94 | set_next_block_after (start + (status - 1) / BLOCKSIZE);
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | if (close (handle) != 0)
|
|---|
| 99 | close_error (file_name);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /* Implement the 'r' (add files to end of archive), and 'u' (add files
|
|---|
| 103 | to end of archive if they aren't there, or are more up to date than
|
|---|
| 104 | the version in the archive) commands. */
|
|---|
| 105 | void
|
|---|
| 106 | update_archive (void)
|
|---|
| 107 | {
|
|---|
| 108 | enum read_header previous_status = HEADER_STILL_UNREAD;
|
|---|
| 109 | bool found_end = false;
|
|---|
| 110 |
|
|---|
| 111 | name_gather ();
|
|---|
| 112 | open_archive (ACCESS_UPDATE);
|
|---|
| 113 | xheader_write_global ();
|
|---|
| 114 |
|
|---|
| 115 | while (!found_end)
|
|---|
| 116 | {
|
|---|
| 117 | enum read_header status = read_header (false);
|
|---|
| 118 |
|
|---|
| 119 | switch (status)
|
|---|
| 120 | {
|
|---|
| 121 | case HEADER_STILL_UNREAD:
|
|---|
| 122 | case HEADER_SUCCESS_EXTENDED:
|
|---|
| 123 | abort ();
|
|---|
| 124 |
|
|---|
| 125 | case HEADER_SUCCESS:
|
|---|
| 126 | {
|
|---|
| 127 | struct name *name;
|
|---|
| 128 |
|
|---|
| 129 | decode_header (current_header, ¤t_stat_info,
|
|---|
| 130 | ¤t_format, 0);
|
|---|
| 131 | archive_format = current_format;
|
|---|
| 132 |
|
|---|
| 133 | if (subcommand_option == UPDATE_SUBCOMMAND
|
|---|
| 134 | && (name = name_scan (current_stat_info.file_name)) != NULL)
|
|---|
| 135 | {
|
|---|
| 136 | struct stat s;
|
|---|
| 137 |
|
|---|
| 138 | chdir_do (name->change_dir);
|
|---|
| 139 | if (deref_stat (dereference_option,
|
|---|
| 140 | current_stat_info.file_name, &s) == 0
|
|---|
| 141 | && (tar_timespec_cmp (get_stat_mtime (&s),
|
|---|
| 142 | current_stat_info.mtime)
|
|---|
| 143 | <= 0))
|
|---|
| 144 | add_avoided_name (current_stat_info.file_name);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | skip_member ();
|
|---|
| 148 | break;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | case HEADER_ZERO_BLOCK:
|
|---|
| 152 | current_block = current_header;
|
|---|
| 153 | found_end = true;
|
|---|
| 154 | break;
|
|---|
| 155 |
|
|---|
| 156 | case HEADER_END_OF_FILE:
|
|---|
| 157 | found_end = true;
|
|---|
| 158 | break;
|
|---|
| 159 |
|
|---|
| 160 | case HEADER_FAILURE:
|
|---|
| 161 | set_next_block_after (current_header);
|
|---|
| 162 | switch (previous_status)
|
|---|
| 163 | {
|
|---|
| 164 | case HEADER_STILL_UNREAD:
|
|---|
| 165 | WARN ((0, 0, _("This does not look like a tar archive")));
|
|---|
| 166 | /* Fall through. */
|
|---|
| 167 |
|
|---|
| 168 | case HEADER_SUCCESS:
|
|---|
| 169 | case HEADER_ZERO_BLOCK:
|
|---|
| 170 | ERROR ((0, 0, _("Skipping to next header")));
|
|---|
| 171 | /* Fall through. */
|
|---|
| 172 |
|
|---|
| 173 | case HEADER_FAILURE:
|
|---|
| 174 | break;
|
|---|
| 175 |
|
|---|
| 176 | case HEADER_END_OF_FILE:
|
|---|
| 177 | case HEADER_SUCCESS_EXTENDED:
|
|---|
| 178 | abort ();
|
|---|
| 179 | }
|
|---|
| 180 | break;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | tar_stat_destroy (¤t_stat_info);
|
|---|
| 184 | xheader_destroy (&extended_header);
|
|---|
| 185 | previous_status = status;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | reset_eof ();
|
|---|
| 189 | time_to_start_writing = true;
|
|---|
| 190 | output_start = current_block->buffer;
|
|---|
| 191 |
|
|---|
| 192 | {
|
|---|
| 193 | char *file_name;
|
|---|
| 194 |
|
|---|
| 195 | while ((file_name = name_from_list ()) != NULL)
|
|---|
| 196 | {
|
|---|
| 197 | if (excluded_name (file_name))
|
|---|
| 198 | continue;
|
|---|
| 199 | if (interactive_option && !confirm ("add", file_name))
|
|---|
| 200 | continue;
|
|---|
| 201 | if (subcommand_option == CAT_SUBCOMMAND)
|
|---|
| 202 | append_file (file_name);
|
|---|
| 203 | else
|
|---|
| 204 | dump_file (file_name, 1, (dev_t) 0);
|
|---|
| 205 | }
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | write_eot ();
|
|---|
| 209 | close_archive ();
|
|---|
| 210 | names_notfound ();
|
|---|
| 211 | }
|
|---|