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