Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-67837, gh-112998: Fix dirs creation in concurrent extraction #115082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2411,7 +2411,7 @@ def _extract_member(self, tarinfo, targetpath, set_attrs=True,
if upperdirs and not os.path.exists(upperdirs):
# Create directories that are not part of the archive with
# default permissions.
os.makedirs(upperdirs)
os.makedirs(upperdirs, exist_ok=True)

if tarinfo.islnk() or tarinfo.issym():
self._dbg(1, "%s -> %s" % (tarinfo.name, tarinfo.linkname))
Expand Down
22 changes: 22 additions & 0 deletions Lib/test/archiver_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import sys

from test.support import swap_attr
from test.support import os_helper

class OverwriteTests:
Expand Down Expand Up @@ -153,3 +154,24 @@ def test_overwrite_broken_dir_symlink_as_implicit_dir(self):
self.extractall(ar)
self.assertTrue(os.path.islink(target))
self.assertFalse(os.path.exists(target2))

def test_concurrent_extract_dir(self):
target = os.path.join(self.testdir, 'test')
def concurrent_mkdir(*args, **kwargs):
orig_mkdir(*args, **kwargs)
orig_mkdir(*args, **kwargs)
with swap_attr(os, 'mkdir', concurrent_mkdir) as orig_mkdir:
with self.open(self.ar_with_dir) as ar:
self.extractall(ar)
self.assertTrue(os.path.isdir(target))

def test_concurrent_extract_implicit_dir(self):
target = os.path.join(self.testdir, 'test')
def concurrent_mkdir(*args, **kwargs):
orig_mkdir(*args, **kwargs)
orig_mkdir(*args, **kwargs)
with swap_attr(os, 'mkdir', concurrent_mkdir) as orig_mkdir:
with self.open(self.ar_with_implicit_dir) as ar:
self.extractall(ar)
self.assertTrue(os.path.isdir(target))
self.assertTrue(os.path.isfile(os.path.join(target, 'file')))
8 changes: 6 additions & 2 deletions Lib/zipfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1802,11 +1802,15 @@ def _extract_member(self, member, targetpath, pwd):
# Create all upper directories if necessary.
upperdirs = os.path.dirname(targetpath)
if upperdirs and not os.path.exists(upperdirs):
os.makedirs(upperdirs)
os.makedirs(upperdirs, exist_ok=True)

if member.is_dir():
if not os.path.isdir(targetpath):
os.mkdir(targetpath)
try:
os.mkdir(targetpath)
except FileExistsError:
if not os.path.isdir(targetpath):
raise
return targetpath

with self.open(member, pwd=pwd) as source, \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Avoid race conditions in the creation of directories during concurrent
extraction in :mod:`tarfile` and :mod:`zipfile`.