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