Skip to content

Commit

Permalink
FIX: Avoid directory clobber during zip extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
mgxd committed Mar 18, 2024
1 parent b172865 commit 7ede8f2
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions templateflow/conf/_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,28 @@ def _update_skeleton(skel_file, dest, overwrite=True, silent=False):
dest = Path(dest)
dest.mkdir(exist_ok=True, parents=True)
with ZipFile(skel_file, 'r') as zipref:
allfiles = sorted(zipref.namelist())

if overwrite:
zipref.extractall(str(dest))
return True
newfiles = allfiles
else:
current_files = [s.relative_to(dest) for s in dest.glob('**/*')]
existing = sorted({'%s/' % s.parent for s in current_files}) + [
str(s) for s in current_files
]
newfiles = sorted(set(allfiles) - set(existing))

allfiles = zipref.namelist()
current_files = [s.relative_to(dest) for s in dest.glob('**/*')]
existing = sorted({'%s/' % s.parent for s in current_files}) + [
str(s) for s in current_files
]
newfiles = sorted(set(allfiles) - set(existing))
if newfiles:
if not silent:
print(
'Updating TEMPLATEFLOW_HOME using S3. Adding:\n%s'
% '\n'.join(newfiles)
)
zipref.extractall(str(dest), members=newfiles)
for fl in newfiles:
localpath = dest / fl
if localpath.is_dir(): # avoid extracting existing directories
continue
zipref.extract(fl, path=dest)
return True
if not silent:
print('TEMPLATEFLOW_HOME directory (S3 type) was up-to-date.')
Expand Down

0 comments on commit 7ede8f2

Please sign in to comment.