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