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