]> Dogcows Code - chaz/tar/blob - src/extract.c
(we_are_root): Now global.
[chaz/tar] / src / extract.c
1 /* Extract files from a tar archive.
2 Copyright 1988, 92,93,94,96,97,98,99, 2000 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 *cursor; /* points into path */
324 int did_something = 0; /* did we do anything yet? */
325 int mode;
326 int invert_permissions;
327 int status;
328
329 for (cursor = strchr (file_name, '/');
330 cursor;
331 cursor = strchr (cursor + 1, '/'))
332 {
333 /* Avoid mkdir of empty string, if leading or double '/'. */
334
335 if (cursor == file_name || cursor[-1] == '/')
336 continue;
337
338 /* Avoid mkdir where last part of path is "." or "..". */
339
340 if (cursor[-1] == '.'
341 && (cursor == file_name + 1 || cursor[-2] == '/'
342 || (cursor[-2] == '.'
343 && (cursor == file_name + 2 || cursor[-3] == '/'))))
344 continue;
345
346 *cursor = '\0'; /* truncate the path there */
347 mode = MODE_RWX & ~ newdir_umask;
348 invert_permissions = we_are_root ? 0 : MODE_WXUSR & ~ mode;
349 status = mkdir (file_name, mode ^ invert_permissions);
350
351 if (status == 0)
352 {
353 /* Create a struct delayed_set_stat even if
354 invert_permissions is zero, because
355 repair_delayed_set_stat may need to update the struct. */
356 delay_set_stat (file_name,
357 &current_stat /* ignored */,
358 invert_permissions, INTERDIR_PERMSTATUS);
359
360 print_for_mkdir (file_name, cursor - file_name, mode);
361 did_something = 1;
362
363 *cursor = '/';
364 continue;
365 }
366
367 *cursor = '/';
368
369 if (errno == EEXIST
370 #if MSDOS
371 /* Turbo C mkdir gives a funny errno. */
372 || errno == EACCES
373 #endif
374 )
375 /* Directory already exists. */
376 continue;
377
378 /* Some other error in the mkdir. We return to the caller. */
379 break;
380 }
381
382 return did_something; /* tell them to retry if we made one */
383 }
384
385 /* Prepare to extract a file.
386 Return zero if extraction should not proceed. */
387
388 static int
389 prepare_to_extract (char const *file_name)
390 {
391 if (to_stdout_option)
392 return 0;
393
394 if (old_files_option == UNLINK_FIRST_OLD_FILES
395 && !remove_any_file (file_name, recursive_unlink_option)
396 && errno && errno != ENOENT)
397 {
398 unlink_error (file_name);
399 return 0;
400 }
401
402 return 1;
403 }
404
405 /* Attempt repairing what went wrong with the extraction. Delete an
406 already existing file or create missing intermediate directories.
407 Return nonzero if we somewhat increased our chances at a successful
408 extraction. errno is properly restored on zero return. */
409 static int
410 maybe_recoverable (char *file_name, int *interdir_made)
411 {
412 if (*interdir_made)
413 return 0;
414
415 switch (errno)
416 {
417 case EEXIST:
418 /* Remove an old file, if the options allow this. */
419
420 switch (old_files_option)
421 {
422 default:
423 return 0;
424
425 case DEFAULT_OLD_FILES:
426 case OVERWRITE_OLD_FILES:
427 {
428 int r = remove_any_file (file_name, 0);
429 errno = EEXIST;
430 return r;
431 }
432 }
433
434 case ENOENT:
435 /* Attempt creating missing intermediate directories. */
436 if (! make_directories (file_name))
437 {
438 errno = ENOENT;
439 return 0;
440 }
441 *interdir_made = 1;
442 return 1;
443
444 default:
445 /* Just say we can't do anything about it... */
446
447 return 0;
448 }
449 }
450
451 static void
452 extract_sparse_file (int fd, off_t *sizeleft, off_t totalsize, char *name)
453 {
454 int sparse_ind = 0;
455 size_t written;
456 ssize_t count;
457
458 /* assuming sizeleft is initially totalsize */
459
460 while (*sizeleft > 0)
461 {
462 union block *data_block = find_next_block ();
463 if (! data_block)
464 {
465 ERROR ((0, 0, _("Unexpected EOF in archive")));
466 return;
467 }
468 if (lseek (fd, sparsearray[sparse_ind].offset, SEEK_SET) < 0)
469 {
470 seek_error_details (name, sparsearray[sparse_ind].offset);
471 return;
472 }
473 written = sparsearray[sparse_ind++].numbytes;
474 while (written > BLOCKSIZE)
475 {
476 count = full_write (fd, data_block->buffer, BLOCKSIZE);
477 if (count < 0)
478 write_error (name);
479 written -= count;
480 *sizeleft -= count;
481 set_next_block_after (data_block);
482 data_block = find_next_block ();
483 if (! data_block)
484 {
485 ERROR ((0, 0, _("Unexpected EOF in archive")));
486 return;
487 }
488 }
489
490 count = full_write (fd, data_block->buffer, written);
491
492 if (count < 0)
493 write_error (name);
494 else if (count != written)
495 {
496 write_error_details (name, count, written);
497 skip_file (*sizeleft);
498 }
499
500 written -= count;
501 *sizeleft -= count;
502 set_next_block_after (data_block);
503 }
504
505 free (sparsearray);
506 }
507
508 /* Fix the statuses of all directories whose statuses need fixing, and
509 which are not ancestors of FILE_NAME. */
510 static void
511 apply_nonancestor_delayed_set_stat (char const *file_name)
512 {
513 size_t file_name_len = strlen (file_name);
514
515 while (delayed_set_stat_head)
516 {
517 struct delayed_set_stat *data = delayed_set_stat_head;
518 if (data->file_name_len < file_name_len
519 && file_name[data->file_name_len]
520 && (file_name[data->file_name_len] == '/'
521 || file_name[data->file_name_len - 1] == '/')
522 && memcmp (file_name, data->file_name, data->file_name_len) == 0)
523 break;
524 delayed_set_stat_head = data->next;
525 set_stat (data->file_name, &data->stat_info,
526 data->invert_permissions, data->permstatus, DIRTYPE);
527 free (data);
528 }
529 }
530
531 /* Extract a file from the archive. */
532 void
533 extract_archive (void)
534 {
535 union block *data_block;
536 int fd;
537 int status;
538 ssize_t sstatus;
539 size_t name_length;
540 size_t written;
541 int openflag;
542 mode_t mode;
543 off_t size;
544 int skipcrud;
545 int counter;
546 int interdir_made = 0;
547 char typeflag;
548 union block *exhdr;
549
550 #define CURRENT_FILE_NAME (skipcrud + current_file_name)
551
552 set_next_block_after (current_header);
553 decode_header (current_header, &current_stat, &current_format, 1);
554
555 if (interactive_option && !confirm ("extract", current_file_name))
556 {
557 skip_member ();
558 return;
559 }
560
561 /* Print the block from current_header and current_stat. */
562
563 if (verbose_option)
564 print_header ();
565
566 /* Check for fully specified file names and other atrocities. */
567
568 skipcrud = 0;
569 if (! absolute_names_option)
570 {
571 while (CURRENT_FILE_NAME[0] == '/')
572 {
573 static int warned_once;
574
575 if (!warned_once)
576 {
577 warned_once = 1;
578 WARN ((0, 0, _("Removing leading `/' from member names")));
579 }
580 skipcrud++; /* force relative path */
581 }
582
583 if (contains_dot_dot (CURRENT_FILE_NAME))
584 {
585 ERROR ((0, 0, _("%s: Member name contains `..'"),
586 quotearg_colon (CURRENT_FILE_NAME)));
587 skip_member ();
588 return;
589 }
590 }
591
592 apply_nonancestor_delayed_set_stat (CURRENT_FILE_NAME);
593
594 /* Take a safety backup of a previously existing file. */
595
596 if (backup_option && !to_stdout_option)
597 if (!maybe_backup_file (CURRENT_FILE_NAME, 0))
598 {
599 int e = errno;
600 ERROR ((0, e, _("%s: Was unable to backup this file"),
601 quotearg_colon (CURRENT_FILE_NAME)));
602 skip_member ();
603 return;
604 }
605
606 /* Extract the archive entry according to its type. */
607
608 typeflag = current_header->header.typeflag;
609 switch (typeflag)
610 {
611 /* JK - What we want to do if the file is sparse is loop through
612 the array of sparse structures in the header and read in and
613 translate the character strings representing 1) the offset at
614 which to write and 2) how many bytes to write into numbers,
615 which we store into the scratch array, "sparsearray". This
616 array makes our life easier the same way it did in creating the
617 tar file that had to deal with a sparse file.
618
619 After we read in the first five (at most) sparse structures, we
620 check to see if the file has an extended header, i.e., if more
621 sparse structures are needed to describe the contents of the new
622 file. If so, we read in the extended headers and continue to
623 store their contents into the sparsearray. */
624
625 case GNUTYPE_SPARSE:
626 sp_array_size = 10;
627 sparsearray =
628 xmalloc (sp_array_size * sizeof (struct sp_array));
629
630 for (counter = 0; counter < SPARSES_IN_OLDGNU_HEADER; counter++)
631 {
632 struct sparse const *s = &current_header->oldgnu_header.sp[counter];
633 sparsearray[counter].offset = OFF_FROM_HEADER (s->offset);
634 sparsearray[counter].numbytes = SIZE_FROM_HEADER (s->numbytes);
635 if (!sparsearray[counter].numbytes)
636 break;
637 }
638
639 if (current_header->oldgnu_header.isextended)
640 {
641 /* Read in the list of extended headers and translate them
642 into the sparsearray as before. Note that this
643 invalidates current_header. */
644
645 /* static */ int ind = SPARSES_IN_OLDGNU_HEADER;
646
647 while (1)
648 {
649 exhdr = find_next_block ();
650 if (! exhdr)
651 {
652 ERROR ((0, 0, _("Unexpected EOF in archive")));
653 return;
654 }
655 for (counter = 0; counter < SPARSES_IN_SPARSE_HEADER; counter++)
656 {
657 struct sparse const *s = &exhdr->sparse_header.sp[counter];
658 if (counter + ind > sp_array_size - 1)
659 {
660 /* Realloc the scratch area since we've run out of
661 room. */
662
663 sp_array_size *= 2;
664 sparsearray =
665 xrealloc (sparsearray,
666 sp_array_size * sizeof (struct sp_array));
667 }
668 if (s->numbytes[0] == 0)
669 break;
670 sparsearray[counter + ind].offset =
671 OFF_FROM_HEADER (s->offset);
672 sparsearray[counter + ind].numbytes =
673 SIZE_FROM_HEADER (s->numbytes);
674 }
675 if (!exhdr->sparse_header.isextended)
676 break;
677 else
678 {
679 ind += SPARSES_IN_SPARSE_HEADER;
680 set_next_block_after (exhdr);
681 }
682 }
683 set_next_block_after (exhdr);
684 }
685 /* Fall through. */
686
687 case AREGTYPE:
688 case REGTYPE:
689 case CONTTYPE:
690
691 /* Appears to be a file. But BSD tar uses the convention that a slash
692 suffix means a directory. */
693
694 name_length = strlen (CURRENT_FILE_NAME);
695 if (name_length && CURRENT_FILE_NAME[name_length - 1] == '/')
696 goto really_dir;
697
698 /* FIXME: deal with protection issues. */
699
700 again_file:
701 openflag = (O_WRONLY | O_BINARY | O_CREAT
702 | (old_files_option == OVERWRITE_OLD_FILES
703 ? O_TRUNC
704 : O_EXCL));
705 mode = current_stat.st_mode & MODE_RWX & ~ current_umask;
706
707 if (to_stdout_option)
708 {
709 fd = STDOUT_FILENO;
710 goto extract_file;
711 }
712
713 if (! prepare_to_extract (CURRENT_FILE_NAME))
714 {
715 skip_member ();
716 if (backup_option)
717 undo_last_backup ();
718 break;
719 }
720
721 #if O_CTG
722 /* Contiguous files (on the Masscomp) have to specify the size in
723 the open call that creates them. */
724
725 if (typeflag == CONTTYPE)
726 fd = open (CURRENT_FILE_NAME, openflag | O_CTG,
727 mode, current_stat.st_size);
728 else
729 fd = open (CURRENT_FILE_NAME, openflag, mode);
730
731 #else /* not O_CTG */
732 if (typeflag == CONTTYPE)
733 {
734 static int conttype_diagnosed;
735
736 if (!conttype_diagnosed)
737 {
738 conttype_diagnosed = 1;
739 WARN ((0, 0, _("Extracting contiguous files as regular files")));
740 }
741 }
742 fd = open (CURRENT_FILE_NAME, openflag, mode);
743
744 #endif /* not O_CTG */
745
746 if (fd < 0)
747 {
748 if (maybe_recoverable (CURRENT_FILE_NAME, &interdir_made))
749 goto again_file;
750
751 open_error (CURRENT_FILE_NAME);
752 skip_member ();
753 if (backup_option)
754 undo_last_backup ();
755 break;
756 }
757
758 extract_file:
759 if (typeflag == GNUTYPE_SPARSE)
760 {
761 char *name;
762 size_t name_length_bis;
763
764 /* Kludge alert. NAME is assigned to header.name because
765 during the extraction, the space that contains the header
766 will get scribbled on, and the name will get munged, so any
767 error messages that happen to contain the filename will look
768 REAL interesting unless we do this. */
769
770 name_length_bis = strlen (CURRENT_FILE_NAME) + 1;
771 name = xmalloc (name_length_bis);
772 memcpy (name, CURRENT_FILE_NAME, name_length_bis);
773 size = current_stat.st_size;
774 extract_sparse_file (fd, &size, current_stat.st_size, name);
775 }
776 else
777 for (size = current_stat.st_size;
778 size > 0;
779 size -= written)
780 {
781 if (multi_volume_option)
782 {
783 assign_string (&save_name, current_file_name);
784 save_totsize = current_stat.st_size;
785 save_sizeleft = size;
786 }
787
788 /* Locate data, determine max length writeable, write it,
789 block that we have used the data, then check if the write
790 worked. */
791
792 data_block = find_next_block ();
793 if (! data_block)
794 {
795 ERROR ((0, 0, _("Unexpected EOF in archive")));
796 break; /* FIXME: What happens, then? */
797 }
798
799 written = available_space_after (data_block);
800
801 if (written > size)
802 written = size;
803 errno = 0;
804 sstatus = full_write (fd, data_block->buffer, written);
805
806 set_next_block_after ((union block *)
807 (data_block->buffer + written - 1));
808 if (sstatus == written)
809 continue;
810
811 /* Error in writing to file. Print it, skip to next file in
812 archive. */
813
814 write_error_details (CURRENT_FILE_NAME, sstatus, written);
815 skip_file (size - written);
816 break; /* still do the close, mod time, chmod, etc */
817 }
818
819 if (multi_volume_option)
820 assign_string (&save_name, 0);
821
822 /* If writing to stdout, don't try to do anything to the filename;
823 it doesn't exist, or we don't want to touch it anyway. */
824
825 if (to_stdout_option)
826 break;
827
828 status = close (fd);
829 if (status < 0)
830 {
831 close_error (CURRENT_FILE_NAME);
832 if (backup_option)
833 undo_last_backup ();
834 }
835
836 set_stat (CURRENT_FILE_NAME, &current_stat, 0,
837 (old_files_option == OVERWRITE_OLD_FILES
838 ? UNKNOWN_PERMSTATUS
839 : ARCHIVED_PERMSTATUS),
840 typeflag);
841 break;
842
843 case SYMTYPE:
844 #ifdef HAVE_SYMLINK
845 if (! prepare_to_extract (CURRENT_FILE_NAME))
846 break;
847
848 if (absolute_names_option
849 || (current_link_name[0] != '/'
850 && ! contains_dot_dot (current_link_name)))
851 {
852 while (status = symlink (current_link_name, CURRENT_FILE_NAME),
853 status != 0)
854 if (!maybe_recoverable (CURRENT_FILE_NAME, &interdir_made))
855 break;
856
857 if (status == 0)
858 set_stat (CURRENT_FILE_NAME, &current_stat, 0, 0, SYMTYPE);
859 else
860 symlink_error (current_link_name, CURRENT_FILE_NAME);
861 }
862 else
863 {
864 /* This symbolic link is potentially dangerous. Don't
865 create it now; instead, create a placeholder file, which
866 will be replaced after other extraction is done. */
867 struct stat st;
868
869 while (fd = open (CURRENT_FILE_NAME, O_WRONLY | O_CREAT | O_EXCL, 0),
870 fd < 0)
871 if (! maybe_recoverable (CURRENT_FILE_NAME, &interdir_made))
872 break;
873
874 status = -1;
875 if (fd < 0)
876 open_error (CURRENT_FILE_NAME);
877 else if (fstat (fd, &st) != 0)
878 {
879 stat_error (CURRENT_FILE_NAME);
880 close (fd);
881 }
882 else if (close (fd) != 0)
883 close_error (CURRENT_FILE_NAME);
884 else
885 {
886 size_t filelen = strlen (CURRENT_FILE_NAME);
887 size_t linklen = strlen (current_link_name);
888 struct delayed_symlink *p =
889 xmalloc (sizeof *p + filelen + linklen + 1);
890 p->next = delayed_symlink_head;
891 delayed_symlink_head = p;
892 p->dev = st.st_dev;
893 p->ino = st.st_ino;
894 p->mtime = st.st_mtime;
895 p->uid = current_stat.st_uid;
896 p->gid = current_stat.st_gid;
897 memcpy (p->names, CURRENT_FILE_NAME, filelen + 1);
898 memcpy (p->names + filelen + 1, current_link_name, linklen + 1);
899 status = 0;
900 }
901 }
902
903 if (status != 0 && backup_option)
904 undo_last_backup ();
905 break;
906
907 #else
908 {
909 static int warned_once;
910
911 if (!warned_once)
912 {
913 warned_once = 1;
914 WARN ((0, 0,
915 _("Attempting extraction of symbolic links as hard links")));
916 }
917 }
918 typeflag = LNKTYPE;
919 /* Fall through. */
920 #endif
921
922 case LNKTYPE:
923 if (! prepare_to_extract (CURRENT_FILE_NAME))
924 break;
925
926 again_link:
927 {
928 struct stat st1, st2;
929 int e;
930
931 /* MSDOS does not implement links. However, djgpp's link() actually
932 copies the file. */
933 status = link (current_link_name, CURRENT_FILE_NAME);
934
935 if (status == 0)
936 break;
937 if (maybe_recoverable (CURRENT_FILE_NAME, &interdir_made))
938 goto again_link;
939
940 if (incremental_option && errno == EEXIST)
941 break;
942 e = errno;
943 if (stat (current_link_name, &st1) == 0
944 && stat (CURRENT_FILE_NAME, &st2) == 0
945 && st1.st_dev == st2.st_dev
946 && st1.st_ino == st2.st_ino)
947 break;
948
949 ERROR ((0, e, _("%s: Cannot link to %s"),
950 quotearg_colon (CURRENT_FILE_NAME),
951 quote (current_link_name)));
952 if (backup_option)
953 undo_last_backup ();
954 }
955 break;
956
957 #if S_IFCHR
958 case CHRTYPE:
959 current_stat.st_mode |= S_IFCHR;
960 goto make_node;
961 #endif
962
963 #if S_IFBLK
964 case BLKTYPE:
965 current_stat.st_mode |= S_IFBLK;
966 #endif
967
968 #if S_IFCHR || S_IFBLK
969 make_node:
970 if (! prepare_to_extract (CURRENT_FILE_NAME))
971 break;
972
973 status = mknod (CURRENT_FILE_NAME, current_stat.st_mode,
974 current_stat.st_rdev);
975 if (status != 0)
976 {
977 if (maybe_recoverable (CURRENT_FILE_NAME, &interdir_made))
978 goto make_node;
979 mknod_error (CURRENT_FILE_NAME);
980 if (backup_option)
981 undo_last_backup ();
982 break;
983 };
984 set_stat (CURRENT_FILE_NAME, &current_stat, 0,
985 ARCHIVED_PERMSTATUS, typeflag);
986 break;
987 #endif
988
989 #if HAVE_MKFIFO || defined mkfifo
990 case FIFOTYPE:
991 if (! prepare_to_extract (CURRENT_FILE_NAME))
992 break;
993
994 while (status = mkfifo (CURRENT_FILE_NAME, current_stat.st_mode),
995 status != 0)
996 if (!maybe_recoverable (CURRENT_FILE_NAME, &interdir_made))
997 break;
998
999 if (status == 0)
1000 set_stat (CURRENT_FILE_NAME, &current_stat, 0,
1001 ARCHIVED_PERMSTATUS, typeflag);
1002 else
1003 {
1004 mkfifo_error (CURRENT_FILE_NAME);
1005 if (backup_option)
1006 undo_last_backup ();
1007 }
1008 break;
1009 #endif
1010
1011 case DIRTYPE:
1012 case GNUTYPE_DUMPDIR:
1013 name_length = strlen (CURRENT_FILE_NAME);
1014
1015 really_dir:
1016 if (incremental_option)
1017 {
1018 /* Read the entry and delete files that aren't listed in the
1019 archive. */
1020
1021 gnu_restore (skipcrud);
1022 }
1023 else if (typeflag == GNUTYPE_DUMPDIR)
1024 skip_member ();
1025
1026 if (! prepare_to_extract (CURRENT_FILE_NAME))
1027 break;
1028
1029 mode = ((current_stat.st_mode
1030 | (we_are_root ? 0 : MODE_WXUSR))
1031 & MODE_RWX);
1032
1033 again_dir:
1034 status = mkdir (CURRENT_FILE_NAME, mode);
1035 if (status != 0)
1036 {
1037 if (errno == EEXIST && interdir_made)
1038 {
1039 struct stat st;
1040 if (stat (CURRENT_FILE_NAME, &st) == 0)
1041 {
1042 repair_delayed_set_stat (CURRENT_FILE_NAME, &st);
1043 break;
1044 }
1045 errno = EEXIST;
1046 }
1047
1048 if (maybe_recoverable (CURRENT_FILE_NAME, &interdir_made))
1049 goto again_dir;
1050
1051 if (errno != EEXIST || old_files_option == KEEP_OLD_FILES)
1052 {
1053 mkdir_error (CURRENT_FILE_NAME);
1054 if (backup_option)
1055 undo_last_backup ();
1056 break;
1057 }
1058 }
1059
1060 if (status == 0
1061 || old_files_option == OVERWRITE_OLD_FILES)
1062 delay_set_stat (CURRENT_FILE_NAME, &current_stat,
1063 mode & ~ current_stat.st_mode,
1064 (status == 0
1065 ? ARCHIVED_PERMSTATUS
1066 : UNKNOWN_PERMSTATUS));
1067 break;
1068
1069 case GNUTYPE_VOLHDR:
1070 if (verbose_option)
1071 fprintf (stdlis, _("Reading %s\n"), quote (current_file_name));
1072 break;
1073
1074 case GNUTYPE_NAMES:
1075 extract_mangle ();
1076 break;
1077
1078 case GNUTYPE_MULTIVOL:
1079 ERROR ((0, 0,
1080 _("%s: Cannot extract -- file is continued from another volume"),
1081 quotearg_colon (current_file_name)));
1082 skip_member ();
1083 if (backup_option)
1084 undo_last_backup ();
1085 break;
1086
1087 case GNUTYPE_LONGNAME:
1088 case GNUTYPE_LONGLINK:
1089 ERROR ((0, 0, _("Visible long name error")));
1090 skip_member ();
1091 if (backup_option)
1092 undo_last_backup ();
1093 break;
1094
1095 default:
1096 WARN ((0, 0,
1097 _("%s: Unknown file type '%c', extracted as normal file"),
1098 quotearg_colon (CURRENT_FILE_NAME), typeflag));
1099 goto again_file;
1100 }
1101
1102 #undef CURRENT_FILE_NAME
1103 }
1104
1105 /* Extract the symbolic links whose final extraction were delayed. */
1106 static void
1107 apply_delayed_symlinks (void)
1108 {
1109 struct delayed_symlink *p;
1110 struct delayed_symlink *next;
1111
1112 for (p = delayed_symlink_head; p; p = next)
1113 {
1114 char const *file = p->names;
1115 struct stat st;
1116
1117 /* Before doing anything, make sure the placeholder file is still
1118 there. If the placeholder isn't there, don't worry about it, as
1119 it may have been removed by a later extraction. */
1120 if (lstat (file, &st) == 0
1121 && st.st_dev == p->dev
1122 && st.st_ino == p->ino
1123 && st.st_mtime == p->mtime)
1124 {
1125 if (unlink (file) != 0)
1126 unlink_error (file);
1127 else
1128 {
1129 char const *contents = file + strlen (file) + 1;
1130 if (symlink (contents, file) != 0)
1131 symlink_error (contents, file);
1132 else
1133 {
1134 st.st_uid = p->uid;
1135 st.st_gid = p->gid;
1136 set_stat (file, &st, 0, 0, SYMTYPE);
1137 }
1138 }
1139 }
1140
1141 next = p->next;
1142 free (p);
1143 }
1144
1145 delayed_symlink_head = 0;
1146 }
1147
1148 /* Finish the extraction of an archive. */
1149 void
1150 extract_finish (void)
1151 {
1152 /* Apply delayed symlinks last, so that they don't affect
1153 delayed directory status-setting. */
1154 apply_nonancestor_delayed_set_stat ("");
1155 apply_delayed_symlinks ();
1156 }
1157
1158 void
1159 fatal_exit (void)
1160 {
1161 extract_finish ();
1162 error (TAREXIT_FAILURE, 0, _("Error is not recoverable: exiting now"));
1163 abort ();
1164 }
This page took 0.094147 seconds and 5 git commands to generate.