1 /* Extract files from a tar archive.
2 Copyright 1988, 92,93,94,96,97,98, 1999 Free Software Foundation, Inc.
3 Written by John Gilmore, on 1985-11-19.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any later
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
34 static int we_are_root
; /* true if our effective uid == 0 */
35 static mode_t newdir_umask
; /* umask when creating new directories */
36 static mode_t current_umask
; /* current umask (which is set to 0 if -p) */
38 /* Status of the permissions of a file that we are extracting. */
41 /* This file may have existed already; its permissions are unknown. */
44 /* This file was created using the permissions from the archive. */
47 /* This is an intermediate directory; the archive did not specify
52 /* List of directories whose statuses we need to extract after we've
53 finished extracting their subsidiary files. The head of the list
54 has the longest name; each non-head element in the list is an
55 ancestor (in the directory hierarchy) of the preceding element. */
56 struct delayed_set_stat
58 struct delayed_set_stat
*next
;
59 struct stat stat_info
;
61 mode_t invert_permissions
;
62 enum permstatus permstatus
;
66 static struct delayed_set_stat
*delayed_set_stat_head
;
68 /*--------------------------.
69 | Set up to extract files. |
70 `--------------------------*/
75 we_are_root
= geteuid () == 0;
76 same_permissions_option
+= we_are_root
;
77 same_owner_option
+= we_are_root
;
79 /* Option -p clears the kernel umask, so it does not affect proper
80 restoration of file permissions. New intermediate directories will
81 comply with umask at start of program. */
83 newdir_umask
= umask (0);
84 if (0 < same_permissions_option
)
88 umask (newdir_umask
); /* restore the kernel umask */
89 current_umask
= newdir_umask
;
93 /* If restoring permissions, restore the mode for FILE_NAME from
94 information given in *STAT_INFO; otherwise invert the
95 INVERT_PERMISSIONS bits from the file's current permissions.
96 PERMSTATUS specifies the status of the file's permissions.
97 TYPEFLAG specifies the type of the file. */
99 set_mode (char *file_name
, struct stat
*stat_info
,
100 mode_t invert_permissions
, enum permstatus permstatus
,
105 if (0 < same_permissions_option
106 && permstatus
!= INTERDIR_PERMSTATUS
)
108 mode
= stat_info
->st_mode
;
110 /* If we created the file and it has a usual mode, then its mode
111 is normally set correctly already. But on many hosts, some
112 directories inherit the setgid bits from their parents, so we
113 we must set directories' modes explicitly. */
114 if (permstatus
== ARCHIVED_PERMSTATUS
115 && ! (mode
& ~ MODE_RWX
)
116 && typeflag
!= DIRTYPE
117 && typeflag
!= GNUTYPE_DUMPDIR
)
120 else if (! invert_permissions
)
124 /* We must inspect a directory's current permissions, since the
125 directory may have inherited its setgid bit from its parent.
127 INVERT_PERMISSIONS happens to be nonzero only for directories
128 that we created, so there's no point optimizing this code for
131 if (stat (file_name
, &st
) != 0)
133 stat_error (file_name
);
136 mode
= st
.st_mode
^ invert_permissions
;
139 if (chmod (file_name
, mode
) != 0)
142 ERROR ((0, e
, _("%s: Cannot change mode to %04lo"),
143 quotearg_colon (file_name
), (unsigned long) mode
));
147 /* Restore stat attributes (owner, group, mode and times) for
148 FILE_NAME, using information given in *STAT_INFO.
149 If not restoring permissions, invert the
150 INVERT_PERMISSIONS bits from the file's current permissions.
151 PERMSTATUS specifies the status of the file's permissions.
152 TYPEFLAG specifies the type of the file. */
154 /* FIXME: About proper restoration of symbolic link attributes, we still do
155 not have it right. Pretesters' reports tell us we need further study and
156 probably more configuration. For now, just use lchown if it exists, and
157 punt for the rest. Sigh! */
160 set_stat (char *file_name
, struct stat
*stat_info
,
161 mode_t invert_permissions
, enum permstatus permstatus
,
164 struct utimbuf utimbuf
;
166 if (typeflag
!= SYMTYPE
)
168 /* We do the utime before the chmod because some versions of utime are
169 broken and trash the modes of the file. */
171 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
173 /* We set the accessed time to `now', which is really the time we
174 started extracting files, unless incremental_option is used, in
175 which case .st_atime is used. */
177 /* FIXME: incremental_option should set ctime too, but how? */
179 if (incremental_option
)
180 utimbuf
.actime
= stat_info
->st_atime
;
182 utimbuf
.actime
= start_time
;
184 utimbuf
.modtime
= stat_info
->st_mtime
;
186 if (utime (file_name
, &utimbuf
) < 0)
190 _("%s: Cannot change access and modification times"),
191 quotearg_colon (file_name
)));
195 /* Some systems allow non-root users to give files away. Once this
196 done, it is not possible anymore to change file permissions, so we
197 have to set permissions prior to possibly giving files away. */
199 set_mode (file_name
, stat_info
,
200 invert_permissions
, permstatus
, typeflag
);
203 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
205 /* When lchown exists, it should be used to change the attributes of
206 the symbolic link itself. In this case, a mere chown would change
207 the attributes of the file the symbolic link is pointing to, and
208 should be avoided. */
210 if (typeflag
== SYMTYPE
)
213 if (lchown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
216 ERROR ((0, e
, _("%s: Cannot lchown to uid %lu gid %lu"),
217 quotearg_colon (file_name
),
218 (unsigned long) stat_info
->st_uid
,
219 (unsigned long) stat_info
->st_gid
));
225 if (chown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
228 ERROR ((0, e
, _("%s: Cannot chown to uid %lu gid %lu"),
229 quotearg_colon (file_name
),
230 (unsigned long) stat_info
->st_uid
,
231 (unsigned long) stat_info
->st_gid
));
234 /* On a few systems, and in particular, those allowing to give files
235 away, changing the owner or group destroys the suid or sgid bits.
236 So let's attempt setting these bits once more. */
237 if (stat_info
->st_mode
& (S_ISUID
| S_ISGID
| S_ISVTX
))
238 set_mode (file_name
, stat_info
,
239 invert_permissions
, permstatus
, typeflag
);
244 /* Remember to restore stat attributes (owner, group, mode and times)
245 for the directory FILE_NAME, using information given in *STAT_INFO,
246 once we stop extracting files into that directory.
247 If not restoring permissions, remember to invert the
248 INVERT_PERMISSIONS bits from the file's current permissions.
249 PERMSTATUS specifies the status of the file's permissions. */
251 delay_set_stat (char const *file_name
, struct stat
const *stat_info
,
252 mode_t invert_permissions
, enum permstatus permstatus
)
254 size_t file_name_len
= strlen (file_name
);
255 struct delayed_set_stat
*data
= xmalloc (sizeof *data
+ file_name_len
);
256 data
->file_name_len
= file_name_len
;
257 strcpy (data
->file_name
, file_name
);
258 data
->invert_permissions
= invert_permissions
;
259 data
->permstatus
= permstatus
;
260 data
->stat_info
= *stat_info
;
261 data
->next
= delayed_set_stat_head
;
262 delayed_set_stat_head
= data
;
265 /* Update the delayed_set_stat info for an intermediate directory
266 created on the path to DIR_NAME. The intermediate directory
267 turned out to be the same as this directory, due to ".." or
268 symbolic links. *DIR_STAT_INFO is the status of the directory. */
270 repair_delayed_set_stat (char const *dir_name
,
271 struct stat
const *dir_stat_info
)
273 struct delayed_set_stat
*data
;
274 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
277 if (stat (data
->file_name
, &st
) != 0)
279 stat_error (data
->file_name
);
283 if (st
.st_dev
== dir_stat_info
->st_dev
284 && st
.st_ino
== dir_stat_info
->st_ino
)
286 data
->stat_info
= current_stat
;
287 data
->invert_permissions
= (MODE_RWX
288 & (current_stat
.st_mode
^ st
.st_mode
));
289 data
->permstatus
= ARCHIVED_PERMSTATUS
;
294 ERROR ((0, 0, _("Unexpected inconsistency when making directory %s"),
298 /*-----------------------------------------------------------------------.
299 | After a file/link/symlink/directory creation has failed, see if it's |
300 | because some required directory was not present, and if so, create all |
301 | required directories. Return non-zero if a directory was created. |
302 `-----------------------------------------------------------------------*/
305 make_directories (char *file_name
)
307 char *cursor
; /* points into path */
308 int did_something
= 0; /* did we do anything yet? */
309 int saved_errno
= errno
; /* remember caller's errno */
311 int invert_permissions
;
314 for (cursor
= strchr (file_name
, '/');
316 cursor
= strchr (cursor
+ 1, '/'))
318 /* Avoid mkdir of empty string, if leading or double '/'. */
320 if (cursor
== file_name
|| cursor
[-1] == '/')
323 /* Avoid mkdir where last part of path is "." or "..". */
325 if (cursor
[-1] == '.'
326 && (cursor
== file_name
+ 1 || cursor
[-2] == '/'
327 || (cursor
[-2] == '.'
328 && (cursor
== file_name
+ 2 || cursor
[-3] == '/'))))
331 *cursor
= '\0'; /* truncate the path there */
332 mode
= MODE_RWX
& ~ newdir_umask
;
333 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
334 status
= mkdir (file_name
, mode
^ invert_permissions
);
338 /* Create a struct delayed_set_stat even if
339 invert_permissions is zero, because
340 repair_delayed_set_stat may need to update the struct. */
341 delay_set_stat (file_name
,
342 ¤t_stat
/* ignored */,
343 invert_permissions
, INTERDIR_PERMSTATUS
);
345 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
356 /* Turbo C mkdir gives a funny errno. */
360 /* Directory already exists. */
363 /* Some other error in the mkdir. We return to the caller. */
368 return did_something
; /* tell them to retry if we made one */
371 /* Prepare to extract a file.
372 Return zero if extraction should not proceed. */
375 prepare_to_extract (char const *file_name
)
377 if (to_stdout_option
)
380 if (old_files_option
== UNLINK_FIRST_OLD_FILES
381 && !remove_any_file (file_name
, recursive_unlink_option
)
384 unlink_error (file_name
);
391 /*--------------------------------------------------------------------.
392 | Attempt repairing what went wrong with the extraction. Delete an |
393 | already existing file or create missing intermediate directories. |
394 | Return nonzero if we somewhat increased our chances at a successful |
395 | extraction. errno is properly restored on zero return. |
396 `--------------------------------------------------------------------*/
399 maybe_recoverable (char *file_name
, int *interdir_made
)
407 /* Remove an old file, if the options allow this. */
409 switch (old_files_option
)
414 case DEFAULT_OLD_FILES
:
415 case OVERWRITE_OLD_FILES
:
417 int r
= remove_any_file (file_name
, 0);
424 /* Attempt creating missing intermediate directories. */
425 if (! make_directories (file_name
))
431 /* Just say we can't do anything about it... */
442 extract_sparse_file (int fd
, off_t
*sizeleft
, off_t totalsize
, char *name
)
448 /* assuming sizeleft is initially totalsize */
450 while (*sizeleft
> 0)
452 union block
*data_block
= find_next_block ();
455 ERROR ((0, 0, _("Unexpected EOF on archive file")));
458 if (lseek (fd
, sparsearray
[sparse_ind
].offset
, SEEK_SET
) < 0)
460 char buf
[UINTMAX_STRSIZE_BOUND
];
462 ERROR ((0, e
, _("%s: lseek error at byte %s"),
463 quotearg_colon (name
),
464 STRINGIFY_BIGINT (sparsearray
[sparse_ind
].offset
, buf
)));
467 written
= sparsearray
[sparse_ind
++].numbytes
;
468 while (written
> BLOCKSIZE
)
470 count
= full_write (fd
, data_block
->buffer
, BLOCKSIZE
);
475 set_next_block_after (data_block
);
476 data_block
= find_next_block ();
479 ERROR ((0, 0, _("Unexpected EOF on archive file")));
484 count
= full_write (fd
, data_block
->buffer
, written
);
488 else if (count
!= written
)
490 char buf1
[UINTMAX_STRSIZE_BOUND
];
491 char buf2
[UINTMAX_STRSIZE_BOUND
];
492 ERROR ((0, 0, _("%s: Could only write %s of %s bytes"),
493 quotearg_colon (name
),
494 STRINGIFY_BIGINT (totalsize
- *sizeleft
, buf1
),
495 STRINGIFY_BIGINT (totalsize
, buf2
)));
496 skip_file (*sizeleft
);
501 set_next_block_after (data_block
);
507 /*----------------------------------.
508 | Extract a file from the archive. |
509 `----------------------------------*/
512 extract_archive (void)
514 union block
*data_block
;
525 int interdir_made
= 0;
532 #define CURRENT_FILE_NAME (skipcrud + current_file_name)
534 set_next_block_after (current_header
);
535 decode_header (current_header
, ¤t_stat
, ¤t_format
, 1);
537 if (interactive_option
&& !confirm ("extract", current_file_name
))
539 if (current_header
->oldgnu_header
.isextended
)
540 skip_extended_headers ();
541 skip_file (current_stat
.st_size
);
545 /* Print the block from `current_header' and `current_stat'. */
550 /* Check for fully specified file names and other atrocities. */
553 if (! absolute_names_option
)
555 while (CURRENT_FILE_NAME
[0] == '/')
557 static int warned_once
;
562 WARN ((0, 0, _("Removing leading `/' from member names")));
564 skipcrud
++; /* force relative path */
567 if (contains_dot_dot (CURRENT_FILE_NAME
))
569 ERROR ((0, 0, _("%s: Member name contains `..'"),
570 quotearg_colon (CURRENT_FILE_NAME
)));
571 if (current_header
->oldgnu_header
.isextended
)
572 skip_extended_headers ();
573 skip_file (current_stat
.st_size
);
578 /* Take a safety backup of a previously existing file. */
580 if (backup_option
&& !to_stdout_option
)
581 if (!maybe_backup_file (CURRENT_FILE_NAME
, 0))
584 ERROR ((0, e
, _("%s: Was unable to backup this file"),
585 quotearg_colon (CURRENT_FILE_NAME
)));
586 if (current_header
->oldgnu_header
.isextended
)
587 skip_extended_headers ();
588 skip_file (current_stat
.st_size
);
592 /* Extract the archive entry according to its type. */
594 typeflag
= current_header
->header
.typeflag
;
597 /* JK - What we want to do if the file is sparse is loop through
598 the array of sparse structures in the header and read in and
599 translate the character strings representing 1) the offset at
600 which to write and 2) how many bytes to write into numbers,
601 which we store into the scratch array, "sparsearray". This
602 array makes our life easier the same way it did in creating the
603 tar file that had to deal with a sparse file.
605 After we read in the first five (at most) sparse structures, we
606 check to see if the file has an extended header, i.e., if more
607 sparse structures are needed to describe the contents of the new
608 file. If so, we read in the extended headers and continue to
609 store their contents into the sparsearray. */
614 xmalloc (sp_array_size
* sizeof (struct sp_array
));
616 for (counter
= 0; counter
< SPARSES_IN_OLDGNU_HEADER
; counter
++)
618 struct sparse
const *s
= ¤t_header
->oldgnu_header
.sp
[counter
];
619 sparsearray
[counter
].offset
= OFF_FROM_HEADER (s
->offset
);
620 sparsearray
[counter
].numbytes
= SIZE_FROM_HEADER (s
->numbytes
);
621 if (!sparsearray
[counter
].numbytes
)
625 if (current_header
->oldgnu_header
.isextended
)
627 /* Read in the list of extended headers and translate them
628 into the sparsearray as before. Note that this
629 invalidates current_header. */
631 /* static */ int ind
= SPARSES_IN_OLDGNU_HEADER
;
635 exhdr
= find_next_block ();
638 ERROR ((0, 0, _("Unexpected EOF on archive file")));
641 for (counter
= 0; counter
< SPARSES_IN_SPARSE_HEADER
; counter
++)
643 struct sparse
const *s
= &exhdr
->sparse_header
.sp
[counter
];
644 if (counter
+ ind
> sp_array_size
- 1)
646 /* Realloc the scratch area since we've run out of
651 xrealloc (sparsearray
,
652 sp_array_size
* sizeof (struct sp_array
));
654 if (s
->numbytes
[0] == 0)
656 sparsearray
[counter
+ ind
].offset
=
657 OFF_FROM_HEADER (s
->offset
);
658 sparsearray
[counter
+ ind
].numbytes
=
659 SIZE_FROM_HEADER (s
->numbytes
);
661 if (!exhdr
->sparse_header
.isextended
)
665 ind
+= SPARSES_IN_SPARSE_HEADER
;
666 set_next_block_after (exhdr
);
669 set_next_block_after (exhdr
);
677 /* Appears to be a file. But BSD tar uses the convention that a slash
678 suffix means a directory. */
680 name_length
= strlen (CURRENT_FILE_NAME
);
681 if (name_length
&& CURRENT_FILE_NAME
[name_length
- 1] == '/')
684 /* FIXME: deal with protection issues. */
687 openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
688 | (old_files_option
== OVERWRITE_OLD_FILES
691 mode
= current_stat
.st_mode
& MODE_RWX
& ~ current_umask
;
693 if (to_stdout_option
)
699 if (! prepare_to_extract (CURRENT_FILE_NAME
))
701 if (current_header
->oldgnu_header
.isextended
)
702 skip_extended_headers ();
703 skip_file (current_stat
.st_size
);
710 /* Contiguous files (on the Masscomp) have to specify the size in
711 the open call that creates them. */
713 if (typeflag
== CONTTYPE
)
714 fd
= open (CURRENT_FILE_NAME
, openflag
| O_CTG
,
715 mode
, current_stat
.st_size
);
717 fd
= open (CURRENT_FILE_NAME
, openflag
, mode
);
719 #else /* not O_CTG */
720 if (typeflag
== CONTTYPE
)
722 static int conttype_diagnosed
;
724 if (!conttype_diagnosed
)
726 conttype_diagnosed
= 1;
727 WARN ((0, 0, _("Extracting contiguous files as regular files")));
730 fd
= open (CURRENT_FILE_NAME
, openflag
, mode
);
732 #endif /* not O_CTG */
736 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
739 open_error (CURRENT_FILE_NAME
);
740 if (current_header
->oldgnu_header
.isextended
)
741 skip_extended_headers ();
742 skip_file (current_stat
.st_size
);
749 if (typeflag
== GNUTYPE_SPARSE
)
752 size_t name_length_bis
;
754 /* Kludge alert. NAME is assigned to header.name because
755 during the extraction, the space that contains the header
756 will get scribbled on, and the name will get munged, so any
757 error messages that happen to contain the filename will look
758 REAL interesting unless we do this. */
760 name_length_bis
= strlen (CURRENT_FILE_NAME
) + 1;
761 name
= xmalloc (name_length_bis
);
762 memcpy (name
, CURRENT_FILE_NAME
, name_length_bis
);
763 size
= current_stat
.st_size
;
764 extract_sparse_file (fd
, &size
, current_stat
.st_size
, name
);
767 for (size
= current_stat
.st_size
;
771 if (multi_volume_option
)
773 assign_string (&save_name
, current_file_name
);
774 save_totsize
= current_stat
.st_size
;
775 save_sizeleft
= size
;
778 /* Locate data, determine max length writeable, write it,
779 block that we have used the data, then check if the write
782 data_block
= find_next_block ();
785 ERROR ((0, 0, _("Unexpected EOF on archive file")));
786 break; /* FIXME: What happens, then? */
789 written
= available_space_after (data_block
);
794 sstatus
= full_write (fd
, data_block
->buffer
, written
);
796 set_next_block_after ((union block
*)
797 (data_block
->buffer
+ written
- 1));
798 if (sstatus
== written
)
801 /* Error in writing to file. Print it, skip to next file in
805 write_error (CURRENT_FILE_NAME
);
807 ERROR ((0, 0, _("%s: Could only write %lu of %lu bytes"),
808 quotearg_colon (CURRENT_FILE_NAME
),
809 (unsigned long) sstatus
,
810 (unsigned long) written
));
811 skip_file (size
- written
);
812 break; /* still do the close, mod time, chmod, etc */
815 if (multi_volume_option
)
816 assign_string (&save_name
, 0);
818 /* If writing to stdout, don't try to do anything to the filename;
819 it doesn't exist, or we don't want to touch it anyway. */
821 if (to_stdout_option
)
827 close_error (CURRENT_FILE_NAME
);
832 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0,
833 (old_files_option
== OVERWRITE_OLD_FILES
835 : ARCHIVED_PERMSTATUS
),
841 if (! prepare_to_extract (CURRENT_FILE_NAME
))
844 while (status
= symlink (current_link_name
, CURRENT_FILE_NAME
),
846 if (!maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
851 /* Setting the attributes of symbolic links might, on some systems,
852 change the pointed to file, instead of the symbolic link itself.
853 At least some of these systems have a lchown call, and the
854 set_stat routine knows about this. */
856 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0,
857 ARCHIVED_PERMSTATUS
, typeflag
);
862 ERROR ((0, e
, _("%s: Cannot create symlink to %s"),
863 quotearg_colon (CURRENT_FILE_NAME
),
864 quote (current_link_name
)));
872 static int warned_once
;
878 _("Attempting extraction of symbolic links as hard links")));
886 if (! prepare_to_extract (CURRENT_FILE_NAME
))
891 struct stat st1
, st2
;
894 /* MSDOS does not implement links. However, djgpp's link() actually
896 status
= link (current_link_name
, CURRENT_FILE_NAME
);
900 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
903 if (incremental_option
&& errno
== EEXIST
)
906 if (stat (current_link_name
, &st1
) == 0
907 && stat (CURRENT_FILE_NAME
, &st2
) == 0
908 && st1
.st_dev
== st2
.st_dev
909 && st1
.st_ino
== st2
.st_ino
)
912 ERROR ((0, e
, _("%s: Cannot link to %s"),
913 quotearg_colon (CURRENT_FILE_NAME
),
914 quote (current_link_name
)));
922 current_stat
.st_mode
|= S_IFCHR
;
928 current_stat
.st_mode
|= S_IFBLK
;
931 #if S_IFCHR || S_IFBLK
933 if (! prepare_to_extract (CURRENT_FILE_NAME
))
936 status
= mknod (CURRENT_FILE_NAME
, current_stat
.st_mode
,
937 current_stat
.st_rdev
);
940 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
942 mknod_error (CURRENT_FILE_NAME
);
947 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0,
948 ARCHIVED_PERMSTATUS
, typeflag
);
952 #if HAVE_MKFIFO || defined mkfifo
954 if (! prepare_to_extract (CURRENT_FILE_NAME
))
957 while (status
= mkfifo (CURRENT_FILE_NAME
, current_stat
.st_mode
),
959 if (!maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
963 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0,
964 ARCHIVED_PERMSTATUS
, typeflag
);
967 mkfifo_error (CURRENT_FILE_NAME
);
975 case GNUTYPE_DUMPDIR
:
976 name_length
= strlen (CURRENT_FILE_NAME
);
979 /* Remove trailing "/" and "/.", unless that would result in the
983 if (1 < name_length
&& CURRENT_FILE_NAME
[name_length
- 1] == '/')
984 CURRENT_FILE_NAME
[--name_length
] = '\0';
985 else if (2 < name_length
986 && CURRENT_FILE_NAME
[name_length
- 1] == '.'
987 && CURRENT_FILE_NAME
[name_length
- 2] == '/')
988 CURRENT_FILE_NAME
[name_length
-= 2] = '\0';
993 if (incremental_option
)
995 /* Read the entry and delete files that aren't listed in the
998 gnu_restore (skipcrud
);
1000 else if (typeflag
== GNUTYPE_DUMPDIR
)
1001 skip_file (current_stat
.st_size
);
1003 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1006 mode
= ((current_stat
.st_mode
1007 | (we_are_root
? 0 : MODE_WXUSR
))
1011 status
= mkdir (CURRENT_FILE_NAME
, mode
);
1014 if (errno
== EEXIST
&& interdir_made
1015 && contains_dot_dot (CURRENT_FILE_NAME
))
1019 if (stat (CURRENT_FILE_NAME
, &st
) == 0)
1021 repair_delayed_set_stat (CURRENT_FILE_NAME
, &st
);
1027 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1030 if (errno
!= EEXIST
|| old_files_option
== KEEP_OLD_FILES
)
1033 ERROR ((0, e
, "%s: mkdir", quotearg_colon (CURRENT_FILE_NAME
)));
1035 undo_last_backup ();
1041 || old_files_option
== OVERWRITE_OLD_FILES
)
1042 delay_set_stat (CURRENT_FILE_NAME
, ¤t_stat
,
1043 mode
& ~ current_stat
.st_mode
,
1045 ? ARCHIVED_PERMSTATUS
1046 : UNKNOWN_PERMSTATUS
));
1049 case GNUTYPE_VOLHDR
:
1051 fprintf (stdlis
, _("Reading %s\n"), quote (current_file_name
));
1058 case GNUTYPE_MULTIVOL
:
1060 _("Cannot extract %s -- file is continued from another volume"),
1061 quote (current_file_name
)));
1062 skip_file (current_stat
.st_size
);
1064 undo_last_backup ();
1067 case GNUTYPE_LONGNAME
:
1068 case GNUTYPE_LONGLINK
:
1069 ERROR ((0, 0, _("Visible long name error")));
1070 skip_file (current_stat
.st_size
);
1072 undo_last_backup ();
1077 _("Unknown file type '%c' for %s, extracted as normal file"),
1078 typeflag
, quote (CURRENT_FILE_NAME
)));
1082 #undef CURRENT_FILE_NAME
1085 /* Fix the statuses of all directories that are not ancestors of FILE_NAME. */
1087 apply_delayed_set_stat (char const *file_name
)
1089 size_t file_name_len
= strlen (file_name
);
1091 while (delayed_set_stat_head
)
1093 struct delayed_set_stat
*data
= delayed_set_stat_head
;
1094 delayed_set_stat_head
= data
->next
;
1095 if (data
->file_name_len
< file_name_len
1096 && file_name
[data
->file_name_len
] == '/'
1097 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0)
1099 set_stat (data
->file_name
, &data
->stat_info
,
1100 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
1108 apply_delayed_set_stat ("");
1109 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));