look up max filename length automatically

This commit is contained in:
Adrian Sampson 2013-02-08 11:02:29 -08:00
parent f652662d5b
commit d4ddfd1091
2 changed files with 20 additions and 5 deletions

View file

@ -1152,6 +1152,7 @@ class Library(BaseLibrary):
"""
pathmod = pathmod or os.path
platform = platform or sys.platform
basedir = basedir or self.directory
# Use a path format based on a query, falling back on the
# default.
@ -1197,15 +1198,15 @@ class Library(BaseLibrary):
subpath += extension.lower()
# Truncate too-long components.
subpath = util.truncate_path(
subpath, pathmod,
beets.config['max_filename_length'].get(int),
)
maxlen = beets.config['max_filename_length'].get(int)
if not maxlen:
# When zero, try to determine from filesystem.
maxlen = util.max_filename_length(self.directory)
subpath = util.truncate_path(subpath, pathmod, maxlen)
if fragment:
return subpath
else:
basedir = basedir or self.directory
return normpath(os.path.join(basedir, subpath))

View file

@ -601,3 +601,17 @@ def command_output(cmd):
if proc.returncode:
raise subprocess.CalledProcessError(proc.returncode, cmd)
return stdout
def max_filename_length(path, fallback=MAX_FILENAME_LENGTH):
"""Attempt to determine the maximum filename length for the
filesystem containing `path`. If it cannot be determined, return a
predetermined fallback value.
"""
if hasattr(os, 'statvfs'):
try:
res = os.statvfs(path)
except OSError:
return fallback
return res[9]
else:
return fallback