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