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