Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit length of filenames #311

Merged
merged 4 commits into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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