From 66c4618556225eed4e2625dd55f591cdae252c35 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 6 Nov 2015 20:25:24 +0530 Subject: [PATCH] Fix #1513849 [Edit E-Book will not save after editing if original file is not in its directory.](https://bugs.launchpad.net/calibre/+bug/1513849) --- src/calibre/gui2/tweak_book/save.py | 39 +++++++++++++++++------------ 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/calibre/gui2/tweak_book/save.py b/src/calibre/gui2/tweak_book/save.py index fc6babb648..6dedb8ab92 100644 --- a/src/calibre/gui2/tweak_book/save.py +++ b/src/calibre/gui2/tweak_book/save.py @@ -34,6 +34,7 @@ def save_container(container, path): if hasattr(os, 'fchmod'): # Ensure file permissions and owner information is preserved fno = temp.fileno() + st = None try: st = os.stat(path) except EnvironmentError as err: @@ -41,22 +42,28 @@ def save_container(container, path): raise # 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) - 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: - if err.errno not in (errno.EPERM, errno.EACCES): - # ignore chown failure as user could be editing file belonging - # to a different user, in which case we really cant do anything - # about it short of making the file update non-atomic - raise + try: + st = os.stat(container.path_to_ebook) + except EnvironmentError as err: + if err.errno != errno.ENOENT: + raise + # Somebody deleted the original file + if st is not None: + 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: + if err.errno not in (errno.EPERM, errno.EACCES): + # ignore chown failure as user could be editing file belonging + # to a different user, in which case we really cant do anything + # about it short of making the file update non-atomic + raise temp.close() temp = temp.name