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