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