⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 extract.c

📁 gnu tar 源码包。 tar 软件是 Unix 系统下的一个打包软件
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Extract files from a tar archive.   Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,   2001, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.   Written by John Gilmore, on 1985-11-19.   This program is free software; you can redistribute it and/or modify it   under the terms of the GNU General Public License as published by the   Free Software Foundation; either version 3, or (at your option) any later   version.   This program is distributed in the hope that it will be useful, but   WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General   Public License for more details.   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.,   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */#include <system.h>#include <quotearg.h>#include <utimens.h>#include <errno.h>#include <xgetcwd.h>#include "common.h"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) *//* Status of the permissions of a file that we are extracting.  */enum permstatus{  /* This file may have existed already; its permissions are unknown.  */  UNKNOWN_PERMSTATUS,  /* This file was created using the permissions from the archive,     except with S_IRWXG | S_IRWXO masked out if 0 < same_owner_option.  */  ARCHIVED_PERMSTATUS,  /* This is an intermediate directory; the archive did not specify     its permissions.  */  INTERDIR_PERMSTATUS};/* List of directories whose statuses we need to extract after we've   finished extracting their subsidiary files.  If you consider each   contiguous subsequence of elements of the form [D]?[^D]*, where [D]   represents an element where AFTER_LINKS is nonzero and [^D]   represents an element where AFTER_LINKS is zero, then the head   of the subsequence has the longest name, and each non-head element   in the prefix is an ancestor (in the directory hierarchy) of the   preceding element.  */struct delayed_set_stat  {    struct delayed_set_stat *next;    dev_t dev;    ino_t ino;    mode_t mode;    uid_t uid;    gid_t gid;    struct timespec atime;    struct timespec mtime;    size_t file_name_len;    mode_t invert_permissions;    enum permstatus permstatus;    bool after_links;    char file_name[1];  };static struct delayed_set_stat *delayed_set_stat_head;/* List of links whose creation we have delayed.  */struct delayed_link  {    /* The next delayed link in the list.  */    struct delayed_link *next;    /* The device, inode number and last-modified time of the placeholder.  */    dev_t dev;    ino_t ino;    struct timespec mtime;    /* True if the link is symbolic.  */    bool is_symlink;    /* The desired owner and group of the link, if it is a symlink.  */    uid_t uid;    gid_t gid;    /* A list of sources for this link.  The sources are all to be       hard-linked together.  */    struct string_list *sources;    /* The desired target of the desired link.  */    char target[1];  };static struct delayed_link *delayed_link_head;struct string_list  {    struct string_list *next;    char string[1];  };/*  Set up to extract files.  */voidextr_init (void){  we_are_root = geteuid () == 0;  same_permissions_option += we_are_root;  same_owner_option += we_are_root;  /* 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.  */  newdir_umask = umask (0);  if (0 < same_permissions_option)    current_umask = 0;  else    {      umask (newdir_umask);	/* restore the kernel umask */      current_umask = newdir_umask;    }}/* If restoring permissions, restore the mode for FILE_NAME from   information given in *STAT_INFO (where *CUR_INFO gives   the current status if CUR_INFO is nonzero); otherwise invert the   INVERT_PERMISSIONS bits from the file's current permissions.   PERMSTATUS specifies the status of the file's permissions.   TYPEFLAG specifies the type of the file.  */static voidset_mode (char const *file_name,	  struct stat const *stat_info,	  struct stat const *cur_info,	  mode_t invert_permissions, enum permstatus permstatus,	  char typeflag){  mode_t mode;  if (0 < same_permissions_option      && permstatus != INTERDIR_PERMSTATUS)    {      mode = stat_info->st_mode;      /* If we created the file and it has a mode that we set already	 with O_CREAT, then its mode is often set correctly already.	 But if we are changing ownership, the mode's group and and	 other permission bits were omitted originally, so it's less	 likely that the mode is OK now.  Also, on many hosts, some	 directories inherit the setgid bits from their parents, so we	 we must set directories' modes explicitly.  */      if ((permstatus == ARCHIVED_PERMSTATUS	   && ! (mode & ~ (0 < same_owner_option ? S_IRWXU : MODE_RWX)))	  && typeflag != DIRTYPE	  && typeflag != GNUTYPE_DUMPDIR)	return;    }  else if (! invert_permissions)    return;  else    {      /* We must inspect a directory's current permissions, since the	 directory may have inherited its setgid bit from its parent.	 INVERT_PERMISSIONS happens to be nonzero only for directories	 that we created, so there's no point optimizing this code for	 other cases.  */      struct stat st;      if (! cur_info)	{	  if (stat (file_name, &st) != 0)	    {	      stat_error (file_name);	      return;	    }	  cur_info = &st;	}      mode = cur_info->st_mode ^ invert_permissions;    }  if (chmod (file_name, mode) != 0)    chmod_error_details (file_name, mode);}/* Check time after successfully setting FILE_NAME's time stamp to T.  */static voidcheck_time (char const *file_name, struct timespec t){  if (t.tv_sec <= 0)    WARN ((0, 0, _("%s: implausibly old time stamp %s"),	   file_name, tartime (t, true)));  else if (timespec_cmp (volume_start_time, t) < 0)    {      struct timespec now;      gettime (&now);      if (timespec_cmp (now, t) < 0)	{	  char buf[TIMESPEC_STRSIZE_BOUND];	  struct timespec diff;	  diff.tv_sec = t.tv_sec - now.tv_sec;	  diff.tv_nsec = t.tv_nsec - now.tv_nsec;	  if (diff.tv_nsec < 0)	    {	      diff.tv_nsec += BILLION;	      diff.tv_sec--;	    }	  WARN ((0, 0, _("%s: time stamp %s is %s s in the future"),		 file_name, tartime (t, true), code_timespec (diff, buf)));	}    }}/* Restore stat attributes (owner, group, mode and times) for   FILE_NAME, using information given in *ST.   If CUR_INFO is nonzero, *CUR_INFO is the   file's current status.   If not restoring permissions, invert the   INVERT_PERMISSIONS bits from the file's current permissions.   PERMSTATUS specifies the status of the file's permissions.   TYPEFLAG specifies the type of the file.  *//* FIXME: About proper restoration of symbolic link attributes, we still do   not have it right.  Pretesters' reports tell us we need further study and   probably more configuration.  For now, just use lchown if it exists, and   punt for the rest.  Sigh!  */static voidset_stat (char const *file_name,	  struct tar_stat_info const *st,	  struct stat const *cur_info,	  mode_t invert_permissions, enum permstatus permstatus,	  char typeflag){  if (typeflag != SYMTYPE)    {      /* We do the utime before the chmod because some versions of utime are	 broken and trash the modes of the file.  */      if (! touch_option && permstatus != INTERDIR_PERMSTATUS)	{	  /* We set the accessed time to `now', which is really the time we	     started extracting files, unless incremental_option is used, in	     which case .st_atime is used.  */	  /* FIXME: incremental_option should set ctime too, but how?  */	  struct timespec ts[2];	  if (incremental_option)	    ts[0] = st->atime;	  else	    ts[0] = start_time;	  ts[1] = st->mtime;	  if (utimens (file_name, ts) != 0)	    utime_error (file_name);	  else	    {	      check_time (file_name, ts[0]);	      check_time (file_name, ts[1]);	    }	}      /* Some systems allow non-root users to give files away.  Once this	 done, it is not possible anymore to change file permissions.	 However, setting file permissions now would be incorrect, since	 they would apply to the wrong user, and there would be a race	 condition.  So, don't use systems that allow non-root users to	 give files away.  */    }  if (0 < same_owner_option && permstatus != INTERDIR_PERMSTATUS)    {      /* When lchown exists, it should be used to change the attributes of	 the symbolic link itself.  In this case, a mere chown would change	 the attributes of the file the symbolic link is pointing to, and	 should be avoided.  */      int chown_result = 1;      if (typeflag == SYMTYPE)	{#if HAVE_LCHOWN	  chown_result = lchown (file_name, st->stat.st_uid, st->stat.st_gid);#endif	}      else	{	  chown_result = chown (file_name, st->stat.st_uid, st->stat.st_gid);	}      if (chown_result == 0)	{	  /* Changing the owner can flip st_mode bits in some cases, so	     ignore cur_info if it might be obsolete now.  */	  if (cur_info	      && cur_info->st_mode & S_IXUGO	      && cur_info->st_mode & (S_ISUID | S_ISGID))	    cur_info = NULL;	}      else if (chown_result < 0)	chown_error_details (file_name,			     st->stat.st_uid, st->stat.st_gid);    }  if (typeflag != SYMTYPE)    set_mode (file_name, &st->stat, cur_info,	      invert_permissions, permstatus, typeflag);}/* Remember to restore stat attributes (owner, group, mode and times)   for the directory FILE_NAME, using information given in *ST,   once we stop extracting files into that directory.   If not restoring permissions, remember to invert the   INVERT_PERMISSIONS bits from the file's current permissions.   PERMSTATUS specifies the status of the file's permissions.   NOTICE: this works only if the archive has usual member order, i.e.   directory, then the files in that directory. Incremental archive have   somewhat reversed order: first go subdirectories, then all other   members. To help cope with this case the variable   delay_directory_restore_option is set by prepare_to_extract.   If an archive was explicitely created so that its member order is   reversed, some directory timestamps can be restored incorrectly,   e.g.:       tar --no-recursion -cf archive dir dir/file1 foo dir/file2*/static voiddelay_set_stat (char const *file_name, struct tar_stat_info const *st,		mode_t invert_permissions, enum permstatus permstatus){  size_t file_name_len = strlen (file_name);  struct delayed_set_stat *data =    xmalloc (offsetof (struct delayed_set_stat, file_name)	     + file_name_len + 1);  data->next = delayed_set_stat_head;  data->dev = st->stat.st_dev;  data->ino = st->stat.st_ino;  data->mode = st->stat.st_mode;  data->uid = st->stat.st_uid;  data->gid = st->stat.st_gid;  data->atime = st->atime;  data->mtime = st->mtime;  data->file_name_len = file_name_len;  data->invert_permissions = invert_permissions;  data->permstatus = permstatus;  data->after_links = 0;  strcpy (data->file_name, file_name);  delayed_set_stat_head = data;}/* Update the delayed_set_stat info for an intermediate directory   created within the file name of DIR.  The intermediate directory turned   out to be the same as this directory, e.g. due to ".." or symbolic   links.  *DIR_STAT_INFO is the status of the directory.  */static voidrepair_delayed_set_stat (char const *dir,			 struct stat const *dir_stat_info){  struct delayed_set_stat *data;  for (data = delayed_set_stat_head;  data;  data = data->next)    {      struct stat st;      if (stat (data->file_name, &st) != 0)	{	  stat_error (data->file_name);	  return;	}      if (st.st_dev == dir_stat_info->st_dev	  && st.st_ino == dir_stat_info->st_ino)	{	  data->dev = current_stat_info.stat.st_dev;	  data->ino = current_stat_info.stat.st_ino;	  data->mode = current_stat_info.stat.st_mode;	  data->uid = current_stat_info.stat.st_uid;	  data->gid = current_stat_info.stat.st_gid;	  data->atime = current_stat_info.atime;	  data->mtime = current_stat_info.mtime;	  data->invert_permissions =	    ((current_stat_info.stat.st_mode ^ st.st_mode)	     & MODE_RWX & ~ current_umask);	  data->permstatus = ARCHIVED_PERMSTATUS;	  return;	}    }  ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),	  quotearg_colon (dir)));}/* After a file/link/directory creation has failed, see if   it's because some required directory was not present, and if so,   create all required directories.  Return non-zero if a directory   was created.  */static intmake_directories (char *file_name){  char *cursor0 = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);  char *cursor;	        	/* points into the file name */  int did_something = 0;	/* did we do anything yet? */  int mode;  int invert_permissions;  int status;  for (cursor = cursor0; *cursor; cursor++)    {      if (! ISSLASH (*cursor))	continue;      /* Avoid mkdir of empty string, if leading or double '/'.  */      if (cursor == cursor0 || ISSLASH (cursor[-1]))	continue;      /* Avoid mkdir where last part of file name is "." or "..".  */      if (cursor[-1] == '.'	  && (cursor == cursor0 + 1 || ISSLASH (cursor[-2])	      || (cursor[-2] == '.'		  && (cursor == cursor0 + 2 || ISSLASH (cursor[-3])))))	continue;      *cursor = '\0';		/* truncate the name there */      mode = MODE_RWX & ~ newdir_umask;      invert_permissions = we_are_root ? 0 : MODE_WXUSR & ~ mode;      status = mkdir (file_name, mode ^ invert_permissions);      if (status == 0)	{	  /* Create a struct delayed_set_stat even if	     invert_permissions is zero, because	     repair_delayed_set_stat may need to update the struct.  */	  delay_set_stat (file_name,			  &current_stat_info,			  invert_permissions, INTERDIR_PERMSTATUS);	  print_for_mkdir (file_name, cursor - file_name, mode);	  did_something = 1;	  *cursor = '/';	  continue;	}      *cursor = '/';      if (errno == EEXIST)	continue;	        /* Directory already exists.  */      else if ((errno == ENOSYS /* Automounted dirs on Solaris return				   this. Reported by Warren Hyde				   <Warren.Hyde@motorola.com> */	       || ERRNO_IS_EACCES)  /* Turbo C mkdir gives a funny errno.  */	       && access (file_name, W_OK) == 0)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -