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