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