Skip to content

Commit

Permalink
fix directory race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
lunixbochs committed Jan 3, 2023
1 parent ff31134 commit 9b07520
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions internetarchive/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,11 @@ def download(self, file_path=None, verbose=None, ignore_existing=None,
file_path = file_path or self.name

if destdir:
if not os.path.exists(destdir) and return_responses is not True:
os.mkdir(destdir)
if return_responses is not True:
try:
os.mkdir(destdir)
except FileExistsError:
pass
if os.path.isfile(destdir):
raise OSError(f'{destdir} is not a directory!')
file_path = os.path.join(destdir, file_path)
Expand Down Expand Up @@ -233,12 +236,10 @@ def download(self, file_path=None, verbose=None, ignore_existing=None,
return

parent_dir = os.path.dirname(file_path)
if parent_dir != '' \
and not os.path.exists(parent_dir) \
and return_responses is not True:
os.makedirs(parent_dir)

try:
if parent_dir != '' and return_responses is not True:
os.makedirs(parent_dir, exist_ok=True)

response = self.item.session.get(self.url,
stream=True,
timeout=12,
Expand Down Expand Up @@ -272,8 +273,10 @@ def download(self, file_path=None, verbose=None, ignore_existing=None,
except (RetryError, HTTPError, ConnectTimeout, OSError, ReadTimeout) as exc:
msg = f'error downloading file {file_path}, exception raised: {exc}'
log.error(msg)
if os.path.exists(file_path):
try:
os.remove(file_path)
except OSError:
pass
if verbose:
print(f' {msg}', file=sys.stderr)
if ignore_errors:
Expand Down

0 comments on commit 9b07520

Please sign in to comment.