Edit Book: More descriptive error message when saving fails because of a bad umask

This commit is contained in:
Kovid Goyal 2014-10-06 21:36:47 +05:30
parent 37352fefd7
commit d96714ed24
2 changed files with 25 additions and 2 deletions

View File

@ -16,7 +16,7 @@ from calibre.constants import iswindows
from calibre.ptempfile import PersistentTemporaryFile
from calibre.gui2.progress_indicator import ProgressIndicator
from calibre.utils import join_with_timeout
from calibre.utils.filenames import atomic_rename
from calibre.utils.filenames import atomic_rename, format_permissions
from calibre.utils.ipc import RC
def save_container(container, path):
@ -33,7 +33,13 @@ def save_container(container, path):
# path may not exist if we are saving a copy, in which case we use
# the metadata from the original book
st = os.stat(container.path_to_ebook)
os.fchmod(fno, st.st_mode)
try:
os.fchmod(fno, st.st_mode)
except EnvironmentError as err:
if err.errno != errno.EPERM:
raise
raise EnvironmentError('Failed to change permissions of %s to %s (%s), with error: %s. Most likely the %s directory has a restrictive umask' % (
temp.name, oct(st.st_mode), format_permissions(st.st_mode), errno.errorcode[err.errno], os.path.dirname(temp.name)))
try:
os.fchown(fno, st.st_uid, st.st_gid)
except EnvironmentError as err:

View File

@ -506,3 +506,20 @@ if iswindows:
return userhome + path[i:]
else:
expanduser = os.path.expanduser
def format_permissions(st_mode):
import stat
for func, letter in (x.split(':') for x in 'REG:- DIR:d BLK:b CHR:c FIFO:p LNK:l SOCK:s'.split()):
if getattr(stat, 'S_IS' + func)(st_mode):
break
else:
letter = '?'
rwx = ('---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx')
ans = [letter] + list(rwx[(st_mode >> 6) & 7]) + list(rwx[(st_mode >> 3) & 7]) + list(rwx[(st_mode & 7)])
if st_mode & stat.S_ISUID:
ans[3] = 's' if (st_mode & stat.S_IXUSR) else 'S'
if st_mode & stat.S_ISGID:
ans[6] = 's' if (st_mode & stat.S_IXGRP) else 'l'
if st_mode & stat.S_ISVTX:
ans[9] = 't' if (st_mode & stat.S_IXUSR) else 'T'
return ''.join(ans)