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