]> Dogcows Code - chaz/tar/blob - src/extract.c
Updated invocations of safer_name_suffix
[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 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 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 #include "system.h"
23 #include <quotearg.h>
24 #include <errno.h>
25
26 #if HAVE_UTIME_H
27 # include <utime.h>
28 #else
29 struct utimbuf
30 {
31 long actime;
32 long modtime;
33 };
34 #endif
35
36 #include "common.h"
37
38 bool we_are_root; /* true if our effective uid == 0 */
39 static mode_t newdir_umask; /* umask when creating new directories */
40 static mode_t current_umask; /* current umask (which is set to 0 if -p) */
41
42 /* Status of the permissions of a file that we are extracting. */
43 enum permstatus
44 {
45 /* This file may have existed already; its permissions are unknown. */
46 UNKNOWN_PERMSTATUS,
47
48 /* This file was created using the permissions from the archive. */
49 ARCHIVED_PERMSTATUS,
50
51 /* This is an intermediate directory; the archive did not specify
52 its permissions. */
53 INTERDIR_PERMSTATUS
54 };
55
56 /* List of directories whose statuses we need to extract after we've
57 finished extracting their subsidiary files. If you consider each
58 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
59 represents an element where AFTER_SYMLINKS is nonzero and [^D]
60 represents an element where AFTER_SYMLINKS is zero, then the head
61 of the subsequence has the longest name, and each non-head element
62 in the prefix is an ancestor (in the directory hierarchy) of the
63 preceding element. */
64
65 struct delayed_set_stat
66 {
67 struct delayed_set_stat *next;
68 struct stat stat_info;
69 size_t file_name_len;
70 mode_t invert_permissions;
71 enum permstatus permstatus;
72 bool after_symlinks;
73 char file_name[1];
74 };
75
76 static struct delayed_set_stat *delayed_set_stat_head;
77
78 /* List of symbolic links whose creation we have delayed. */
79 struct delayed_symlink
80 {
81 /* The next delayed symbolic link in the list. */
82 struct delayed_symlink *next;
83
84 /* The device, inode number and last-modified time of the placeholder. */
85 dev_t dev;
86 ino_t ino;
87 time_t mtime;
88
89 /* The desired owner and group of the symbolic link. */
90 uid_t uid;
91 gid_t gid;
92
93 /* A list of sources for this symlink. The sources are all to be
94 hard-linked together. */
95 struct string_list *sources;
96
97 /* The desired target of the desired link. */
98 char target[1];
99 };
100
101 static struct delayed_symlink *delayed_symlink_head;
102
103 struct string_list
104 {
105 struct string_list *next;
106 char string[1];
107 };
108
109 /* Set up to extract files. */
110 void
111 extr_init (void)
112 {
113 we_are_root = geteuid () == 0;
114 same_permissions_option += we_are_root;
115 same_owner_option += we_are_root;
116 xalloc_fail_func = extract_finish;
117
118 /* Option -p clears the kernel umask, so it does not affect proper
119 restoration of file permissions. New intermediate directories will
120 comply with umask at start of program. */
121
122 newdir_umask = umask (0);
123 if (0 < same_permissions_option)
124 current_umask = 0;
125 else
126 {
127 umask (newdir_umask); /* restore the kernel umask */
128 current_umask = newdir_umask;
129 }
130 }
131
132 /* If restoring permissions, restore the mode for FILE_NAME from
133 information given in *STAT_INFO (where *CUR_INFO gives
134 the current status if CUR_INFO is nonzero); otherwise invert the
135 INVERT_PERMISSIONS bits from the file's current permissions.
136 PERMSTATUS specifies the status of the file's permissions.
137 TYPEFLAG specifies the type of the file. */
138 static void
139 set_mode (char const *file_name,
140 struct stat const *stat_info,
141 struct stat const *cur_info,
142 mode_t invert_permissions, enum permstatus permstatus,
143 char typeflag)
144 {
145 mode_t mode;
146
147 if (0 < same_permissions_option
148 && permstatus != INTERDIR_PERMSTATUS)
149 {
150 mode = stat_info->st_mode;
151
152 /* If we created the file and it has a usual mode, then its mode
153 is normally set correctly already. But on many hosts, some
154 directories inherit the setgid bits from their parents, so we
155 we must set directories' modes explicitly. */
156 if (permstatus == ARCHIVED_PERMSTATUS
157 && ! (mode & ~ MODE_RWX)
158 && typeflag != DIRTYPE
159 && typeflag != GNUTYPE_DUMPDIR)
160 return;
161 }
162 else if (! invert_permissions)
163 return;
164 else
165 {
166 /* We must inspect a directory's current permissions, since the
167 directory may have inherited its setgid bit from its parent.
168
169 INVERT_PERMISSIONS happens to be nonzero only for directories
170 that we created, so there's no point optimizing this code for
171 other cases. */
172 struct stat st;
173 if (! cur_info)
174 {
175 if (stat (file_name, &st) != 0)
176 {
177 stat_error (file_name);
178 return;
179 }
180 cur_info = &st;
181 }
182 mode = cur_info->st_mode ^ invert_permissions;
183 }
184
185 if (chmod (file_name, mode) != 0)
186 chmod_error_details (file_name, mode);
187 }
188
189 /* Check time after successfully setting FILE_NAME's time stamp to T. */
190 static void
191 check_time (char const *file_name, time_t t)
192 {
193 time_t now;
194 if (t <= 0)
195 WARN ((0, 0, _("%s: implausibly old time stamp %s"),
196 file_name, tartime (t)));
197 else if (start_time < t && (now = time (0)) < t)
198 WARN ((0, 0, _("%s: time stamp %s is %lu s in the future"),
199 file_name, tartime (t), (unsigned long) (t - now)));
200 }
201
202 /* Restore stat attributes (owner, group, mode and times) for
203 FILE_NAME, using information given in *STAT_INFO.
204 If CUR_INFO is nonzero, *CUR_INFO is the
205 file's currernt status.
206 If not restoring permissions, invert the
207 INVERT_PERMISSIONS bits from the file's current permissions.
208 PERMSTATUS specifies the status of the file's permissions.
209 TYPEFLAG specifies the type of the file. */
210
211 /* FIXME: About proper restoration of symbolic link attributes, we still do
212 not have it right. Pretesters' reports tell us we need further study and
213 probably more configuration. For now, just use lchown if it exists, and
214 punt for the rest. Sigh! */
215
216 static void
217 set_stat (char const *file_name,
218 struct stat const *stat_info,
219 struct stat const *cur_info,
220 mode_t invert_permissions, enum permstatus permstatus,
221 char typeflag)
222 {
223 struct utimbuf utimbuf;
224
225 if (typeflag != SYMTYPE)
226 {
227 /* We do the utime before the chmod because some versions of utime are
228 broken and trash the modes of the file. */
229
230 if (! touch_option && permstatus != INTERDIR_PERMSTATUS)
231 {
232 /* We set the accessed time to `now', which is really the time we
233 started extracting files, unless incremental_option is used, in
234 which case .st_atime is used. */
235
236 /* FIXME: incremental_option should set ctime too, but how? */
237
238 if (incremental_option)
239 utimbuf.actime = stat_info->st_atime;
240 else
241 utimbuf.actime = start_time;
242
243 utimbuf.modtime = stat_info->st_mtime;
244
245 if (utime (file_name, &utimbuf) < 0)
246 utime_error (file_name);
247 else
248 {
249 check_time (file_name, utimbuf.actime);
250 check_time (file_name, utimbuf.modtime);
251 }
252 }
253
254 /* Some systems allow non-root users to give files away. Once this
255 done, it is not possible anymore to change file permissions, so we
256 have to set permissions prior to possibly giving files away. */
257
258 set_mode (file_name, stat_info, cur_info,
259 invert_permissions, permstatus, typeflag);
260 }
261
262 if (0 < same_owner_option && permstatus != INTERDIR_PERMSTATUS)
263 {
264 /* When lchown exists, it should be used to change the attributes of
265 the symbolic link itself. In this case, a mere chown would change
266 the attributes of the file the symbolic link is pointing to, and
267 should be avoided. */
268
269 if (typeflag == SYMTYPE)
270 {
271 #if HAVE_LCHOWN
272 if (lchown (file_name, stat_info->st_uid, stat_info->st_gid) < 0)
273 chown_error_details (file_name,
274 stat_info->st_uid, stat_info->st_gid);
275 #endif
276 }
277 else
278 {
279 if (chown (file_name, stat_info->st_uid, stat_info->st_gid) < 0)
280 chown_error_details (file_name,
281 stat_info->st_uid, stat_info->st_gid);
282
283 /* On a few systems, and in particular, those allowing to give files
284 away, changing the owner or group destroys the suid or sgid bits.
285 So let's attempt setting these bits once more. */
286 if (stat_info->st_mode & (S_ISUID | S_ISGID | S_ISVTX))
287 set_mode (file_name, stat_info, 0,
288 invert_permissions, permstatus, typeflag);
289 }
290 }
291 }
292
293 /* Remember to restore stat attributes (owner, group, mode and times)
294 for the directory FILE_NAME, using information given in *STAT_INFO,
295 once we stop extracting files into that directory.
296 If not restoring permissions, remember to invert the
297 INVERT_PERMISSIONS bits from the file's current permissions.
298 PERMSTATUS specifies the status of the file's permissions. */
299 static void
300 delay_set_stat (char const *file_name, struct stat const *stat_info,
301 mode_t invert_permissions, enum permstatus permstatus)
302 {
303 size_t file_name_len = strlen (file_name);
304 struct delayed_set_stat *data =
305 xmalloc (offsetof (struct delayed_set_stat, file_name)
306 + file_name_len + 1);
307 data->file_name_len = file_name_len;
308 strcpy (data->file_name, file_name);
309 data->invert_permissions = invert_permissions;
310 data->permstatus = permstatus;
311 data->after_symlinks = 0;
312 data->stat_info = *stat_info;
313 data->next = delayed_set_stat_head;
314 delayed_set_stat_head = data;
315 }
316
317 /* Update the delayed_set_stat info for an intermediate directory
318 created on the path to DIR_NAME. The intermediate directory turned
319 out to be the same as this directory, e.g. due to ".." or symbolic
320 links. *DIR_STAT_INFO is the status of the directory. */
321 static void
322 repair_delayed_set_stat (char const *dir_name,
323 struct stat const *dir_stat_info)
324 {
325 struct delayed_set_stat *data;
326 for (data = delayed_set_stat_head; data; data = data->next)
327 {
328 struct stat st;
329 if (stat (data->file_name, &st) != 0)
330 {
331 stat_error (data->file_name);
332 return;
333 }
334
335 if (st.st_dev == dir_stat_info->st_dev
336 && st.st_ino == dir_stat_info->st_ino)
337 {
338 data->stat_info = current_stat_info.stat;
339 data->invert_permissions =
340 (MODE_RWX & (current_stat_info.stat.st_mode ^ st.st_mode));
341 data->permstatus = ARCHIVED_PERMSTATUS;
342 return;
343 }
344 }
345
346 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
347 quotearg_colon (dir_name)));
348 }
349
350 /* After a file/link/symlink/directory creation has failed, see if
351 it's because some required directory was not present, and if so,
352 create all required directories. Return non-zero if a directory
353 was created. */
354 static int
355 make_directories (char *file_name)
356 {
357 char *cursor0 = file_name + FILESYSTEM_PREFIX_LEN (file_name);
358 char *cursor; /* points into path */
359 int did_something = 0; /* did we do anything yet? */
360 int mode;
361 int invert_permissions;
362 int status;
363
364
365 for (cursor = cursor0; *cursor; cursor++)
366 {
367 if (! ISSLASH (*cursor))
368 continue;
369
370 /* Avoid mkdir of empty string, if leading or double '/'. */
371
372 if (cursor == cursor0 || ISSLASH (cursor[-1]))
373 continue;
374
375 /* Avoid mkdir where last part of path is "." or "..". */
376
377 if (cursor[-1] == '.'
378 && (cursor == cursor0 + 1 || ISSLASH (cursor[-2])
379 || (cursor[-2] == '.'
380 && (cursor == cursor0 + 2 || ISSLASH (cursor[-3])))))
381 continue;
382
383 *cursor = '\0'; /* truncate the path there */
384 mode = MODE_RWX & ~ newdir_umask;
385 invert_permissions = we_are_root ? 0 : MODE_WXUSR & ~ mode;
386 status = mkdir (file_name, mode ^ invert_permissions);
387
388 if (status == 0)
389 {
390 /* Create a struct delayed_set_stat even if
391 invert_permissions is zero, because
392 repair_delayed_set_stat may need to update the struct. */
393 delay_set_stat (file_name,
394 &current_stat_info.stat /* ignored */,
395 invert_permissions, INTERDIR_PERMSTATUS);
396
397 print_for_mkdir (file_name, cursor - file_name, mode);
398 did_something = 1;
399
400 *cursor = '/';
401 continue;
402 }
403
404 *cursor = '/';
405
406 if (errno == EEXIST)
407 continue; /* Directory already exists. */
408 else if ((errno == ENOSYS /* Automounted dirs on Solaris return
409 this. Reported by Warren Hyde
410 <Warren.Hyde@motorola.com> */
411 || ERRNO_IS_EACCES) /* Turbo C mkdir gives a funny errno. */
412 && access (file_name, W_OK) == 0)
413 continue;
414
415 /* Some other error in the mkdir. We return to the caller. */
416 break;
417 }
418
419 return did_something; /* tell them to retry if we made one */
420 }
421
422 /* Prepare to extract a file.
423 Return zero if extraction should not proceed. */
424
425 static int
426 prepare_to_extract (char const *file_name, bool directory)
427 {
428 if (to_stdout_option)
429 return 0;
430
431 if (old_files_option == UNLINK_FIRST_OLD_FILES
432 && !remove_any_file (file_name, recursive_unlink_option)
433 && errno && errno != ENOENT)
434 {
435 unlink_error (file_name);
436 return 0;
437 }
438
439 return 1;
440 }
441
442 /* Attempt repairing what went wrong with the extraction. Delete an
443 already existing file or create missing intermediate directories.
444 Return nonzero if we somewhat increased our chances at a successful
445 extraction. errno is properly restored on zero return. */
446 static int
447 maybe_recoverable (char *file_name, int *interdir_made)
448 {
449 if (*interdir_made)
450 return 0;
451
452 switch (errno)
453 {
454 case EEXIST:
455 /* Remove an old file, if the options allow this. */
456
457 switch (old_files_option)
458 {
459 default:
460 return 0;
461
462 case DEFAULT_OLD_FILES:
463 case NO_OVERWRITE_DIR_OLD_FILES:
464 case OVERWRITE_OLD_FILES:
465 {
466 int r = remove_any_file (file_name, 0);
467 errno = EEXIST;
468 return r;
469 }
470 }
471
472 case ENOENT:
473 /* Attempt creating missing intermediate directories. */
474 if (! make_directories (file_name))
475 {
476 errno = ENOENT;
477 return 0;
478 }
479 *interdir_made = 1;
480 return 1;
481
482 default:
483 /* Just say we can't do anything about it... */
484
485 return 0;
486 }
487 }
488
489 /* Fix the statuses of all directories whose statuses need fixing, and
490 which are not ancestors of FILE_NAME. If AFTER_SYMLINKS is
491 nonzero, do this for all such directories; otherwise, stop at the
492 first directory that is marked to be fixed up only after delayed
493 symlinks are applied. */
494 static void
495 apply_nonancestor_delayed_set_stat (char const *file_name, bool after_symlinks)
496 {
497 size_t file_name_len = strlen (file_name);
498 bool check_for_renamed_directories = 0;
499
500 while (delayed_set_stat_head)
501 {
502 struct delayed_set_stat *data = delayed_set_stat_head;
503 bool skip_this_one = 0;
504 struct stat st;
505 struct stat const *cur_info = 0;
506
507 check_for_renamed_directories |= data->after_symlinks;
508
509 if (after_symlinks < data->after_symlinks
510 || (data->file_name_len < file_name_len
511 && file_name[data->file_name_len]
512 && (ISSLASH (file_name[data->file_name_len])
513 || ISSLASH (file_name[data->file_name_len - 1]))
514 && memcmp (file_name, data->file_name, data->file_name_len) == 0))
515 break;
516
517 if (check_for_renamed_directories)
518 {
519 cur_info = &st;
520 if (stat (data->file_name, &st) != 0)
521 {
522 stat_error (data->file_name);
523 skip_this_one = 1;
524 }
525 else if (! (st.st_dev == data->stat_info.st_dev
526 && (st.st_ino == data->stat_info.st_ino)))
527 {
528 ERROR ((0, 0,
529 _("%s: Directory renamed before its status could be extracted"),
530 quotearg_colon (data->file_name)));
531 skip_this_one = 1;
532 }
533 }
534
535 if (! skip_this_one)
536 set_stat (data->file_name, &data->stat_info, cur_info,
537 data->invert_permissions, data->permstatus, DIRTYPE);
538
539 delayed_set_stat_head = data->next;
540 free (data);
541 }
542 }
543
544 /* Extract a file from the archive. */
545 void
546 extract_archive (void)
547 {
548 union block *data_block;
549 int fd;
550 int status;
551 size_t count;
552 size_t written;
553 int openflag;
554 mode_t mode;
555 off_t size;
556 off_t file_size;
557 int interdir_made = 0;
558 char typeflag;
559 char *file_name;
560
561 set_next_block_after (current_header);
562 decode_header (current_header, &current_stat_info, &current_format, 1);
563
564 if (interactive_option && !confirm ("extract", current_stat_info.file_name))
565 {
566 skip_member ();
567 return;
568 }
569
570 /* Print the block from current_header and current_stat. */
571
572 if (verbose_option)
573 print_header (&current_stat_info, -1);
574
575 file_name = safer_name_suffix (current_stat_info.file_name, false);
576 if (strip_path_elements)
577 {
578 size_t prefix_len = stripped_prefix_len (file_name, strip_path_elements);
579 if (prefix_len == (size_t) -1)
580 {
581 skip_member ();
582 return;
583 }
584 file_name += prefix_len;
585 }
586
587 apply_nonancestor_delayed_set_stat (file_name, 0);
588
589 /* Take a safety backup of a previously existing file. */
590
591 if (backup_option && !to_stdout_option)
592 if (!maybe_backup_file (file_name, 0))
593 {
594 int e = errno;
595 ERROR ((0, e, _("%s: Was unable to backup this file"),
596 quotearg_colon (file_name)));
597 skip_member ();
598 return;
599 }
600
601 /* Extract the archive entry according to its type. */
602
603 typeflag = current_header->header.typeflag;
604 /*KLUDGE */
605 if (current_format == POSIX_FORMAT
606 && current_stat_info.archive_file_size != current_stat_info.stat.st_size)
607 typeflag = GNUTYPE_SPARSE;
608
609 switch (typeflag)
610 {
611 case GNUTYPE_SPARSE:
612 /* Fall through. */
613
614 case AREGTYPE:
615 case REGTYPE:
616 case CONTTYPE:
617
618 /* Appears to be a file. But BSD tar uses the convention that a slash
619 suffix means a directory. */
620
621 if (current_stat_info.had_trailing_slash)
622 goto really_dir;
623
624 /* FIXME: deal with protection issues. */
625
626 again_file:
627 openflag = (O_WRONLY | O_BINARY | O_CREAT
628 | (old_files_option == OVERWRITE_OLD_FILES
629 ? O_TRUNC
630 : O_EXCL));
631 mode = current_stat_info.stat.st_mode & MODE_RWX & ~ current_umask;
632
633 if (to_stdout_option)
634 {
635 fd = STDOUT_FILENO;
636 goto extract_file;
637 }
638
639 if (! prepare_to_extract (file_name, 0))
640 {
641 skip_member ();
642 if (backup_option)
643 undo_last_backup ();
644 break;
645 }
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 if (fd < 0)
672 {
673 if (maybe_recoverable (file_name, &interdir_made))
674 goto again_file;
675
676 open_error (file_name);
677 skip_member ();
678 if (backup_option)
679 undo_last_backup ();
680 break;
681 }
682
683 extract_file:
684 if (typeflag == GNUTYPE_SPARSE)
685 {
686 sparse_extract_file (fd, &current_stat_info, &size);
687 }
688 else
689 for (size = current_stat_info.stat.st_size; size > 0; )
690 {
691 if (multi_volume_option)
692 {
693 assign_string (&save_name, current_stat_info.file_name);
694 save_totsize = current_stat_info.stat.st_size;
695 save_sizeleft = size;
696 }
697
698 /* Locate data, determine max length writeable, write it,
699 block that we have used the data, then check if the write
700 worked. */
701
702 data_block = find_next_block ();
703 if (! data_block)
704 {
705 ERROR ((0, 0, _("Unexpected EOF in archive")));
706 break; /* FIXME: What happens, then? */
707 }
708
709 written = available_space_after (data_block);
710
711 if (written > size)
712 written = size;
713 errno = 0;
714 count = full_write (fd, data_block->buffer, written);
715 size -= count;
716
717 set_next_block_after ((union block *)
718 (data_block->buffer + written - 1));
719 if (count != written)
720 {
721 write_error_details (file_name, count, written);
722 break;
723 }
724 }
725
726 skip_file (size);
727
728 if (multi_volume_option)
729 assign_string (&save_name, 0);
730
731 /* If writing to stdout, don't try to do anything to the filename;
732 it doesn't exist, or we don't want to touch it anyway. */
733
734 if (to_stdout_option)
735 break;
736
737 status = close (fd);
738 if (status < 0)
739 {
740 close_error (file_name);
741 if (backup_option)
742 undo_last_backup ();
743 }
744
745 set_stat (file_name, &current_stat_info.stat, 0, 0,
746 (old_files_option == OVERWRITE_OLD_FILES
747 ? UNKNOWN_PERMSTATUS
748 : ARCHIVED_PERMSTATUS),
749 typeflag);
750 break;
751
752 case SYMTYPE:
753 #ifdef HAVE_SYMLINK
754 if (! prepare_to_extract (file_name, 0))
755 break;
756
757 if (absolute_names_option
758 || ! (ISSLASH (current_stat_info.link_name
759 [FILESYSTEM_PREFIX_LEN (current_stat_info.link_name)])
760 || contains_dot_dot (current_stat_info.link_name)))
761 {
762 while (status = symlink (current_stat_info.link_name, file_name),
763 status != 0)
764 if (!maybe_recoverable (file_name, &interdir_made))
765 break;
766
767 if (status == 0)
768 set_stat (file_name, &current_stat_info.stat, 0, 0, 0, SYMTYPE);
769 else
770 symlink_error (current_stat_info.link_name, file_name);
771 }
772 else
773 {
774 /* This symbolic link is potentially dangerous. Don't
775 create it now; instead, create a placeholder file, which
776 will be replaced after other extraction is done. */
777 struct stat st;
778
779 while (fd = open (file_name, O_WRONLY | O_CREAT | O_EXCL, 0),
780 fd < 0)
781 if (! maybe_recoverable (file_name, &interdir_made))
782 break;
783
784 status = -1;
785 if (fd < 0)
786 open_error (file_name);
787 else if (fstat (fd, &st) != 0)
788 {
789 stat_error (file_name);
790 close (fd);
791 }
792 else if (close (fd) != 0)
793 close_error (file_name);
794 else
795 {
796 struct delayed_set_stat *h;
797 struct delayed_symlink *p =
798 xmalloc (offsetof (struct delayed_symlink, target)
799 + strlen (current_stat_info.link_name) + 1);
800 p->next = delayed_symlink_head;
801 delayed_symlink_head = p;
802 p->dev = st.st_dev;
803 p->ino = st.st_ino;
804 p->mtime = st.st_mtime;
805 p->uid = current_stat_info.stat.st_uid;
806 p->gid = current_stat_info.stat.st_gid;
807 p->sources = xmalloc (offsetof (struct string_list, string)
808 + strlen (file_name) + 1);
809 p->sources->next = 0;
810 strcpy (p->sources->string, file_name);
811 strcpy (p->target, current_stat_info.link_name);
812
813 h = delayed_set_stat_head;
814 if (h && ! h->after_symlinks
815 && strncmp (file_name, h->file_name, h->file_name_len) == 0
816 && ISSLASH (file_name[h->file_name_len])
817 && (base_name (file_name)
818 == file_name + h->file_name_len + 1))
819 {
820 do
821 {
822 h->after_symlinks = 1;
823
824 if (stat (h->file_name, &st) != 0)
825 stat_error (h->file_name);
826 else
827 {
828 h->stat_info.st_dev = st.st_dev;
829 h->stat_info.st_ino = st.st_ino;
830 }
831 }
832 while ((h = h->next) && ! h->after_symlinks);
833 }
834
835 status = 0;
836 }
837 }
838
839 if (status != 0 && backup_option)
840 undo_last_backup ();
841 break;
842
843 #else
844 {
845 static int warned_once;
846
847 if (!warned_once)
848 {
849 warned_once = 1;
850 WARN ((0, 0,
851 _("Attempting extraction of symbolic links as hard links")));
852 }
853 }
854 typeflag = LNKTYPE;
855 /* Fall through. */
856 #endif
857
858 case LNKTYPE:
859 if (! prepare_to_extract (file_name, 0))
860 break;
861
862 again_link:
863 {
864 char const *link_name = safer_name_suffix (current_stat_info.link_name,
865 true);
866 struct stat st1, st2;
867 int e;
868
869 /* MSDOS does not implement links. However, djgpp's link() actually
870 copies the file. */
871 status = link (link_name, file_name);
872
873 if (status == 0)
874 {
875 struct delayed_symlink *ds = delayed_symlink_head;
876 if (ds && stat (link_name, &st1) == 0)
877 for (; ds; ds = ds->next)
878 if (ds->dev == st1.st_dev
879 && ds->ino == st1.st_ino
880 && ds->mtime == st1.st_mtime)
881 {
882 struct string_list *p =
883 xmalloc (offsetof (struct string_list, string)
884 + strlen (file_name) + 1);
885 strcpy (p->string, file_name);
886 p->next = ds->sources;
887 ds->sources = p;
888 break;
889 }
890 break;
891 }
892 if (maybe_recoverable (file_name, &interdir_made))
893 goto again_link;
894
895 if (incremental_option && errno == EEXIST)
896 break;
897 e = errno;
898 if (stat (link_name, &st1) == 0
899 && stat (file_name, &st2) == 0
900 && st1.st_dev == st2.st_dev
901 && st1.st_ino == st2.st_ino)
902 break;
903
904 link_error (link_name, file_name);
905 if (backup_option)
906 undo_last_backup ();
907 }
908 break;
909
910 #if S_IFCHR
911 case CHRTYPE:
912 current_stat_info.stat.st_mode |= S_IFCHR;
913 goto make_node;
914 #endif
915
916 #if S_IFBLK
917 case BLKTYPE:
918 current_stat_info.stat.st_mode |= S_IFBLK;
919 #endif
920
921 #if S_IFCHR || S_IFBLK
922 make_node:
923 if (! prepare_to_extract (file_name, 0))
924 break;
925
926 status = mknod (file_name, current_stat_info.stat.st_mode,
927 current_stat_info.stat.st_rdev);
928 if (status != 0)
929 {
930 if (maybe_recoverable (file_name, &interdir_made))
931 goto make_node;
932 mknod_error (file_name);
933 if (backup_option)
934 undo_last_backup ();
935 break;
936 };
937 set_stat (file_name, &current_stat_info.stat, 0, 0,
938 ARCHIVED_PERMSTATUS, typeflag);
939 break;
940 #endif
941
942 #if HAVE_MKFIFO || defined mkfifo
943 case FIFOTYPE:
944 if (! prepare_to_extract (file_name, 0))
945 break;
946
947 while (status = mkfifo (file_name, current_stat_info.stat.st_mode),
948 status != 0)
949 if (!maybe_recoverable (file_name, &interdir_made))
950 break;
951
952 if (status == 0)
953 set_stat (file_name, &current_stat_info.stat, NULL, 0,
954 ARCHIVED_PERMSTATUS, typeflag);
955 else
956 {
957 mkfifo_error (file_name);
958 if (backup_option)
959 undo_last_backup ();
960 }
961 break;
962 #endif
963
964 case DIRTYPE:
965 case GNUTYPE_DUMPDIR:
966 really_dir:
967 if (incremental_option)
968 {
969 /* Read the entry and delete files that aren't listed in the
970 archive. */
971
972 gnu_restore (file_name);
973 }
974 else if (typeflag == GNUTYPE_DUMPDIR)
975 skip_member ();
976
977 mode = ((current_stat_info.stat.st_mode
978 | (we_are_root ? 0 : MODE_WXUSR))
979 & MODE_RWX);
980
981 status = prepare_to_extract (file_name, 1);
982 if (status == 0)
983 break;
984 if (status < 0)
985 goto directory_exists;
986
987 again_dir:
988 status = mkdir (file_name, mode);
989
990 if (status != 0)
991 {
992 if (errno == EEXIST
993 && (interdir_made
994 || old_files_option == DEFAULT_OLD_FILES
995 || old_files_option == OVERWRITE_OLD_FILES))
996 {
997 struct stat st;
998 if (stat (file_name, &st) == 0)
999 {
1000 if (interdir_made)
1001 {
1002 repair_delayed_set_stat (file_name, &st);
1003 break;
1004 }
1005 if (S_ISDIR (st.st_mode))
1006 {
1007 mode = st.st_mode & ~ current_umask;
1008 goto directory_exists;
1009 }
1010 }
1011 errno = EEXIST;
1012 }
1013
1014 if (maybe_recoverable (file_name, &interdir_made))
1015 goto again_dir;
1016
1017 if (errno != EEXIST)
1018 {
1019 mkdir_error (file_name);
1020 if (backup_option)
1021 undo_last_backup ();
1022 break;
1023 }
1024 }
1025
1026 directory_exists:
1027 if (status == 0
1028 || old_files_option == DEFAULT_OLD_FILES
1029 || old_files_option == OVERWRITE_OLD_FILES)
1030 delay_set_stat (file_name, &current_stat_info.stat,
1031 MODE_RWX & (mode ^ current_stat_info.stat.st_mode),
1032 (status == 0
1033 ? ARCHIVED_PERMSTATUS
1034 : UNKNOWN_PERMSTATUS));
1035 break;
1036
1037 case GNUTYPE_VOLHDR:
1038 if (verbose_option)
1039 fprintf (stdlis, _("Reading %s\n"), quote (current_stat_info.file_name));
1040 break;
1041
1042 case GNUTYPE_NAMES:
1043 extract_mangle ();
1044 break;
1045
1046 case GNUTYPE_MULTIVOL:
1047 ERROR ((0, 0,
1048 _("%s: Cannot extract -- file is continued from another volume"),
1049 quotearg_colon (current_stat_info.file_name)));
1050 skip_member ();
1051 if (backup_option)
1052 undo_last_backup ();
1053 break;
1054
1055 case GNUTYPE_LONGNAME:
1056 case GNUTYPE_LONGLINK:
1057 ERROR ((0, 0, _("Visible long name error")));
1058 skip_member ();
1059 if (backup_option)
1060 undo_last_backup ();
1061 break;
1062
1063 default:
1064 WARN ((0, 0,
1065 _("%s: Unknown file type '%c', extracted as normal file"),
1066 quotearg_colon (file_name), typeflag));
1067 goto again_file;
1068 }
1069 }
1070
1071 /* Extract the symbolic links whose final extraction were delayed. */
1072 static void
1073 apply_delayed_symlinks (void)
1074 {
1075 struct delayed_symlink *ds;
1076
1077 for (ds = delayed_symlink_head; ds; )
1078 {
1079 struct string_list *sources = ds->sources;
1080 char const *valid_source = 0;
1081
1082 for (sources = ds->sources; sources; sources = sources->next)
1083 {
1084 char const *source = sources->string;
1085 struct stat st;
1086
1087 /* Make sure the placeholder file is still there. If not,
1088 don't create a symlink, as the placeholder was probably
1089 removed by a later extraction. */
1090 if (lstat (source, &st) == 0
1091 && st.st_dev == ds->dev
1092 && st.st_ino == ds->ino
1093 && st.st_mtime == ds->mtime)
1094 {
1095 /* Unlink the placeholder, then create a hard link if possible,
1096 a symbolic link otherwise. */
1097 if (unlink (source) != 0)
1098 unlink_error (source);
1099 else if (valid_source && link (valid_source, source) == 0)
1100 ;
1101 else if (symlink (ds->target, source) != 0)
1102 symlink_error (ds->target, source);
1103 else
1104 {
1105 valid_source = source;
1106 st.st_uid = ds->uid;
1107 st.st_gid = ds->gid;
1108 set_stat (source, &st, 0, 0, 0, SYMTYPE);
1109 }
1110 }
1111 }
1112
1113 for (sources = ds->sources; sources; )
1114 {
1115 struct string_list *next = sources->next;
1116 free (sources);
1117 sources = next;
1118 }
1119
1120 {
1121 struct delayed_symlink *next = ds->next;
1122 free (ds);
1123 ds = next;
1124 }
1125 }
1126
1127 delayed_symlink_head = 0;
1128 }
1129
1130 /* Finish the extraction of an archive. */
1131 void
1132 extract_finish (void)
1133 {
1134 /* First, fix the status of ordinary directories that need fixing. */
1135 apply_nonancestor_delayed_set_stat ("", 0);
1136
1137 /* Then, apply delayed symlinks, so that they don't affect delayed
1138 directory status-setting for ordinary directories. */
1139 apply_delayed_symlinks ();
1140
1141 /* Finally, fix the status of directories that are ancestors
1142 of delayed symlinks. */
1143 apply_nonancestor_delayed_set_stat ("", 1);
1144 }
1145
1146 void
1147 fatal_exit (void)
1148 {
1149 extract_finish ();
1150 error (TAREXIT_FAILURE, 0, _("Error is not recoverable: exiting now"));
1151 abort ();
1152 }
This page took 0.095398 seconds and 5 git commands to generate.