Limit length of filenames (#311)

* Limit length of filenames

If whipper generated filenames are longer thant the maximum value supported by the filesystem, the I/O operations are going to fail.
With this commit filenames which may be too long are truncated to the maximum allowable length.

Fixes #197.
This commit is contained in:
JoeLametta
2018-10-22 20:51:14 +02:00
committed by GitHub
parent 669b356daa
commit 02fd962094
3 changed files with 20 additions and 5 deletions

View File

@@ -23,6 +23,7 @@ import os
import os.path
import math
import subprocess
import unicodedata
from whipper.extern import asyncsub
@@ -153,6 +154,20 @@ class MissingFrames(Exception):
pass
def truncate_filename(path):
"""
Truncate filename to the max. len. allowed by the path's filesystem
Hopefully it handles Unicode strings correctly
"""
p, f = os.path.split(os.path.normpath(path))
f, e = os.path.splitext(f)
fn_lim = os.pathconf(p, 'PC_NAME_MAX') # max filenmae length in bytes
max = fn_lim - len(e.encode('utf-8'))
f = unicodedata.normalize('NFC', f)
f_trunc = unicode(f.encode('utf-8')[:max], 'utf-8', errors='ignore')
return os.path.join(p, f_trunc + e)
def shrinkPath(path):
"""
Shrink a full path to a shorter version.