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