This PR makes Geany switch documents to read-only mode when the files being opened do not have write permission. This will prevent the user from making changes that cannot later be saved. If the document is saved with a different name, read-only mode will be turned off. The document can also be manually switched out of read-only mode from the Documents menu.
`g_access()` is used to check permissions. On Windows, it does not do a full permissions check, so files the user does not have permissions to change may still be opened in normal editing mode. You can view, comment on, or merge this pull request online at:
https://github.com/geany/geany/pull/2989
-- Commit Summary --
* <a href="https://github.com/geany/geany/pull/2989/commits/4917d296bf31b128276a5a0dd1e1d2aded61a431">Open files without write permission as read only</a>
-- File Changes --
M src/document.c (5)
-- Patch Links --
https://github.com/geany/geany/pull/2989.patch https://github.com/geany/geany/pull/2989.diff
I just had `g_access()` report that a file does not have write permission when it definitely does (644). So a different method to detect write permissions needs to be used.
AFAIR Geany deliberately allows editing buffers that cannot be written, instead of forcing the file to be saved elsewhere or the permissions changed before allowing editing. For me this is an unwanted change.
@elextr I can move this behind an option. As far as I can tell, this cannot be implemented in a plugin because there is no function to change a document to read-only mode and just changing the document readonly flag is insufficient.
@xiota sorry for the short response, I was possibly going to be away for a few days but that turns out not to be the case. I didn't want you to do work unnecessarily before we discuss it.
To me there are two problems with the concept, technical and philosophical.
The philosophical problem is that Geany does not edit files, it edits documents in memory. There is no reason for the two to match. Therefore there is nothing linking the state of the document in memory (in this case editability) to the file state (in this case permissions) because Geany has no idea what the user wants to do with their document and is likely to be wrong just as often as right.
And "writable" is not a static state, it can change at any time, for example if permissions change or a network stutters. That means a state determined at opening can be wrong at any point thereafter. But it doesn't matter if its wrong because the document in memory is not the file and so shouldn't be linked to the file, except when actual write operations are performed.
But the technical issues are even greater, first I suggest you read my [PHD thesis](https://wiki.geany.org/config/all_you_never_wanted_to_know_about_file_saving) on saving files and note the differing ways Geany can do it.
Not all saving methods depend on the writability of the file, and others depend on more than just that. For example atomic saving writes a new file and renames it over the old one. On Linux that needs write permission to the directory, not the existing file. On windows rename has a number of "interesting" kinks and can fail for other reasons. So the accuracy of determining if a file is "writable" is not good in any but the simplest case. And the simplest case isn't the default in Geany.
So not only can "writable" change at any time, but it can't even be determined very well at the initial load file point in time.
As @eht16 has said "who knew it was so hard to write a file" :smile:
Closed #2989.
github-comments@lists.geany.org