X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Fextract.c;h=9be0a8b1392b95ae200192bdb131c86f0963f532;hb=d659cbaccdc1f3279c49107cf15f15a639738529;hp=3f4eb06ad6e75c13d1419b97b592e114bc8782f3;hpb=5179c0b5e6a658ff002916058f95f246ed3216c8;p=chaz%2Ftar diff --git a/src/extract.c b/src/extract.c index 3f4eb06..9be0a8b 100644 --- a/src/extract.c +++ b/src/extract.c @@ -1,7 +1,7 @@ /* Extract files from a tar archive. Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000, - 2001, 2003, 2004 Free Software Foundation, Inc. + 2001, 2003, 2004, 2005 Free Software Foundation, Inc. Written by John Gilmore, on 1985-11-19. @@ -17,26 +17,17 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., - 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include +#include #include #include -#if HAVE_UTIME_H -# include -#else -struct utimbuf - { - long actime; - long modtime; - }; -#endif - #include "common.h" -bool we_are_root; /* true if our effective uid == 0 */ +static bool we_are_root; /* true if our effective uid == 0 */ static mode_t newdir_umask; /* umask when creating new directories */ static mode_t current_umask; /* current umask (which is set to 0 if -p) */ @@ -119,7 +110,7 @@ extr_init (void) FIXME: Should the same be done after handling -C option ? */ if (one_file_system_option) { - struct stat st; + struct stat st; char *dir = xgetcwd (); if (deref_stat (true, dir, &st)) @@ -127,7 +118,7 @@ extr_init (void) else root_device = st.st_dev; } - + /* Option -p clears the kernel umask, so it does not affect proper restoration of file permissions. New intermediate directories will comply with umask at start of program. */ @@ -201,15 +192,30 @@ set_mode (char const *file_name, /* Check time after successfully setting FILE_NAME's time stamp to T. */ static void -check_time (char const *file_name, time_t t) +check_time (char const *file_name, struct timespec t) { - time_t now; - if (t <= 0) + if (t.tv_sec <= 0) WARN ((0, 0, _("%s: implausibly old time stamp %s"), - file_name, tartime (t))); - else if (start_time < t && (now = time (0)) < t) - WARN ((0, 0, _("%s: time stamp %s is %lu s in the future"), - file_name, tartime (t), (unsigned long) (t - now))); + file_name, tartime (t, true))); + else if (timespec_lt (start_time, t)) + { + struct timespec now; + gettime (&now); + if (timespec_lt (now, t)) + { + unsigned long int ds = t.tv_sec - now.tv_sec; + int dns = t.tv_nsec - now.tv_nsec; + char dnsbuf[sizeof ".FFFFFFFFF"]; + if (dns < 0) + { + dns += 1000000000; + ds--; + } + code_ns_fraction (dns, dnsbuf); + WARN ((0, 0, _("%s: time stamp %s is %lu%s s in the future"), + file_name, tartime (t, true), ds, dnsbuf)); + } + } } /* Restore stat attributes (owner, group, mode and times) for @@ -233,8 +239,6 @@ set_stat (char const *file_name, mode_t invert_permissions, enum permstatus permstatus, char typeflag) { - struct utimbuf utimbuf; - if (typeflag != SYMTYPE) { /* We do the utime before the chmod because some versions of utime are @@ -248,19 +252,16 @@ set_stat (char const *file_name, /* FIXME: incremental_option should set ctime too, but how? */ - if (incremental_option) - utimbuf.actime = stat_info->st_atime; - else - utimbuf.actime = start_time; - - utimbuf.modtime = stat_info->st_mtime; + struct timespec ts[2]; + ts[0] = incremental_option ? get_stat_atime (stat_info) : start_time; + ts[1] = get_stat_mtime (stat_info); - if (utime (file_name, &utimbuf) < 0) + if (utimens (file_name, ts) != 0) utime_error (file_name); else { - check_time (file_name, utimbuf.actime); - check_time (file_name, utimbuf.modtime); + check_time (file_name, ts[0]); + check_time (file_name, ts[1]); } } @@ -440,7 +441,8 @@ file_newer_p (const char *file_name, struct tar_stat_info *tar_stat) if (stat (file_name, &st)) { stat_warn (file_name); - return true; /* Be on the safe side */ + /* Be on the safe side: if the file does exist assume it is newer */ + return errno != ENOENT; } if (!S_ISDIR (st.st_mode) && st.st_mtime >= tar_stat->stat.st_mtime) @@ -575,7 +577,7 @@ extract_dir (char *file_name, int typeflag) int status; mode_t mode; int interdir_made = 0; - + if (incremental_option) /* Read the entry and delete files that aren't listed in the archive. */ purge_directory (file_name); @@ -607,10 +609,10 @@ extract_dir (char *file_name, int typeflag) } errno = EEXIST; } - + if (maybe_recoverable (file_name, &interdir_made)) continue; - + if (errno != EEXIST) { mkdir_error (file_name); @@ -618,7 +620,7 @@ extract_dir (char *file_name, int typeflag) } break; } - + if (status == 0 || old_files_option == DEFAULT_OLD_FILES || old_files_option == OVERWRITE_OLD_FILES) @@ -665,7 +667,7 @@ open_output_file (char *file_name, int typeflag) fd = open (file_name, openflag, mode); #endif /* not O_CTG */ - + return fd; } @@ -679,24 +681,33 @@ extract_file (char *file_name, int typeflag) size_t count; size_t written; int interdir_made = 0; - + /* FIXME: deal with protection issues. */ if (to_stdout_option) fd = STDOUT_FILENO; + else if (to_command_option) + { + fd = sys_exec_command (file_name, 'f', ¤t_stat_info); + if (fd < 0) + { + skip_member (); + return 0; + } + } else { do fd = open_output_file (file_name, typeflag); while (fd < 0 && maybe_recoverable (file_name, &interdir_made)); - + if (fd < 0) { open_error (file_name); return 1; } } - + if (current_stat_info.is_sparse) sparse_extract_file (fd, ¤t_stat_info, &size); else @@ -704,7 +715,7 @@ extract_file (char *file_name, int typeflag) { if (multi_volume_option) { - assign_string (&save_name, current_stat_info.file_name); + assign_string (&save_name, current_stat_info.orig_file_name); save_totsize = current_stat_info.stat.st_size; save_sizeleft = size; } @@ -712,7 +723,7 @@ extract_file (char *file_name, int typeflag) /* Locate data, determine max length writeable, write it, block that we have used the data, then check if the write worked. */ - + data_block = find_next_block (); if (! data_block) { @@ -721,23 +732,24 @@ extract_file (char *file_name, int typeflag) } written = available_space_after (data_block); - + if (written > size) written = size; errno = 0; count = full_write (fd, data_block->buffer, written); - size -= count; - + size -= written; + set_next_block_after ((union block *) (data_block->buffer + written - 1)); if (count != written) { - write_error_details (file_name, count, written); /* FIXME: shouldn't we - restore from backup? */ + if (!to_command_option) + write_error_details (file_name, count, written); + /* FIXME: shouldn't we restore from backup? */ break; } } - + skip_file (size); if (multi_volume_option) @@ -753,20 +765,24 @@ extract_file (char *file_name, int typeflag) if (status < 0) close_error (file_name); - set_stat (file_name, ¤t_stat_info.stat, 0, 0, - (old_files_option == OVERWRITE_OLD_FILES ? - UNKNOWN_PERMSTATUS : ARCHIVED_PERMSTATUS), - typeflag); + if (to_command_option) + sys_wait_command (); + else + set_stat (file_name, ¤t_stat_info.stat, 0, 0, + (old_files_option == OVERWRITE_OLD_FILES ? + UNKNOWN_PERMSTATUS : ARCHIVED_PERMSTATUS), + typeflag); return status; -} +} static int extract_link (char *file_name, int typeflag) { - char const *link_name = safer_name_suffix (current_stat_info.link_name, true); + char const *link_name = safer_name_suffix (current_stat_info.link_name, + true, absolute_names_option); int interdir_made = 0; - + do { struct stat st1, st2; @@ -798,7 +814,7 @@ extract_link (char *file_name, int typeflag) && st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)) return 0; - + errno = e; } while (maybe_recoverable (file_name, &interdir_made)); @@ -809,7 +825,7 @@ extract_link (char *file_name, int typeflag) return 1; } return 0; -} +} static int extract_symlink (char *file_name, int typeflag) @@ -817,7 +833,7 @@ extract_symlink (char *file_name, int typeflag) #ifdef HAVE_SYMLINK int status, fd; int interdir_made = 0; - + if (absolute_names_option || ! (IS_ABSOLUTE_FILE_NAME (current_stat_info.link_name) || contains_dot_dot (current_stat_info.link_name))) @@ -880,7 +896,7 @@ extract_symlink (char *file_name, int typeflag) do { h->after_symlinks = 1; - + if (stat (h->file_name, &st) != 0) stat_error (h->file_name); else @@ -891,7 +907,7 @@ extract_symlink (char *file_name, int typeflag) } while ((h = h->next) && ! h->after_symlinks); } - + status = 0; } } @@ -906,9 +922,9 @@ extract_symlink (char *file_name, int typeflag) warned_once = 1; WARN ((0, 0, _("Attempting extraction of symbolic links as hard links"))); } - return extract_link (file_name, typeflag); + return extract_link (file_name, typeflag); #endif -} +} #if S_IFCHR || S_IFBLK static int @@ -916,12 +932,12 @@ extract_node (char *file_name, int typeflag) { int status; int interdir_made = 0; - + do status = mknod (file_name, current_stat_info.stat.st_mode, current_stat_info.stat.st_rdev); while (status && maybe_recoverable (file_name, &interdir_made)); - + if (status != 0) mknod_error (file_name); else @@ -936,7 +952,7 @@ extract_fifo (char *file_name, int typeflag) { int status; int interdir_made = 0; - + while ((status = mkfifo (file_name, current_stat_info.stat.st_mode))) if (!maybe_recoverable (file_name, &interdir_made)) break; @@ -947,7 +963,7 @@ extract_fifo (char *file_name, int typeflag) else mkfifo_error (file_name); return status; -} +} #endif static int @@ -957,7 +973,7 @@ extract_mangle_wrapper (char *file_name, int typeflag) return 0; } - + static int extract_failure (char *file_name, int typeflag) { @@ -975,8 +991,8 @@ static int prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun) { int rc = 1; - - if (to_stdout_option) + + if (EXTRACT_OVER_PIPE) rc = 0; /* Select the extractor */ @@ -986,7 +1002,7 @@ prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun) *fun = extract_file; rc = 1; break; - + case AREGTYPE: case REGTYPE: case CONTTYPE: @@ -1059,7 +1075,7 @@ prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun) default: WARN ((0, 0, - _("%s: Unknown file type '%c', extracted as normal file"), + _("%s: Unknown file type `%c', extracted as normal file"), quotearg_colon (file_name), typeflag)); *fun = extract_file; } @@ -1067,12 +1083,12 @@ prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun) /* Determine whether the extraction should proceed */ if (rc == 0) return 0; - + switch (old_files_option) { case UNLINK_FIRST_OLD_FILES: - if (!remove_any_file (file_name, - recursive_unlink_option ? RECURSIVE_REMOVE_OPTION + if (!remove_any_file (file_name, + recursive_unlink_option ? RECURSIVE_REMOVE_OPTION : ORDINARY_REMOVE_OPTION) && errno && errno != ENOENT) { @@ -1084,7 +1100,8 @@ prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun) case KEEP_NEWER_FILES: if (file_newer_p (file_name, ¤t_stat_info)) { - WARN ((0, 0, _("Current %s is newer"), quote (file_name))); + WARN ((0, 0, _("Current %s is newer or same age"), + quote (file_name))); return 0; } break; @@ -1103,7 +1120,7 @@ extract_archive (void) char typeflag; char *file_name; tar_extractor_t fun; - + set_next_block_after (current_header); decode_header (current_header, ¤t_stat_info, ¤t_format, 1); @@ -1118,7 +1135,8 @@ extract_archive (void) if (verbose_option) print_header (¤t_stat_info, -1); - file_name = safer_name_suffix (current_stat_info.file_name, false); + file_name = safer_name_suffix (current_stat_info.file_name, + false, absolute_names_option); if (strip_name_components) { size_t prefix_len = stripped_prefix_len (file_name, strip_name_components); @@ -1134,7 +1152,7 @@ extract_archive (void) /* Take a safety backup of a previously existing file. */ - if (backup_option && !to_stdout_option) + if (backup_option) if (!maybe_backup_file (file_name, 0)) { int e = errno; @@ -1157,7 +1175,7 @@ extract_archive (void) } else skip_member (); - + } /* Extract the symbolic links whose final extraction were delayed. */