]> Dogcows Code - chaz/tar/blob - src/extract.c
(start_header): Removed debugging hook
[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 /* Translate the sparse information on the header, and in any
490 subsequent extended headers, into an array of structures with true
491 numbers, as opposed to character strings. Return nonzero if
492 successful.
493
494 This function invalidates current_header. */
495
496 bool
497 fill_in_sparse_array (void)
498 {
499 off_t sparse_data_size = current_stat_info.stat.st_size;
500 off_t file_size = OFF_FROM_HEADER (current_header->oldgnu_header.realsize);
501 int sparses;
502 int counter;
503 union block *h = current_header;
504
505 init_sparsearray ();
506
507 for (sparses = 0; sparses < SPARSES_IN_OLDGNU_HEADER; sparses++)
508 {
509 struct sparse const *s = &h->oldgnu_header.sp[sparses];
510 off_t offset;
511 size_t numbytes;
512 if (s->numbytes[0] == '\0')
513 break;
514 sparsearray[sparses].offset = offset = OFF_FROM_HEADER (s->offset);
515 sparsearray[sparses].numbytes = numbytes =
516 SIZE_FROM_HEADER (s->numbytes);
517 sparse_data_size -= numbytes;
518 if (offset < 0 || file_size < offset + numbytes || sparse_data_size < 0)
519 goto invalid_member;
520 }
521
522 if (h->oldgnu_header.isextended)
523 do
524 {
525 h = find_next_block ();
526 if (! h)
527 {
528 ERROR ((0, 0, _("Unexpected EOF in archive")));
529 return 0;
530 }
531
532 for (counter = 0; counter < SPARSES_IN_SPARSE_HEADER; counter++)
533 {
534 struct sparse const *s = &h->sparse_header.sp[counter];
535 off_t offset;
536 size_t numbytes;
537 if (s->numbytes[0] == '\0')
538 break;
539
540 if (sparses == sp_array_size)
541 {
542 sp_array_size *= 2;
543 sparsearray =
544 xrealloc (sparsearray,
545 sp_array_size * sizeof *sparsearray);
546 }
547
548 sparsearray[sparses].offset = offset =
549 OFF_FROM_HEADER (s->offset);
550 sparsearray[sparses].numbytes = numbytes =
551 SIZE_FROM_HEADER (s->numbytes);
552 sparse_data_size -= numbytes;
553 if (offset < 0 || file_size < offset + numbytes
554 || sparse_data_size < 0)
555 goto invalid_member;
556 sparses++;
557 }
558
559 set_next_block_after (h);
560
561 } while (h->sparse_header.isextended);
562
563 return 1;
564
565 invalid_member:
566 ERROR ((0, 0, _("%s: invalid sparse archive member"),
567 current_stat_info.file_name));
568 return 0;
569 }
570
571
572 static off_t
573 extract_sparse_file (int fd, char const *name,
574 off_t sizeleft, off_t file_size)
575 {
576 int sparse_ind = 0;
577
578 while (sizeleft != 0)
579 {
580 size_t written;
581 size_t count;
582 union block *data_block = find_next_block ();
583 if (! data_block)
584 {
585 ERROR ((0, 0, _("Unexpected EOF in archive")));
586 return sizeleft;
587 }
588 if (lseek (fd, sparsearray[sparse_ind].offset, SEEK_SET) < 0)
589 {
590 seek_error_details (name, sparsearray[sparse_ind].offset);
591 return sizeleft;
592 }
593 written = sparsearray[sparse_ind++].numbytes;
594 while (written > BLOCKSIZE)
595 {
596 count = full_write (fd, data_block->buffer, BLOCKSIZE);
597 written -= count;
598 sizeleft -= count;
599 if (count != BLOCKSIZE)
600 {
601 write_error_details (name, count, BLOCKSIZE);
602 return sizeleft;
603 }
604 set_next_block_after (data_block);
605 data_block = find_next_block ();
606 if (! data_block)
607 {
608 ERROR ((0, 0, _("Unexpected EOF in archive")));
609 return sizeleft;
610 }
611 }
612
613 count = full_write (fd, data_block->buffer, written);
614 sizeleft -= count;
615
616 if (count != written)
617 {
618 write_error_details (name, count, written);
619 return sizeleft;
620 }
621
622 set_next_block_after (data_block);
623 }
624
625 if (ftruncate (fd, file_size) != 0)
626 truncate_error (name);
627
628 return 0;
629 }
630
631 /* Fix the statuses of all directories whose statuses need fixing, and
632 which are not ancestors of FILE_NAME. If AFTER_SYMLINKS is
633 nonzero, do this for all such directories; otherwise, stop at the
634 first directory that is marked to be fixed up only after delayed
635 symlinks are applied. */
636 static void
637 apply_nonancestor_delayed_set_stat (char const *file_name, bool after_symlinks)
638 {
639 size_t file_name_len = strlen (file_name);
640 bool check_for_renamed_directories = 0;
641
642 while (delayed_set_stat_head)
643 {
644 struct delayed_set_stat *data = delayed_set_stat_head;
645 bool skip_this_one = 0;
646 struct stat st;
647 struct stat const *cur_info = 0;
648
649 check_for_renamed_directories |= data->after_symlinks;
650
651 if (after_symlinks < data->after_symlinks
652 || (data->file_name_len < file_name_len
653 && file_name[data->file_name_len]
654 && (ISSLASH (file_name[data->file_name_len])
655 || ISSLASH (file_name[data->file_name_len - 1]))
656 && memcmp (file_name, data->file_name, data->file_name_len) == 0))
657 break;
658
659 if (check_for_renamed_directories)
660 {
661 cur_info = &st;
662 if (stat (data->file_name, &st) != 0)
663 {
664 stat_error (data->file_name);
665 skip_this_one = 1;
666 }
667 else if (! (st.st_dev == data->stat_info.st_dev
668 && (st.st_ino == data->stat_info.st_ino)))
669 {
670 ERROR ((0, 0,
671 _("%s: Directory renamed before its status could be extracted"),
672 quotearg_colon (data->file_name)));
673 skip_this_one = 1;
674 }
675 }
676
677 if (! skip_this_one)
678 set_stat (data->file_name, &data->stat_info, cur_info,
679 data->invert_permissions, data->permstatus, DIRTYPE);
680
681 delayed_set_stat_head = data->next;
682 free (data);
683 }
684 }
685
686 /* Extract a file from the archive. */
687 void
688 extract_archive (void)
689 {
690 union block *data_block;
691 int fd;
692 int status;
693 size_t count;
694 size_t written;
695 int openflag;
696 mode_t mode;
697 off_t size;
698 off_t file_size;
699 int interdir_made = 0;
700 char typeflag;
701 char *file_name;
702
703 set_next_block_after (current_header);
704 decode_header (current_header, &current_stat_info, &current_format, 1);
705
706 if (interactive_option && !confirm ("extract", current_stat_info.file_name))
707 {
708 skip_member ();
709 return;
710 }
711
712 /* Print the block from current_header and current_stat. */
713
714 if (verbose_option)
715 print_header (-1);
716
717 file_name = safer_name_suffix (current_stat_info.file_name, 0);
718 if (strip_path_elements)
719 {
720 size_t prefix_len = stripped_prefix_len (file_name, strip_path_elements);
721 if (prefix_len == (size_t) -1)
722 {
723 skip_member ();
724 return;
725 }
726 file_name += prefix_len;
727 }
728
729 apply_nonancestor_delayed_set_stat (file_name, 0);
730
731 /* Take a safety backup of a previously existing file. */
732
733 if (backup_option && !to_stdout_option)
734 if (!maybe_backup_file (file_name, 0))
735 {
736 int e = errno;
737 ERROR ((0, e, _("%s: Was unable to backup this file"),
738 quotearg_colon (file_name)));
739 skip_member ();
740 return;
741 }
742
743 /* Extract the archive entry according to its type. */
744
745 typeflag = current_header->header.typeflag;
746 switch (typeflag)
747 {
748 case GNUTYPE_SPARSE:
749 file_size = OFF_FROM_HEADER (current_header->oldgnu_header.realsize);
750 if (! fill_in_sparse_array ())
751 return;
752 /* Fall through. */
753
754 case AREGTYPE:
755 case REGTYPE:
756 case CONTTYPE:
757
758 /* Appears to be a file. But BSD tar uses the convention that a slash
759 suffix means a directory. */
760
761 if (current_stat_info.had_trailing_slash)
762 goto really_dir;
763
764 /* FIXME: deal with protection issues. */
765
766 again_file:
767 openflag = (O_WRONLY | O_BINARY | O_CREAT
768 | (old_files_option == OVERWRITE_OLD_FILES
769 ? O_TRUNC
770 : O_EXCL));
771 mode = current_stat_info.stat.st_mode & MODE_RWX & ~ current_umask;
772
773 if (to_stdout_option)
774 {
775 fd = STDOUT_FILENO;
776 goto extract_file;
777 }
778
779 if (! prepare_to_extract (file_name, 0))
780 {
781 skip_member ();
782 if (backup_option)
783 undo_last_backup ();
784 break;
785 }
786
787 #if O_CTG
788 /* Contiguous files (on the Masscomp) have to specify the size in
789 the open call that creates them. */
790
791 if (typeflag == CONTTYPE)
792 fd = open (file_name, openflag | O_CTG, mode, current_stat_info.stat.st_size);
793 else
794 fd = open (file_name, openflag, mode);
795
796 #else /* not O_CTG */
797 if (typeflag == CONTTYPE)
798 {
799 static int conttype_diagnosed;
800
801 if (!conttype_diagnosed)
802 {
803 conttype_diagnosed = 1;
804 WARN ((0, 0, _("Extracting contiguous files as regular files")));
805 }
806 }
807 fd = open (file_name, openflag, mode);
808
809 #endif /* not O_CTG */
810
811 if (fd < 0)
812 {
813 if (maybe_recoverable (file_name, &interdir_made))
814 goto again_file;
815
816 open_error (file_name);
817 skip_member ();
818 if (backup_option)
819 undo_last_backup ();
820 break;
821 }
822
823 extract_file:
824 if (typeflag == GNUTYPE_SPARSE)
825 {
826 char *name;
827 size_t name_length_bis;
828
829 /* Kludge alert. NAME is assigned to header.name because
830 during the extraction, the space that contains the header
831 will get scribbled on, and the name will get munged, so any
832 error messages that happen to contain the filename will look
833 REAL interesting unless we do this. */
834
835 name_length_bis = strlen (file_name) + 1;
836 name = xmalloc (name_length_bis);
837 memcpy (name, file_name, name_length_bis);
838 size = extract_sparse_file (fd, name,
839 current_stat_info.stat.st_size, file_size);
840 free (sparsearray);
841 }
842 else
843 for (size = current_stat_info.stat.st_size; size > 0; )
844 {
845 if (multi_volume_option)
846 {
847 assign_string (&save_name, current_stat_info.file_name);
848 save_totsize = current_stat_info.stat.st_size;
849 save_sizeleft = size;
850 }
851
852 /* Locate data, determine max length writeable, write it,
853 block that we have used the data, then check if the write
854 worked. */
855
856 data_block = find_next_block ();
857 if (! data_block)
858 {
859 ERROR ((0, 0, _("Unexpected EOF in archive")));
860 break; /* FIXME: What happens, then? */
861 }
862
863 written = available_space_after (data_block);
864
865 if (written > size)
866 written = size;
867 errno = 0;
868 count = full_write (fd, data_block->buffer, written);
869 size -= count;
870
871 set_next_block_after ((union block *)
872 (data_block->buffer + written - 1));
873 if (count != written)
874 {
875 write_error_details (file_name, count, written);
876 break;
877 }
878 }
879
880 skip_file (size);
881
882 if (multi_volume_option)
883 assign_string (&save_name, 0);
884
885 /* If writing to stdout, don't try to do anything to the filename;
886 it doesn't exist, or we don't want to touch it anyway. */
887
888 if (to_stdout_option)
889 break;
890
891 status = close (fd);
892 if (status < 0)
893 {
894 close_error (file_name);
895 if (backup_option)
896 undo_last_backup ();
897 }
898
899 set_stat (file_name, &current_stat_info.stat, 0, 0,
900 (old_files_option == OVERWRITE_OLD_FILES
901 ? UNKNOWN_PERMSTATUS
902 : ARCHIVED_PERMSTATUS),
903 typeflag);
904 break;
905
906 case SYMTYPE:
907 #ifdef HAVE_SYMLINK
908 if (! prepare_to_extract (file_name, 0))
909 break;
910
911 if (absolute_names_option
912 || ! (ISSLASH (current_stat_info.link_name
913 [FILESYSTEM_PREFIX_LEN (current_stat_info.link_name)])
914 || contains_dot_dot (current_stat_info.link_name)))
915 {
916 while (status = symlink (current_stat_info.link_name, file_name),
917 status != 0)
918 if (!maybe_recoverable (file_name, &interdir_made))
919 break;
920
921 if (status == 0)
922 set_stat (file_name, &current_stat_info.stat, 0, 0, 0, SYMTYPE);
923 else
924 symlink_error (current_stat_info.link_name, file_name);
925 }
926 else
927 {
928 /* This symbolic link is potentially dangerous. Don't
929 create it now; instead, create a placeholder file, which
930 will be replaced after other extraction is done. */
931 struct stat st;
932
933 while (fd = open (file_name, O_WRONLY | O_CREAT | O_EXCL, 0),
934 fd < 0)
935 if (! maybe_recoverable (file_name, &interdir_made))
936 break;
937
938 status = -1;
939 if (fd < 0)
940 open_error (file_name);
941 else if (fstat (fd, &st) != 0)
942 {
943 stat_error (file_name);
944 close (fd);
945 }
946 else if (close (fd) != 0)
947 close_error (file_name);
948 else
949 {
950 struct delayed_set_stat *h;
951 struct delayed_symlink *p =
952 xmalloc (offsetof (struct delayed_symlink, target)
953 + strlen (current_stat_info.link_name) + 1);
954 p->next = delayed_symlink_head;
955 delayed_symlink_head = p;
956 p->dev = st.st_dev;
957 p->ino = st.st_ino;
958 p->mtime = st.st_mtime;
959 p->uid = current_stat_info.stat.st_uid;
960 p->gid = current_stat_info.stat.st_gid;
961 p->sources = xmalloc (offsetof (struct string_list, string)
962 + strlen (file_name) + 1);
963 p->sources->next = 0;
964 strcpy (p->sources->string, file_name);
965 strcpy (p->target, current_stat_info.link_name);
966
967 h = delayed_set_stat_head;
968 if (h && ! h->after_symlinks
969 && strncmp (file_name, h->file_name, h->file_name_len) == 0
970 && ISSLASH (file_name[h->file_name_len])
971 && (base_name (file_name)
972 == file_name + h->file_name_len + 1))
973 {
974 do
975 {
976 h->after_symlinks = 1;
977
978 if (stat (h->file_name, &st) != 0)
979 stat_error (h->file_name);
980 else
981 {
982 h->stat_info.st_dev = st.st_dev;
983 h->stat_info.st_ino = st.st_ino;
984 }
985 }
986 while ((h = h->next) && ! h->after_symlinks);
987 }
988
989 status = 0;
990 }
991 }
992
993 if (status != 0 && backup_option)
994 undo_last_backup ();
995 break;
996
997 #else
998 {
999 static int warned_once;
1000
1001 if (!warned_once)
1002 {
1003 warned_once = 1;
1004 WARN ((0, 0,
1005 _("Attempting extraction of symbolic links as hard links")));
1006 }
1007 }
1008 typeflag = LNKTYPE;
1009 /* Fall through. */
1010 #endif
1011
1012 case LNKTYPE:
1013 if (! prepare_to_extract (file_name, 0))
1014 break;
1015
1016 again_link:
1017 {
1018 char const *link_name = safer_name_suffix (current_stat_info.link_name, 1);
1019 struct stat st1, st2;
1020 int e;
1021
1022 /* MSDOS does not implement links. However, djgpp's link() actually
1023 copies the file. */
1024 status = link (link_name, file_name);
1025
1026 if (status == 0)
1027 {
1028 struct delayed_symlink *ds = delayed_symlink_head;
1029 if (ds && stat (link_name, &st1) == 0)
1030 for (; ds; ds = ds->next)
1031 if (ds->dev == st1.st_dev
1032 && ds->ino == st1.st_ino
1033 && ds->mtime == st1.st_mtime)
1034 {
1035 struct string_list *p =
1036 xmalloc (offsetof (struct string_list, string)
1037 + strlen (file_name) + 1);
1038 strcpy (p->string, file_name);
1039 p->next = ds->sources;
1040 ds->sources = p;
1041 break;
1042 }
1043 break;
1044 }
1045 if (maybe_recoverable (file_name, &interdir_made))
1046 goto again_link;
1047
1048 if (incremental_option && errno == EEXIST)
1049 break;
1050 e = errno;
1051 if (stat (link_name, &st1) == 0
1052 && stat (file_name, &st2) == 0
1053 && st1.st_dev == st2.st_dev
1054 && st1.st_ino == st2.st_ino)
1055 break;
1056
1057 link_error (link_name, file_name);
1058 if (backup_option)
1059 undo_last_backup ();
1060 }
1061 break;
1062
1063 #if S_IFCHR
1064 case CHRTYPE:
1065 current_stat_info.stat.st_mode |= S_IFCHR;
1066 goto make_node;
1067 #endif
1068
1069 #if S_IFBLK
1070 case BLKTYPE:
1071 current_stat_info.stat.st_mode |= S_IFBLK;
1072 #endif
1073
1074 #if S_IFCHR || S_IFBLK
1075 make_node:
1076 if (! prepare_to_extract (file_name, 0))
1077 break;
1078
1079 status = mknod (file_name, current_stat_info.stat.st_mode,
1080 current_stat_info.stat.st_rdev);
1081 if (status != 0)
1082 {
1083 if (maybe_recoverable (file_name, &interdir_made))
1084 goto make_node;
1085 mknod_error (file_name);
1086 if (backup_option)
1087 undo_last_backup ();
1088 break;
1089 };
1090 set_stat (file_name, &current_stat_info.stat, 0, 0,
1091 ARCHIVED_PERMSTATUS, typeflag);
1092 break;
1093 #endif
1094
1095 #if HAVE_MKFIFO || defined mkfifo
1096 case FIFOTYPE:
1097 if (! prepare_to_extract (file_name, 0))
1098 break;
1099
1100 while (status = mkfifo (file_name, current_stat_info.stat.st_mode),
1101 status != 0)
1102 if (!maybe_recoverable (file_name, &interdir_made))
1103 break;
1104
1105 if (status == 0)
1106 set_stat (file_name, &current_stat_info.stat, NULL, 0,
1107 ARCHIVED_PERMSTATUS, typeflag);
1108 else
1109 {
1110 mkfifo_error (file_name);
1111 if (backup_option)
1112 undo_last_backup ();
1113 }
1114 break;
1115 #endif
1116
1117 case DIRTYPE:
1118 case GNUTYPE_DUMPDIR:
1119 really_dir:
1120 if (incremental_option)
1121 {
1122 /* Read the entry and delete files that aren't listed in the
1123 archive. */
1124
1125 gnu_restore (file_name);
1126 }
1127 else if (typeflag == GNUTYPE_DUMPDIR)
1128 skip_member ();
1129
1130 mode = ((current_stat_info.stat.st_mode
1131 | (we_are_root ? 0 : MODE_WXUSR))
1132 & MODE_RWX);
1133
1134 status = prepare_to_extract (file_name, 1);
1135 if (status == 0)
1136 break;
1137 if (status < 0)
1138 goto directory_exists;
1139
1140 again_dir:
1141 status = mkdir (file_name, mode);
1142
1143 if (status != 0)
1144 {
1145 if (errno == EEXIST
1146 && (interdir_made
1147 || old_files_option == DEFAULT_OLD_FILES
1148 || old_files_option == OVERWRITE_OLD_FILES))
1149 {
1150 struct stat st;
1151 if (stat (file_name, &st) == 0)
1152 {
1153 if (interdir_made)
1154 {
1155 repair_delayed_set_stat (file_name, &st);
1156 break;
1157 }
1158 if (S_ISDIR (st.st_mode))
1159 {
1160 mode = st.st_mode & ~ current_umask;
1161 goto directory_exists;
1162 }
1163 }
1164 errno = EEXIST;
1165 }
1166
1167 if (maybe_recoverable (file_name, &interdir_made))
1168 goto again_dir;
1169
1170 if (errno != EEXIST)
1171 {
1172 mkdir_error (file_name);
1173 if (backup_option)
1174 undo_last_backup ();
1175 break;
1176 }
1177 }
1178
1179 directory_exists:
1180 if (status == 0
1181 || old_files_option == DEFAULT_OLD_FILES
1182 || old_files_option == OVERWRITE_OLD_FILES)
1183 delay_set_stat (file_name, &current_stat_info.stat,
1184 MODE_RWX & (mode ^ current_stat_info.stat.st_mode),
1185 (status == 0
1186 ? ARCHIVED_PERMSTATUS
1187 : UNKNOWN_PERMSTATUS));
1188 break;
1189
1190 case GNUTYPE_VOLHDR:
1191 if (verbose_option)
1192 fprintf (stdlis, _("Reading %s\n"), quote (current_stat_info.file_name));
1193 break;
1194
1195 case GNUTYPE_NAMES:
1196 extract_mangle ();
1197 break;
1198
1199 case GNUTYPE_MULTIVOL:
1200 ERROR ((0, 0,
1201 _("%s: Cannot extract -- file is continued from another volume"),
1202 quotearg_colon (current_stat_info.file_name)));
1203 skip_member ();
1204 if (backup_option)
1205 undo_last_backup ();
1206 break;
1207
1208 case GNUTYPE_LONGNAME:
1209 case GNUTYPE_LONGLINK:
1210 ERROR ((0, 0, _("Visible long name error")));
1211 skip_member ();
1212 if (backup_option)
1213 undo_last_backup ();
1214 break;
1215
1216 default:
1217 WARN ((0, 0,
1218 _("%s: Unknown file type '%c', extracted as normal file"),
1219 quotearg_colon (file_name), typeflag));
1220 goto again_file;
1221 }
1222 }
1223
1224 /* Extract the symbolic links whose final extraction were delayed. */
1225 static void
1226 apply_delayed_symlinks (void)
1227 {
1228 struct delayed_symlink *ds;
1229
1230 for (ds = delayed_symlink_head; ds; )
1231 {
1232 struct string_list *sources = ds->sources;
1233 char const *valid_source = 0;
1234
1235 for (sources = ds->sources; sources; sources = sources->next)
1236 {
1237 char const *source = sources->string;
1238 struct stat st;
1239
1240 /* Make sure the placeholder file is still there. If not,
1241 don't create a symlink, as the placeholder was probably
1242 removed by a later extraction. */
1243 if (lstat (source, &st) == 0
1244 && st.st_dev == ds->dev
1245 && st.st_ino == ds->ino
1246 && st.st_mtime == ds->mtime)
1247 {
1248 /* Unlink the placeholder, then create a hard link if possible,
1249 a symbolic link otherwise. */
1250 if (unlink (source) != 0)
1251 unlink_error (source);
1252 else if (valid_source && link (valid_source, source) == 0)
1253 ;
1254 else if (symlink (ds->target, source) != 0)
1255 symlink_error (ds->target, source);
1256 else
1257 {
1258 valid_source = source;
1259 st.st_uid = ds->uid;
1260 st.st_gid = ds->gid;
1261 set_stat (source, &st, 0, 0, 0, SYMTYPE);
1262 }
1263 }
1264 }
1265
1266 for (sources = ds->sources; sources; )
1267 {
1268 struct string_list *next = sources->next;
1269 free (sources);
1270 sources = next;
1271 }
1272
1273 {
1274 struct delayed_symlink *next = ds->next;
1275 free (ds);
1276 ds = next;
1277 }
1278 }
1279
1280 delayed_symlink_head = 0;
1281 }
1282
1283 /* Finish the extraction of an archive. */
1284 void
1285 extract_finish (void)
1286 {
1287 /* First, fix the status of ordinary directories that need fixing. */
1288 apply_nonancestor_delayed_set_stat ("", 0);
1289
1290 /* Then, apply delayed symlinks, so that they don't affect delayed
1291 directory status-setting for ordinary directories. */
1292 apply_delayed_symlinks ();
1293
1294 /* Finally, fix the status of directories that are ancestors
1295 of delayed symlinks. */
1296 apply_nonancestor_delayed_set_stat ("", 1);
1297 }
1298
1299 void
1300 fatal_exit (void)
1301 {
1302 extract_finish ();
1303 error (TAREXIT_FAILURE, 0, _("Error is not recoverable: exiting now"));
1304 abort ();
1305 }
This page took 0.091198 seconds and 4 git commands to generate.