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