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