Skip to content

Commit

Permalink
Limit length of filenames (#311)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
JoeLametta authored Oct 22, 2018
1 parent 669b356 commit 02fd962
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
15 changes: 15 additions & 0 deletions whipper/common/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import os.path
import math
import subprocess
import unicodedata

from whipper.extern import asyncsub

Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions whipper/common/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ def verifyImage(self, runner, table):
return accurip.verify_result(self.result, responses, checksums)

def write_m3u(self, discname):
m3uPath = u'%s.m3u' % discname
m3uPath = common.truncate_filename(discname + '.m3u')
with open(m3uPath, 'w') as f:
f.write(u'#EXTM3U\n'.encode('utf-8'))
for track in self.result.tracks:
Expand All @@ -596,7 +596,7 @@ def write_m3u(self, discname):

def writeCue(self, discName):
assert self.result.table.canCue()
cuePath = '%s.cue' % discName
cuePath = common.truncate_filename(discName + '.cue')
logger.debug('write .cue file to %s', cuePath)
handle = open(cuePath, 'w')
# FIXME: do we always want utf-8 ?
Expand All @@ -608,7 +608,7 @@ def writeCue(self, discName):
return cuePath

def writeLog(self, discName, logger):
logPath = '%s.log' % discName
logPath = common.truncate_filename(discName + '.log')
handle = open(logPath, 'w')
log = logger.log(self.result)
handle.write(log.encode('utf-8'))
Expand Down
4 changes: 2 additions & 2 deletions whipper/program/cdparanoia.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,8 @@ def __init__(self, path, table, start, stop, overread, offset=0,
except IOError as e:
if errno.ENAMETOOLONG != e.errno:
raise
path = common.shrinkPath(path)
tmpoutpath = path + u'.part'
path = common.truncate_filename(common.shrinkPath(path))
tmpoutpath = common.truncate_filename(path + u'.part')
open(tmpoutpath, 'wb').close()
self._tmppath = tmpoutpath
self.path = path
Expand Down

0 comments on commit 02fd962

Please sign in to comment.