Quote:
Originally Posted by kovidgoyal
Why are you even editing read only files? what is it you hope to achieve by doing that? Editing a file that you dont want changed is well...
|
Why are you even thinking I'm
editing read-only files? Of course I don't
edit them. But I do open and keep them open so I can quickly consult them while indeed editing other ones. I make them write-protected on purpose, to make sure they stay unchanged, because I open them in an
editor after all, and s#%t happens. You make a mistake and edit wrong file, and then too quickly click those “Save” and “Close” buttons, giving yourself no chance to revert the changes. Or your cat steps on the ‘Delete’ key on your keyboard while you went to the loo and you don't notice when you're back. You do know Murphy's law, don't you? That read-only file attribute is supposed to protect you from such things, so it's just not right to silently ignore/remove that protection and overwrite files without owner's knowledge and approval…
Quote:
Originally Posted by kovidgoyal
there could be a warning dialog, but implementing one is too much effort for this, at least for me, patches welcome.
|
Here's a patch for the problem Lomkiri originally reported. With this applied, if the original file is write-protected, ‘Save’ will fail with the ‘Could not save’ error message, but ‘Save a copy’ will succeed. I think it works OK but I only tested it with epubs, and I'm not a proper Python programmer, so someone better verify it.
Code:
--- calibre-6.10.0/src/calibre/gui2/tweak_book/save.py.6.10.0.orig 2022-12-16 03:29:36.000000000 +0100
+++ calibre-6.10.0/src/calibre/gui2/tweak_book/save.py 2022-12-23 23:01:01.483650018 +0100
@@ -48,7 +48,16 @@
raise
# Somebody deleted the original file
+ if st is not None:
+ try:
+ os.fchmod(fno, st.st_mode | stat.S_IWUSR)
+ except OSError as err:
+ if err.errno != errno.EPERM:
+ raise
+ raise OSError('Failed to change permissions of {} to {} ({}), with error: {}. Most likely the {} directory has a restrictive umask'.format(
+ temp.name, oct(st.st_mode), format_permissions(st.st_mode), errno.errorcode[err.errno], os.path.dirname(temp.name)))
+ st = os.stat(fno)
if st is not None:
try:
- os.fchmod(fno, st.st_mode | stat.S_IWUSR)
+ os.fchmod(fno, st.st_mode)
except OSError as err:
if err.errno != errno.EPERM:
Quote:
Originally Posted by kovidgoyal
On second thoughts, its much easier to show the warning once when the book is opened
|
Well, perhaps useful for someone unaware of opening a read-only file, but not good enough for me.