Commits

Adrian Sampson  committed 2e19f72

look up max filename length automatically

  • Participants
  • Parent commits b0bc4d0

Comments (0)

Files changed (2)

File beets/library.py

         """
         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.
         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))
 
 

File beets/util/__init__.py

     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