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