Skip to content

Commit

Permalink
sanitize_dir: use pathlib to handle case-insensitive filesystems
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Dec 31, 2019
1 parent e9158b4 commit f1ac150
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions mesonbuild/coredata.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ def sanitize_prefix(self, prefix):
prefix = prefix[:-1]
return prefix

def sanitize_dir_option_value(self, prefix, option, value):
def sanitize_dir_option_value(self, prefix: str, option: str, value: PurePath) -> str:
'''
If the option is an installation directory option and the value is an
absolute path, check that it resides within prefix and return the value
Expand All @@ -475,21 +475,25 @@ def sanitize_dir_option_value(self, prefix, option, value):
This way everyone can do f.ex, get_option('libdir') and be sure to get
the library directory relative to prefix.
'''
if option.endswith('dir') and os.path.isabs(value) and \
value = PurePath(value)
if option.endswith('dir') and value.is_absolute() and \
option not in builtin_dir_noprefix_options:
# Value must be a subdir of the prefix
# commonpath will always return a path in the native format, so we
# must use pathlib.PurePath to do the same conversion before
# comparing.
if os.path.commonpath([value, prefix]) != str(PurePath(prefix)):
m = 'The value of the {!r} option is {!r} which must be a ' \
'subdir of the prefix {!r}.\nNote that if you pass a ' \
'relative path, it is assumed to be a subdir of prefix.'
raise MesonException(m.format(option, value, prefix))
# Convert path to be relative to prefix
skip = len(prefix) + 1
value = value[skip:]
return value
msg = ('The value of the {!r} option is {!r} which must be a '
'subdir of the prefix {!r}.\nNote that if you pass a '
'relative path, it is assumed to be a subdir of prefix.')
# os.path.commonpath doesn't understand case-insensitive filesystems,
# but PurePath().relative_to() does.
try:
value = value.relative_to(prefix)
except ValueError:
raise MesonException(msg.format(option, value, prefix))
if '..' in str(value):
raise MesonException(msg.format(option, value, prefix))
return str(value)

def init_builtins(self):
# Create builtin options with default values
Expand Down

0 comments on commit f1ac150

Please sign in to comment.