]> Dogcows Code - chaz/tar/blob - src/extract.c
a3877583437c98147c240aa93847e01e7c690630
[chaz/tar] / src / extract.c
1 /* Extract files from a tar archive.
2
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
4 2001, 2003 Free Software Foundation, Inc.
5
6 Written by John Gilmore, on 1985-11-19.
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 #include "system.h"
23 #include <quotearg.h>
24
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 bool 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 *CUR_INFO gives
133 the current status if CUR_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,
139 struct stat const *stat_info,
140 struct stat const *cur_info,
141 mode_t invert_permissions, enum permstatus permstatus,
142 char typeflag)
143 {
144 mode_t mode;
145
146 if (0 < same_permissions_option
147 && permstatus != INTERDIR_PERMSTATUS)
148 {
149 mode = stat_info->st_mode;
150
151 /* If we created the file and it has a usual mode, then its mode
152 is normally set correctly already. But on many hosts, some
153 directories inherit the setgid bits from their parents, so we
154 we must set directories' modes explicitly. */
155 if (permstatus == ARCHIVED_PERMSTATUS
156 && ! (mode & ~ MODE_RWX)
157 && typeflag != DIRTYPE
158 && typeflag != GNUTYPE_DUMPDIR)
159 return;
160 }
161 else if (! invert_permissions)
162 return;
163 else
164 {
165 /* We must inspect a directory's current permissions, since the
166 directory may have inherited its setgid bit from its parent.
167
168 INVERT_PERMISSIONS happens to be nonzero only for directories
169 that we created, so there's no point optimizing this code for
170 other cases. */
171 struct stat st;
172 if (! cur_info)
173 {
174 if (stat (file_name, &st) != 0)
175 {
176 stat_error (file_name);
177 return;
178 }
179 cur_info = &st;
180 }
181 mode = cur_info->st_mode ^ invert_permissions;
182 }
183
184 if (chmod (file_name, mode) != 0)
185 chmod_error_details (file_name, mode);
186 }
187
188 /* Check time after successfully setting FILE_NAME's time stamp to T. */
189 static void
190 check_time (char const *file_name, time_t t)
191 {
192 time_t now;
193 if (t <= 0)
194 WARN ((0, 0, _("%s: implausibly old time stamp %s"),
195 file_name, tartime (t)));
196 else if (start_time < t && (now = time (0)) < t)
197 WARN ((0, 0, _("%s: time stamp %s is %lu s in the future"),
198 file_name, tartime (t), (unsigned long) (t - now)));
199 }
200
201 /* Restore stat attributes (owner, group, mode and times) for
202 FILE_NAME, using information given in *STAT_INFO.
203 If CUR_INFO is nonzero, *CUR_INFO is the
204 file's currernt status.
205 If not restoring permissions, invert the
206 INVERT_PERMISSIONS bits from the file's current permissions.
207 PERMSTATUS specifies the status of the file's permissions.
208 TYPEFLAG specifies the type of the file. */
209
210 /* FIXME: About proper restoration of symbolic link attributes, we still do
211 not have it right. Pretesters' reports tell us we need further study and
212 probably more configuration. For now, just use lchown if it exists, and
213 punt for the rest. Sigh! */
214
215 static void
216 set_stat (char const *file_name,
217 struct stat const *stat_info,
218 struct stat const *cur_info,
219 mode_t invert_permissions, enum permstatus permstatus,
220 char typeflag)
221 {
222 struct utimbuf utimbuf;
223
224 if (typeflag != SYMTYPE)
225 {
226 /* We do the utime before the chmod because some versions of utime are
227 broken and trash the modes of the file. */
228
229 if (! touch_option && permstatus != INTERDIR_PERMSTATUS)
230 {
231 /* We set the accessed time to `now', which is really the time we
232 started extracting files, unless incremental_option is used, in
233 which case .st_atime is used. */
234
235 /* FIXME: incremental_option should set ctime too, but how? */
236
237 if (incremental_option)
238 utimbuf.actime = stat_info->st_atime;
239 else
240 utimbuf.actime = start_time;
241
242 utimbuf.modtime = stat_info->st_mtime;
243
244 if (utime (file_name, &utimbuf) < 0)
245 utime_error (file_name);
246 else
247 {
248 check_time (file_name, utimbuf.actime);
249 check_time (file_name, utimbuf.modtime);
250 }
251 }
252
253 /* Some systems allow non-root users to give files away. Once this
254 done, it is not possible anymore to change file permissions, so we
255 have to set permissions prior to possibly giving files away. */
256
257 set_mode (file_name, stat_info, cur_info,
258 invert_permissions, permstatus, typeflag);
259 }
260
261 if (0 < same_owner_option && permstatus != INTERDIR_PERMSTATUS)
262 {
263 /* When lchown exists, it should be used to change the attributes of
264 the symbolic link itself. In this case, a mere chown would change
265 the attributes of the file the symbolic link is pointing to, and
266 should be avoided. */
267
268 if (typeflag == SYMTYPE)
269 {
270 #if HAVE_LCHOWN
271 if (lchown (file_name, stat_info->st_uid, stat_info->st_gid) < 0)
272 chown_error_details (file_name,
273 stat_info->st_uid, stat_info->st_gid);
274 #endif
275 }
276 else
277 {
278 if (chown (file_name, stat_info->st_uid, stat_info->st_gid) < 0)
279 chown_error_details (file_name,
280 stat_info->st_uid, stat_info->st_gid);
281
282 /* On a few systems, and in particular, those allowing to give files
283 away, changing the owner or group destroys the suid or sgid bits.
284 So let's attempt setting these bits once more. */
285 if (stat_info->st_mode & (S_ISUID | S_ISGID | S_ISVTX))
286 set_mode (file_name, stat_info, 0,
287 invert_permissions, permstatus, typeflag);
288 }
289 }
290 }
291
292 /* Remember to restore stat attributes (owner, group, mode and times)
293 for the directory FILE_NAME, using information given in *STAT_INFO,
294 once we stop extracting files into that directory.
295 If not restoring permissions, remember to invert the
296 INVERT_PERMISSIONS bits from the file's current permissions.
297 PERMSTATUS specifies the status of the file's permissions. */
298 static void
299 delay_set_stat (char const *file_name, struct stat const *stat_info,
300 mode_t invert_permissions, enum permstatus permstatus)
301 {
302 size_t file_name_len = strlen (file_name);
303 struct delayed_set_stat *data =
304 xmalloc (offsetof (struct delayed_set_stat, file_name)
305 + file_name_len + 1);
306 data->file_name_len = file_name_len;
307 strcpy (data->file_name, file_name);
308 data->invert_permissions = invert_permissions;
309 data->permstatus = permstatus;
310 data->after_symlinks = 0;
311 data->stat_info = *stat_info;
312 data->next = delayed_set_stat_head;
313 delayed_set_stat_head = data;
314 }
315
316 /* Update the delayed_set_stat info for an intermediate directory
317 created on the path to DIR_NAME. The intermediate directory turned
318 out to be the same as this directory, e.g. due to ".." or symbolic
319 links. *DIR_STAT_INFO is the status of the directory. */
320 static void
321 repair_delayed_set_stat (char const *dir_name,
322 struct stat const *dir_stat_info)
323 {
324 struct delayed_set_stat *data;
325 for (data = delayed_set_stat_head; data; data = data->next)
326 {
327 struct stat st;
328 if (stat (data->file_name, &st) != 0)
329 {
330 stat_error (data->file_name);
331 return;
332 }
333
334 if (st.st_dev == dir_stat_info->st_dev
335 && st.st_ino == dir_stat_info->st_ino)
336 {
337 data->stat_info = current_stat_info.stat;
338 data->invert_permissions =
339 (MODE_RWX & (current_stat_info.stat.st_mode ^ st.st_mode));
340 data->permstatus = ARCHIVED_PERMSTATUS;
341 return;
342 }
343 }
344
345 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
346 quotearg_colon (dir_name)));
347 }
348
349 /* After a file/link/symlink/directory creation has failed, see if
350 it's because some required directory was not present, and if so,
351 create all required directories. Return non-zero if a directory
352 was created. */
353 static int
354 make_directories (char *file_name)
355 {
356 char *cursor0 = file_name + FILESYSTEM_PREFIX_LEN (file_name);
357 char *cursor; /* points into path */
358 int did_something = 0; /* did we do anything yet? */
359 int mode;
360 int invert_permissions;
361 int status;
362
363
364 for (cursor = cursor0; *cursor; cursor++)
365 {
366 if (! ISSLASH (*cursor))
367 continue;
368
369 /* Avoid mkdir of empty string, if leading or double '/'. */
370
371 if (cursor == cursor0 || ISSLASH (cursor[-1]))
372 continue;
373
374 /* Avoid mkdir where last part of path is "." or "..". */
375
376 if (cursor[-1] == '.'
377 && (cursor == cursor0 + 1 || ISSLASH (cursor[-2])
378 || (cursor[-2] == '.'
379 && (cursor == cursor0 + 2 || ISSLASH (cursor[-3])))))
380 continue;
381
382 *cursor = '\0'; /* truncate the path there */
383 mode = MODE_RWX & ~ newdir_umask;
384 invert_permissions = we_are_root ? 0 : MODE_WXUSR & ~ mode;
385 status = mkdir (file_name, mode ^ invert_permissions);
386
387 if (status == 0)
388 {
389 /* Create a struct delayed_set_stat even if
390 invert_permissions is zero, because
391 repair_delayed_set_stat may need to update the struct. */
392 delay_set_stat (file_name,
393 &current_stat_info.stat /* ignored */,
394 invert_permissions, INTERDIR_PERMSTATUS);
395
396 print_for_mkdir (file_name, cursor - file_name, mode);
397 did_something = 1;
398
399 *cursor = '/';
400 continue;
401 }
402
403 *cursor = '/';
404
405 if (errno == EEXIST)
406 continue; /* Directory already exists. */
407 else if ((errno == ENOSYS /* Automounted dirs on Solaris return
408 this. Reported by Warren Hyde
409 <Warren.Hyde@motorola.com> */
410 || ERRNO_IS_EACCESS) /* Turbo C mkdir gives a funny errno. */
411 && access (file_name, W_OK) == 0)
412 continue;
413
414 /* Some other error in the mkdir. We return to the caller. */
415 break;
416 }
417
418 return did_something; /* tell them to retry if we made one */
419 }
420
421 /* Prepare to extract a file.
422 Return zero if extraction should not proceed. */
423
424 static int
425 prepare_to_extract (char const *file_name, bool directory)
426 {
427 if (to_stdout_option)
428 return 0;
429
430 if (old_files_option == UNLINK_FIRST_OLD_FILES
431 && !remove_any_file (file_name, recursive_unlink_option)
432 && errno && errno != ENOENT)
433 {
434 unlink_error (file_name);
435 return 0;
436 }
437
438 return 1;
439 }
440
441 /* Attempt repairing what went wrong with the extraction. Delete an
442 already existing file or create missing intermediate directories.
443 Return nonzero if we somewhat increased our chances at a successful
444 extraction. errno is properly restored on zero return. */
445 static int
446 maybe_recoverable (char *file_name, int *interdir_made)
447 {
448 if (*interdir_made)
449 return 0;
450
451 switch (errno)
452 {
453 case EEXIST:
454 /* Remove an old file, if the options allow this. */
455
456 switch (old_files_option)
457 {
458 default:
459 return 0;
460
461 case DEFAULT_OLD_FILES:
462 case NO_OVERWRITE_DIR_OLD_FILES:
463 case OVERWRITE_OLD_FILES:
464 {
465 int r = remove_any_file (file_name, 0);
466 errno = EEXIST;
467 return r;
468 }
469 }
470
471 case ENOENT:
472 /* Attempt creating missing intermediate directories. */
473 if (! make_directories (file_name))
474 {
475 errno = ENOENT;
476 return 0;
477 }
478 *interdir_made = 1;
479 return 1;
480
481 default:
482 /* Just say we can't do anything about it... */
483
484 return 0;
485 }
486 }
487
488 /* Translate the sparse information on the header, and in any
489 subsequent extended headers, into an array of structures with true
490 numbers, as opposed to character strings. Return nonzero if
491 successful.
492
493 This function invalidates current_header. */
494
495 bool
496 fill_in_sparse_array (void)
497 {
498 off_t sparse_data_size = current_stat_info.stat.st_size;
499 off_t file_size = OFF_FROM_HEADER (current_header->oldgnu_header.realsize);
500 int sparses;
501 int counter;
502 union block *h = current_header;
503
504 init_sparsearray ();
505
506 for (sparses = 0; sparses < SPARSES_IN_OLDGNU_HEADER; sparses++)
507 {
508 struct sparse const *s = &h->oldgnu_header.sp[sparses];
509 off_t offset;
510 size_t numbytes;
511 if (s->numbytes[0] == '\0')
512 break;
513 sparsearray[sparses].offset = offset = OFF_FROM_HEADER (s->offset);
514 sparsearray[sparses].numbytes = numbytes =
515 SIZE_FROM_HEADER (s->numbytes);
516 sparse_data_size -= numbytes;
517 if (offset < 0 || file_size < offset + numbytes || sparse_data_size < 0)
518 goto invalid_member;
519 }
520
521 if (h->oldgnu_header.isextended)
522 do
523 {
524 h = find_next_block ();
525 if (! h)
526 {
527 ERROR ((0, 0, _("Unexpected EOF in archive")));
528 return 0;
529 }
530
531 for (counter = 0; counter < SPARSES_IN_SPARSE_HEADER; counter++)
532 {
533 struct sparse const *s = &h->sparse_header.sp[counter];
534 off_t offset;
535 size_t numbytes;
536 if (s->numbytes[0] == '\0')
537 break;
538
539 if (sparses == sp_array_size)
540 {
541 sp_array_size *= 2;
542 sparsearray =
543 xrealloc (sparsearray,
544 sp_array_size * sizeof *sparsearray);
545 }
546
547 sparsearray[sparses].offset = offset =
548 OFF_FROM_HEADER (s->offset);
549 sparsearray[sparses].numbytes = numbytes =
550 SIZE_FROM_HEADER (s->numbytes);
551 sparse_data_size -= numbytes;
552 if (offset < 0 || file_size < offset + numbytes
553 || sparse_data_size < 0)
554 goto invalid_member;
555 sparses++;
556 }
557
558 set_next_block_after (h);
559
560 } while (h->sparse_header.isextended);
561
562 return 1;
563
564 invalid_member:
565 ERROR ((0, 0, _("%s: invalid sparse archive member"),
566 current_stat_info.file_name));
567 return 0;
568 }
569
570
571 static off_t
572 extract_sparse_file (int fd, char const *name,
573 off_t sizeleft, off_t file_size)
574 {
575 int sparse_ind = 0;
576
577 while (sizeleft != 0)
578 {
579 size_t written;
580 size_t count;
581 union block *data_block = find_next_block ();
582 if (! data_block)
583 {
584 ERROR ((0, 0, _("Unexpected EOF in archive")));
585 return sizeleft;
586 }
587 if (lseek (fd, sparsearray[sparse_ind].offset, SEEK_SET) < 0)
588 {
589 seek_error_details (name, sparsearray[sparse_ind].offset);
590 return sizeleft;
591 }
592 written = sparsearray[sparse_ind++].numbytes;
593 while (written > BLOCKSIZE)
594 {
595 count = full_write (fd, data_block->buffer, BLOCKSIZE);
596 written -= count;
597 sizeleft -= count;
598 if (count != BLOCKSIZE)
599 {
600 write_error_details (name, count, BLOCKSIZE);
601 return sizeleft;
602 }
603 set_next_block_after (data_block);
604 data_block = find_next_block ();
605 if (! data_block)
606 {
607 ERROR ((0, 0, _("Unexpected EOF in archive")));
608 return sizeleft;
609 }
610 }
611
612 count = full_write (fd, data_block->buffer, written);
613 sizeleft -= count;
614
615 if (count != written)
616 {
617 write_error_details (name, count, written);
618 return sizeleft;
619 }
620
621 set_next_block_after (data_block);
622 }
623
624 if (ftruncate (fd, file_size) != 0)
625 truncate_error (name);
626
627 return 0;
628 }
629
630 /* Fix the statuses of all directories whose statuses need fixing, and
631 which are not ancestors of FILE_NAME. If AFTER_SYMLINKS is
632 nonzero, do this for all such directories; otherwise, stop at the
633 first directory that is marked to be fixed up only after delayed
634 symlinks are applied. */
635 static void
636 apply_nonancestor_delayed_set_stat (char const *file_name, bool after_symlinks)
637 {
638 size_t file_name_len = strlen (file_name);
639 bool check_for_renamed_directories = 0;
640
641 while (delayed_set_stat_head)
642 {
643 struct delayed_set_stat *data = delayed_set_stat_head;
644 bool skip_this_one = 0;
645 struct stat st;
646 struct stat const *cur_info = 0;
647
648 check_for_renamed_directories |= data->after_symlinks;
649
650 if (after_symlinks < data->after_symlinks
651 || (data->file_name_len < file_name_len
652 && file_name[data->file_name_len]
653 && (ISSLASH (file_name[data->file_name_len])
654 || ISSLASH (file_name[data->file_name_len - 1]))
655 && memcmp (file_name, data->file_name, data->file_name_len) == 0))
656 break;
657
658 if (check_for_renamed_directories)
659 {
660 cur_info = &st;
661 if (stat (data->file_name, &st) != 0)
662 {
663 stat_error (data->file_name);
664 skip_this_one = 1;
665 }
666 else if (! (st.st_dev == data->stat_info.st_dev
667 && (st.st_ino == data->stat_info.st_ino)))
668 {
669 ERROR ((0, 0,
670 _("%s: Directory renamed before its status could be extracted"),
671 quotearg_colon (data->file_name)));
672 skip_this_one = 1;
673 }
674 }
675
676 if (! skip_this_one)
677 set_stat (data->file_name, &data->stat_info, cur_info,
678 data->invert_permissions, data->permstatus, DIRTYPE);
679
680 delayed_set_stat_head = data->next;
681 free (data);
682 }
683 }
684
685 /* Extract a file from the archive. */
686 void
687 extract_archive (void)
688 {
689 union block *data_block;
690 int fd;
691 int status;
692 size_t count;
693 size_t written;
694 int openflag;
695 mode_t mode;
696 off_t size;
697 off_t file_size;
698 int interdir_made = 0;
699 char typeflag;
700 char *file_name;
701
702 set_next_block_after (current_header);
703 decode_header (current_header, &current_stat_info, &current_format, 1);
704
705 if (interactive_option && !confirm ("extract", current_stat_info.file_name))
706 {
707 skip_member ();
708 return;
709 }
710
711 /* Print the block from current_header and current_stat. */
712
713 if (verbose_option)
714 print_header (-1);
715
716 file_name = safer_name_suffix (current_stat_info.file_name, 0);
717 if (strip_path_elements)
718 {
719 size_t prefix_len = stripped_prefix_len (file_name, strip_path_elements);
720 if (prefix_len == (size_t) -1)
721 {
722 skip_member ();
723 return;
724 }
725 file_name += prefix_len;
726 }
727
728 apply_nonancestor_delayed_set_stat (file_name, 0);
729
730 /* Take a safety backup of a previously existing file. */
731
732 if (backup_option && !to_stdout_option)
733 if (!maybe_backup_file (file_name, 0))
734 {
735 int e = errno;
736 ERROR ((0, e, _("%s: Was unable to backup this file"),
737 quotearg_colon (file_name)));
738 skip_member ();
739 return;
740 }
741
742 /* Extract the archive entry according to its type. */
743
744 typeflag = current_header->header.typeflag;
745 switch (typeflag)
746 {
747 case GNUTYPE_SPARSE:
748 file_size = OFF_FROM_HEADER (current_header->oldgnu_header.realsize);
749 if (! fill_in_sparse_array ())
750 return;
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 if (current_stat_info.had_trailing_slash)
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_info.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 (file_name, 0))
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 (file_name, openflag | O_CTG, mode, current_stat_info.stat.st_size);
792 else
793 fd = open (file_name, openflag, mode);
794
795 #else /* not O_CTG */
796 if (typeflag == CONTTYPE)
797 {
798 static int conttype_diagnosed;
799
800 if (!conttype_diagnosed)
801 {
802 conttype_diagnosed = 1;
803 WARN ((0, 0, _("Extracting contiguous files as regular files")));
804 }
805 }
806 fd = open (file_name, openflag, mode);
807
808 #endif /* not O_CTG */
809
810 if (fd < 0)
811 {
812 if (maybe_recoverable (file_name, &interdir_made))
813 goto again_file;
814
815 open_error (file_name);
816 skip_member ();
817 if (backup_option)
818 undo_last_backup ();
819 break;
820 }
821
822 extract_file:
823 if (typeflag == GNUTYPE_SPARSE)
824 {
825 char *name;
826 size_t name_length_bis;
827
828 /* Kludge alert. NAME is assigned to header.name because
829 during the extraction, the space that contains the header
830 will get scribbled on, and the name will get munged, so any
831 error messages that happen to contain the filename will look
832 REAL interesting unless we do this. */
833
834 name_length_bis = strlen (file_name) + 1;
835 name = xmalloc (name_length_bis);
836 memcpy (name, file_name, name_length_bis);
837 size = extract_sparse_file (fd, name,
838 current_stat_info.stat.st_size, file_size);
839 free (sparsearray);
840 }
841 else
842 for (size = current_stat_info.stat.st_size; size > 0; )
843 {
844 if (multi_volume_option)
845 {
846 assign_string (&save_name, current_stat_info.file_name);
847 save_totsize = current_stat_info.stat.st_size;
848 save_sizeleft = size;
849 }
850
851 /* Locate data, determine max length writeable, write it,
852 block that we have used the data, then check if the write
853 worked. */
854
855 data_block = find_next_block ();
856 if (! data_block)
857 {
858 ERROR ((0, 0, _("Unexpected EOF in archive")));
859 break; /* FIXME: What happens, then? */
860 }
861
862 written = available_space_after (data_block);
863
864 if (written > size)
865 written = size;
866 errno = 0;
867 count = full_write (fd, data_block->buffer, written);
868 size -= count;
869
870 set_next_block_after ((union block *)
871 (data_block->buffer + written - 1));
872 if (count != written)
873 {
874 write_error_details (file_name, count, written);
875 break;
876 }
877 }
878
879 skip_file (size);
880
881 if (multi_volume_option)
882 assign_string (&save_name, 0);
883
884 /* If writing to stdout, don't try to do anything to the filename;
885 it doesn't exist, or we don't want to touch it anyway. */
886
887 if (to_stdout_option)
888 break;
889
890 status = close (fd);
891 if (status < 0)
892 {
893 close_error (file_name);
894 if (backup_option)
895 undo_last_backup ();
896 }
897
898 set_stat (file_name, &current_stat_info.stat, 0, 0,
899 (old_files_option == OVERWRITE_OLD_FILES
900 ? UNKNOWN_PERMSTATUS
901 : ARCHIVED_PERMSTATUS),
902 typeflag);
903 break;
904
905 case SYMTYPE:
906 #ifdef HAVE_SYMLINK
907 if (! prepare_to_extract (file_name, 0))
908 break;
909
910 if (absolute_names_option
911 || ! (ISSLASH (current_stat_info.link_name
912 [FILESYSTEM_PREFIX_LEN (current_stat_info.link_name)])
913 || contains_dot_dot (current_stat_info.link_name)))
914 {
915 while (status = symlink (current_stat_info.link_name, file_name),
916 status != 0)
917 if (!maybe_recoverable (file_name, &interdir_made))
918 break;
919
920 if (status == 0)
921 set_stat (file_name, &current_stat_info.stat, 0, 0, 0, SYMTYPE);
922 else
923 symlink_error (current_stat_info.link_name, file_name);
924 }
925 else
926 {
927 /* This symbolic link is potentially dangerous. Don't
928 create it now; instead, create a placeholder file, which
929 will be replaced after other extraction is done. */
930 struct stat st;
931
932 while (fd = open (file_name, O_WRONLY | O_CREAT | O_EXCL, 0),
933 fd < 0)
934 if (! maybe_recoverable (file_name, &interdir_made))
935 break;
936
937 status = -1;
938 if (fd < 0)
939 open_error (file_name);
940 else if (fstat (fd, &st) != 0)
941 {
942 stat_error (file_name);
943 close (fd);
944 }
945 else if (close (fd) != 0)
946 close_error (file_name);
947 else
948 {
949 struct delayed_set_stat *h;
950 struct delayed_symlink *p =
951 xmalloc (offsetof (struct delayed_symlink, target)
952 + strlen (current_stat_info.link_name) + 1);
953 p->next = delayed_symlink_head;
954 delayed_symlink_head = p;
955 p->dev = st.st_dev;
956 p->ino = st.st_ino;
957 p->mtime = st.st_mtime;
958 p->uid = current_stat_info.stat.st_uid;
959 p->gid = current_stat_info.stat.st_gid;
960 p->sources = xmalloc (offsetof (struct string_list, string)
961 + strlen (file_name) + 1);
962 p->sources->next = 0;
963 strcpy (p->sources->string, file_name);
964 strcpy (p->target, current_stat_info.link_name);
965
966 h = delayed_set_stat_head;
967 if (h && ! h->after_symlinks
968 && strncmp (file_name, h->file_name, h->file_name_len) == 0
969 && ISSLASH (file_name[h->file_name_len])
970 && (base_name (file_name)
971 == file_name + h->file_name_len + 1))
972 {
973 do
974 {
975 h->after_symlinks = 1;
976
977 if (stat (h->file_name, &st) != 0)
978 stat_error (h->file_name);
979 else
980 {
981 h->stat_info.st_dev = st.st_dev;
982 h->stat_info.st_ino = st.st_ino;
983 }
984 }
985 while ((h = h->next) && ! h->after_symlinks);
986 }
987
988 status = 0;
989 }
990 }
991
992 if (status != 0 && backup_option)
993 undo_last_backup ();
994 break;
995
996 #else
997 {
998 static int warned_once;
999
1000 if (!warned_once)
1001 {
1002 warned_once = 1;
1003 WARN ((0, 0,
1004 _("Attempting extraction of symbolic links as hard links")));
1005 }
1006 }
1007 typeflag = LNKTYPE;
1008 /* Fall through. */
1009 #endif
1010
1011 case LNKTYPE:
1012 if (! prepare_to_extract (file_name, 0))
1013 break;
1014
1015 again_link:
1016 {
1017 char const *link_name = safer_name_suffix (current_stat_info.link_name, 1);
1018 struct stat st1, st2;
1019 int e;
1020
1021 /* MSDOS does not implement links. However, djgpp's link() actually
1022 copies the file. */
1023 status = link (link_name, file_name);
1024
1025 if (status == 0)
1026 {
1027 struct delayed_symlink *ds = delayed_symlink_head;
1028 if (ds && stat (link_name, &st1) == 0)
1029 for (; ds; ds = ds->next)
1030 if (ds->dev == st1.st_dev
1031 && ds->ino == st1.st_ino
1032 && ds->mtime == st1.st_mtime)
1033 {
1034 struct string_list *p =
1035 xmalloc (offsetof (struct string_list, string)
1036 + strlen (file_name) + 1);
1037 strcpy (p->string, file_name);
1038 p->next = ds->sources;
1039 ds->sources = p;
1040 break;
1041 }
1042 break;
1043 }
1044 if (maybe_recoverable (file_name, &interdir_made))
1045 goto again_link;
1046
1047 if (incremental_option && errno == EEXIST)
1048 break;
1049 e = errno;
1050 if (stat (link_name, &st1) == 0
1051 && stat (file_name, &st2) == 0
1052 && st1.st_dev == st2.st_dev
1053 && st1.st_ino == st2.st_ino)
1054 break;
1055
1056 link_error (link_name, file_name);
1057 if (backup_option)
1058 undo_last_backup ();
1059 }
1060 break;
1061
1062 #if S_IFCHR
1063 case CHRTYPE:
1064 current_stat_info.stat.st_mode |= S_IFCHR;
1065 goto make_node;
1066 #endif
1067
1068 #if S_IFBLK
1069 case BLKTYPE:
1070 current_stat_info.stat.st_mode |= S_IFBLK;
1071 #endif
1072
1073 #if S_IFCHR || S_IFBLK
1074 make_node:
1075 if (! prepare_to_extract (file_name, 0))
1076 break;
1077
1078 status = mknod (file_name, current_stat_info.stat.st_mode,
1079 current_stat_info.stat.st_rdev);
1080 if (status != 0)
1081 {
1082 if (maybe_recoverable (file_name, &interdir_made))
1083 goto make_node;
1084 mknod_error (file_name);
1085 if (backup_option)
1086 undo_last_backup ();
1087 break;
1088 };
1089 set_stat (file_name, &current_stat_info.stat, 0, 0,
1090 ARCHIVED_PERMSTATUS, typeflag);
1091 break;
1092 #endif
1093
1094 #if HAVE_MKFIFO || defined mkfifo
1095 case FIFOTYPE:
1096 if (! prepare_to_extract (file_name, 0))
1097 break;
1098
1099 while (status = mkfifo (file_name, current_stat_info.stat.st_mode),
1100 status != 0)
1101 if (!maybe_recoverable (file_name, &interdir_made))
1102 break;
1103
1104 if (status == 0)
1105 set_stat (file_name, &current_stat_info.stat, NULL, 0,
1106 ARCHIVED_PERMSTATUS, typeflag);
1107 else
1108 {
1109 mkfifo_error (file_name);
1110 if (backup_option)
1111 undo_last_backup ();
1112 }
1113 break;
1114 #endif
1115
1116 case DIRTYPE:
1117 case GNUTYPE_DUMPDIR:
1118 really_dir:
1119 if (incremental_option)
1120 {
1121 /* Read the entry and delete files that aren't listed in the
1122 archive. */
1123
1124 gnu_restore (file_name);
1125 }
1126 else if (typeflag == GNUTYPE_DUMPDIR)
1127 skip_member ();
1128
1129 mode = ((current_stat_info.stat.st_mode
1130 | (we_are_root ? 0 : MODE_WXUSR))
1131 & MODE_RWX);
1132
1133 status = prepare_to_extract (file_name, 1);
1134 if (status == 0)
1135 break;
1136 if (status < 0)
1137 goto directory_exists;
1138
1139 again_dir:
1140 status = mkdir (file_name, mode);
1141
1142 if (status != 0)
1143 {
1144 if (errno == EEXIST
1145 && (interdir_made
1146 || old_files_option == DEFAULT_OLD_FILES
1147 || old_files_option == OVERWRITE_OLD_FILES))
1148 {
1149 struct stat st;
1150 if (stat (file_name, &st) == 0)
1151 {
1152 if (interdir_made)
1153 {
1154 repair_delayed_set_stat (file_name, &st);
1155 break;
1156 }
1157 if (S_ISDIR (st.st_mode))
1158 {
1159 mode = st.st_mode & ~ current_umask;
1160 goto directory_exists;
1161 }
1162 }
1163 errno = EEXIST;
1164 }
1165
1166 if (maybe_recoverable (file_name, &interdir_made))
1167 goto again_dir;
1168
1169 if (errno != EEXIST)
1170 {
1171 mkdir_error (file_name);
1172 if (backup_option)
1173 undo_last_backup ();
1174 break;
1175 }
1176 }
1177
1178 directory_exists:
1179 if (status == 0
1180 || old_files_option == DEFAULT_OLD_FILES
1181 || old_files_option == OVERWRITE_OLD_FILES)
1182 delay_set_stat (file_name, &current_stat_info.stat,
1183 MODE_RWX & (mode ^ current_stat_info.stat.st_mode),
1184 (status == 0
1185 ? ARCHIVED_PERMSTATUS
1186 : UNKNOWN_PERMSTATUS));
1187 break;
1188
1189 case GNUTYPE_VOLHDR:
1190 if (verbose_option)
1191 fprintf (stdlis, _("Reading %s\n"), quote (current_stat_info.file_name));
1192 break;
1193
1194 case GNUTYPE_NAMES:
1195 extract_mangle ();
1196 break;
1197
1198 case GNUTYPE_MULTIVOL:
1199 ERROR ((0, 0,
1200 _("%s: Cannot extract -- file is continued from another volume"),
1201 quotearg_colon (current_stat_info.file_name)));
1202 skip_member ();
1203 if (backup_option)
1204 undo_last_backup ();
1205 break;
1206
1207 case GNUTYPE_LONGNAME:
1208 case GNUTYPE_LONGLINK:
1209 ERROR ((0, 0, _("Visible long name error")));
1210 skip_member ();
1211 if (backup_option)
1212 undo_last_backup ();
1213 break;
1214
1215 default:
1216 WARN ((0, 0,
1217 _("%s: Unknown file type '%c', extracted as normal file"),
1218 quotearg_colon (file_name), typeflag));
1219 goto again_file;
1220 }
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.096839 seconds and 3 git commands to generate.