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