This commit is contained in:
Kovid Goyal 2011-08-15 11:51:48 -06:00
parent 7d5a38355a
commit 25f99cc540

View File

@ -3,7 +3,7 @@ Make strings safe for use as ASCII filenames, while trying to preserve as much
meaning as possible. meaning as possible.
''' '''
import os, errno import os
from math import ceil from math import ceil
from calibre import sanitize_file_name, isbytestring, force_unicode from calibre import sanitize_file_name, isbytestring, force_unicode
@ -152,31 +152,27 @@ def case_preserving_open_file(path, mode='wb', mkdir_mode=0777):
# the first os.listdir works correctly # the first os.listdir works correctly
cpath = components[0].upper() + sep cpath = components[0].upper() + sep
# Create all the directories in path, putting the on disk case version of bdir = path if mode is None else os.path.dirname(path)
if not os.path.exists(bdir):
os.makedirs(bdir, mkdir_mode)
# Walk all the directories in path, putting the on disk case version of
# the directory into cpath # the directory into cpath
dirs = components[1:] if mode is None else components[1:-1] dirs = components[1:] if mode is None else components[1:-1]
for comp in dirs: for comp in dirs:
cdir = os.path.join(cpath, comp) cdir = os.path.join(cpath, comp)
cl = comp.lower()
try: try:
os.mkdir(cdir, mkdir_mode) candidates = [c for c in os.listdir(cpath) if c.lower() == cl]
except OSError as e: except:
if e.errno != errno.EEXIST: # Dont have permission to do the listdir, assume the case is
if not os.path.exists(cdir): # correct
# Check for exists again, as we could have got a permission pass
# denied error else:
raise if len(candidates) == 1:
# This component already exists, ensure the case is correct cdir = os.path.join(cpath, candidates[0])
cl = comp.lower() # else: We are on a case sensitive file system so cdir must already
try: # be correct
candidates = [c for c in os.listdir(cpath) if c.lower() == cl]
except:
# No permission to do a listdir, just use the original case
pass
else:
if len(candidates) == 1:
cdir = os.path.join(cpath, candidates[0])
# else: We are on a case sensitive file system so cdir must already
# be correct
cpath = cdir cpath = cdir
if mode is None: if mode is None: