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