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