| 1 |
/* Extract files from a tar archive. |
| 2 |
|
| 3 |
Copyright 1988-2023 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is part of GNU tar. |
| 6 |
|
| 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. |
| 11 |
|
| 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. |
| 16 |
|
| 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/>. |
| 19 |
|
| 20 |
Written by John Gilmore, on 1985-11-19. */ |
| 21 |
|
| 22 |
#include <system.h> |
| 23 |
#include <quotearg.h> |
| 24 |
#include <errno.h> |
| 25 |
#include <flexmember.h> |
| 26 |
#include <hash.h> |
| 27 |
#include <priv-set.h> |
| 28 |
#include <root-uid.h> |
| 29 |
#include <utimens.h> |
| 30 |
|
| 31 |
#include "common.h" |
| 32 |
|
| 33 |
static bool we_are_root; /* true if our effective uid == 0 */ |
| 34 |
static mode_t newdir_umask; /* umask when creating new directories */ |
| 35 |
static mode_t current_umask; /* current umask (which is set to 0 if -p) */ |
| 36 |
|
| 37 |
#define ALL_MODE_BITS ((mode_t) ~ (mode_t) 0) |
| 38 |
|
| 39 |
#if ! HAVE_FCHMOD && ! defined fchmod |
| 40 |
# define fchmod(fd, mode) (errno = ENOSYS, -1) |
| 41 |
#endif |
| 42 |
#if ! HAVE_FCHOWN && ! defined fchown |
| 43 |
# define fchown(fd, uid, gid) (errno = ENOSYS, -1) |
| 44 |
#endif |
| 45 |
|
| 46 |
#if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \ |
| 47 |
|| defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \ |
| 48 |
|| defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC \ |
| 49 |
|| (defined _WIN32 && ! defined __CYGWIN__)) |
| 50 |
# define HAVE_BIRTHTIME 1 |
| 51 |
#else |
| 52 |
# define HAVE_BIRTHTIME 0 |
| 53 |
#endif |
| 54 |
|
| 55 |
#if HAVE_BIRTHTIME |
| 56 |
# define BIRTHTIME_EQ(a, b) (timespec_cmp (a, b) == 0) |
| 57 |
#else |
| 58 |
# define BIRTHTIME_EQ(a, b) true |
| 59 |
#endif |
| 60 |
|
| 61 |
/* Return true if an error number ERR means the system call is |
| 62 |
supported in this case. */ |
| 63 |
static bool |
| 64 |
implemented (int err) |
| 65 |
{ |
| 66 |
return ! (err == ENOSYS |
| 67 |
|| err == ENOTSUP |
| 68 |
|| (EOPNOTSUPP != ENOTSUP && err == EOPNOTSUPP)); |
| 69 |
} |
| 70 |
|
| 71 |
/* List of directories whose statuses we need to extract after we've |
| 72 |
finished extracting their subsidiary files. If you consider each |
| 73 |
contiguous subsequence of elements of the form [D]?[^D]*, where [D] |
| 74 |
represents an element where AFTER_LINKS is nonzero and [^D] |
| 75 |
represents an element where AFTER_LINKS is zero, then the head |
| 76 |
of the subsequence has the longest name, and each non-head element |
| 77 |
in the prefix is an ancestor (in the directory hierarchy) of the |
| 78 |
preceding element. */ |
| 79 |
|
| 80 |
struct delayed_set_stat |
| 81 |
{ |
| 82 |
/* Next directory in list. */ |
| 83 |
struct delayed_set_stat *next; |
| 84 |
|
| 85 |
/* Metadata for this directory. */ |
| 86 |
dev_t dev; |
| 87 |
ino_t ino; |
| 88 |
mode_t mode; /* The desired mode is MODE & ~ current_umask. */ |
| 89 |
uid_t uid; |
| 90 |
gid_t gid; |
| 91 |
struct timespec atime; |
| 92 |
struct timespec mtime; |
| 93 |
|
| 94 |
/* An estimate of the directory's current mode, along with a mask |
| 95 |
specifying which bits of this estimate are known to be correct. |
| 96 |
If CURRENT_MODE_MASK is zero, CURRENT_MODE's value doesn't |
| 97 |
matter. */ |
| 98 |
mode_t current_mode; |
| 99 |
mode_t current_mode_mask; |
| 100 |
|
| 101 |
/* This directory is an intermediate directory that was created |
| 102 |
as an ancestor of some other directory; it was not mentioned |
| 103 |
in the archive, so do not set its uid, gid, atime, or mtime, |
| 104 |
and don't alter its mode outside of MODE_RWX. */ |
| 105 |
bool interdir; |
| 106 |
|
| 107 |
/* Whether symbolic links should be followed when accessing the |
| 108 |
directory. */ |
| 109 |
int atflag; |
| 110 |
|
| 111 |
/* Do not set the status of this directory until after delayed |
| 112 |
links are created. */ |
| 113 |
bool after_links; |
| 114 |
|
| 115 |
/* Directory that the name is relative to. */ |
| 116 |
int change_dir; |
| 117 |
|
| 118 |
/* extended attributes*/ |
| 119 |
char *cntx_name; |
| 120 |
char *acls_a_ptr; |
| 121 |
size_t acls_a_len; |
| 122 |
char *acls_d_ptr; |
| 123 |
size_t acls_d_len; |
| 124 |
size_t xattr_map_size; |
| 125 |
struct xattr_map xattr_map; |
| 126 |
/* Length and contents of name. */ |
| 127 |
size_t file_name_len; |
| 128 |
char *file_name; |
| 129 |
}; |
| 130 |
|
| 131 |
static struct delayed_set_stat *delayed_set_stat_head; |
| 132 |
|
| 133 |
/* A link whose creation we have delayed. */ |
| 134 |
struct delayed_link |
| 135 |
{ |
| 136 |
/* The next in a list of delayed links that should be made after |
| 137 |
this delayed link. */ |
| 138 |
struct delayed_link *next; |
| 139 |
|
| 140 |
/* The device, inode number and birthtime of the placeholder. |
| 141 |
birthtime.tv_nsec is negative if the birthtime is not available. |
| 142 |
Don't use mtime as this would allow for false matches if some |
| 143 |
other process removes the placeholder. Don't use ctime as |
| 144 |
this would cause race conditions and other screwups, e.g., |
| 145 |
when restoring hard-linked symlinks. */ |
| 146 |
dev_t dev; |
| 147 |
ino_t ino; |
| 148 |
#if HAVE_BIRTHTIME |
| 149 |
struct timespec birthtime; |
| 150 |
#endif |
| 151 |
|
| 152 |
/* True if the link is symbolic. */ |
| 153 |
bool is_symlink; |
| 154 |
|
| 155 |
/* The desired metadata, valid only the link is symbolic. */ |
| 156 |
mode_t mode; |
| 157 |
uid_t uid; |
| 158 |
gid_t gid; |
| 159 |
struct timespec atime; |
| 160 |
struct timespec mtime; |
| 161 |
|
| 162 |
/* The directory that the sources and target are relative to. */ |
| 163 |
int change_dir; |
| 164 |
|
| 165 |
/* A list of sources for this link. The sources are all to be |
| 166 |
hard-linked together. */ |
| 167 |
struct string_list *sources; |
| 168 |
|
| 169 |
/* SELinux context */ |
| 170 |
char *cntx_name; |
| 171 |
|
| 172 |
/* ACLs */ |
| 173 |
char *acls_a_ptr; |
| 174 |
size_t acls_a_len; |
| 175 |
char *acls_d_ptr; |
| 176 |
size_t acls_d_len; |
| 177 |
|
| 178 |
struct xattr_map xattr_map; |
| 179 |
|
| 180 |
/* The desired target of the desired link. */ |
| 181 |
char target[FLEXIBLE_ARRAY_MEMBER]; |
| 182 |
}; |
| 183 |
|
| 184 |
/* Table of delayed links hashed by device and inode; null if none. */ |
| 185 |
static Hash_table *delayed_link_table; |
| 186 |
|
| 187 |
/* A list of the delayed links in tar file order, |
| 188 |
and the tail of that list. */ |
| 189 |
static struct delayed_link *delayed_link_head; |
| 190 |
static struct delayed_link **delayed_link_tail = &delayed_link_head; |
| 191 |
|
| 192 |
struct string_list |
| 193 |
{ |
| 194 |
struct string_list *next; |
| 195 |
char string[FLEXIBLE_ARRAY_MEMBER]; |
| 196 |
}; |
| 197 |
|
| 198 |
static size_t |
| 199 |
dl_hash (void const *entry, size_t table_size) |
| 200 |
{ |
| 201 |
struct delayed_link const *dl = entry; |
| 202 |
uintmax_t n = dl->dev; |
| 203 |
int nshift = TYPE_WIDTH (n) - TYPE_WIDTH (dl->dev); |
| 204 |
if (0 < nshift) |
| 205 |
n <<= nshift; |
| 206 |
n ^= dl->ino; |
| 207 |
return n % table_size; |
| 208 |
} |
| 209 |
|
| 210 |
static bool |
| 211 |
dl_compare (void const *a, void const *b) |
| 212 |
{ |
| 213 |
struct delayed_link const *da = a, *db = b; |
| 214 |
return (da->dev == db->dev) & (da->ino == db->ino); |
| 215 |
} |
| 216 |
|
| 217 |
/* Set up to extract files. */ |
| 218 |
void |
| 219 |
extr_init (void) |
| 220 |
{ |
| 221 |
we_are_root = geteuid () == ROOT_UID; |
| 222 |
same_permissions_option += we_are_root; |
| 223 |
same_owner_option += we_are_root; |
| 224 |
|
| 225 |
/* Option -p clears the kernel umask, so it does not affect proper |
| 226 |
restoration of file permissions. New intermediate directories will |
| 227 |
comply with umask at start of program. */ |
| 228 |
|
| 229 |
newdir_umask = umask (0); |
| 230 |
if (0 < same_permissions_option) |
| 231 |
current_umask = 0; |
| 232 |
else |
| 233 |
{ |
| 234 |
umask (newdir_umask); /* restore the kernel umask */ |
| 235 |
current_umask = newdir_umask; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/* Use fchmod if possible, fchmodat otherwise. */ |
| 240 |
static int |
| 241 |
fd_i_chmod (int fd, char const *file, mode_t mode, int atflag) |
| 242 |
{ |
| 243 |
if (0 <= fd) |
| 244 |
{ |
| 245 |
int result = fchmod (fd, mode); |
| 246 |
if (result == 0 || implemented (errno)) |
| 247 |
return result; |
| 248 |
} |
| 249 |
return fchmodat (chdir_fd, file, mode, atflag); |
| 250 |
} |
| 251 |
|
| 252 |
/* A version of fd_i_chmod which gracefully handles several common error |
| 253 |
conditions. Additional argument TYPEFLAG is the type of file in tar |
| 254 |
notation. |
| 255 |
*/ |
| 256 |
static int |
| 257 |
fd_chmod(int fd, char const *file_name, int mode, int atflag, int typeflag) |
| 258 |
{ |
| 259 |
int chmod_errno = fd_i_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno; |
| 260 |
|
| 261 |
/* On Solaris, chmod may fail if we don't have PRIV_ALL, because |
| 262 |
setuid-root files would otherwise be a backdoor. See |
| 263 |
http://opensolaris.org/jive/thread.jspa?threadID=95826 |
| 264 |
(2009-09-03). */ |
| 265 |
if (chmod_errno == EPERM && (mode & S_ISUID) |
| 266 |
&& priv_set_restore_linkdir () == 0) |
| 267 |
{ |
| 268 |
chmod_errno = fd_i_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno; |
| 269 |
priv_set_remove_linkdir (); |
| 270 |
} |
| 271 |
|
| 272 |
/* Linux fchmodat does not support AT_SYMLINK_NOFOLLOW, and |
| 273 |
returns ENOTSUP even when operating on non-symlinks, try |
| 274 |
again with the flag disabled if it does not appear to be |
| 275 |
supported and if the file is not a symlink. This |
| 276 |
introduces a race, alas. */ |
| 277 |
if (atflag && typeflag != SYMTYPE && ! implemented (chmod_errno)) |
| 278 |
chmod_errno = fd_i_chmod (fd, file_name, mode, 0) == 0 ? 0 : errno; |
| 279 |
|
| 280 |
if (chmod_errno && (typeflag != SYMTYPE || implemented (chmod_errno))) |
| 281 |
{ |
| 282 |
errno = chmod_errno; |
| 283 |
return -1; |
| 284 |
} |
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
/* Use fchown if possible, fchownat otherwise. */ |
| 289 |
static int |
| 290 |
fd_chown (int fd, char const *file, uid_t uid, gid_t gid, int atflag) |
| 291 |
{ |
| 292 |
if (0 <= fd) |
| 293 |
{ |
| 294 |
int result = fchown (fd, uid, gid); |
| 295 |
if (result == 0 || implemented (errno)) |
| 296 |
return result; |
| 297 |
} |
| 298 |
return fchownat (chdir_fd, file, uid, gid, atflag); |
| 299 |
} |
| 300 |
|
| 301 |
/* Use fstat if possible, fstatat otherwise. */ |
| 302 |
static int |
| 303 |
fd_stat (int fd, char const *file, struct stat *st, int atflag) |
| 304 |
{ |
| 305 |
return (0 <= fd |
| 306 |
? fstat (fd, st) |
| 307 |
: fstatat (chdir_fd, file, st, atflag)); |
| 308 |
} |
| 309 |
|
| 310 |
/* Set the mode for FILE_NAME to MODE. |
| 311 |
MODE_MASK specifies the bits of MODE that we care about; |
| 312 |
thus if MODE_MASK is zero, do nothing. |
| 313 |
If FD is nonnegative, it is a file descriptor for the file. |
| 314 |
CURRENT_MODE and CURRENT_MODE_MASK specify information known about |
| 315 |
the file's current mode, using the style of struct delayed_set_stat. |
| 316 |
TYPEFLAG specifies the type of the file. |
| 317 |
ATFLAG specifies the flag to use when statting the file. */ |
| 318 |
static void |
| 319 |
set_mode (char const *file_name, |
| 320 |
mode_t mode, mode_t mode_mask, int fd, |
| 321 |
mode_t current_mode, mode_t current_mode_mask, |
| 322 |
char typeflag, int atflag) |
| 323 |
{ |
| 324 |
if (((current_mode ^ mode) | ~ current_mode_mask) & mode_mask) |
| 325 |
{ |
| 326 |
if (MODE_ALL & ~ mode_mask & ~ current_mode_mask) |
| 327 |
{ |
| 328 |
struct stat st; |
| 329 |
if (fd_stat (fd, file_name, &st, atflag) != 0) |
| 330 |
{ |
| 331 |
stat_error (file_name); |
| 332 |
return; |
| 333 |
} |
| 334 |
current_mode = st.st_mode; |
| 335 |
} |
| 336 |
|
| 337 |
current_mode &= MODE_ALL; |
| 338 |
mode = (current_mode & ~ mode_mask) | (mode & mode_mask); |
| 339 |
|
| 340 |
if (current_mode != mode) |
| 341 |
{ |
| 342 |
if (fd_chmod (fd, file_name, mode, atflag, typeflag)) |
| 343 |
chmod_error_details (file_name, mode); |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
/* Check time after successfully setting FILE_NAME's time stamp to T. */ |
| 349 |
static void |
| 350 |
check_time (char const *file_name, struct timespec t) |
| 351 |
{ |
| 352 |
if (t.tv_sec < 0) |
| 353 |
WARNOPT (WARN_TIMESTAMP, |
| 354 |
(0, 0, _("%s: implausibly old time stamp %s"), |
| 355 |
file_name, tartime (t, true))); |
| 356 |
else if (timespec_cmp (volume_start_time, t) < 0) |
| 357 |
{ |
| 358 |
struct timespec now; |
| 359 |
gettime (&now); |
| 360 |
if (timespec_cmp (now, t) < 0) |
| 361 |
{ |
| 362 |
char buf[TIMESPEC_STRSIZE_BOUND]; |
| 363 |
struct timespec diff; |
| 364 |
diff.tv_sec = t.tv_sec - now.tv_sec; |
| 365 |
diff.tv_nsec = t.tv_nsec - now.tv_nsec; |
| 366 |
if (diff.tv_nsec < 0) |
| 367 |
{ |
| 368 |
diff.tv_nsec += BILLION; |
| 369 |
diff.tv_sec--; |
| 370 |
} |
| 371 |
WARNOPT (WARN_TIMESTAMP, |
| 372 |
(0, 0, _("%s: time stamp %s is %s s in the future"), |
| 373 |
file_name, tartime (t, true), code_timespec (diff, buf))); |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
/* Restore stat attributes (owner, group, mode and times) for |
| 379 |
FILE_NAME, using information given in *ST. |
| 380 |
If FD is nonnegative, it is a file descriptor for the file. |
| 381 |
CURRENT_MODE and CURRENT_MODE_MASK specify information known about |
| 382 |
the file's current mode, using the style of struct delayed_set_stat. |
| 383 |
TYPEFLAG specifies the type of the file. |
| 384 |
If INTERDIR, this is an intermediate directory. |
| 385 |
ATFLAG specifies the flag to use when statting the file. */ |
| 386 |
|
| 387 |
static void |
| 388 |
set_stat (char const *file_name, |
| 389 |
struct tar_stat_info const *st, |
| 390 |
int fd, mode_t current_mode, mode_t current_mode_mask, |
| 391 |
char typeflag, bool interdir, int atflag) |
| 392 |
{ |
| 393 |
/* Do the utime before the chmod because some versions of utime are |
| 394 |
broken and trash the modes of the file. */ |
| 395 |
|
| 396 |
if (! touch_option && ! interdir) |
| 397 |
{ |
| 398 |
struct timespec ts[2]; |
| 399 |
if (incremental_option) |
| 400 |
ts[0] = st->atime; |
| 401 |
else |
| 402 |
ts[0].tv_nsec = UTIME_OMIT; |
| 403 |
ts[1] = st->mtime; |
| 404 |
|
| 405 |
if (fdutimensat (fd, chdir_fd, file_name, ts, atflag) == 0) |
| 406 |
{ |
| 407 |
if (incremental_option) |
| 408 |
check_time (file_name, ts[0]); |
| 409 |
check_time (file_name, ts[1]); |
| 410 |
} |
| 411 |
else if (typeflag != SYMTYPE || implemented (errno)) |
| 412 |
utime_error (file_name); |
| 413 |
} |
| 414 |
|
| 415 |
if (0 < same_owner_option && ! interdir) |
| 416 |
{ |
| 417 |
/* Some systems allow non-root users to give files away. Once this |
| 418 |
done, it is not possible anymore to change file permissions. |
| 419 |
However, setting file permissions now would be incorrect, since |
| 420 |
they would apply to the wrong user, and there would be a race |
| 421 |
condition. So, don't use systems that allow non-root users to |
| 422 |
give files away. */ |
| 423 |
uid_t uid = st->stat.st_uid; |
| 424 |
gid_t gid = st->stat.st_gid; |
| 425 |
|
| 426 |
if (fd_chown (fd, file_name, uid, gid, atflag) == 0) |
| 427 |
{ |
| 428 |
/* Changing the owner can clear st_mode bits in some cases. */ |
| 429 |
if ((current_mode | ~ current_mode_mask) & S_IXUGO) |
| 430 |
current_mode_mask &= ~ (current_mode & (S_ISUID | S_ISGID)); |
| 431 |
} |
| 432 |
else if (typeflag != SYMTYPE || implemented (errno)) |
| 433 |
chown_error_details (file_name, uid, gid); |
| 434 |
} |
| 435 |
|
| 436 |
set_mode (file_name, |
| 437 |
st->stat.st_mode & ~ current_umask, |
| 438 |
0 < same_permissions_option && ! interdir ? MODE_ALL : MODE_RWX, |
| 439 |
fd, current_mode, current_mode_mask, typeflag, atflag); |
| 440 |
|
| 441 |
/* these three calls must be done *after* fd_chown() call because fd_chown |
| 442 |
causes that linux capabilities becomes cleared. */ |
| 443 |
xattrs_xattrs_set (st, file_name, typeflag, 1); |
| 444 |
xattrs_acls_set (st, file_name, typeflag); |
| 445 |
xattrs_selinux_set (st, file_name, typeflag); |
| 446 |
} |
| 447 |
|
| 448 |
/* Find the direct ancestor of FILE_NAME in the delayed_set_stat list. |
| 449 |
*/ |
| 450 |
static struct delayed_set_stat * |
| 451 |
find_direct_ancestor (char const *file_name) |
| 452 |
{ |
| 453 |
struct delayed_set_stat *h = delayed_set_stat_head; |
| 454 |
while (h) |
| 455 |
{ |
| 456 |
if (! h->after_links |
| 457 |
&& strncmp (file_name, h->file_name, h->file_name_len) == 0 |
| 458 |
&& ISSLASH (file_name[h->file_name_len]) |
| 459 |
&& (last_component (file_name) == file_name + h->file_name_len + 1)) |
| 460 |
break; |
| 461 |
h = h->next; |
| 462 |
} |
| 463 |
return h; |
| 464 |
} |
| 465 |
|
| 466 |
/* For each entry H in the leading prefix of entries in HEAD that do |
| 467 |
not have after_links marked, mark H and fill in its dev and ino |
| 468 |
members. Assume HEAD && ! HEAD->after_links. */ |
| 469 |
static void |
| 470 |
mark_after_links (struct delayed_set_stat *head) |
| 471 |
{ |
| 472 |
struct delayed_set_stat *h = head; |
| 473 |
|
| 474 |
do |
| 475 |
{ |
| 476 |
struct stat st; |
| 477 |
h->after_links = 1; |
| 478 |
|
| 479 |
if (deref_stat (h->file_name, &st) != 0) |
| 480 |
stat_error (h->file_name); |
| 481 |
else |
| 482 |
{ |
| 483 |
h->dev = st.st_dev; |
| 484 |
h->ino = st.st_ino; |
| 485 |
} |
| 486 |
} |
| 487 |
while ((h = h->next) && ! h->after_links); |
| 488 |
} |
| 489 |
|
| 490 |
/* Remember to restore stat attributes (owner, group, mode and times) |
| 491 |
for the directory FILE_NAME, using information given in *ST, |
| 492 |
once we stop extracting files into that directory. |
| 493 |
|
| 494 |
If ST is null, merely create a placeholder node for an intermediate |
| 495 |
directory that was created by make_directories. |
| 496 |
|
| 497 |
NOTICE: this works only if the archive has usual member order, i.e. |
| 498 |
directory, then the files in that directory. Incremental archive have |
| 499 |
somewhat reversed order: first go subdirectories, then all other |
| 500 |
members. To help cope with this case the variable |
| 501 |
delay_directory_restore_option is set by prepare_to_extract. |
| 502 |
|
| 503 |
If an archive was explicitly created so that its member order is |
| 504 |
reversed, some directory timestamps can be restored incorrectly, |
| 505 |
e.g.: |
| 506 |
tar --no-recursion -cf archive dir dir/file1 foo dir/file2 |
| 507 |
*/ |
| 508 |
static void |
| 509 |
delay_set_stat (char const *file_name, struct tar_stat_info const *st, |
| 510 |
mode_t current_mode, mode_t current_mode_mask, |
| 511 |
mode_t mode, int atflag) |
| 512 |
{ |
| 513 |
size_t file_name_len = strlen (file_name); |
| 514 |
struct delayed_set_stat *data; |
| 515 |
|
| 516 |
for (data = delayed_set_stat_head; data; data = data->next) |
| 517 |
if (strcmp (data->file_name, file_name) == 0) |
| 518 |
break; |
| 519 |
|
| 520 |
if (data) |
| 521 |
{ |
| 522 |
if (data->interdir) |
| 523 |
{ |
| 524 |
struct stat real_st; |
| 525 |
if (fstatat (chdir_fd, data->file_name, |
| 526 |
&real_st, data->atflag) != 0) |
| 527 |
{ |
| 528 |
stat_error (data->file_name); |
| 529 |
} |
| 530 |
else |
| 531 |
{ |
| 532 |
data->dev = real_st.st_dev; |
| 533 |
data->ino = real_st.st_ino; |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
else |
| 538 |
{ |
| 539 |
data = xmalloc (sizeof (*data)); |
| 540 |
data->next = delayed_set_stat_head; |
| 541 |
delayed_set_stat_head = data; |
| 542 |
data->file_name_len = file_name_len; |
| 543 |
data->file_name = xstrdup (file_name); |
| 544 |
data->after_links = false; |
| 545 |
if (st) |
| 546 |
{ |
| 547 |
data->dev = st->stat.st_dev; |
| 548 |
data->ino = st->stat.st_ino; |
| 549 |
} |
| 550 |
xattr_map_init (&data->xattr_map); |
| 551 |
} |
| 552 |
|
| 553 |
data->mode = mode; |
| 554 |
if (st) |
| 555 |
{ |
| 556 |
data->uid = st->stat.st_uid; |
| 557 |
data->gid = st->stat.st_gid; |
| 558 |
data->atime = st->atime; |
| 559 |
data->mtime = st->mtime; |
| 560 |
} |
| 561 |
data->current_mode = current_mode; |
| 562 |
data->current_mode_mask = current_mode_mask; |
| 563 |
data->interdir = ! st; |
| 564 |
data->atflag = atflag; |
| 565 |
data->change_dir = chdir_current; |
| 566 |
data->cntx_name = NULL; |
| 567 |
if (st) |
| 568 |
assign_string_or_null (&data->cntx_name, st->cntx_name); |
| 569 |
if (st && st->acls_a_ptr) |
| 570 |
{ |
| 571 |
data->acls_a_ptr = xmemdup (st->acls_a_ptr, st->acls_a_len + 1); |
| 572 |
data->acls_a_len = st->acls_a_len; |
| 573 |
} |
| 574 |
else |
| 575 |
{ |
| 576 |
data->acls_a_ptr = NULL; |
| 577 |
data->acls_a_len = 0; |
| 578 |
} |
| 579 |
if (st && st->acls_d_ptr) |
| 580 |
{ |
| 581 |
data->acls_d_ptr = xmemdup (st->acls_d_ptr, st->acls_d_len + 1); |
| 582 |
data->acls_d_len = st->acls_d_len; |
| 583 |
} |
| 584 |
else |
| 585 |
{ |
| 586 |
data->acls_d_ptr = NULL; |
| 587 |
data->acls_d_len = 0; |
| 588 |
} |
| 589 |
if (st) |
| 590 |
xattr_map_copy (&data->xattr_map, &st->xattr_map); |
| 591 |
if (must_be_dot_or_slash (file_name)) |
| 592 |
mark_after_links (data); |
| 593 |
} |
| 594 |
|
| 595 |
/* Update the delayed_set_stat info for an intermediate directory |
| 596 |
created within the file name of DIR. The intermediate directory turned |
| 597 |
out to be the same as this directory, e.g. due to ".." or symbolic |
| 598 |
links. *DIR_STAT_INFO is the status of the directory. */ |
| 599 |
static void |
| 600 |
repair_delayed_set_stat (char const *dir, |
| 601 |
struct stat const *dir_stat_info) |
| 602 |
{ |
| 603 |
struct delayed_set_stat *data; |
| 604 |
for (data = delayed_set_stat_head; data; data = data->next) |
| 605 |
{ |
| 606 |
struct stat st; |
| 607 |
if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0) |
| 608 |
{ |
| 609 |
stat_error (data->file_name); |
| 610 |
return; |
| 611 |
} |
| 612 |
|
| 613 |
if (st.st_dev == dir_stat_info->st_dev |
| 614 |
&& st.st_ino == dir_stat_info->st_ino) |
| 615 |
{ |
| 616 |
data->dev = current_stat_info.stat.st_dev; |
| 617 |
data->ino = current_stat_info.stat.st_ino; |
| 618 |
data->mode = current_stat_info.stat.st_mode; |
| 619 |
data->uid = current_stat_info.stat.st_uid; |
| 620 |
data->gid = current_stat_info.stat.st_gid; |
| 621 |
data->atime = current_stat_info.atime; |
| 622 |
data->mtime = current_stat_info.mtime; |
| 623 |
data->current_mode = st.st_mode; |
| 624 |
data->current_mode_mask = ALL_MODE_BITS; |
| 625 |
data->interdir = false; |
| 626 |
return; |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"), |
| 631 |
quotearg_colon (dir))); |
| 632 |
} |
| 633 |
|
| 634 |
static void |
| 635 |
free_delayed_set_stat (struct delayed_set_stat *data) |
| 636 |
{ |
| 637 |
free (data->file_name); |
| 638 |
xattr_map_free (&data->xattr_map); |
| 639 |
free (data->cntx_name); |
| 640 |
free (data->acls_a_ptr); |
| 641 |
free (data->acls_d_ptr); |
| 642 |
free (data); |
| 643 |
} |
| 644 |
|
| 645 |
void |
| 646 |
remove_delayed_set_stat (const char *fname) |
| 647 |
{ |
| 648 |
struct delayed_set_stat *data, *next, *prev = NULL; |
| 649 |
for (data = delayed_set_stat_head; data; data = next) |
| 650 |
{ |
| 651 |
next = data->next; |
| 652 |
if (chdir_current == data->change_dir |
| 653 |
&& strcmp (data->file_name, fname) == 0) |
| 654 |
{ |
| 655 |
free_delayed_set_stat (data); |
| 656 |
if (prev) |
| 657 |
prev->next = next; |
| 658 |
else |
| 659 |
delayed_set_stat_head = next; |
| 660 |
return; |
| 661 |
} |
| 662 |
else |
| 663 |
prev = data; |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
static void |
| 668 |
fixup_delayed_set_stat (char const *src, char const *dst) |
| 669 |
{ |
| 670 |
struct delayed_set_stat *data; |
| 671 |
for (data = delayed_set_stat_head; data; data = data->next) |
| 672 |
{ |
| 673 |
if (chdir_current == data->change_dir |
| 674 |
&& strcmp (data->file_name, src) == 0) |
| 675 |
{ |
| 676 |
free (data->file_name); |
| 677 |
data->file_name = xstrdup (dst); |
| 678 |
data->file_name_len = strlen (dst); |
| 679 |
return; |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
/* After a file/link/directory creation has failed due to ENOENT, |
| 685 |
create all required directories. Return zero if all the required |
| 686 |
directories were created, nonzero (issuing a diagnostic) otherwise. |
| 687 |
Set *INTERDIR_MADE if at least one directory was created. */ |
| 688 |
static int |
| 689 |
make_directories (char *file_name) |
| 690 |
{ |
| 691 |
char *cursor0 = file_name + FILE_SYSTEM_PREFIX_LEN (file_name); |
| 692 |
char *cursor; /* points into the file name */ |
| 693 |
char *parent_end = NULL; |
| 694 |
int parent_errno; |
| 695 |
|
| 696 |
for (cursor = cursor0; *cursor; cursor++) |
| 697 |
{ |
| 698 |
mode_t mode; |
| 699 |
mode_t desired_mode; |
| 700 |
int status; |
| 701 |
|
| 702 |
if (! ISSLASH (*cursor)) |
| 703 |
continue; |
| 704 |
|
| 705 |
/* Avoid mkdir of empty string, if leading or double '/'. */ |
| 706 |
|
| 707 |
if (cursor == cursor0 || ISSLASH (cursor[-1])) |
| 708 |
continue; |
| 709 |
|
| 710 |
/* Avoid mkdir where last part of file name is "." or "..". */ |
| 711 |
|
| 712 |
if (cursor[-1] == '.' |
| 713 |
&& (cursor == cursor0 + 1 || ISSLASH (cursor[-2]) |
| 714 |
|| (cursor[-2] == '.' |
| 715 |
&& (cursor == cursor0 + 2 || ISSLASH (cursor[-3]))))) |
| 716 |
continue; |
| 717 |
|
| 718 |
*cursor = '\0'; /* truncate the name there */ |
| 719 |
desired_mode = MODE_RWX & ~ newdir_umask; |
| 720 |
mode = desired_mode | (we_are_root ? 0 : MODE_WXUSR); |
| 721 |
status = mkdirat (chdir_fd, file_name, mode); |
| 722 |
|
| 723 |
if (status == 0) |
| 724 |
{ |
| 725 |
/* Create a struct delayed_set_stat even if |
| 726 |
mode == desired_mode, because |
| 727 |
repair_delayed_set_stat may need to update the struct. */ |
| 728 |
delay_set_stat (file_name, |
| 729 |
0, mode & ~ current_umask, MODE_RWX, |
| 730 |
desired_mode, AT_SYMLINK_NOFOLLOW); |
| 731 |
|
| 732 |
print_for_mkdir (file_name, desired_mode); |
| 733 |
parent_end = NULL; |
| 734 |
} |
| 735 |
else |
| 736 |
switch (errno) |
| 737 |
{ |
| 738 |
case ELOOP: case ENAMETOOLONG: case ENOENT: case ENOTDIR: |
| 739 |
/* FILE_NAME doesn't exist and couldn't be created; fail now. */ |
| 740 |
mkdir_error (file_name); |
| 741 |
*cursor = '/'; |
| 742 |
return status; |
| 743 |
|
| 744 |
default: |
| 745 |
/* FILE_NAME may be an existing directory so do not fail now. |
| 746 |
Instead, arrange to check at loop exit, assuming this is |
| 747 |
the last loop iteration. */ |
| 748 |
parent_end = cursor; |
| 749 |
parent_errno = errno; |
| 750 |
break; |
| 751 |
} |
| 752 |
|
| 753 |
*cursor = '/'; |
| 754 |
} |
| 755 |
|
| 756 |
if (!parent_end) |
| 757 |
return 0; |
| 758 |
|
| 759 |
/* Although we did not create the parent directory, some other |
| 760 |
process may have created it, so check whether it exists now. */ |
| 761 |
*parent_end = '\0'; |
| 762 |
struct stat st; |
| 763 |
int stat_status = fstatat (chdir_fd, file_name, &st, 0); |
| 764 |
if (!stat_status && !S_ISDIR (st.st_mode)) |
| 765 |
stat_status = -1; |
| 766 |
if (stat_status) |
| 767 |
{ |
| 768 |
errno = parent_errno; |
| 769 |
mkdir_error (file_name); |
| 770 |
} |
| 771 |
*parent_end = '/'; |
| 772 |
|
| 773 |
return stat_status; |
| 774 |
} |
| 775 |
|
| 776 |
/* Return true if FILE_NAME (with status *STP, if STP) is not a |
| 777 |
directory, and has a time stamp newer than (or equal to) that of |
| 778 |
TAR_STAT. */ |
| 779 |
static bool |
| 780 |
file_newer_p (const char *file_name, struct stat const *stp, |
| 781 |
struct tar_stat_info *tar_stat) |
| 782 |
{ |
| 783 |
struct stat st; |
| 784 |
|
| 785 |
if (!stp) |
| 786 |
{ |
| 787 |
if (deref_stat (file_name, &st) != 0) |
| 788 |
{ |
| 789 |
if (errno != ENOENT) |
| 790 |
{ |
| 791 |
stat_warn (file_name); |
| 792 |
/* Be safer: if the file exists, assume it is newer. */ |
| 793 |
return true; |
| 794 |
} |
| 795 |
return false; |
| 796 |
} |
| 797 |
stp = &st; |
| 798 |
} |
| 799 |
|
| 800 |
return (! S_ISDIR (stp->st_mode) |
| 801 |
&& tar_timespec_cmp (tar_stat->mtime, get_stat_mtime (stp)) <= 0); |
| 802 |
} |
| 803 |
|
| 804 |
#define RECOVER_NO 0 |
| 805 |
#define RECOVER_OK 1 |
| 806 |
#define RECOVER_SKIP 2 |
| 807 |
|
| 808 |
/* Attempt repairing what went wrong with the extraction. Delete an |
| 809 |
already existing file or create missing intermediate directories. |
| 810 |
Return RECOVER_OK if we somewhat increased our chances at a successful |
| 811 |
extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the |
| 812 |
caller should skip extraction of that member. The value of errno is |
| 813 |
properly restored on returning RECOVER_NO. |
| 814 |
|
| 815 |
If REGULAR, the caller was trying to extract onto a regular file. |
| 816 |
|
| 817 |
Set *INTERDIR_MADE if an intermediate directory is made as part of |
| 818 |
the recovery process. */ |
| 819 |
|
| 820 |
static int |
| 821 |
maybe_recoverable (char *file_name, bool regular, bool *interdir_made) |
| 822 |
{ |
| 823 |
int e = errno; |
| 824 |
struct stat st; |
| 825 |
struct stat const *stp = 0; |
| 826 |
|
| 827 |
if (*interdir_made) |
| 828 |
return RECOVER_NO; |
| 829 |
|
| 830 |
switch (e) |
| 831 |
{ |
| 832 |
case ELOOP: |
| 833 |
|
| 834 |
/* With open ("symlink", O_NOFOLLOW|...), POSIX says errno == ELOOP, |
| 835 |
but some operating systems do not conform to the standard. */ |
| 836 |
#ifdef EFTYPE |
| 837 |
/* NetBSD uses errno == EFTYPE; see <http://gnats.netbsd.org/43154>. */ |
| 838 |
case EFTYPE: |
| 839 |
#endif |
| 840 |
/* FreeBSD 8.1 uses errno == EMLINK. */ |
| 841 |
case EMLINK: |
| 842 |
/* Tru64 5.1B uses errno == ENOTSUP. */ |
| 843 |
case ENOTSUP: |
| 844 |
|
| 845 |
if (! regular |
| 846 |
|| old_files_option != OVERWRITE_OLD_FILES || dereference_option) |
| 847 |
break; |
| 848 |
if (strchr (file_name, '/')) |
| 849 |
{ |
| 850 |
if (deref_stat (file_name, &st) != 0) |
| 851 |
break; |
| 852 |
stp = &st; |
| 853 |
} |
| 854 |
/* The caller tried to open a symbolic link with O_NOFOLLOW. |
| 855 |
Fall through, treating it as an already-existing file. */ |
| 856 |
FALLTHROUGH; |
| 857 |
case EEXIST: |
| 858 |
/* Remove an old file, if the options allow this. */ |
| 859 |
|
| 860 |
switch (old_files_option) |
| 861 |
{ |
| 862 |
case SKIP_OLD_FILES: |
| 863 |
WARNOPT (WARN_EXISTING_FILE, |
| 864 |
(0, 0, _("%s: skipping existing file"), file_name)); |
| 865 |
return RECOVER_SKIP; |
| 866 |
|
| 867 |
case KEEP_OLD_FILES: |
| 868 |
return RECOVER_NO; |
| 869 |
|
| 870 |
case KEEP_NEWER_FILES: |
| 871 |
if (file_newer_p (file_name, stp, ¤t_stat_info)) |
| 872 |
break; |
| 873 |
FALLTHROUGH; |
| 874 |
case DEFAULT_OLD_FILES: |
| 875 |
case NO_OVERWRITE_DIR_OLD_FILES: |
| 876 |
case OVERWRITE_OLD_FILES: |
| 877 |
if (0 < remove_any_file (file_name, ORDINARY_REMOVE_OPTION)) |
| 878 |
return RECOVER_OK; |
| 879 |
break; |
| 880 |
|
| 881 |
case UNLINK_FIRST_OLD_FILES: |
| 882 |
break; |
| 883 |
} |
| 884 |
FALLTHROUGH; |
| 885 |
|
| 886 |
case ENOENT: |
| 887 |
/* Attempt creating missing intermediate directories. */ |
| 888 |
if (make_directories (file_name) == 0) |
| 889 |
{ |
| 890 |
*interdir_made = true; |
| 891 |
return RECOVER_OK; |
| 892 |
} |
| 893 |
break; |
| 894 |
|
| 895 |
default: |
| 896 |
/* Just say we can't do anything about it... */ |
| 897 |
break; |
| 898 |
} |
| 899 |
|
| 900 |
errno = e; |
| 901 |
return RECOVER_NO; |
| 902 |
} |
| 903 |
|
| 904 |
/* Restore stat extended attributes (xattr) for FILE_NAME, using information |
| 905 |
given in *ST. Restore before extraction because they may affect file layout |
| 906 |
(e.g. on Lustre distributed parallel filesystem - setting info about how many |
| 907 |
servers is this file striped over, stripe size, mirror copies, etc. |
| 908 |
in advance dramatically improves the following performance of reading and |
| 909 |
writing a file). TYPEFLAG specifies the type of the file. Return a negative |
| 910 |
number (setting errno) on failure, zero if successful but FILE_NAME was not |
| 911 |
created (e.g., xattrs not available), and a positive number if FILE_NAME was |
| 912 |
created. */ |
| 913 |
static int |
| 914 |
set_xattr (char const *file_name, struct tar_stat_info const *st, |
| 915 |
mode_t mode, char typeflag) |
| 916 |
{ |
| 917 |
#ifdef HAVE_XATTRS |
| 918 |
if ((xattrs_option > 0) && st->xattr_map.xm_size) |
| 919 |
{ |
| 920 |
int r = mknodat (chdir_fd, file_name, mode, 0); |
| 921 |
if (r < 0) |
| 922 |
return r; |
| 923 |
xattrs_xattrs_set (st, file_name, typeflag, 0); |
| 924 |
return 1; |
| 925 |
} |
| 926 |
#endif |
| 927 |
|
| 928 |
return 0; |
| 929 |
} |
| 930 |
|
| 931 |
/* Fix the statuses of all directories whose statuses need fixing, and |
| 932 |
which are not ancestors of FILE_NAME. If AFTER_LINKS is |
| 933 |
nonzero, do this for all such directories; otherwise, stop at the |
| 934 |
first directory that is marked to be fixed up only after delayed |
| 935 |
links are applied. */ |
| 936 |
static void |
| 937 |
apply_nonancestor_delayed_set_stat (char const *file_name, bool after_links) |
| 938 |
{ |
| 939 |
size_t file_name_len = strlen (file_name); |
| 940 |
bool check_for_renamed_directories = 0; |
| 941 |
|
| 942 |
while (delayed_set_stat_head) |
| 943 |
{ |
| 944 |
struct delayed_set_stat *data = delayed_set_stat_head; |
| 945 |
bool skip_this_one = 0; |
| 946 |
struct stat st; |
| 947 |
mode_t current_mode = data->current_mode; |
| 948 |
mode_t current_mode_mask = data->current_mode_mask; |
| 949 |
|
| 950 |
check_for_renamed_directories |= data->after_links; |
| 951 |
|
| 952 |
if (after_links < data->after_links |
| 953 |
|| (data->file_name_len < file_name_len |
| 954 |
&& file_name[data->file_name_len] |
| 955 |
&& (ISSLASH (file_name[data->file_name_len]) |
| 956 |
|| ISSLASH (file_name[data->file_name_len - 1])) |
| 957 |
&& memcmp (file_name, data->file_name, data->file_name_len) == 0)) |
| 958 |
break; |
| 959 |
|
| 960 |
chdir_do (data->change_dir); |
| 961 |
|
| 962 |
if (check_for_renamed_directories) |
| 963 |
{ |
| 964 |
if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0) |
| 965 |
{ |
| 966 |
stat_error (data->file_name); |
| 967 |
skip_this_one = 1; |
| 968 |
} |
| 969 |
else |
| 970 |
{ |
| 971 |
current_mode = st.st_mode; |
| 972 |
current_mode_mask = ALL_MODE_BITS; |
| 973 |
if (! (st.st_dev == data->dev && st.st_ino == data->ino)) |
| 974 |
{ |
| 975 |
ERROR ((0, 0, |
| 976 |
_("%s: Directory renamed before its status could be extracted"), |
| 977 |
quotearg_colon (data->file_name))); |
| 978 |
skip_this_one = 1; |
| 979 |
} |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
if (! skip_this_one) |
| 984 |
{ |
| 985 |
struct tar_stat_info sb; |
| 986 |
sb.stat.st_mode = data->mode; |
| 987 |
sb.stat.st_uid = data->uid; |
| 988 |
sb.stat.st_gid = data->gid; |
| 989 |
sb.atime = data->atime; |
| 990 |
sb.mtime = data->mtime; |
| 991 |
sb.cntx_name = data->cntx_name; |
| 992 |
sb.acls_a_ptr = data->acls_a_ptr; |
| 993 |
sb.acls_a_len = data->acls_a_len; |
| 994 |
sb.acls_d_ptr = data->acls_d_ptr; |
| 995 |
sb.acls_d_len = data->acls_d_len; |
| 996 |
sb.xattr_map = data->xattr_map; |
| 997 |
set_stat (data->file_name, &sb, |
| 998 |
-1, current_mode, current_mode_mask, |
| 999 |
DIRTYPE, data->interdir, data->atflag); |
| 1000 |
} |
| 1001 |
|
| 1002 |
delayed_set_stat_head = data->next; |
| 1003 |
free_delayed_set_stat (data); |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|
| 1007 |
|
| 1008 |
static bool |
| 1009 |
is_directory_link (char const *file_name, struct stat *st) |
| 1010 |
{ |
| 1011 |
char buf[1]; |
| 1012 |
return (0 <= readlinkat (chdir_fd, file_name, buf, sizeof buf) |
| 1013 |
&& fstatat (chdir_fd, file_name, st, 0) == 0 |
| 1014 |
&& S_ISDIR (st->st_mode)); |
| 1015 |
} |
| 1016 |
|
| 1017 |
/* Given struct stat of a directory (or directory member) whose ownership |
| 1018 |
or permissions of will be restored later, return the temporary permissions |
| 1019 |
for that directory, sufficiently restrictive so that in the meantime |
| 1020 |
processes owned by other users do not inadvertently create files under this |
| 1021 |
directory that inherit the wrong owner, group, or permissions from the |
| 1022 |
directory. |
| 1023 |
|
| 1024 |
If not root, though, make the directory writeable and searchable at first, |
| 1025 |
so that files can be created under it. |
| 1026 |
*/ |
| 1027 |
static int |
| 1028 |
safe_dir_mode (struct stat const *st) |
| 1029 |
{ |
| 1030 |
return ((st->st_mode |
| 1031 |
& (0 < same_owner_option || 0 < same_permissions_option |
| 1032 |
? S_IRWXU |
| 1033 |
: MODE_RWX)) |
| 1034 |
| (we_are_root ? 0 : MODE_WXUSR)); |
| 1035 |
} |
| 1036 |
|
| 1037 |
/* Extractor functions for various member types */ |
| 1038 |
|
| 1039 |
static int |
| 1040 |
extract_dir (char *file_name, int typeflag) |
| 1041 |
{ |
| 1042 |
int status; |
| 1043 |
mode_t mode; |
| 1044 |
mode_t current_mode = 0; |
| 1045 |
mode_t current_mode_mask = 0; |
| 1046 |
int atflag = 0; |
| 1047 |
bool interdir_made = false; |
| 1048 |
|
| 1049 |
/* Save 'root device' to avoid purging mount points. */ |
| 1050 |
if (one_file_system_option && root_device == 0) |
| 1051 |
{ |
| 1052 |
struct stat st; |
| 1053 |
|
| 1054 |
if (fstatat (chdir_fd, ".", &st, 0) != 0) |
| 1055 |
stat_diag ("."); |
| 1056 |
else |
| 1057 |
root_device = st.st_dev; |
| 1058 |
} |
| 1059 |
|
| 1060 |
if (incremental_option) |
| 1061 |
/* Read the entry and delete files that aren't listed in the archive. */ |
| 1062 |
purge_directory (file_name); |
| 1063 |
else if (typeflag == GNUTYPE_DUMPDIR) |
| 1064 |
skip_member (); |
| 1065 |
|
| 1066 |
mode = safe_dir_mode (¤t_stat_info.stat); |
| 1067 |
|
| 1068 |
for (;;) |
| 1069 |
{ |
| 1070 |
status = mkdirat (chdir_fd, file_name, mode); |
| 1071 |
if (status == 0) |
| 1072 |
{ |
| 1073 |
current_mode = mode & ~ current_umask; |
| 1074 |
current_mode_mask = MODE_RWX; |
| 1075 |
atflag = AT_SYMLINK_NOFOLLOW; |
| 1076 |
break; |
| 1077 |
} |
| 1078 |
|
| 1079 |
if (errno == EEXIST) |
| 1080 |
{ |
| 1081 |
if (interdir_made |
| 1082 |
|| keep_directory_symlink_option |
| 1083 |
|| old_files_option == NO_OVERWRITE_DIR_OLD_FILES |
| 1084 |
|| old_files_option == DEFAULT_OLD_FILES |
| 1085 |
|| old_files_option == OVERWRITE_OLD_FILES) |
| 1086 |
{ |
| 1087 |
struct stat st; |
| 1088 |
st.st_mode = 0; |
| 1089 |
|
| 1090 |
if (keep_directory_symlink_option |
| 1091 |
&& is_directory_link (file_name, &st)) |
| 1092 |
return 0; |
| 1093 |
|
| 1094 |
if ((st.st_mode != 0 && fstatat_flags == 0) |
| 1095 |
|| deref_stat (file_name, &st) == 0) |
| 1096 |
{ |
| 1097 |
current_mode = st.st_mode; |
| 1098 |
current_mode_mask = ALL_MODE_BITS; |
| 1099 |
|
| 1100 |
if (S_ISDIR (current_mode)) |
| 1101 |
{ |
| 1102 |
if (interdir_made) |
| 1103 |
{ |
| 1104 |
repair_delayed_set_stat (file_name, &st); |
| 1105 |
return 0; |
| 1106 |
} |
| 1107 |
else if (old_files_option == NO_OVERWRITE_DIR_OLD_FILES) |
| 1108 |
{ |
| 1109 |
/* Temporarily change the directory mode to a safe |
| 1110 |
value, to be able to create files in it, should |
| 1111 |
the need be. |
| 1112 |
*/ |
| 1113 |
mode = safe_dir_mode (&st); |
| 1114 |
status = fd_chmod (-1, file_name, mode, |
| 1115 |
AT_SYMLINK_NOFOLLOW, DIRTYPE); |
| 1116 |
if (status == 0) |
| 1117 |
{ |
| 1118 |
/* Store the actual directory mode, to be restored |
| 1119 |
later. |
| 1120 |
*/ |
| 1121 |
current_stat_info.stat = st; |
| 1122 |
current_mode = mode & ~ current_umask; |
| 1123 |
current_mode_mask = MODE_RWX; |
| 1124 |
atflag = AT_SYMLINK_NOFOLLOW; |
| 1125 |
break; |
| 1126 |
} |
| 1127 |
else |
| 1128 |
{ |
| 1129 |
chmod_error_details (file_name, mode); |
| 1130 |
} |
| 1131 |
} |
| 1132 |
break; |
| 1133 |
} |
| 1134 |
} |
| 1135 |
} |
| 1136 |
else if (old_files_option == UNLINK_FIRST_OLD_FILES) |
| 1137 |
{ |
| 1138 |
status = 0; |
| 1139 |
break; |
| 1140 |
} |
| 1141 |
|
| 1142 |
errno = EEXIST; |
| 1143 |
} |
| 1144 |
|
| 1145 |
switch (maybe_recoverable (file_name, false, &interdir_made)) |
| 1146 |
{ |
| 1147 |
case RECOVER_OK: |
| 1148 |
continue; |
| 1149 |
|
| 1150 |
case RECOVER_SKIP: |
| 1151 |
break; |
| 1152 |
|
| 1153 |
case RECOVER_NO: |
| 1154 |
if (errno != EEXIST) |
| 1155 |
{ |
| 1156 |
mkdir_error (file_name); |
| 1157 |
return 1; |
| 1158 |
} |
| 1159 |
break; |
| 1160 |
} |
| 1161 |
break; |
| 1162 |
} |
| 1163 |
|
| 1164 |
if (status == 0 |
| 1165 |
|| old_files_option == DEFAULT_OLD_FILES |
| 1166 |
|| old_files_option == OVERWRITE_OLD_FILES) |
| 1167 |
delay_set_stat (file_name, ¤t_stat_info, |
| 1168 |
current_mode, current_mode_mask, |
| 1169 |
current_stat_info.stat.st_mode, atflag); |
| 1170 |
return status; |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
static int |
| 1176 |
open_output_file (char const *file_name, int typeflag, mode_t mode, |
| 1177 |
int file_created, mode_t *current_mode, |
| 1178 |
mode_t *current_mode_mask) |
| 1179 |
{ |
| 1180 |
int fd; |
| 1181 |
bool overwriting_old_files = old_files_option == OVERWRITE_OLD_FILES; |
| 1182 |
int openflag = (O_WRONLY | O_BINARY | O_CLOEXEC | O_NOCTTY | O_NONBLOCK |
| 1183 |
| (file_created |
| 1184 |
? O_NOFOLLOW |
| 1185 |
: (O_CREAT |
| 1186 |
| (overwriting_old_files |
| 1187 |
? O_TRUNC | (dereference_option ? 0 : O_NOFOLLOW) |
| 1188 |
: O_EXCL)))); |
| 1189 |
|
| 1190 |
if (typeflag == CONTTYPE) |
| 1191 |
{ |
| 1192 |
static int conttype_diagnosed; |
| 1193 |
|
| 1194 |
if (!conttype_diagnosed) |
| 1195 |
{ |
| 1196 |
conttype_diagnosed = 1; |
| 1197 |
WARNOPT (WARN_CONTIGUOUS_CAST, |
| 1198 |
(0, 0, _("Extracting contiguous files as regular files"))); |
| 1199 |
} |
| 1200 |
} |
| 1201 |
|
| 1202 |
/* If O_NOFOLLOW is needed but does not work, check for a symlink |
| 1203 |
separately. There's a race condition, but that cannot be avoided |
| 1204 |
on hosts lacking O_NOFOLLOW. */ |
| 1205 |
if (! HAVE_WORKING_O_NOFOLLOW |
| 1206 |
&& overwriting_old_files && ! dereference_option) |
| 1207 |
{ |
| 1208 |
char buf[1]; |
| 1209 |
if (0 <= readlinkat (chdir_fd, file_name, buf, sizeof buf)) |
| 1210 |
{ |
| 1211 |
errno = ELOOP; |
| 1212 |
return -1; |
| 1213 |
} |
| 1214 |
} |
| 1215 |
|
| 1216 |
fd = openat (chdir_fd, file_name, openflag, mode); |
| 1217 |
if (0 <= fd) |
| 1218 |
{ |
| 1219 |
if (openflag & O_EXCL) |
| 1220 |
{ |
| 1221 |
*current_mode = mode & ~ current_umask; |
| 1222 |
*current_mode_mask = MODE_RWX; |
| 1223 |
} |
| 1224 |
else |
| 1225 |
{ |
| 1226 |
struct stat st; |
| 1227 |
if (fstat (fd, &st) != 0) |
| 1228 |
{ |
| 1229 |
int e = errno; |
| 1230 |
close (fd); |
| 1231 |
errno = e; |
| 1232 |
return -1; |
| 1233 |
} |
| 1234 |
if (! S_ISREG (st.st_mode)) |
| 1235 |
{ |
| 1236 |
close (fd); |
| 1237 |
errno = EEXIST; |
| 1238 |
return -1; |
| 1239 |
} |
| 1240 |
*current_mode = st.st_mode; |
| 1241 |
*current_mode_mask = ALL_MODE_BITS; |
| 1242 |
} |
| 1243 |
} |
| 1244 |
|
| 1245 |
return fd; |
| 1246 |
} |
| 1247 |
|
| 1248 |
static int |
| 1249 |
extract_file (char *file_name, int typeflag) |
| 1250 |
{ |
| 1251 |
int fd; |
| 1252 |
off_t size; |
| 1253 |
union block *data_block; |
| 1254 |
int status; |
| 1255 |
size_t count; |
| 1256 |
size_t written; |
| 1257 |
bool interdir_made = false; |
| 1258 |
mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX |
| 1259 |
& ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0)); |
| 1260 |
mode_t current_mode = 0; |
| 1261 |
mode_t current_mode_mask = 0; |
| 1262 |
|
| 1263 |
if (to_stdout_option) |
| 1264 |
fd = STDOUT_FILENO; |
| 1265 |
else if (to_command_option) |
| 1266 |
{ |
| 1267 |
fd = sys_exec_command (file_name, 'f', ¤t_stat_info); |
| 1268 |
if (fd < 0) |
| 1269 |
{ |
| 1270 |
skip_member (); |
| 1271 |
return 0; |
| 1272 |
} |
| 1273 |
} |
| 1274 |
else |
| 1275 |
{ |
| 1276 |
int file_created; |
| 1277 |
/* Either we pre-create the file in set_xattr(), or we just directly open |
| 1278 |
the file in open_output_file() with O_CREAT. If pre-creating, we need |
| 1279 |
to use S_IWUSR so we can open the file O_WRONLY in open_output_file(). |
| 1280 |
The additional mode bit is cleared later by set_stat()->set_mode(). */ |
| 1281 |
while (((file_created = set_xattr (file_name, ¤t_stat_info, |
| 1282 |
mode | S_IWUSR, typeflag)) |
| 1283 |
< 0) |
| 1284 |
|| ((fd = open_output_file (file_name, typeflag, mode, |
| 1285 |
file_created, ¤t_mode, |
| 1286 |
¤t_mode_mask)) |
| 1287 |
< 0)) |
| 1288 |
{ |
| 1289 |
int recover = maybe_recoverable (file_name, true, &interdir_made); |
| 1290 |
if (recover != RECOVER_OK) |
| 1291 |
{ |
| 1292 |
skip_member (); |
| 1293 |
if (recover == RECOVER_SKIP) |
| 1294 |
return 0; |
| 1295 |
open_error (file_name); |
| 1296 |
return 1; |
| 1297 |
} |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
mv_begin_read (¤t_stat_info); |
| 1302 |
if (current_stat_info.is_sparse) |
| 1303 |
sparse_extract_file (fd, ¤t_stat_info, &size); |
| 1304 |
else |
| 1305 |
for (size = current_stat_info.stat.st_size; size > 0; ) |
| 1306 |
{ |
| 1307 |
mv_size_left (size); |
| 1308 |
|
| 1309 |
/* Locate data, determine max length writeable, write it, |
| 1310 |
block that we have used the data, then check if the write |
| 1311 |
worked. */ |
| 1312 |
|
| 1313 |
data_block = find_next_block (); |
| 1314 |
if (! data_block) |
| 1315 |
{ |
| 1316 |
ERROR ((0, 0, _("Unexpected EOF in archive"))); |
| 1317 |
break; /* FIXME: What happens, then? */ |
| 1318 |
} |
| 1319 |
|
| 1320 |
written = available_space_after (data_block); |
| 1321 |
|
| 1322 |
if (written > size) |
| 1323 |
written = size; |
| 1324 |
errno = 0; |
| 1325 |
count = blocking_write (fd, data_block->buffer, written); |
| 1326 |
size -= written; |
| 1327 |
|
| 1328 |
set_next_block_after ((union block *) |
| 1329 |
(data_block->buffer + written - 1)); |
| 1330 |
if (count != written) |
| 1331 |
{ |
| 1332 |
if (!to_command_option) |
| 1333 |
write_error_details (file_name, count, written); |
| 1334 |
/* FIXME: shouldn't we restore from backup? */ |
| 1335 |
break; |
| 1336 |
} |
| 1337 |
} |
| 1338 |
|
| 1339 |
skim_file (size, false); |
| 1340 |
|
| 1341 |
mv_end (); |
| 1342 |
|
| 1343 |
/* If writing to stdout, don't try to do anything to the filename; |
| 1344 |
it doesn't exist, or we don't want to touch it anyway. */ |
| 1345 |
|
| 1346 |
if (to_stdout_option) |
| 1347 |
return 0; |
| 1348 |
|
| 1349 |
if (! to_command_option) |
| 1350 |
set_stat (file_name, ¤t_stat_info, fd, |
| 1351 |
current_mode, current_mode_mask, typeflag, false, |
| 1352 |
(old_files_option == OVERWRITE_OLD_FILES |
| 1353 |
? 0 : AT_SYMLINK_NOFOLLOW)); |
| 1354 |
|
| 1355 |
status = close (fd); |
| 1356 |
if (status < 0) |
| 1357 |
close_error (file_name); |
| 1358 |
|
| 1359 |
if (to_command_option) |
| 1360 |
sys_wait_command (); |
| 1361 |
|
| 1362 |
return status; |
| 1363 |
} |
| 1364 |
|
| 1365 |
/* Return true if NAME is a delayed link. This can happen only if the link |
| 1366 |
placeholder file has been created. Therefore, try to stat the NAME |
| 1367 |
first. If it doesn't exist, there is no matching entry in the table. |
| 1368 |
Otherwise, look for the entry in the table that has the matching dev |
| 1369 |
and ino numbers. Return false if not found. |
| 1370 |
|
| 1371 |
Do not rely on comparing file names, which may differ for |
| 1372 |
various reasons (e.g. relative vs. absolute file names). |
| 1373 |
*/ |
| 1374 |
static bool |
| 1375 |
find_delayed_link_source (char const *name) |
| 1376 |
{ |
| 1377 |
struct stat st; |
| 1378 |
|
| 1379 |
if (!delayed_link_table) |
| 1380 |
return false; |
| 1381 |
|
| 1382 |
if (fstatat (chdir_fd, name, &st, AT_SYMLINK_NOFOLLOW)) |
| 1383 |
{ |
| 1384 |
if (errno != ENOENT) |
| 1385 |
stat_error (name); |
| 1386 |
return false; |
| 1387 |
} |
| 1388 |
|
| 1389 |
struct delayed_link dl; |
| 1390 |
dl.dev = st.st_dev; |
| 1391 |
dl.ino = st.st_ino; |
| 1392 |
return hash_lookup (delayed_link_table, &dl) != NULL; |
| 1393 |
} |
| 1394 |
|
| 1395 |
/* Create a placeholder file with name FILE_NAME, which will be |
| 1396 |
replaced after other extraction is done by a symbolic link if |
| 1397 |
IS_SYMLINK is true, and by a hard link otherwise. Set |
| 1398 |
*INTERDIR_MADE if an intermediate directory is made in the |
| 1399 |
process. |
| 1400 |
*/ |
| 1401 |
|
| 1402 |
static int |
| 1403 |
create_placeholder_file (char *file_name, bool is_symlink, bool *interdir_made) |
| 1404 |
{ |
| 1405 |
int fd; |
| 1406 |
struct stat st; |
| 1407 |
|
| 1408 |
while ((fd = openat (chdir_fd, file_name, O_WRONLY | O_CREAT | O_EXCL, 0)) < 0) |
| 1409 |
{ |
| 1410 |
if (errno == EEXIST && find_delayed_link_source (file_name)) |
| 1411 |
{ |
| 1412 |
/* The placeholder file has already been created. This means |
| 1413 |
that the link being extracted is a duplicate of an already |
| 1414 |
processed one. Skip it. |
| 1415 |
*/ |
| 1416 |
return 0; |
| 1417 |
} |
| 1418 |
|
| 1419 |
switch (maybe_recoverable (file_name, false, interdir_made)) |
| 1420 |
{ |
| 1421 |
case RECOVER_OK: |
| 1422 |
continue; |
| 1423 |
|
| 1424 |
case RECOVER_SKIP: |
| 1425 |
return 0; |
| 1426 |
|
| 1427 |
case RECOVER_NO: |
| 1428 |
open_error (file_name); |
| 1429 |
return -1; |
| 1430 |
} |
| 1431 |
} |
| 1432 |
|
| 1433 |
if (fstat (fd, &st) != 0) |
| 1434 |
{ |
| 1435 |
stat_error (file_name); |
| 1436 |
close (fd); |
| 1437 |
} |
| 1438 |
else if (close (fd) != 0) |
| 1439 |
close_error (file_name); |
| 1440 |
else |
| 1441 |
{ |
| 1442 |
struct delayed_set_stat *h; |
| 1443 |
struct delayed_link *p = |
| 1444 |
xmalloc (FLEXNSIZEOF (struct delayed_link, target, |
| 1445 |
strlen (current_stat_info.link_name) + 1)); |
| 1446 |
p->next = NULL; |
| 1447 |
p->dev = st.st_dev; |
| 1448 |
p->ino = st.st_ino; |
| 1449 |
#if HAVE_BIRTHTIME |
| 1450 |
p->birthtime = get_stat_birthtime (&st); |
| 1451 |
#endif |
| 1452 |
p->is_symlink = is_symlink; |
| 1453 |
if (is_symlink) |
| 1454 |
{ |
| 1455 |
p->mode = current_stat_info.stat.st_mode; |
| 1456 |
p->uid = current_stat_info.stat.st_uid; |
| 1457 |
p->gid = current_stat_info.stat.st_gid; |
| 1458 |
p->atime = current_stat_info.atime; |
| 1459 |
p->mtime = current_stat_info.mtime; |
| 1460 |
} |
| 1461 |
p->change_dir = chdir_current; |
| 1462 |
p->sources = xmalloc (FLEXNSIZEOF (struct string_list, string, |
| 1463 |
strlen (file_name) + 1)); |
| 1464 |
p->sources->next = 0; |
| 1465 |
strcpy (p->sources->string, file_name); |
| 1466 |
p->cntx_name = NULL; |
| 1467 |
assign_string_or_null (&p->cntx_name, current_stat_info.cntx_name); |
| 1468 |
p->acls_a_ptr = NULL; |
| 1469 |
p->acls_a_len = 0; |
| 1470 |
p->acls_d_ptr = NULL; |
| 1471 |
p->acls_d_len = 0; |
| 1472 |
xattr_map_init (&p->xattr_map); |
| 1473 |
xattr_map_copy (&p->xattr_map, ¤t_stat_info.xattr_map); |
| 1474 |
strcpy (p->target, current_stat_info.link_name); |
| 1475 |
|
| 1476 |
*delayed_link_tail = p; |
| 1477 |
delayed_link_tail = &p->next; |
| 1478 |
if (! ((delayed_link_table |
| 1479 |
|| (delayed_link_table = hash_initialize (0, 0, dl_hash, |
| 1480 |
dl_compare, free))) |
| 1481 |
&& hash_insert (delayed_link_table, p))) |
| 1482 |
xalloc_die (); |
| 1483 |
|
| 1484 |
if ((h = find_direct_ancestor (file_name)) != NULL) |
| 1485 |
mark_after_links (h); |
| 1486 |
|
| 1487 |
return 0; |
| 1488 |
} |
| 1489 |
|
| 1490 |
return -1; |
| 1491 |
} |
| 1492 |
|
| 1493 |
static int |
| 1494 |
extract_link (char *file_name, MAYBE_UNUSED int typeflag) |
| 1495 |
{ |
| 1496 |
bool interdir_made = false; |
| 1497 |
char const *link_name; |
| 1498 |
int rc; |
| 1499 |
|
| 1500 |
link_name = current_stat_info.link_name; |
| 1501 |
|
| 1502 |
if ((! absolute_names_option && contains_dot_dot (link_name)) |
| 1503 |
|| find_delayed_link_source (link_name)) |
| 1504 |
return create_placeholder_file (file_name, false, &interdir_made); |
| 1505 |
|
| 1506 |
do |
| 1507 |
{ |
| 1508 |
struct stat st1, st2; |
| 1509 |
int e; |
| 1510 |
int status = linkat (chdir_fd, link_name, chdir_fd, file_name, 0); |
| 1511 |
e = errno; |
| 1512 |
|
| 1513 |
if (status == 0) |
| 1514 |
{ |
| 1515 |
if (delayed_link_table |
| 1516 |
&& fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW) == 0) |
| 1517 |
{ |
| 1518 |
struct delayed_link dl1; |
| 1519 |
dl1.ino = st1.st_ino; |
| 1520 |
dl1.dev = st1.st_dev; |
| 1521 |
struct delayed_link *ds = hash_lookup (delayed_link_table, &dl1); |
| 1522 |
if (ds && ds->change_dir == chdir_current |
| 1523 |
&& BIRTHTIME_EQ (ds->birthtime, get_stat_birthtime (&st1))) |
| 1524 |
{ |
| 1525 |
struct string_list *p |
| 1526 |
= xmalloc (FLEXNSIZEOF (struct string_list, |
| 1527 |
string, strlen (file_name) + 1)); |
| 1528 |
strcpy (p->string, file_name); |
| 1529 |
p->next = ds->sources; |
| 1530 |
ds->sources = p; |
| 1531 |
} |
| 1532 |
} |
| 1533 |
|
| 1534 |
return 0; |
| 1535 |
} |
| 1536 |
else if ((e == EEXIST && strcmp (link_name, file_name) == 0) |
| 1537 |
|| ((fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW) |
| 1538 |
== 0) |
| 1539 |
&& (fstatat (chdir_fd, file_name, &st2, AT_SYMLINK_NOFOLLOW) |
| 1540 |
== 0) |
| 1541 |
&& st1.st_dev == st2.st_dev |
| 1542 |
&& st1.st_ino == st2.st_ino)) |
| 1543 |
return 0; |
| 1544 |
|
| 1545 |
errno = e; |
| 1546 |
} |
| 1547 |
while ((rc = maybe_recoverable (file_name, false, &interdir_made)) |
| 1548 |
== RECOVER_OK); |
| 1549 |
|
| 1550 |
if (rc == RECOVER_SKIP) |
| 1551 |
return 0; |
| 1552 |
if (!(incremental_option && errno == EEXIST)) |
| 1553 |
{ |
| 1554 |
link_error (link_name, file_name); |
| 1555 |
return 1; |
| 1556 |
} |
| 1557 |
return 0; |
| 1558 |
} |
| 1559 |
|
| 1560 |
static int |
| 1561 |
extract_symlink (char *file_name, MAYBE_UNUSED int typeflag) |
| 1562 |
{ |
| 1563 |
#ifdef HAVE_SYMLINK |
| 1564 |
bool interdir_made = false; |
| 1565 |
|
| 1566 |
if (! absolute_names_option |
| 1567 |
&& (IS_ABSOLUTE_FILE_NAME (current_stat_info.link_name) |
| 1568 |
|| contains_dot_dot (current_stat_info.link_name))) |
| 1569 |
return create_placeholder_file (file_name, true, &interdir_made); |
| 1570 |
|
| 1571 |
while (symlinkat (current_stat_info.link_name, chdir_fd, file_name) != 0) |
| 1572 |
switch (maybe_recoverable (file_name, false, &interdir_made)) |
| 1573 |
{ |
| 1574 |
case RECOVER_OK: |
| 1575 |
continue; |
| 1576 |
|
| 1577 |
case RECOVER_SKIP: |
| 1578 |
return 0; |
| 1579 |
|
| 1580 |
case RECOVER_NO: |
| 1581 |
symlink_error (current_stat_info.link_name, file_name); |
| 1582 |
return -1; |
| 1583 |
} |
| 1584 |
|
| 1585 |
set_stat (file_name, ¤t_stat_info, -1, 0, 0, |
| 1586 |
SYMTYPE, false, AT_SYMLINK_NOFOLLOW); |
| 1587 |
return 0; |
| 1588 |
|
| 1589 |
#else |
| 1590 |
static int warned_once; |
| 1591 |
|
| 1592 |
if (!warned_once) |
| 1593 |
{ |
| 1594 |
warned_once = 1; |
| 1595 |
WARNOPT (WARN_SYMLINK_CAST, |
| 1596 |
(0, 0, |
| 1597 |
_("Attempting extraction of symbolic links as hard links"))); |
| 1598 |
} |
| 1599 |
return extract_link (file_name, typeflag); |
| 1600 |
#endif |
| 1601 |
} |
| 1602 |
|
| 1603 |
#if S_IFCHR || S_IFBLK |
| 1604 |
static int |
| 1605 |
extract_node (char *file_name, int typeflag) |
| 1606 |
{ |
| 1607 |
bool interdir_made = false; |
| 1608 |
mode_t mode = (current_stat_info.stat.st_mode & (MODE_RWX | S_IFBLK | S_IFCHR) |
| 1609 |
& ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0)); |
| 1610 |
|
| 1611 |
while (mknodat (chdir_fd, file_name, mode, current_stat_info.stat.st_rdev) |
| 1612 |
!= 0) |
| 1613 |
switch (maybe_recoverable (file_name, false, &interdir_made)) |
| 1614 |
{ |
| 1615 |
case RECOVER_OK: |
| 1616 |
continue; |
| 1617 |
|
| 1618 |
case RECOVER_SKIP: |
| 1619 |
return 0; |
| 1620 |
|
| 1621 |
case RECOVER_NO: |
| 1622 |
mknod_error (file_name); |
| 1623 |
return -1; |
| 1624 |
} |
| 1625 |
|
| 1626 |
set_stat (file_name, ¤t_stat_info, -1, |
| 1627 |
mode & ~ current_umask, MODE_RWX, |
| 1628 |
typeflag, false, AT_SYMLINK_NOFOLLOW); |
| 1629 |
return 0; |
| 1630 |
} |
| 1631 |
#endif |
| 1632 |
|
| 1633 |
#if HAVE_MKFIFO || defined mkfifo |
| 1634 |
static int |
| 1635 |
extract_fifo (char *file_name, int typeflag) |
| 1636 |
{ |
| 1637 |
bool interdir_made = false; |
| 1638 |
mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX |
| 1639 |
& ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0)); |
| 1640 |
|
| 1641 |
while (mkfifoat (chdir_fd, file_name, mode) != 0) |
| 1642 |
switch (maybe_recoverable (file_name, false, &interdir_made)) |
| 1643 |
{ |
| 1644 |
case RECOVER_OK: |
| 1645 |
continue; |
| 1646 |
|
| 1647 |
case RECOVER_SKIP: |
| 1648 |
return 0; |
| 1649 |
|
| 1650 |
case RECOVER_NO: |
| 1651 |
mkfifo_error (file_name); |
| 1652 |
return -1; |
| 1653 |
} |
| 1654 |
|
| 1655 |
set_stat (file_name, ¤t_stat_info, -1, |
| 1656 |
mode & ~ current_umask, MODE_RWX, |
| 1657 |
typeflag, false, AT_SYMLINK_NOFOLLOW); |
| 1658 |
return 0; |
| 1659 |
} |
| 1660 |
#endif |
| 1661 |
|
| 1662 |
typedef int (*tar_extractor_t) (char *file_name, int typeflag); |
| 1663 |
|
| 1664 |
|
| 1665 |
/* Prepare to extract a file. Find extractor function. |
| 1666 |
Return true to proceed with the extraction, false to skip the current |
| 1667 |
member. */ |
| 1668 |
|
| 1669 |
static bool |
| 1670 |
prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun) |
| 1671 |
{ |
| 1672 |
tar_extractor_t extractor = NULL; |
| 1673 |
|
| 1674 |
/* Select the extractor */ |
| 1675 |
switch (typeflag) |
| 1676 |
{ |
| 1677 |
case GNUTYPE_SPARSE: |
| 1678 |
extractor = extract_file; |
| 1679 |
break; |
| 1680 |
|
| 1681 |
case AREGTYPE: |
| 1682 |
case REGTYPE: |
| 1683 |
case CONTTYPE: |
| 1684 |
/* Appears to be a file. But BSD tar uses the convention that a slash |
| 1685 |
suffix means a directory. */ |
| 1686 |
if (current_stat_info.had_trailing_slash) |
| 1687 |
extractor = extract_dir; |
| 1688 |
else |
| 1689 |
extractor = extract_file; |
| 1690 |
break; |
| 1691 |
|
| 1692 |
case SYMTYPE: |
| 1693 |
extractor = extract_symlink; |
| 1694 |
break; |
| 1695 |
|
| 1696 |
case LNKTYPE: |
| 1697 |
extractor = extract_link; |
| 1698 |
break; |
| 1699 |
|
| 1700 |
#if S_IFCHR |
| 1701 |
case CHRTYPE: |
| 1702 |
current_stat_info.stat.st_mode |= S_IFCHR; |
| 1703 |
extractor = extract_node; |
| 1704 |
break; |
| 1705 |
#endif |
| 1706 |
|
| 1707 |
#if S_IFBLK |
| 1708 |
case BLKTYPE: |
| 1709 |
current_stat_info.stat.st_mode |= S_IFBLK; |
| 1710 |
extractor = extract_node; |
| 1711 |
break; |
| 1712 |
#endif |
| 1713 |
|
| 1714 |
#if HAVE_MKFIFO || defined mkfifo |
| 1715 |
case FIFOTYPE: |
| 1716 |
extractor = extract_fifo; |
| 1717 |
break; |
| 1718 |
#endif |
| 1719 |
|
| 1720 |
case DIRTYPE: |
| 1721 |
case GNUTYPE_DUMPDIR: |
| 1722 |
extractor = extract_dir; |
| 1723 |
if (current_stat_info.is_dumpdir) |
| 1724 |
delay_directory_restore_option = true; |
| 1725 |
break; |
| 1726 |
|
| 1727 |
case GNUTYPE_VOLHDR: |
| 1728 |
return false; |
| 1729 |
|
| 1730 |
case GNUTYPE_MULTIVOL: |
| 1731 |
ERROR ((0, 0, |
| 1732 |
_("%s: Cannot extract -- file is continued from another volume"), |
| 1733 |
quotearg_colon (current_stat_info.file_name))); |
| 1734 |
return false; |
| 1735 |
|
| 1736 |
case GNUTYPE_LONGNAME: |
| 1737 |
case GNUTYPE_LONGLINK: |
| 1738 |
ERROR ((0, 0, _("Unexpected long name header"))); |
| 1739 |
return false; |
| 1740 |
|
| 1741 |
default: |
| 1742 |
WARNOPT (WARN_UNKNOWN_CAST, |
| 1743 |
(0, 0, |
| 1744 |
_("%s: Unknown file type '%c', extracted as normal file"), |
| 1745 |
quotearg_colon (file_name), typeflag)); |
| 1746 |
extractor = extract_file; |
| 1747 |
} |
| 1748 |
|
| 1749 |
if (EXTRACT_OVER_PIPE) |
| 1750 |
{ |
| 1751 |
if (extractor != extract_file) |
| 1752 |
return false; |
| 1753 |
} |
| 1754 |
else |
| 1755 |
{ |
| 1756 |
switch (old_files_option) |
| 1757 |
{ |
| 1758 |
case UNLINK_FIRST_OLD_FILES: |
| 1759 |
if (!remove_any_file (file_name, |
| 1760 |
recursive_unlink_option |
| 1761 |
? RECURSIVE_REMOVE_OPTION |
| 1762 |
: ORDINARY_REMOVE_OPTION) |
| 1763 |
&& errno && errno != ENOENT) |
| 1764 |
{ |
| 1765 |
unlink_error (file_name); |
| 1766 |
return false; |
| 1767 |
} |
| 1768 |
break; |
| 1769 |
|
| 1770 |
case KEEP_NEWER_FILES: |
| 1771 |
if (file_newer_p (file_name, 0, ¤t_stat_info)) |
| 1772 |
{ |
| 1773 |
WARNOPT (WARN_IGNORE_NEWER, |
| 1774 |
(0, 0, _("Current %s is newer or same age"), |
| 1775 |
quote (file_name))); |
| 1776 |
return false; |
| 1777 |
} |
| 1778 |
break; |
| 1779 |
|
| 1780 |
default: |
| 1781 |
break; |
| 1782 |
} |
| 1783 |
} |
| 1784 |
*fun = extractor; |
| 1785 |
|
| 1786 |
return true; |
| 1787 |
} |
| 1788 |
|
| 1789 |
/* Extract a file from the archive. */ |
| 1790 |
void |
| 1791 |
extract_archive (void) |
| 1792 |
{ |
| 1793 |
char typeflag; |
| 1794 |
tar_extractor_t fun; |
| 1795 |
bool skip_dotdot_name; |
| 1796 |
|
| 1797 |
fatal_exit_hook = extract_finish; |
| 1798 |
|
| 1799 |
set_next_block_after (current_header); |
| 1800 |
|
| 1801 |
skip_dotdot_name = (!absolute_names_option |
| 1802 |
&& contains_dot_dot (current_stat_info.orig_file_name)); |
| 1803 |
if (skip_dotdot_name) |
| 1804 |
ERROR ((0, 0, _("%s: Member name contains '..'"), |
| 1805 |
quotearg_colon (current_stat_info.orig_file_name))); |
| 1806 |
|
| 1807 |
if (!current_stat_info.file_name[0] |
| 1808 |
|| skip_dotdot_name |
| 1809 |
|| (interactive_option |
| 1810 |
&& !confirm ("extract", current_stat_info.file_name))) |
| 1811 |
{ |
| 1812 |
skip_member (); |
| 1813 |
return; |
| 1814 |
} |
| 1815 |
|
| 1816 |
/* Print the block from current_header and current_stat. */ |
| 1817 |
if (verbose_option) |
| 1818 |
print_header (¤t_stat_info, current_header, -1); |
| 1819 |
|
| 1820 |
/* Restore stats for all non-ancestor directories, unless |
| 1821 |
it is an incremental archive. |
| 1822 |
(see NOTICE in the comment to delay_set_stat above) */ |
| 1823 |
if (!delay_directory_restore_option) |
| 1824 |
{ |
| 1825 |
int dir = chdir_current; |
| 1826 |
apply_nonancestor_delayed_set_stat (current_stat_info.file_name, 0); |
| 1827 |
chdir_do (dir); |
| 1828 |
} |
| 1829 |
|
| 1830 |
/* Take a safety backup of a previously existing file. */ |
| 1831 |
|
| 1832 |
if (backup_option) |
| 1833 |
if (!maybe_backup_file (current_stat_info.file_name, 0)) |
| 1834 |
{ |
| 1835 |
int e = errno; |
| 1836 |
ERROR ((0, e, _("%s: Was unable to backup this file"), |
| 1837 |
quotearg_colon (current_stat_info.file_name))); |
| 1838 |
skip_member (); |
| 1839 |
return; |
| 1840 |
} |
| 1841 |
|
| 1842 |
/* Extract the archive entry according to its type. */ |
| 1843 |
/* KLUDGE */ |
| 1844 |
typeflag = sparse_member_p (¤t_stat_info) ? |
| 1845 |
GNUTYPE_SPARSE : current_header->header.typeflag; |
| 1846 |
|
| 1847 |
if (prepare_to_extract (current_stat_info.file_name, typeflag, &fun)) |
| 1848 |
{ |
| 1849 |
if (fun (current_stat_info.file_name, typeflag) == 0) |
| 1850 |
return; |
| 1851 |
} |
| 1852 |
else |
| 1853 |
skip_member (); |
| 1854 |
|
| 1855 |
if (backup_option) |
| 1856 |
undo_last_backup (); |
| 1857 |
} |
| 1858 |
|
| 1859 |
/* Extract the link DS whose final extraction was delayed. */ |
| 1860 |
static void |
| 1861 |
apply_delayed_link (struct delayed_link *ds) |
| 1862 |
{ |
| 1863 |
struct string_list *sources = ds->sources; |
| 1864 |
char const *valid_source = 0; |
| 1865 |
|
| 1866 |
chdir_do (ds->change_dir); |
| 1867 |
|
| 1868 |
for (sources = ds->sources; sources; sources = sources->next) |
| 1869 |
{ |
| 1870 |
char const *source = sources->string; |
| 1871 |
struct stat st; |
| 1872 |
|
| 1873 |
/* Make sure the placeholder file is still there. If not, |
| 1874 |
don't create a link, as the placeholder was probably |
| 1875 |
removed by a later extraction. */ |
| 1876 |
if (fstatat (chdir_fd, source, &st, AT_SYMLINK_NOFOLLOW) == 0 |
| 1877 |
&& st.st_dev == ds->dev |
| 1878 |
&& st.st_ino == ds->ino |
| 1879 |
&& BIRTHTIME_EQ (get_stat_birthtime (&st), ds->birthtime)) |
| 1880 |
{ |
| 1881 |
/* Unlink the placeholder, then create a hard link if possible, |
| 1882 |
a symbolic link otherwise. */ |
| 1883 |
if (unlinkat (chdir_fd, source, 0) != 0) |
| 1884 |
unlink_error (source); |
| 1885 |
else if (valid_source |
| 1886 |
&& (linkat (chdir_fd, valid_source, chdir_fd, source, 0) |
| 1887 |
== 0)) |
| 1888 |
; |
| 1889 |
else if (!ds->is_symlink) |
| 1890 |
{ |
| 1891 |
if (linkat (chdir_fd, ds->target, chdir_fd, source, 0) != 0) |
| 1892 |
link_error (ds->target, source); |
| 1893 |
} |
| 1894 |
else if (symlinkat (ds->target, chdir_fd, source) != 0) |
| 1895 |
symlink_error (ds->target, source); |
| 1896 |
else |
| 1897 |
{ |
| 1898 |
struct tar_stat_info st1; |
| 1899 |
st1.stat.st_mode = ds->mode; |
| 1900 |
st1.stat.st_uid = ds->uid; |
| 1901 |
st1.stat.st_gid = ds->gid; |
| 1902 |
st1.atime = ds->atime; |
| 1903 |
st1.mtime = ds->mtime; |
| 1904 |
st1.cntx_name = ds->cntx_name; |
| 1905 |
st1.acls_a_ptr = ds->acls_a_ptr; |
| 1906 |
st1.acls_a_len = ds->acls_a_len; |
| 1907 |
st1.acls_d_ptr = ds->acls_d_ptr; |
| 1908 |
st1.acls_d_len = ds->acls_d_len; |
| 1909 |
st1.xattr_map = ds->xattr_map; |
| 1910 |
set_stat (source, &st1, -1, 0, 0, SYMTYPE, |
| 1911 |
false, AT_SYMLINK_NOFOLLOW); |
| 1912 |
valid_source = source; |
| 1913 |
} |
| 1914 |
} |
| 1915 |
} |
| 1916 |
|
| 1917 |
/* There is little point to freeing, as we are about to exit, |
| 1918 |
and freeing is more likely to cause than cure trouble. */ |
| 1919 |
if (false) |
| 1920 |
{ |
| 1921 |
for (sources = ds->sources; sources; ) |
| 1922 |
{ |
| 1923 |
struct string_list *next = sources->next; |
| 1924 |
free (sources); |
| 1925 |
sources = next; |
| 1926 |
} |
| 1927 |
|
| 1928 |
xattr_map_free (&ds->xattr_map); |
| 1929 |
free (ds->cntx_name); |
| 1930 |
} |
| 1931 |
} |
| 1932 |
|
| 1933 |
/* Extract the links whose final extraction were delayed. */ |
| 1934 |
static void |
| 1935 |
apply_delayed_links (void) |
| 1936 |
{ |
| 1937 |
for (struct delayed_link *ds = delayed_link_head; ds; ds = ds->next) |
| 1938 |
apply_delayed_link (ds); |
| 1939 |
|
| 1940 |
if (false) |
| 1941 |
{ |
| 1942 |
/* There is little point to freeing, as we are about to exit, |
| 1943 |
and freeing is more likely to cause than cure trouble. |
| 1944 |
Also, the above code has not bothered to free the list |
| 1945 |
in delayed_link_head. */ |
| 1946 |
hash_free (delayed_link_table); |
| 1947 |
delayed_link_table = NULL; |
| 1948 |
} |
| 1949 |
} |
| 1950 |
|
| 1951 |
/* Finish the extraction of an archive. */ |
| 1952 |
void |
| 1953 |
extract_finish (void) |
| 1954 |
{ |
| 1955 |
/* First, fix the status of ordinary directories that need fixing. */ |
| 1956 |
apply_nonancestor_delayed_set_stat ("", 0); |
| 1957 |
|
| 1958 |
/* Then, apply delayed links, so that they don't affect delayed |
| 1959 |
directory status-setting for ordinary directories. */ |
| 1960 |
apply_delayed_links (); |
| 1961 |
|
| 1962 |
/* Finally, fix the status of directories that are ancestors |
| 1963 |
of delayed links. */ |
| 1964 |
apply_nonancestor_delayed_set_stat ("", 1); |
| 1965 |
} |
| 1966 |
|
| 1967 |
bool |
| 1968 |
rename_directory (char *src, char *dst) |
| 1969 |
{ |
| 1970 |
if (renameat (chdir_fd, src, chdir_fd, dst) == 0) |
| 1971 |
fixup_delayed_set_stat (src, dst); |
| 1972 |
else |
| 1973 |
{ |
| 1974 |
int e = errno; |
| 1975 |
|
| 1976 |
switch (e) |
| 1977 |
{ |
| 1978 |
case ENOENT: |
| 1979 |
if (make_directories (dst) == 0) |
| 1980 |
{ |
| 1981 |
if (renameat (chdir_fd, src, chdir_fd, dst) == 0) |
| 1982 |
return true; |
| 1983 |
e = errno; |
| 1984 |
} |
| 1985 |
break; |
| 1986 |
|
| 1987 |
case EXDEV: |
| 1988 |
/* FIXME: Fall back to recursive copying */ |
| 1989 |
|
| 1990 |
default: |
| 1991 |
break; |
| 1992 |
} |
| 1993 |
|
| 1994 |
ERROR ((0, e, _("Cannot rename %s to %s"), |
| 1995 |
quote_n (0, src), |
| 1996 |
quote_n (1, dst))); |
| 1997 |
return false; |
| 1998 |
} |
| 1999 |
return true; |
| 2000 |
} |