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