Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4)

Side by Side Diff: Lib/tarfile.py

Issue 19974: tarfile doesn't overwrite symlink by directory
Patch Set: Created 6 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Lib/test/test_tarfile.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2 #------------------------------------------------------------------- 2 #-------------------------------------------------------------------
3 # tarfile.py 3 # tarfile.py
4 #------------------------------------------------------------------- 4 #-------------------------------------------------------------------
5 # Copyright (C) 2002 Lars Gustaebel <lars@gustaebel.de> 5 # Copyright (C) 2002 Lars Gustaebel <lars@gustaebel.de>
6 # All rights reserved. 6 # All rights reserved.
7 # 7 #
8 # Permission is hereby granted, free of charge, to any person 8 # Permission is hereby granted, free of charge, to any person
9 # obtaining a copy of this software and associated documentation 9 # obtaining a copy of this software and associated documentation
10 # files (the "Software"), to deal in the Software without 10 # files (the "Software"), to deal in the Software without
(...skipping 1978 matching lines...) Expand 10 before | Expand all | Expand 10 after
1989 if isinstance(member, str): 1989 if isinstance(member, str):
1990 tarinfo = self.getmember(member) 1990 tarinfo = self.getmember(member)
1991 else: 1991 else:
1992 tarinfo = member 1992 tarinfo = member
1993 1993
1994 # Prepare the link target for makelink(). 1994 # Prepare the link target for makelink().
1995 if tarinfo.islnk(): 1995 if tarinfo.islnk():
1996 tarinfo._link_target = os.path.join(path, tarinfo.linkname) 1996 tarinfo._link_target = os.path.join(path, tarinfo.linkname)
1997 1997
1998 try: 1998 try:
1999 self._extract_member(tarinfo, os.path.join(path, tarinfo.name), 1999 joined_path = '/' + tarinfo.name
2000 set_attrs=set_attrs) 2000 # We don't want to join '/home/user' and 'home/user/file' to avoid
2001 # '/home/user/home/user/file'.
2002 if path != joined_path[:len(path)]:
2003 joined_path = os.path.join(path, tarinfo.name)
2004 self._extract_member(tarinfo, joined_path, set_attrs=set_attrs)
2001 except OSError as e: 2005 except OSError as e:
2002 if self.errorlevel > 0: 2006 if self.errorlevel > 0:
2003 raise 2007 raise
2004 else: 2008 else:
2005 if e.filename is None: 2009 if e.filename is None:
2006 self._dbg(1, "tarfile: %s" % e.strerror) 2010 self._dbg(1, "tarfile: %s" % e.strerror)
2007 else: 2011 else:
2008 self._dbg(1, "tarfile: %s %r" % (e.strerror, e.filename)) 2012 self._dbg(1, "tarfile: %s %r" % (e.strerror, e.filename))
2009 except ExtractError as e: 2013 except ExtractError as e:
2010 if self.errorlevel > 1: 2014 if self.errorlevel > 1:
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
2148 os.makedev(tarinfo.devmajor, tarinfo.devminor)) 2152 os.makedev(tarinfo.devmajor, tarinfo.devminor))
2149 2153
2150 def makelink(self, tarinfo, targetpath): 2154 def makelink(self, tarinfo, targetpath):
2151 """Make a (symbolic) link called targetpath. If it cannot be created 2155 """Make a (symbolic) link called targetpath. If it cannot be created
2152 (platform limitation), we try to make a copy of the referenced file 2156 (platform limitation), we try to make a copy of the referenced file
2153 instead of a link. 2157 instead of a link.
2154 """ 2158 """
2155 try: 2159 try:
2156 # For systems that support symbolic and hard links. 2160 # For systems that support symbolic and hard links.
2157 if tarinfo.issym(): 2161 if tarinfo.issym():
2162 if os.path.exists(targetpath):
2163 os.unlink(targetpath)
2158 os.symlink(tarinfo.linkname, targetpath) 2164 os.symlink(tarinfo.linkname, targetpath)
2159 else: 2165 else:
2160 # See extract(). 2166 # See extract().
2161 if os.path.exists(tarinfo._link_target): 2167 if os.path.exists(tarinfo._link_target):
2162 os.link(tarinfo._link_target, targetpath) 2168 os.link(tarinfo._link_target, targetpath)
2163 else: 2169 else:
2164 self._extract_member(self._find_link_target(tarinfo), 2170 self._extract_member(self._find_link_target(tarinfo),
2165 targetpath) 2171 targetpath)
2166 except symlink_exception: 2172 except symlink_exception:
2167 try: 2173 try:
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
2491 tf.add(file_name) 2497 tf.add(file_name)
2492 2498
2493 if args.verbose: 2499 if args.verbose:
2494 print('{!r} file created.'.format(tar_name)) 2500 print('{!r} file created.'.format(tar_name))
2495 2501
2496 else: 2502 else:
2497 parser.exit(1, parser.format_help()) 2503 parser.exit(1, parser.format_help())
2498 2504
2499 if __name__ == '__main__': 2505 if __name__ == '__main__':
2500 main() 2506 main()
OLDNEW
« no previous file with comments | « no previous file | Lib/test/test_tarfile.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+