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