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