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