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