| 1 | /* Extract files from a tar archive.
|
|---|
| 2 |
|
|---|
| 3 | Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
|
|---|
| 4 | 2001, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
|
|---|
| 5 |
|
|---|
| 6 | Written by John Gilmore, on 1985-11-19.
|
|---|
| 7 |
|
|---|
| 8 | This program is free software; you can redistribute it and/or modify it
|
|---|
| 9 | under the terms of the GNU General Public License as published by the
|
|---|
| 10 | Free Software Foundation; either version 2, or (at your option) any later
|
|---|
| 11 | version.
|
|---|
| 12 |
|
|---|
| 13 | This program is distributed in the hope that it will be useful, but
|
|---|
| 14 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|---|
| 16 | Public License for more details.
|
|---|
| 17 |
|
|---|
| 18 | You should have received a copy of the GNU General Public License along
|
|---|
| 19 | with this program; if not, write to the Free Software Foundation, Inc.,
|
|---|
| 20 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
|---|
| 21 |
|
|---|
| 22 | #include <system.h>
|
|---|
| 23 | #include <quotearg.h>
|
|---|
| 24 | #include <utimens.h>
|
|---|
| 25 | #include <errno.h>
|
|---|
| 26 | #include <xgetcwd.h>
|
|---|
| 27 |
|
|---|
| 28 | #include "common.h"
|
|---|
| 29 |
|
|---|
| 30 | static bool we_are_root; /* true if our effective uid == 0 */
|
|---|
| 31 | static mode_t newdir_umask; /* umask when creating new directories */
|
|---|
| 32 | static mode_t current_umask; /* current umask (which is set to 0 if -p) */
|
|---|
| 33 |
|
|---|
| 34 | /* Status of the permissions of a file that we are extracting. */
|
|---|
| 35 | enum permstatus
|
|---|
| 36 | {
|
|---|
| 37 | /* This file may have existed already; its permissions are unknown. */
|
|---|
| 38 | UNKNOWN_PERMSTATUS,
|
|---|
| 39 |
|
|---|
| 40 | /* This file was created using the permissions from the archive,
|
|---|
| 41 | except with S_IRWXG | S_IRWXO masked out if 0 < same_owner_option. */
|
|---|
| 42 | ARCHIVED_PERMSTATUS,
|
|---|
| 43 |
|
|---|
| 44 | /* This is an intermediate directory; the archive did not specify
|
|---|
| 45 | its permissions. */
|
|---|
| 46 | INTERDIR_PERMSTATUS
|
|---|
| 47 | };
|
|---|
| 48 |
|
|---|
| 49 | /* List of directories whose statuses we need to extract after we've
|
|---|
| 50 | finished extracting their subsidiary files. If you consider each
|
|---|
| 51 | contiguous subsequence of elements of the form [D]?[^D]*, where [D]
|
|---|
| 52 | represents an element where AFTER_LINKS is nonzero and [^D]
|
|---|
| 53 | represents an element where AFTER_LINKS is zero, then the head
|
|---|
| 54 | of the subsequence has the longest name, and each non-head element
|
|---|
| 55 | in the prefix is an ancestor (in the directory hierarchy) of the
|
|---|
| 56 | preceding element. */
|
|---|
| 57 |
|
|---|
| 58 | struct delayed_set_stat
|
|---|
| 59 | {
|
|---|
| 60 | struct delayed_set_stat *next;
|
|---|
| 61 | dev_t dev;
|
|---|
| 62 | ino_t ino;
|
|---|
| 63 | mode_t mode;
|
|---|
| 64 | uid_t uid;
|
|---|
| 65 | gid_t gid;
|
|---|
| 66 | struct timespec atime;
|
|---|
| 67 | struct timespec mtime;
|
|---|
| 68 | size_t file_name_len;
|
|---|
| 69 | mode_t invert_permissions;
|
|---|
| 70 | enum permstatus permstatus;
|
|---|
| 71 | bool after_links;
|
|---|
| 72 | char file_name[1];
|
|---|
| 73 | };
|
|---|
| 74 |
|
|---|
| 75 | static struct delayed_set_stat *delayed_set_stat_head;
|
|---|
| 76 |
|
|---|
| 77 | /* List of links whose creation we have delayed. */
|
|---|
| 78 | struct delayed_link
|
|---|
| 79 | {
|
|---|
| 80 | /* The next delayed link in the list. */
|
|---|
| 81 | struct delayed_link *next;
|
|---|
| 82 |
|
|---|
| 83 | /* The device, inode number and last-modified time of the placeholder. */
|
|---|
| 84 | dev_t dev;
|
|---|
| 85 | ino_t ino;
|
|---|
| 86 | struct timespec mtime;
|
|---|
| 87 |
|
|---|
| 88 | /* True if the link is symbolic. */
|
|---|
| 89 | bool is_symlink;
|
|---|
| 90 |
|
|---|
| 91 | /* The desired owner and group of the link, if it is a symlink. */
|
|---|
| 92 | uid_t uid;
|
|---|
| 93 | gid_t gid;
|
|---|
| 94 |
|
|---|
| 95 | /* A list of sources for this link. The sources are all to be
|
|---|
| 96 | hard-linked together. */
|
|---|
| 97 | struct string_list *sources;
|
|---|
| 98 |
|
|---|
| 99 | /* The desired target of the desired link. */
|
|---|
| 100 | char target[1];
|
|---|
| 101 | };
|
|---|
| 102 |
|
|---|
| 103 | static struct delayed_link *delayed_link_head;
|
|---|
| 104 |
|
|---|
| 105 | struct string_list
|
|---|
| 106 | {
|
|---|
| 107 | struct string_list *next;
|
|---|
| 108 | char string[1];
|
|---|
| 109 | };
|
|---|
| 110 |
|
|---|
| 111 | /* Set up to extract files. */
|
|---|
| 112 | void
|
|---|
| 113 | extr_init (void)
|
|---|
| 114 | {
|
|---|
| 115 | we_are_root = geteuid () == 0;
|
|---|
| 116 | same_permissions_option += we_are_root;
|
|---|
| 117 | same_owner_option += we_are_root;
|
|---|
| 118 |
|
|---|
| 119 | /* Option -p clears the kernel umask, so it does not affect proper
|
|---|
| 120 | restoration of file permissions. New intermediate directories will
|
|---|
| 121 | comply with umask at start of program. */
|
|---|
| 122 |
|
|---|
| 123 | newdir_umask = umask (0);
|
|---|
| 124 | if (0 < same_permissions_option)
|
|---|
| 125 | current_umask = 0;
|
|---|
| 126 | else
|
|---|
| 127 | {
|
|---|
| 128 | umask (newdir_umask); /* restore the kernel umask */
|
|---|
| 129 | current_umask = newdir_umask;
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /* If restoring permissions, restore the mode for FILE_NAME from
|
|---|
| 134 | information given in *STAT_INFO (where *CUR_INFO gives
|
|---|
| 135 | the current status if CUR_INFO is nonzero); otherwise invert the
|
|---|
| 136 | INVERT_PERMISSIONS bits from the file's current permissions.
|
|---|
| 137 | PERMSTATUS specifies the status of the file's permissions.
|
|---|
| 138 | TYPEFLAG specifies the type of the file. */
|
|---|
| 139 | static void
|
|---|
| 140 | set_mode (char const *file_name,
|
|---|
| 141 | struct stat const *stat_info,
|
|---|
| 142 | struct stat const *cur_info,
|
|---|
| 143 | mode_t invert_permissions, enum permstatus permstatus,
|
|---|
| 144 | char typeflag)
|
|---|
| 145 | {
|
|---|
| 146 | mode_t mode;
|
|---|
| 147 |
|
|---|
| 148 | if (0 < same_permissions_option
|
|---|
| 149 | && permstatus != INTERDIR_PERMSTATUS)
|
|---|
| 150 | {
|
|---|
| 151 | mode = stat_info->st_mode;
|
|---|
| 152 |
|
|---|
| 153 | /* If we created the file and it has a mode that we set already
|
|---|
| 154 | with O_CREAT, then its mode is often set correctly already.
|
|---|
| 155 | But if we are changing ownership, the mode's group and and
|
|---|
| 156 | other permission bits were omitted originally, so it's less
|
|---|
| 157 | likely that the mode is OK now. Also, on many hosts, some
|
|---|
| 158 | directories inherit the setgid bits from their parents, so we
|
|---|
| 159 | we must set directories' modes explicitly. */
|
|---|
| 160 | if ((permstatus == ARCHIVED_PERMSTATUS
|
|---|
| 161 | && ! (mode & ~ (0 < same_owner_option ? S_IRWXU : MODE_RWX)))
|
|---|
| 162 | && typeflag != DIRTYPE
|
|---|
| 163 | && typeflag != GNUTYPE_DUMPDIR)
|
|---|
| 164 | return;
|
|---|
| 165 | }
|
|---|
| 166 | else if (! invert_permissions)
|
|---|
| 167 | return;
|
|---|
| 168 | else
|
|---|
| 169 | {
|
|---|
| 170 | /* We must inspect a directory's current permissions, since the
|
|---|
| 171 | directory may have inherited its setgid bit from its parent.
|
|---|
| 172 |
|
|---|
| 173 | INVERT_PERMISSIONS happens to be nonzero only for directories
|
|---|
| 174 | that we created, so there's no point optimizing this code for
|
|---|
| 175 | other cases. */
|
|---|
| 176 | struct stat st;
|
|---|
| 177 | if (! cur_info)
|
|---|
| 178 | {
|
|---|
| 179 | if (stat (file_name, &st) != 0)
|
|---|
| 180 | {
|
|---|
| 181 | stat_error (file_name);
|
|---|
| 182 | return;
|
|---|
| 183 | }
|
|---|
| 184 | cur_info = &st;
|
|---|
| 185 | }
|
|---|
| 186 | mode = cur_info->st_mode ^ invert_permissions;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | if (chmod (file_name, mode) != 0)
|
|---|
| 190 | chmod_error_details (file_name, mode);
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | /* Check time after successfully setting FILE_NAME's time stamp to T. */
|
|---|
| 194 | static void
|
|---|
| 195 | check_time (char const *file_name, struct timespec t)
|
|---|
| 196 | {
|
|---|
| 197 | if (t.tv_sec <= 0)
|
|---|
| 198 | WARN ((0, 0, _("%s: implausibly old time stamp %s"),
|
|---|
| 199 | file_name, tartime (t, true)));
|
|---|
| 200 | else if (timespec_cmp (volume_start_time, t) < 0)
|
|---|
| 201 | {
|
|---|
| 202 | struct timespec now;
|
|---|
| 203 | gettime (&now);
|
|---|
| 204 | if (timespec_cmp (now, t) < 0)
|
|---|
| 205 | {
|
|---|
| 206 | char buf[TIMESPEC_STRSIZE_BOUND];
|
|---|
| 207 | struct timespec diff;
|
|---|
| 208 | diff.tv_sec = t.tv_sec - now.tv_sec;
|
|---|
| 209 | diff.tv_nsec = t.tv_nsec - now.tv_nsec;
|
|---|
| 210 | if (diff.tv_nsec < 0)
|
|---|
| 211 | {
|
|---|
| 212 | diff.tv_nsec += BILLION;
|
|---|
| 213 | diff.tv_sec--;
|
|---|
| 214 | }
|
|---|
| 215 | WARN ((0, 0, _("%s: time stamp %s is %s s in the future"),
|
|---|
| 216 | file_name, tartime (t, true), code_timespec (diff, buf)));
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /* Restore stat attributes (owner, group, mode and times) for
|
|---|
| 222 | FILE_NAME, using information given in *ST.
|
|---|
| 223 | If CUR_INFO is nonzero, *CUR_INFO is the
|
|---|
| 224 | file's current status.
|
|---|
| 225 | If not restoring permissions, invert the
|
|---|
| 226 | INVERT_PERMISSIONS bits from the file's current permissions.
|
|---|
| 227 | PERMSTATUS specifies the status of the file's permissions.
|
|---|
| 228 | TYPEFLAG specifies the type of the file. */
|
|---|
| 229 |
|
|---|
| 230 | /* FIXME: About proper restoration of symbolic link attributes, we still do
|
|---|
| 231 | not have it right. Pretesters' reports tell us we need further study and
|
|---|
| 232 | probably more configuration. For now, just use lchown if it exists, and
|
|---|
| 233 | punt for the rest. Sigh! */
|
|---|
| 234 |
|
|---|
| 235 | static void
|
|---|
| 236 | set_stat (char const *file_name,
|
|---|
| 237 | struct tar_stat_info const *st,
|
|---|
| 238 | struct stat const *cur_info,
|
|---|
| 239 | mode_t invert_permissions, enum permstatus permstatus,
|
|---|
| 240 | char typeflag)
|
|---|
| 241 | {
|
|---|
| 242 | if (typeflag != SYMTYPE)
|
|---|
| 243 | {
|
|---|
| 244 | /* We do the utime before the chmod because some versions of utime are
|
|---|
| 245 | broken and trash the modes of the file. */
|
|---|
| 246 |
|
|---|
| 247 | if (! touch_option && permstatus != INTERDIR_PERMSTATUS)
|
|---|
| 248 | {
|
|---|
| 249 | /* We set the accessed time to `now', which is really the time we
|
|---|
| 250 | started extracting files, unless incremental_option is used, in
|
|---|
| 251 | which case .st_atime is used. */
|
|---|
| 252 |
|
|---|
| 253 | /* FIXME: incremental_option should set ctime too, but how? */
|
|---|
| 254 |
|
|---|
| 255 | struct timespec ts[2];
|
|---|
| 256 | if (incremental_option)
|
|---|
| 257 | ts[0] = st->atime;
|
|---|
| 258 | else
|
|---|
| 259 | ts[0] = start_time;
|
|---|
| 260 | ts[1] = st->mtime;
|
|---|
| 261 |
|
|---|
| 262 | if (utimens (file_name, ts) != 0)
|
|---|
| 263 | utime_error (file_name);
|
|---|
| 264 | else
|
|---|
| 265 | {
|
|---|
| 266 | check_time (file_name, ts[0]);
|
|---|
| 267 | check_time (file_name, ts[1]);
|
|---|
| 268 | }
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | /* Some systems allow non-root users to give files away. Once this
|
|---|
| 272 | done, it is not possible anymore to change file permissions.
|
|---|
| 273 | However, setting file permissions now would be incorrect, since
|
|---|
| 274 | they would apply to the wrong user, and there would be a race
|
|---|
| 275 | condition. So, don't use systems that allow non-root users to
|
|---|
| 276 | give files away. */
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | if (0 < same_owner_option && permstatus != INTERDIR_PERMSTATUS)
|
|---|
| 280 | {
|
|---|
| 281 | /* When lchown exists, it should be used to change the attributes of
|
|---|
| 282 | the symbolic link itself. In this case, a mere chown would change
|
|---|
| 283 | the attributes of the file the symbolic link is pointing to, and
|
|---|
| 284 | should be avoided. */
|
|---|
| 285 | int chown_result = 1;
|
|---|
| 286 |
|
|---|
| 287 | if (typeflag == SYMTYPE)
|
|---|
| 288 | {
|
|---|
| 289 | #if HAVE_LCHOWN
|
|---|
| 290 | chown_result = lchown (file_name, st->stat.st_uid, st->stat.st_gid);
|
|---|
| 291 | #endif
|
|---|
| 292 | }
|
|---|
| 293 | else
|
|---|
| 294 | {
|
|---|
| 295 | chown_result = chown (file_name, st->stat.st_uid, st->stat.st_gid);
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | if (chown_result == 0)
|
|---|
| 299 | {
|
|---|
| 300 | /* Changing the owner can flip st_mode bits in some cases, so
|
|---|
| 301 | ignore cur_info if it might be obsolete now. */
|
|---|
| 302 | if (cur_info
|
|---|
| 303 | && cur_info->st_mode & S_IXUGO
|
|---|
| 304 | && cur_info->st_mode & (S_ISUID | S_ISGID))
|
|---|
| 305 | cur_info = NULL;
|
|---|
| 306 | }
|
|---|
| 307 | else if (chown_result < 0)
|
|---|
| 308 | chown_error_details (file_name,
|
|---|
| 309 | st->stat.st_uid, st->stat.st_gid);
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | if (typeflag != SYMTYPE)
|
|---|
| 313 | set_mode (file_name, &st->stat, cur_info,
|
|---|
| 314 | invert_permissions, permstatus, typeflag);
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | /* Remember to restore stat attributes (owner, group, mode and times)
|
|---|
| 318 | for the directory FILE_NAME, using information given in *ST,
|
|---|
| 319 | once we stop extracting files into that directory.
|
|---|
| 320 | If not restoring permissions, remember to invert the
|
|---|
| 321 | INVERT_PERMISSIONS bits from the file's current permissions.
|
|---|
| 322 | PERMSTATUS specifies the status of the file's permissions.
|
|---|
| 323 |
|
|---|
| 324 | NOTICE: this works only if the archive has usual member order, i.e.
|
|---|
| 325 | directory, then the files in that directory. Incremental archive have
|
|---|
| 326 | somewhat reversed order: first go subdirectories, then all other
|
|---|
| 327 | members. To help cope with this case the variable
|
|---|
| 328 | delay_directory_restore_option is set by prepare_to_extract.
|
|---|
| 329 |
|
|---|
| 330 | If an archive was explicitely created so that its member order is
|
|---|
| 331 | reversed, some directory timestamps can be restored incorrectly,
|
|---|
| 332 | e.g.:
|
|---|
| 333 | tar --no-recursion -cf archive dir dir/file1 foo dir/file2
|
|---|
| 334 | */
|
|---|
| 335 | static void
|
|---|
| 336 | delay_set_stat (char const *file_name, struct tar_stat_info const *st,
|
|---|
| 337 | mode_t invert_permissions, enum permstatus permstatus)
|
|---|
| 338 | {
|
|---|
| 339 | size_t file_name_len = strlen (file_name);
|
|---|
| 340 | struct delayed_set_stat *data =
|
|---|
| 341 | xmalloc (offsetof (struct delayed_set_stat, file_name)
|
|---|
| 342 | + file_name_len + 1);
|
|---|
| 343 | data->next = delayed_set_stat_head;
|
|---|
| 344 | data->dev = st->stat.st_dev;
|
|---|
| 345 | data->ino = st->stat.st_ino;
|
|---|
| 346 | data->mode = st->stat.st_mode;
|
|---|
| 347 | data->uid = st->stat.st_uid;
|
|---|
| 348 | data->gid = st->stat.st_gid;
|
|---|
| 349 | data->atime = st->atime;
|
|---|
| 350 | data->mtime = st->mtime;
|
|---|
| 351 | data->file_name_len = file_name_len;
|
|---|
| 352 | data->invert_permissions = invert_permissions;
|
|---|
| 353 | data->permstatus = permstatus;
|
|---|
| 354 | data->after_links = 0;
|
|---|
| 355 | strcpy (data->file_name, file_name);
|
|---|
| 356 | delayed_set_stat_head = data;
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | /* Update the delayed_set_stat info for an intermediate directory
|
|---|
| 360 | created within the file name of DIR. The intermediate directory turned
|
|---|
| 361 | out to be the same as this directory, e.g. due to ".." or symbolic
|
|---|
| 362 | links. *DIR_STAT_INFO is the status of the directory. */
|
|---|
| 363 | static void
|
|---|
| 364 | repair_delayed_set_stat (char const *dir,
|
|---|
| 365 | struct stat const *dir_stat_info)
|
|---|
| 366 | {
|
|---|
| 367 | struct delayed_set_stat *data;
|
|---|
| 368 | for (data = delayed_set_stat_head; data; data = data->next)
|
|---|
| 369 | {
|
|---|
| 370 | struct stat st;
|
|---|
| 371 | if (stat (data->file_name, &st) != 0)
|
|---|
| 372 | {
|
|---|
| 373 | stat_error (data->file_name);
|
|---|
| 374 | return;
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | if (st.st_dev == dir_stat_info->st_dev
|
|---|
| 378 | && st.st_ino == dir_stat_info->st_ino)
|
|---|
| 379 | {
|
|---|
| 380 | data->dev = current_stat_info.stat.st_dev;
|
|---|
| 381 | data->ino = current_stat_info.stat.st_ino;
|
|---|
| 382 | data->mode = current_stat_info.stat.st_mode;
|
|---|
| 383 | data->uid = current_stat_info.stat.st_uid;
|
|---|
| 384 | data->gid = current_stat_info.stat.st_gid;
|
|---|
| 385 | data->atime = current_stat_info.atime;
|
|---|
| 386 | data->mtime = current_stat_info.mtime;
|
|---|
| 387 | data->invert_permissions =
|
|---|
| 388 | ((current_stat_info.stat.st_mode ^ st.st_mode)
|
|---|
| 389 | & MODE_RWX & ~ current_umask);
|
|---|
| 390 | data->permstatus = ARCHIVED_PERMSTATUS;
|
|---|
| 391 | return;
|
|---|
| 392 | }
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
|
|---|
| 396 | quotearg_colon (dir)));
|
|---|
| 397 | }
|
|---|
| 398 |
|
|---|
| 399 | /* After a file/link/directory creation has failed, see if
|
|---|
| 400 | it's because some required directory was not present, and if so,
|
|---|
| 401 | create all required directories. Return non-zero if a directory
|
|---|
| 402 | was created. */
|
|---|
| 403 | static int
|
|---|
| 404 | make_directories (char *file_name)
|
|---|
| 405 | {
|
|---|
| 406 | char *cursor0 = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
|
|---|
| 407 | char *cursor; /* points into the file name */
|
|---|
| 408 | int did_something = 0; /* did we do anything yet? */
|
|---|
| 409 | int mode;
|
|---|
| 410 | int invert_permissions;
|
|---|
| 411 | int status;
|
|---|
| 412 |
|
|---|
| 413 | for (cursor = cursor0; *cursor; cursor++)
|
|---|
| 414 | {
|
|---|
| 415 | if (! ISSLASH (*cursor))
|
|---|
| 416 | continue;
|
|---|
| 417 |
|
|---|
| 418 | /* Avoid mkdir of empty string, if leading or double '/'. */
|
|---|
| 419 |
|
|---|
| 420 | if (cursor == cursor0 || ISSLASH (cursor[-1]))
|
|---|
| 421 | continue;
|
|---|
| 422 |
|
|---|
| 423 | /* Avoid mkdir where last part of file name is "." or "..". */
|
|---|
| 424 |
|
|---|
| 425 | if (cursor[-1] == '.'
|
|---|
| 426 | && (cursor == cursor0 + 1 || ISSLASH (cursor[-2])
|
|---|
| 427 | || (cursor[-2] == '.'
|
|---|
| 428 | && (cursor == cursor0 + 2 || ISSLASH (cursor[-3])))))
|
|---|
| 429 | continue;
|
|---|
| 430 |
|
|---|
| 431 | *cursor = '\0'; /* truncate the name there */
|
|---|
| 432 | mode = MODE_RWX & ~ newdir_umask;
|
|---|
| 433 | invert_permissions = we_are_root ? 0 : MODE_WXUSR & ~ mode;
|
|---|
| 434 | status = mkdir (file_name, mode ^ invert_permissions);
|
|---|
| 435 |
|
|---|
| 436 | if (status == 0)
|
|---|
| 437 | {
|
|---|
| 438 | /* Create a struct delayed_set_stat even if
|
|---|
| 439 | invert_permissions is zero, because
|
|---|
| 440 | repair_delayed_set_stat may need to update the struct. */
|
|---|
| 441 | delay_set_stat (file_name,
|
|---|
| 442 | ¤t_stat_info,
|
|---|
| 443 | invert_permissions, INTERDIR_PERMSTATUS);
|
|---|
| 444 |
|
|---|
| 445 | print_for_mkdir (file_name, cursor - file_name, mode);
|
|---|
| 446 | did_something = 1;
|
|---|
| 447 |
|
|---|
| 448 | *cursor = '/';
|
|---|
| 449 | continue;
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 | *cursor = '/';
|
|---|
| 453 |
|
|---|
| 454 | if (errno == EEXIST)
|
|---|
| 455 | continue; /* Directory already exists. */
|
|---|
| 456 | else if ((errno == ENOSYS /* Automounted dirs on Solaris return
|
|---|
| 457 | this. Reported by Warren Hyde
|
|---|
| 458 | <Warren.Hyde@motorola.com> */
|
|---|
| 459 | || ERRNO_IS_EACCES) /* Turbo C mkdir gives a funny errno. */
|
|---|
| 460 | && access (file_name, W_OK) == 0)
|
|---|
| 461 | continue;
|
|---|
| 462 |
|
|---|
| 463 | /* Some other error in the mkdir. We return to the caller. */
|
|---|
| 464 | break;
|
|---|
| 465 | }
|
|---|
| 466 |
|
|---|
| 467 | return did_something; /* tell them to retry if we made one */
|
|---|
| 468 | }
|
|---|
| 469 |
|
|---|
| 470 | static bool
|
|---|
| 471 | file_newer_p (const char *file_name, struct tar_stat_info *tar_stat)
|
|---|
| 472 | {
|
|---|
| 473 | struct stat st;
|
|---|
| 474 |
|
|---|
| 475 | if (stat (file_name, &st))
|
|---|
| 476 | {
|
|---|
| 477 | stat_warn (file_name);
|
|---|
| 478 | /* Be on the safe side: if the file does exist assume it is newer */
|
|---|
| 479 | return errno != ENOENT;
|
|---|
| 480 | }
|
|---|
| 481 | if (!S_ISDIR (st.st_mode)
|
|---|
| 482 | && tar_timespec_cmp (tar_stat->mtime, get_stat_mtime (&st)) <= 0)
|
|---|
| 483 | {
|
|---|
| 484 | return true;
|
|---|
| 485 | }
|
|---|
| 486 | return false;
|
|---|
| 487 | }
|
|---|
| 488 |
|
|---|
| 489 | /* Attempt repairing what went wrong with the extraction. Delete an
|
|---|
| 490 | already existing file or create missing intermediate directories.
|
|---|
| 491 | Return nonzero if we somewhat increased our chances at a successful
|
|---|
| 492 | extraction. errno is properly restored on zero return. */
|
|---|
| 493 | static int
|
|---|
| 494 | maybe_recoverable (char *file_name, int *interdir_made)
|
|---|
| 495 | {
|
|---|
| 496 | int e = errno;
|
|---|
| 497 |
|
|---|
| 498 | if (*interdir_made)
|
|---|
| 499 | return 0;
|
|---|
| 500 |
|
|---|
| 501 | switch (errno)
|
|---|
| 502 | {
|
|---|
| 503 | case EEXIST:
|
|---|
| 504 | /* Remove an old file, if the options allow this. */
|
|---|
| 505 |
|
|---|
| 506 | switch (old_files_option)
|
|---|
| 507 | {
|
|---|
| 508 | case KEEP_OLD_FILES:
|
|---|
| 509 | return 0;
|
|---|
| 510 |
|
|---|
| 511 | case KEEP_NEWER_FILES:
|
|---|
| 512 | if (file_newer_p (file_name, ¤t_stat_info))
|
|---|
| 513 | {
|
|---|
| 514 | errno = e;
|
|---|
| 515 | return 0;
|
|---|
| 516 | }
|
|---|
| 517 | /* FALL THROUGH */
|
|---|
| 518 |
|
|---|
| 519 | case DEFAULT_OLD_FILES:
|
|---|
| 520 | case NO_OVERWRITE_DIR_OLD_FILES:
|
|---|
| 521 | case OVERWRITE_OLD_FILES:
|
|---|
| 522 | {
|
|---|
| 523 | int r = remove_any_file (file_name, ORDINARY_REMOVE_OPTION);
|
|---|
| 524 | errno = EEXIST;
|
|---|
| 525 | return r;
|
|---|
| 526 | }
|
|---|
| 527 |
|
|---|
| 528 | case UNLINK_FIRST_OLD_FILES:
|
|---|
| 529 | break;
|
|---|
| 530 | }
|
|---|
| 531 |
|
|---|
| 532 | case ENOENT:
|
|---|
| 533 | /* Attempt creating missing intermediate directories. */
|
|---|
| 534 | if (! make_directories (file_name))
|
|---|
| 535 | {
|
|---|
| 536 | errno = ENOENT;
|
|---|
| 537 | return 0;
|
|---|
| 538 | }
|
|---|
| 539 | *interdir_made = 1;
|
|---|
| 540 | return 1;
|
|---|
| 541 |
|
|---|
| 542 | default:
|
|---|
| 543 | /* Just say we can't do anything about it... */
|
|---|
| 544 |
|
|---|
| 545 | return 0;
|
|---|
| 546 | }
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| 549 | /* Fix the statuses of all directories whose statuses need fixing, and
|
|---|
| 550 | which are not ancestors of FILE_NAME. If AFTER_LINKS is
|
|---|
| 551 | nonzero, do this for all such directories; otherwise, stop at the
|
|---|
| 552 | first directory that is marked to be fixed up only after delayed
|
|---|
| 553 | links are applied. */
|
|---|
| 554 | static void
|
|---|
| 555 | apply_nonancestor_delayed_set_stat (char const *file_name, bool after_links)
|
|---|
| 556 | {
|
|---|
| 557 | size_t file_name_len = strlen (file_name);
|
|---|
| 558 | bool check_for_renamed_directories = 0;
|
|---|
| 559 |
|
|---|
| 560 | while (delayed_set_stat_head)
|
|---|
| 561 | {
|
|---|
| 562 | struct delayed_set_stat *data = delayed_set_stat_head;
|
|---|
| 563 | bool skip_this_one = 0;
|
|---|
| 564 | struct stat st;
|
|---|
| 565 | struct stat const *cur_info = 0;
|
|---|
| 566 |
|
|---|
| 567 | check_for_renamed_directories |= data->after_links;
|
|---|
| 568 |
|
|---|
| 569 | if (after_links < data->after_links
|
|---|
| 570 | || (data->file_name_len < file_name_len
|
|---|
| 571 | && file_name[data->file_name_len]
|
|---|
| 572 | && (ISSLASH (file_name[data->file_name_len])
|
|---|
| 573 | || ISSLASH (file_name[data->file_name_len - 1]))
|
|---|
| 574 | && memcmp (file_name, data->file_name, data->file_name_len) == 0))
|
|---|
| 575 | break;
|
|---|
| 576 |
|
|---|
| 577 | if (check_for_renamed_directories)
|
|---|
| 578 | {
|
|---|
| 579 | cur_info = &st;
|
|---|
| 580 | if (stat (data->file_name, &st) != 0)
|
|---|
| 581 | {
|
|---|
| 582 | stat_error (data->file_name);
|
|---|
| 583 | skip_this_one = 1;
|
|---|
| 584 | }
|
|---|
| 585 | else if (! (st.st_dev == data->dev && st.st_ino == data->ino))
|
|---|
| 586 | {
|
|---|
| 587 | ERROR ((0, 0,
|
|---|
| 588 | _("%s: Directory renamed before its status could be extracted"),
|
|---|
| 589 | quotearg_colon (data->file_name)));
|
|---|
| 590 | skip_this_one = 1;
|
|---|
| 591 | }
|
|---|
| 592 | }
|
|---|
| 593 |
|
|---|
| 594 | if (! skip_this_one)
|
|---|
| 595 | {
|
|---|
| 596 | struct tar_stat_info st;
|
|---|
| 597 | st.stat.st_mode = data->mode;
|
|---|
| 598 | st.stat.st_uid = data->uid;
|
|---|
| 599 | st.stat.st_gid = data->gid;
|
|---|
| 600 | st.atime = data->atime;
|
|---|
| 601 | st.mtime = data->mtime;
|
|---|
| 602 | set_stat (data->file_name, &st, cur_info,
|
|---|
| 603 | data->invert_permissions, data->permstatus, DIRTYPE);
|
|---|
| 604 | }
|
|---|
| 605 |
|
|---|
| 606 | delayed_set_stat_head = data->next;
|
|---|
| 607 | free (data);
|
|---|
| 608 | }
|
|---|
| 609 | }
|
|---|
| 610 |
|
|---|
| 611 | |
|---|
| 612 |
|
|---|
| 613 |
|
|---|
| 614 | /* Extractor functions for various member types */
|
|---|
| 615 |
|
|---|
| 616 | static int
|
|---|
| 617 | extract_dir (char *file_name, int typeflag)
|
|---|
| 618 | {
|
|---|
| 619 | int status;
|
|---|
| 620 | mode_t mode;
|
|---|
| 621 | int interdir_made = 0;
|
|---|
| 622 |
|
|---|
| 623 | /* Save 'root device' to avoid purging mount points. */
|
|---|
| 624 | if (one_file_system_option && root_device == 0)
|
|---|
| 625 | {
|
|---|
| 626 | struct stat st;
|
|---|
| 627 | char *dir = xgetcwd ();
|
|---|
| 628 |
|
|---|
| 629 | if (deref_stat (true, dir, &st))
|
|---|
| 630 | stat_diag (dir);
|
|---|
| 631 | else
|
|---|
| 632 | root_device = st.st_dev;
|
|---|
| 633 | free (dir);
|
|---|
| 634 | }
|
|---|
| 635 |
|
|---|
| 636 | if (incremental_option)
|
|---|
| 637 | /* Read the entry and delete files that aren't listed in the archive. */
|
|---|
| 638 | purge_directory (file_name);
|
|---|
| 639 | else if (typeflag == GNUTYPE_DUMPDIR)
|
|---|
| 640 | skip_member ();
|
|---|
| 641 |
|
|---|
| 642 | mode = current_stat_info.stat.st_mode | (we_are_root ? 0 : MODE_WXUSR);
|
|---|
| 643 | if (0 < same_owner_option || current_stat_info.stat.st_mode & ~ MODE_RWX)
|
|---|
| 644 | mode &= S_IRWXU;
|
|---|
| 645 |
|
|---|
| 646 | while ((status = mkdir (file_name, mode)))
|
|---|
| 647 | {
|
|---|
| 648 | if (errno == EEXIST
|
|---|
| 649 | && (interdir_made
|
|---|
| 650 | || old_files_option == DEFAULT_OLD_FILES
|
|---|
| 651 | || old_files_option == OVERWRITE_OLD_FILES))
|
|---|
| 652 | {
|
|---|
| 653 | struct stat st;
|
|---|
| 654 | if (stat (file_name, &st) == 0)
|
|---|
| 655 | {
|
|---|
| 656 | if (interdir_made)
|
|---|
| 657 | {
|
|---|
| 658 | repair_delayed_set_stat (file_name, &st);
|
|---|
| 659 | return 0;
|
|---|
| 660 | }
|
|---|
| 661 | if (S_ISDIR (st.st_mode))
|
|---|
| 662 | {
|
|---|
| 663 | mode = st.st_mode;
|
|---|
| 664 | break;
|
|---|
| 665 | }
|
|---|
| 666 | }
|
|---|
| 667 | errno = EEXIST;
|
|---|
| 668 | }
|
|---|
| 669 |
|
|---|
| 670 | if (maybe_recoverable (file_name, &interdir_made))
|
|---|
| 671 | continue;
|
|---|
| 672 |
|
|---|
| 673 | if (errno != EEXIST)
|
|---|
| 674 | {
|
|---|
| 675 | mkdir_error (file_name);
|
|---|
| 676 | return 1;
|
|---|
| 677 | }
|
|---|
| 678 | break;
|
|---|
| 679 | }
|
|---|
| 680 |
|
|---|
| 681 | if (status == 0
|
|---|
| 682 | || old_files_option == DEFAULT_OLD_FILES
|
|---|
| 683 | || old_files_option == OVERWRITE_OLD_FILES)
|
|---|
| 684 | {
|
|---|
| 685 | if (status == 0)
|
|---|
| 686 | delay_set_stat (file_name, ¤t_stat_info,
|
|---|
| 687 | ((mode ^ current_stat_info.stat.st_mode)
|
|---|
| 688 | & MODE_RWX & ~ current_umask),
|
|---|
| 689 | ARCHIVED_PERMSTATUS);
|
|---|
| 690 | else /* For an already existing directory, invert_perms must be 0 */
|
|---|
| 691 | delay_set_stat (file_name, ¤t_stat_info,
|
|---|
| 692 | 0,
|
|---|
| 693 | UNKNOWN_PERMSTATUS);
|
|---|
| 694 | }
|
|---|
| 695 | return status;
|
|---|
| 696 | }
|
|---|
| 697 |
|
|---|
| 698 |
|
|---|
| 699 | static int
|
|---|
| 700 | open_output_file (char *file_name, int typeflag, mode_t mode)
|
|---|
| 701 | {
|
|---|
| 702 | int fd;
|
|---|
| 703 | int openflag = (O_WRONLY | O_BINARY | O_CREAT
|
|---|
| 704 | | (old_files_option == OVERWRITE_OLD_FILES
|
|---|
| 705 | ? O_TRUNC
|
|---|
| 706 | : O_EXCL));
|
|---|
| 707 |
|
|---|
| 708 | #if O_CTG
|
|---|
| 709 | /* Contiguous files (on the Masscomp) have to specify the size in
|
|---|
| 710 | the open call that creates them. */
|
|---|
| 711 |
|
|---|
| 712 | if (typeflag == CONTTYPE)
|
|---|
| 713 | fd = open (file_name, openflag | O_CTG, mode, current_stat_info.stat.st_size);
|
|---|
| 714 | else
|
|---|
| 715 | fd = open (file_name, openflag, mode);
|
|---|
| 716 |
|
|---|
| 717 | #else /* not O_CTG */
|
|---|
| 718 | if (typeflag == CONTTYPE)
|
|---|
| 719 | {
|
|---|
| 720 | static int conttype_diagnosed;
|
|---|
| 721 |
|
|---|
| 722 | if (!conttype_diagnosed)
|
|---|
| 723 | {
|
|---|
| 724 | conttype_diagnosed = 1;
|
|---|
| 725 | WARN ((0, 0, _("Extracting contiguous files as regular files")));
|
|---|
| 726 | }
|
|---|
| 727 | }
|
|---|
| 728 | fd = open (file_name, openflag, mode);
|
|---|
| 729 |
|
|---|
| 730 | #endif /* not O_CTG */
|
|---|
| 731 |
|
|---|
| 732 | return fd;
|
|---|
| 733 | }
|
|---|
| 734 |
|
|---|
| 735 | static int
|
|---|
| 736 | extract_file (char *file_name, int typeflag)
|
|---|
| 737 | {
|
|---|
| 738 | int fd;
|
|---|
| 739 | off_t size;
|
|---|
| 740 | union block *data_block;
|
|---|
| 741 | int status;
|
|---|
| 742 | size_t count;
|
|---|
| 743 | size_t written;
|
|---|
| 744 | int interdir_made = 0;
|
|---|
| 745 | mode_t mode = current_stat_info.stat.st_mode & MODE_RWX & ~ current_umask;
|
|---|
| 746 | mode_t invert_permissions =
|
|---|
| 747 | 0 < same_owner_option ? mode & (S_IRWXG | S_IRWXO) : 0;
|
|---|
| 748 |
|
|---|
| 749 | /* FIXME: deal with protection issues. */
|
|---|
| 750 |
|
|---|
| 751 | if (to_stdout_option)
|
|---|
| 752 | fd = STDOUT_FILENO;
|
|---|
| 753 | else if (to_command_option)
|
|---|
| 754 | {
|
|---|
| 755 | fd = sys_exec_command (file_name, 'f', ¤t_stat_info);
|
|---|
| 756 | if (fd < 0)
|
|---|
| 757 | {
|
|---|
| 758 | skip_member ();
|
|---|
| 759 | return 0;
|
|---|
| 760 | }
|
|---|
| 761 | }
|
|---|
| 762 | else
|
|---|
| 763 | {
|
|---|
| 764 | do
|
|---|
| 765 | fd = open_output_file (file_name, typeflag, mode ^ invert_permissions);
|
|---|
| 766 | while (fd < 0 && maybe_recoverable (file_name, &interdir_made));
|
|---|
| 767 |
|
|---|
| 768 | if (fd < 0)
|
|---|
| 769 | {
|
|---|
| 770 | skip_member ();
|
|---|
| 771 | open_error (file_name);
|
|---|
| 772 | return 1;
|
|---|
| 773 | }
|
|---|
| 774 | }
|
|---|
| 775 |
|
|---|
| 776 | mv_begin (¤t_stat_info);
|
|---|
| 777 | if (current_stat_info.is_sparse)
|
|---|
| 778 | sparse_extract_file (fd, ¤t_stat_info, &size);
|
|---|
| 779 | else
|
|---|
| 780 | for (size = current_stat_info.stat.st_size; size > 0; )
|
|---|
| 781 | {
|
|---|
| 782 | mv_size_left (size);
|
|---|
| 783 |
|
|---|
| 784 | /* Locate data, determine max length writeable, write it,
|
|---|
| 785 | block that we have used the data, then check if the write
|
|---|
| 786 | worked. */
|
|---|
| 787 |
|
|---|
| 788 | data_block = find_next_block ();
|
|---|
| 789 | if (! data_block)
|
|---|
| 790 | {
|
|---|
| 791 | ERROR ((0, 0, _("Unexpected EOF in archive")));
|
|---|
| 792 | break; /* FIXME: What happens, then? */
|
|---|
| 793 | }
|
|---|
| 794 |
|
|---|
| 795 | written = available_space_after (data_block);
|
|---|
| 796 |
|
|---|
| 797 | if (written > size)
|
|---|
| 798 | written = size;
|
|---|
| 799 | errno = 0;
|
|---|
| 800 | count = full_write (fd, data_block->buffer, written);
|
|---|
| 801 | size -= written;
|
|---|
| 802 |
|
|---|
| 803 | set_next_block_after ((union block *)
|
|---|
| 804 | (data_block->buffer + written - 1));
|
|---|
| 805 | if (count != written)
|
|---|
| 806 | {
|
|---|
| 807 | if (!to_command_option)
|
|---|
| 808 | write_error_details (file_name, count, written);
|
|---|
| 809 | /* FIXME: shouldn't we restore from backup? */
|
|---|
| 810 | break;
|
|---|
| 811 | }
|
|---|
| 812 | }
|
|---|
| 813 |
|
|---|
| 814 | skip_file (size);
|
|---|
| 815 |
|
|---|
| 816 | mv_end ();
|
|---|
| 817 |
|
|---|
| 818 | /* If writing to stdout, don't try to do anything to the filename;
|
|---|
| 819 | it doesn't exist, or we don't want to touch it anyway. */
|
|---|
| 820 |
|
|---|
| 821 | if (to_stdout_option)
|
|---|
| 822 | return 0;
|
|---|
| 823 |
|
|---|
| 824 | status = close (fd);
|
|---|
| 825 | if (status < 0)
|
|---|
| 826 | close_error (file_name);
|
|---|
| 827 |
|
|---|
| 828 | if (to_command_option)
|
|---|
| 829 | sys_wait_command ();
|
|---|
| 830 | else
|
|---|
| 831 | set_stat (file_name, ¤t_stat_info, NULL, invert_permissions,
|
|---|
| 832 | (old_files_option == OVERWRITE_OLD_FILES ?
|
|---|
| 833 | UNKNOWN_PERMSTATUS : ARCHIVED_PERMSTATUS),
|
|---|
| 834 | typeflag);
|
|---|
| 835 |
|
|---|
| 836 | return status;
|
|---|
| 837 | }
|
|---|
| 838 |
|
|---|
| 839 | /* Create a placeholder file with name FILE_NAME, which will be
|
|---|
| 840 | replaced after other extraction is done by a symbolic link if
|
|---|
| 841 | IS_SYMLINK is true, and by a hard link otherwise. Set
|
|---|
| 842 | *INTERDIR_MADE if an intermediate directory is made in the
|
|---|
| 843 | process. */
|
|---|
| 844 |
|
|---|
| 845 | static int
|
|---|
| 846 | create_placeholder_file (char *file_name, bool is_symlink, int *interdir_made)
|
|---|
| 847 | {
|
|---|
| 848 | int fd;
|
|---|
| 849 | struct stat st;
|
|---|
| 850 |
|
|---|
| 851 | while ((fd = open (file_name, O_WRONLY | O_CREAT | O_EXCL, 0)) < 0)
|
|---|
| 852 | if (! maybe_recoverable (file_name, interdir_made))
|
|---|
| 853 | break;
|
|---|
| 854 |
|
|---|
| 855 | if (fd < 0)
|
|---|
| 856 | open_error (file_name);
|
|---|
| 857 | else if (fstat (fd, &st) != 0)
|
|---|
| 858 | {
|
|---|
| 859 | stat_error (file_name);
|
|---|
| 860 | close (fd);
|
|---|
| 861 | }
|
|---|
| 862 | else if (close (fd) != 0)
|
|---|
| 863 | close_error (file_name);
|
|---|
| 864 | else
|
|---|
| 865 | {
|
|---|
| 866 | struct delayed_set_stat *h;
|
|---|
| 867 | struct delayed_link *p =
|
|---|
| 868 | xmalloc (offsetof (struct delayed_link, target)
|
|---|
| 869 | + strlen (current_stat_info.link_name)
|
|---|
| 870 | + 1);
|
|---|
| 871 | p->next = delayed_link_head;
|
|---|
| 872 | delayed_link_head = p;
|
|---|
| 873 | p->dev = st.st_dev;
|
|---|
| 874 | p->ino = st.st_ino;
|
|---|
| 875 | p->mtime = get_stat_mtime (&st);
|
|---|
| 876 | p->is_symlink = is_symlink;
|
|---|
| 877 | if (is_symlink)
|
|---|
| 878 | {
|
|---|
| 879 | p->uid = current_stat_info.stat.st_uid;
|
|---|
| 880 | p->gid = current_stat_info.stat.st_gid;
|
|---|
| 881 | }
|
|---|
| 882 | p->sources = xmalloc (offsetof (struct string_list, string)
|
|---|
| 883 | + strlen (file_name) + 1);
|
|---|
| 884 | p->sources->next = 0;
|
|---|
| 885 | strcpy (p->sources->string, file_name);
|
|---|
| 886 | strcpy (p->target, current_stat_info.link_name);
|
|---|
| 887 |
|
|---|
| 888 | h = delayed_set_stat_head;
|
|---|
| 889 | if (h && ! h->after_links
|
|---|
| 890 | && strncmp (file_name, h->file_name, h->file_name_len) == 0
|
|---|
| 891 | && ISSLASH (file_name[h->file_name_len])
|
|---|
| 892 | && (last_component (file_name) == file_name + h->file_name_len + 1))
|
|---|
| 893 | {
|
|---|
| 894 | do
|
|---|
| 895 | {
|
|---|
| 896 | h->after_links = 1;
|
|---|
| 897 |
|
|---|
| 898 | if (stat (h->file_name, &st) != 0)
|
|---|
| 899 | stat_error (h->file_name);
|
|---|
| 900 | else
|
|---|
| 901 | {
|
|---|
| 902 | h->dev = st.st_dev;
|
|---|
| 903 | h->ino = st.st_ino;
|
|---|
| 904 | }
|
|---|
| 905 | }
|
|---|
| 906 | while ((h = h->next) && ! h->after_links);
|
|---|
| 907 | }
|
|---|
| 908 |
|
|---|
| 909 | return 0;
|
|---|
| 910 | }
|
|---|
| 911 |
|
|---|
| 912 | return -1;
|
|---|
| 913 | }
|
|---|
| 914 |
|
|---|
| 915 | static int
|
|---|
| 916 | extract_link (char *file_name, int typeflag)
|
|---|
| 917 | {
|
|---|
| 918 | char const *link_name = safer_name_suffix (current_stat_info.link_name,
|
|---|
| 919 | true, absolute_names_option);
|
|---|
| 920 | int interdir_made = 0;
|
|---|
| 921 |
|
|---|
| 922 | if (! absolute_names_option && contains_dot_dot (link_name))
|
|---|
| 923 | return create_placeholder_file (file_name, false, &interdir_made);
|
|---|
| 924 |
|
|---|
| 925 | do
|
|---|
| 926 | {
|
|---|
| 927 | struct stat st1, st2;
|
|---|
| 928 | int e;
|
|---|
| 929 | int status = link (link_name, file_name);
|
|---|
| 930 | e = errno;
|
|---|
| 931 |
|
|---|
| 932 | if (status == 0)
|
|---|
| 933 | {
|
|---|
| 934 | struct delayed_link *ds = delayed_link_head;
|
|---|
| 935 | if (ds && lstat (link_name, &st1) == 0)
|
|---|
| 936 | for (; ds; ds = ds->next)
|
|---|
| 937 | if (ds->dev == st1.st_dev
|
|---|
| 938 | && ds->ino == st1.st_ino
|
|---|
| 939 | && timespec_cmp (ds->mtime, get_stat_mtime (&st1)) == 0)
|
|---|
| 940 | {
|
|---|
| 941 | struct string_list *p = xmalloc (offsetof (struct string_list, string)
|
|---|
| 942 | + strlen (file_name) + 1);
|
|---|
| 943 | strcpy (p->string, file_name);
|
|---|
| 944 | p->next = ds->sources;
|
|---|
| 945 | ds->sources = p;
|
|---|
| 946 | break;
|
|---|
| 947 | }
|
|---|
| 948 | return 0;
|
|---|
| 949 | }
|
|---|
| 950 | else if ((e == EEXIST && strcmp (link_name, file_name) == 0)
|
|---|
| 951 | || (lstat (link_name, &st1) == 0
|
|---|
| 952 | && lstat (file_name, &st2) == 0
|
|---|
| 953 | && st1.st_dev == st2.st_dev
|
|---|
| 954 | && st1.st_ino == st2.st_ino))
|
|---|
| 955 | return 0;
|
|---|
| 956 |
|
|---|
| 957 | errno = e;
|
|---|
| 958 | }
|
|---|
| 959 | while (maybe_recoverable (file_name, &interdir_made));
|
|---|
| 960 |
|
|---|
| 961 | if (!(incremental_option && errno == EEXIST))
|
|---|
| 962 | {
|
|---|
| 963 | link_error (link_name, file_name);
|
|---|
| 964 | return 1;
|
|---|
| 965 | }
|
|---|
| 966 | return 0;
|
|---|
| 967 | }
|
|---|
| 968 |
|
|---|
| 969 | static int
|
|---|
| 970 | extract_symlink (char *file_name, int typeflag)
|
|---|
| 971 | {
|
|---|
| 972 | #ifdef HAVE_SYMLINK
|
|---|
| 973 | int status;
|
|---|
| 974 | int interdir_made = 0;
|
|---|
| 975 |
|
|---|
| 976 | if (! absolute_names_option
|
|---|
| 977 | && (IS_ABSOLUTE_FILE_NAME (current_stat_info.link_name)
|
|---|
| 978 | || contains_dot_dot (current_stat_info.link_name)))
|
|---|
| 979 | return create_placeholder_file (file_name, true, &interdir_made);
|
|---|
| 980 |
|
|---|
| 981 | while ((status = symlink (current_stat_info.link_name, file_name)))
|
|---|
| 982 | if (!maybe_recoverable (file_name, &interdir_made))
|
|---|
| 983 | break;
|
|---|
| 984 |
|
|---|
| 985 | if (status == 0)
|
|---|
| 986 | set_stat (file_name, ¤t_stat_info, NULL, 0, 0, SYMTYPE);
|
|---|
| 987 | else
|
|---|
| 988 | symlink_error (current_stat_info.link_name, file_name);
|
|---|
| 989 | return status;
|
|---|
| 990 |
|
|---|
| 991 | #else
|
|---|
| 992 | static int warned_once;
|
|---|
| 993 |
|
|---|
| 994 | if (!warned_once)
|
|---|
| 995 | {
|
|---|
| 996 | warned_once = 1;
|
|---|
| 997 | WARN ((0, 0, _("Attempting extraction of symbolic links as hard links")));
|
|---|
| 998 | }
|
|---|
| 999 | return extract_link (file_name, typeflag);
|
|---|
| 1000 | #endif
|
|---|
| 1001 | }
|
|---|
| 1002 |
|
|---|
| 1003 | #if S_IFCHR || S_IFBLK
|
|---|
| 1004 | static int
|
|---|
| 1005 | extract_node (char *file_name, int typeflag)
|
|---|
| 1006 | {
|
|---|
| 1007 | int status;
|
|---|
| 1008 | int interdir_made = 0;
|
|---|
| 1009 | mode_t mode = current_stat_info.stat.st_mode & ~ current_umask;
|
|---|
| 1010 | mode_t invert_permissions =
|
|---|
| 1011 | 0 < same_owner_option ? mode & (S_IRWXG | S_IRWXO) : 0;
|
|---|
| 1012 |
|
|---|
| 1013 | do
|
|---|
| 1014 | status = mknod (file_name, mode ^ invert_permissions,
|
|---|
| 1015 | current_stat_info.stat.st_rdev);
|
|---|
| 1016 | while (status && maybe_recoverable (file_name, &interdir_made));
|
|---|
| 1017 |
|
|---|
| 1018 | if (status != 0)
|
|---|
| 1019 | mknod_error (file_name);
|
|---|
| 1020 | else
|
|---|
| 1021 | set_stat (file_name, ¤t_stat_info, NULL, invert_permissions,
|
|---|
| 1022 | ARCHIVED_PERMSTATUS, typeflag);
|
|---|
| 1023 | return status;
|
|---|
| 1024 | }
|
|---|
| 1025 | #endif
|
|---|
| 1026 |
|
|---|
| 1027 | #if HAVE_MKFIFO || defined mkfifo
|
|---|
| 1028 | static int
|
|---|
| 1029 | extract_fifo (char *file_name, int typeflag)
|
|---|
| 1030 | {
|
|---|
| 1031 | int status;
|
|---|
| 1032 | int interdir_made = 0;
|
|---|
| 1033 | mode_t mode = current_stat_info.stat.st_mode & ~ current_umask;
|
|---|
| 1034 | mode_t invert_permissions =
|
|---|
| 1035 | 0 < same_owner_option ? mode & (S_IRWXG | S_IRWXO) : 0;
|
|---|
| 1036 |
|
|---|
| 1037 | while ((status = mkfifo (file_name, mode)) != 0)
|
|---|
| 1038 | if (!maybe_recoverable (file_name, &interdir_made))
|
|---|
| 1039 | break;
|
|---|
| 1040 |
|
|---|
| 1041 | if (status == 0)
|
|---|
| 1042 | set_stat (file_name, ¤t_stat_info, NULL, invert_permissions,
|
|---|
| 1043 | ARCHIVED_PERMSTATUS, typeflag);
|
|---|
| 1044 | else
|
|---|
| 1045 | mkfifo_error (file_name);
|
|---|
| 1046 | return status;
|
|---|
| 1047 | }
|
|---|
| 1048 | #endif
|
|---|
| 1049 |
|
|---|
| 1050 | static int
|
|---|
| 1051 | extract_volhdr (char *file_name, int typeflag)
|
|---|
| 1052 | {
|
|---|
| 1053 | if (verbose_option)
|
|---|
| 1054 | fprintf (stdlis, _("Reading %s\n"), quote (current_stat_info.file_name));
|
|---|
| 1055 | skip_member ();
|
|---|
| 1056 | return 0;
|
|---|
| 1057 | }
|
|---|
| 1058 |
|
|---|
| 1059 | static int
|
|---|
| 1060 | extract_failure (char *file_name, int typeflag)
|
|---|
| 1061 | {
|
|---|
| 1062 | return 1;
|
|---|
| 1063 | }
|
|---|
| 1064 |
|
|---|
| 1065 | typedef int (*tar_extractor_t) (char *file_name, int typeflag);
|
|---|
| 1066 |
|
|---|
| 1067 | |
|---|
| 1068 |
|
|---|
| 1069 |
|
|---|
| 1070 | /* Prepare to extract a file. Find extractor function.
|
|---|
| 1071 | Return zero if extraction should not proceed. */
|
|---|
| 1072 |
|
|---|
| 1073 | static int
|
|---|
| 1074 | prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun)
|
|---|
| 1075 | {
|
|---|
| 1076 | int rc = 1;
|
|---|
| 1077 |
|
|---|
| 1078 | if (EXTRACT_OVER_PIPE)
|
|---|
| 1079 | rc = 0;
|
|---|
| 1080 |
|
|---|
| 1081 | /* Select the extractor */
|
|---|
| 1082 | switch (typeflag)
|
|---|
| 1083 | {
|
|---|
| 1084 | case GNUTYPE_SPARSE:
|
|---|
| 1085 | *fun = extract_file;
|
|---|
| 1086 | rc = 1;
|
|---|
| 1087 | break;
|
|---|
| 1088 |
|
|---|
| 1089 | case AREGTYPE:
|
|---|
| 1090 | case REGTYPE:
|
|---|
| 1091 | case CONTTYPE:
|
|---|
| 1092 | /* Appears to be a file. But BSD tar uses the convention that a slash
|
|---|
| 1093 | suffix means a directory. */
|
|---|
| 1094 | if (current_stat_info.had_trailing_slash)
|
|---|
| 1095 | *fun = extract_dir;
|
|---|
| 1096 | else
|
|---|
| 1097 | {
|
|---|
| 1098 | *fun = extract_file;
|
|---|
| 1099 | rc = 1;
|
|---|
| 1100 | }
|
|---|
| 1101 | break;
|
|---|
| 1102 |
|
|---|
| 1103 | case SYMTYPE:
|
|---|
| 1104 | *fun = extract_symlink;
|
|---|
| 1105 | break;
|
|---|
| 1106 |
|
|---|
| 1107 | case LNKTYPE:
|
|---|
| 1108 | *fun = extract_link;
|
|---|
| 1109 | break;
|
|---|
| 1110 |
|
|---|
| 1111 | #if S_IFCHR
|
|---|
| 1112 | case CHRTYPE:
|
|---|
| 1113 | current_stat_info.stat.st_mode |= S_IFCHR;
|
|---|
| 1114 | *fun = extract_node;
|
|---|
| 1115 | break;
|
|---|
| 1116 | #endif
|
|---|
| 1117 |
|
|---|
| 1118 | #if S_IFBLK
|
|---|
| 1119 | case BLKTYPE:
|
|---|
| 1120 | current_stat_info.stat.st_mode |= S_IFBLK;
|
|---|
| 1121 | *fun = extract_node;
|
|---|
| 1122 | break;
|
|---|
| 1123 | #endif
|
|---|
| 1124 |
|
|---|
| 1125 | #if HAVE_MKFIFO || defined mkfifo
|
|---|
| 1126 | case FIFOTYPE:
|
|---|
| 1127 | *fun = extract_fifo;
|
|---|
| 1128 | break;
|
|---|
| 1129 | #endif
|
|---|
| 1130 |
|
|---|
| 1131 | case DIRTYPE:
|
|---|
| 1132 | case GNUTYPE_DUMPDIR:
|
|---|
| 1133 | *fun = extract_dir;
|
|---|
| 1134 | if (current_stat_info.is_dumpdir)
|
|---|
| 1135 | delay_directory_restore_option = true;
|
|---|
| 1136 | break;
|
|---|
| 1137 |
|
|---|
| 1138 | case GNUTYPE_VOLHDR:
|
|---|
| 1139 | *fun = extract_volhdr;
|
|---|
| 1140 | break;
|
|---|
| 1141 |
|
|---|
| 1142 | case GNUTYPE_MULTIVOL:
|
|---|
| 1143 | ERROR ((0, 0,
|
|---|
| 1144 | _("%s: Cannot extract -- file is continued from another volume"),
|
|---|
| 1145 | quotearg_colon (current_stat_info.file_name)));
|
|---|
| 1146 | *fun = extract_failure;
|
|---|
| 1147 | break;
|
|---|
| 1148 |
|
|---|
| 1149 | case GNUTYPE_LONGNAME:
|
|---|
| 1150 | case GNUTYPE_LONGLINK:
|
|---|
| 1151 | ERROR ((0, 0, _("Unexpected long name header")));
|
|---|
| 1152 | *fun = extract_failure;
|
|---|
| 1153 | break;
|
|---|
| 1154 |
|
|---|
| 1155 | default:
|
|---|
| 1156 | WARN ((0, 0,
|
|---|
| 1157 | _("%s: Unknown file type `%c', extracted as normal file"),
|
|---|
| 1158 | quotearg_colon (file_name), typeflag));
|
|---|
| 1159 | *fun = extract_file;
|
|---|
| 1160 | }
|
|---|
| 1161 |
|
|---|
| 1162 | /* Determine whether the extraction should proceed */
|
|---|
| 1163 | if (rc == 0)
|
|---|
| 1164 | return 0;
|
|---|
| 1165 |
|
|---|
| 1166 | switch (old_files_option)
|
|---|
| 1167 | {
|
|---|
| 1168 | case UNLINK_FIRST_OLD_FILES:
|
|---|
| 1169 | if (!remove_any_file (file_name,
|
|---|
| 1170 | recursive_unlink_option ? RECURSIVE_REMOVE_OPTION
|
|---|
| 1171 | : ORDINARY_REMOVE_OPTION)
|
|---|
| 1172 | && errno && errno != ENOENT)
|
|---|
| 1173 | {
|
|---|
| 1174 | unlink_error (file_name);
|
|---|
| 1175 | return 0;
|
|---|
| 1176 | }
|
|---|
| 1177 | break;
|
|---|
| 1178 |
|
|---|
| 1179 | case KEEP_NEWER_FILES:
|
|---|
| 1180 | if (file_newer_p (file_name, ¤t_stat_info))
|
|---|
| 1181 | {
|
|---|
| 1182 | WARN ((0, 0, _("Current %s is newer or same age"),
|
|---|
| 1183 | quote (file_name)));
|
|---|
| 1184 | return 0;
|
|---|
| 1185 | }
|
|---|
| 1186 | break;
|
|---|
| 1187 |
|
|---|
| 1188 | default:
|
|---|
| 1189 | break;
|
|---|
| 1190 | }
|
|---|
| 1191 |
|
|---|
| 1192 | return 1;
|
|---|
| 1193 | }
|
|---|
| 1194 |
|
|---|
| 1195 | /* Extract a file from the archive. */
|
|---|
| 1196 | void
|
|---|
| 1197 | extract_archive (void)
|
|---|
| 1198 | {
|
|---|
| 1199 | char typeflag;
|
|---|
| 1200 | tar_extractor_t fun;
|
|---|
| 1201 |
|
|---|
| 1202 | set_next_block_after (current_header);
|
|---|
| 1203 | decode_header (current_header, ¤t_stat_info, ¤t_format, 1);
|
|---|
| 1204 | if (!current_stat_info.file_name[0]
|
|---|
| 1205 | || (interactive_option
|
|---|
| 1206 | && !confirm ("extract", current_stat_info.file_name)))
|
|---|
| 1207 | {
|
|---|
| 1208 | skip_member ();
|
|---|
| 1209 | return;
|
|---|
| 1210 | }
|
|---|
| 1211 |
|
|---|
| 1212 | /* Print the block from current_header and current_stat. */
|
|---|
| 1213 | if (verbose_option)
|
|---|
| 1214 | print_header (¤t_stat_info, -1);
|
|---|
| 1215 |
|
|---|
| 1216 | /* Restore stats for all non-ancestor directories, unless
|
|---|
| 1217 | it is an incremental archive.
|
|---|
| 1218 | (see NOTICE in the comment to delay_set_stat above) */
|
|---|
| 1219 | if (!delay_directory_restore_option)
|
|---|
| 1220 | apply_nonancestor_delayed_set_stat (current_stat_info.file_name, 0);
|
|---|
| 1221 |
|
|---|
| 1222 | /* Take a safety backup of a previously existing file. */
|
|---|
| 1223 |
|
|---|
| 1224 | if (backup_option)
|
|---|
| 1225 | if (!maybe_backup_file (current_stat_info.file_name, 0))
|
|---|
| 1226 | {
|
|---|
| 1227 | int e = errno;
|
|---|
| 1228 | ERROR ((0, e, _("%s: Was unable to backup this file"),
|
|---|
| 1229 | quotearg_colon (current_stat_info.file_name)));
|
|---|
| 1230 | skip_member ();
|
|---|
| 1231 | return;
|
|---|
| 1232 | }
|
|---|
| 1233 |
|
|---|
| 1234 | /* Extract the archive entry according to its type. */
|
|---|
| 1235 | /* KLUDGE */
|
|---|
| 1236 | typeflag = sparse_member_p (¤t_stat_info) ?
|
|---|
| 1237 | GNUTYPE_SPARSE : current_header->header.typeflag;
|
|---|
| 1238 |
|
|---|
| 1239 | if (prepare_to_extract (current_stat_info.file_name, typeflag, &fun))
|
|---|
| 1240 | {
|
|---|
| 1241 | if (fun && (*fun) (current_stat_info.file_name, typeflag)
|
|---|
| 1242 | && backup_option)
|
|---|
| 1243 | undo_last_backup ();
|
|---|
| 1244 | }
|
|---|
| 1245 | else
|
|---|
| 1246 | skip_member ();
|
|---|
| 1247 |
|
|---|
| 1248 | }
|
|---|
| 1249 |
|
|---|
| 1250 | /* Extract the symbolic links whose final extraction were delayed. */
|
|---|
| 1251 | static void
|
|---|
| 1252 | apply_delayed_links (void)
|
|---|
| 1253 | {
|
|---|
| 1254 | struct delayed_link *ds;
|
|---|
| 1255 |
|
|---|
| 1256 | for (ds = delayed_link_head; ds; )
|
|---|
| 1257 | {
|
|---|
| 1258 | struct string_list *sources = ds->sources;
|
|---|
| 1259 | char const *valid_source = 0;
|
|---|
| 1260 |
|
|---|
| 1261 | for (sources = ds->sources; sources; sources = sources->next)
|
|---|
| 1262 | {
|
|---|
| 1263 | char const *source = sources->string;
|
|---|
| 1264 | struct stat st;
|
|---|
| 1265 |
|
|---|
| 1266 | /* Make sure the placeholder file is still there. If not,
|
|---|
| 1267 | don't create a link, as the placeholder was probably
|
|---|
| 1268 | removed by a later extraction. */
|
|---|
| 1269 | if (lstat (source, &st) == 0
|
|---|
| 1270 | && st.st_dev == ds->dev
|
|---|
| 1271 | && st.st_ino == ds->ino
|
|---|
| 1272 | && timespec_cmp (get_stat_mtime (&st), ds->mtime) == 0)
|
|---|
| 1273 | {
|
|---|
| 1274 | /* Unlink the placeholder, then create a hard link if possible,
|
|---|
| 1275 | a symbolic link otherwise. */
|
|---|
| 1276 | if (unlink (source) != 0)
|
|---|
| 1277 | unlink_error (source);
|
|---|
| 1278 | else if (valid_source && link (valid_source, source) == 0)
|
|---|
| 1279 | ;
|
|---|
| 1280 | else if (!ds->is_symlink)
|
|---|
| 1281 | {
|
|---|
| 1282 | if (link (ds->target, source) != 0)
|
|---|
| 1283 | link_error (ds->target, source);
|
|---|
| 1284 | }
|
|---|
| 1285 | else if (symlink (ds->target, source) != 0)
|
|---|
| 1286 | symlink_error (ds->target, source);
|
|---|
| 1287 | else
|
|---|
| 1288 | {
|
|---|
| 1289 | struct tar_stat_info st1;
|
|---|
| 1290 | st1.stat.st_uid = ds->uid;
|
|---|
| 1291 | st1.stat.st_gid = ds->gid;
|
|---|
| 1292 | set_stat (source, &st1, NULL, 0, 0, SYMTYPE);
|
|---|
| 1293 | valid_source = source;
|
|---|
| 1294 | }
|
|---|
| 1295 | }
|
|---|
| 1296 | }
|
|---|
| 1297 |
|
|---|
| 1298 | for (sources = ds->sources; sources; )
|
|---|
| 1299 | {
|
|---|
| 1300 | struct string_list *next = sources->next;
|
|---|
| 1301 | free (sources);
|
|---|
| 1302 | sources = next;
|
|---|
| 1303 | }
|
|---|
| 1304 |
|
|---|
| 1305 | {
|
|---|
| 1306 | struct delayed_link *next = ds->next;
|
|---|
| 1307 | free (ds);
|
|---|
| 1308 | ds = next;
|
|---|
| 1309 | }
|
|---|
| 1310 | }
|
|---|
| 1311 |
|
|---|
| 1312 | delayed_link_head = 0;
|
|---|
| 1313 | }
|
|---|
| 1314 |
|
|---|
| 1315 | /* Finish the extraction of an archive. */
|
|---|
| 1316 | void
|
|---|
| 1317 | extract_finish (void)
|
|---|
| 1318 | {
|
|---|
| 1319 | /* First, fix the status of ordinary directories that need fixing. */
|
|---|
| 1320 | apply_nonancestor_delayed_set_stat ("", 0);
|
|---|
| 1321 |
|
|---|
| 1322 | /* Then, apply delayed links, so that they don't affect delayed
|
|---|
| 1323 | directory status-setting for ordinary directories. */
|
|---|
| 1324 | apply_delayed_links ();
|
|---|
| 1325 |
|
|---|
| 1326 | /* Finally, fix the status of directories that are ancestors
|
|---|
| 1327 | of delayed links. */
|
|---|
| 1328 | apply_nonancestor_delayed_set_stat ("", 1);
|
|---|
| 1329 | }
|
|---|
| 1330 |
|
|---|
| 1331 | bool
|
|---|
| 1332 | rename_directory (char *src, char *dst)
|
|---|
| 1333 | {
|
|---|
| 1334 | if (rename (src, dst))
|
|---|
| 1335 | {
|
|---|
| 1336 | int e = errno;
|
|---|
| 1337 |
|
|---|
| 1338 | switch (e)
|
|---|
| 1339 | {
|
|---|
| 1340 | case ENOENT:
|
|---|
| 1341 | if (make_directories (dst))
|
|---|
| 1342 | {
|
|---|
| 1343 | if (rename (src, dst) == 0)
|
|---|
| 1344 | return true;
|
|---|
| 1345 | e = errno;
|
|---|
| 1346 | }
|
|---|
| 1347 | break;
|
|---|
| 1348 |
|
|---|
| 1349 | case EXDEV:
|
|---|
| 1350 | /* FIXME: Fall back to recursive copying */
|
|---|
| 1351 |
|
|---|
| 1352 | default:
|
|---|
| 1353 | break;
|
|---|
| 1354 | }
|
|---|
| 1355 |
|
|---|
| 1356 | ERROR ((0, e, _("Cannot rename %s to %s"),
|
|---|
| 1357 | quote_n (0, src),
|
|---|
| 1358 | quote_n (1, dst)));
|
|---|
| 1359 | return false;
|
|---|
| 1360 | }
|
|---|
| 1361 | return true;
|
|---|
| 1362 | }
|
|---|
| 1363 |
|
|---|
| 1364 | void
|
|---|
| 1365 | fatal_exit (void)
|
|---|
| 1366 | {
|
|---|
| 1367 | extract_finish ();
|
|---|
| 1368 | error (TAREXIT_FAILURE, 0, _("Error is not recoverable: exiting now"));
|
|---|
| 1369 | abort ();
|
|---|
| 1370 | }
|
|---|
| 1371 |
|
|---|
| 1372 | void
|
|---|
| 1373 | xalloc_die (void)
|
|---|
| 1374 | {
|
|---|
| 1375 | error (0, 0, "%s", _("memory exhausted"));
|
|---|
| 1376 | fatal_exit ();
|
|---|
| 1377 | }
|
|---|