1

Is it possible to set the permissions on a buffer before it's written to a file? Or is it necessary to change the permissions after writing it?

(technically this would allow some small time-frame for another user to read the file, I would rather avoid this).


Note that this is for a package that writes files, so I rather not change the default system/emacs wide.

ideasman42
  • 8,375
  • 1
  • 28
  • 105
  • Not sure what the scenario is: who is the owner of the emacs process? Is there a shared directory that it is writing into? Why can't you have private directories? All of this seems more appropriate to Unix & Linux SE than to Emacs SE. – NickD Dec 16 '21 at 23:39
  • The directory isn't shared, it's in `~/.config/emacs` - would this be a case where permissions should be 600? – ideasman42 Dec 17 '21 at 00:16
  • 2
    So you chmod the directory to 700 and you are done - once and for all. – NickD Dec 17 '21 at 02:48
  • 1
    When I want to create a new read-only file once in a while I save it already when it's empty; change the protection of the saved file; and then continue to edit it. The autosave file, the backup file, and the next save will all be protected. It's a bit clumsy perhaps, but is enough if this is something seldom done. – pst Dec 17 '21 at 07:08

1 Answers1

2

Change your umask so that only your user can read newly–created files. Note that this can be done in the shell before you start Emacs (shells usually provide a command called umask for this purpose), or while Emacs is running by calling set-default-file-modes. See chapter 26.7 Changing File Names and Attributes.

Note that all processes have a umask, so you can use this solution with all programs, not just Emacs.

Edit: your modification changes the nature of the question quite significantly. However, if you take another look at chapter 26.7 Changing File Names and Attributes of the Emacs Lisp manual, you will find a macro called with-file-modes that allows you to do exactly what you want.

Also, NickD’s suggestion to simply change the permissions of the cache directory is a sound one. It doesn’t matter much if the files in a directory are readable or not if other users cannot traverse into it.

db48x
  • 15,741
  • 1
  • 19
  • 23