Skip to content

Commit

Permalink
Limit length of filenames
Browse files Browse the repository at this point in the history
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 committed Oct 18, 2018
1 parent a1e6d60 commit 903ef41
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
10 changes: 10 additions & 0 deletions whipper/common/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ class MissingFrames(Exception):
"""
pass

def truncate_filename(path):
"""
Truncate filename to the max. len. allowed by the path's filesystem
Should handle unicode correctly too
"""
p, f = os.path.split(os.path.normpath(path))
f, e = os.path.splitext(f)
fn_lim = os.pathconf(p, 'PC_NAME_MAX')
length = fn_lim - len(e)
return os.path.join(p, f[:length] + e)

def shrinkPath(path):
"""
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 903ef41

Please sign in to comment.