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