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

Side by Side Diff: Lib/tarfile.py

Issue 19974: tarfile doesn't overwrite symlink by directory
Patch Set: Created 6 years, 1 month 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 2061 matching lines...) Expand 10 before | Expand all | Expand 10 after
2072 if upperdirs and not os.path.exists(upperdirs): 2072 if upperdirs and not os.path.exists(upperdirs):
2073 # Create directories that are not part of the archive with 2073 # Create directories that are not part of the archive with
2074 # default permissions. 2074 # default permissions.
2075 os.makedirs(upperdirs) 2075 os.makedirs(upperdirs)
2076 2076
2077 if tarinfo.islnk() or tarinfo.issym(): 2077 if tarinfo.islnk() or tarinfo.issym():
2078 self._dbg(1, "%s -> %s" % (tarinfo.name, tarinfo.linkname)) 2078 self._dbg(1, "%s -> %s" % (tarinfo.name, tarinfo.linkname))
2079 else: 2079 else:
2080 self._dbg(1, tarinfo.name) 2080 self._dbg(1, tarinfo.name)
2081 2081
2082 if os.path.exists(targetpath):
2083 if os.path.isdir(targetpath) and not os.path.islink(targetpath):
2084 os.rmdir(targetpath)
2085 else:
2086 os.remove(targetpath)
2082 if tarinfo.isreg(): 2087 if tarinfo.isreg():
2083 self.makefile(tarinfo, targetpath) 2088 self.makefile(tarinfo, targetpath)
2084 elif tarinfo.isdir(): 2089 elif tarinfo.isdir():
2085 self.makedir(tarinfo, targetpath) 2090 self.makedir(tarinfo, targetpath)
2086 elif tarinfo.isfifo(): 2091 elif tarinfo.isfifo():
2087 self.makefifo(tarinfo, targetpath) 2092 self.makefifo(tarinfo, targetpath)
2088 elif tarinfo.ischr() or tarinfo.isblk(): 2093 elif tarinfo.ischr() or tarinfo.isblk():
2089 self.makedev(tarinfo, targetpath) 2094 self.makedev(tarinfo, targetpath)
2090 elif tarinfo.islnk() or tarinfo.issym(): 2095 elif tarinfo.islnk() or tarinfo.issym():
2091 self.makelink(tarinfo, targetpath) 2096 self.makelink(tarinfo, targetpath)
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
2162 os.makedev(tarinfo.devmajor, tarinfo.devminor)) 2167 os.makedev(tarinfo.devmajor, tarinfo.devminor))
2163 2168
2164 def makelink(self, tarinfo, targetpath): 2169 def makelink(self, tarinfo, targetpath):
2165 """Make a (symbolic) link called targetpath. If it cannot be created 2170 """Make a (symbolic) link called targetpath. If it cannot be created
2166 (platform limitation), we try to make a copy of the referenced file 2171 (platform limitation), we try to make a copy of the referenced file
2167 instead of a link. 2172 instead of a link.
2168 """ 2173 """
2169 try: 2174 try:
2170 # For systems that support symbolic and hard links. 2175 # For systems that support symbolic and hard links.
2171 if tarinfo.issym(): 2176 if tarinfo.issym():
2172 os.symlink(tarinfo.linkname, targetpath) 2177 if os.name == 'nt' and os.path.isdir(tarinfo.linkname):
2178 os.symlink(tarinfo.linkname, targetpath, target_is_directory =True)
2179 else:
2180 os.symlink(tarinfo.linkname, targetpath)
2173 else: 2181 else:
2174 # See extract(). 2182 # See extract().
2175 if os.path.exists(tarinfo._link_target): 2183 if os.path.exists(tarinfo._link_target):
2176 os.link(tarinfo._link_target, targetpath) 2184 os.link(tarinfo._link_target, targetpath)
2177 else: 2185 else:
2178 self._extract_member(self._find_link_target(tarinfo), 2186 self._extract_member(self._find_link_target(tarinfo),
2179 targetpath) 2187 targetpath)
2180 except symlink_exception: 2188 except symlink_exception:
2181 try: 2189 try:
2182 self._extract_member(self._find_link_target(tarinfo), 2190 self._extract_member(self._find_link_target(tarinfo),
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
2505 tf.add(file_name) 2513 tf.add(file_name)
2506 2514
2507 if args.verbose: 2515 if args.verbose:
2508 print('{!r} file created.'.format(tar_name)) 2516 print('{!r} file created.'.format(tar_name))
2509 2517
2510 else: 2518 else:
2511 parser.exit(1, parser.format_help()) 2519 parser.exit(1, parser.format_help())
2512 2520
2513 if __name__ == '__main__': 2521 if __name__ == '__main__':
2514 main() 2522 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+