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