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