virtinst: Fix TOCTOU race in user scratch dir creation

When starting many virt-install processes in parallel, some often crash
with

    ERROR    [Errno 17] File exists: '/home/kstest/.cache/virt-manager/boot'

Fix that by ignoring existing directories instead of explicitly testing
for existence.

The `exist_ok` parameter exists since Python 3.2, and the minimum
supported version is 3.4 now.
This commit is contained in:
Martin Pitt 2020-11-03 15:50:27 +01:00 committed by Cole Robinson
parent d0650aa48f
commit 7cb6a6062a
1 changed files with 1 additions and 2 deletions

View File

@ -94,8 +94,7 @@ class InstallerTreeMedia(object):
if (guest.conn.is_unprivileged() or if (guest.conn.is_unprivileged() or
not os.path.exists(system_scratchdir) or not os.path.exists(system_scratchdir) or
not os.access(system_scratchdir, os.W_OK)): not os.access(system_scratchdir, os.W_OK)):
if not os.path.exists(user_scratchdir): os.makedirs(user_scratchdir, 0o751, exist_ok=True)
os.makedirs(user_scratchdir, 0o751) # pragma: no cover
return user_scratchdir return user_scratchdir
return system_scratchdir # pragma: no cover return system_scratchdir # pragma: no cover