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