Today, tempfile sets the permissions to 0600. When the file is persisted, this mode is then propagated. Unfortunately 0600 isn't a universally applicable mode, especially in cases where the temporary file is being shared with peers on the machine. This is kind-of-but-not-really easy to remedy: programs can chmod the file as they wish.
However, I think it is a mistake to set the mode by default at all.
In order for a program to obey the configured umask, the Rust program has to either parse /proc/self/status
for the umask, or set / reset the current program's umask. This second option isn't thread safe, and the first option is a bit yucky too. The program then has to apply the umask and calculate the appropriate bits to chmod the file. There could of course be an error in the duration, leaving a file with an improper mode.
One reason this is such a pain is when libraries I don't necessarily control use and persist a NamedTemporaryFile. Should the libraries do the umask dance? I don't think so: it isn't safe for them to set/reset the umask since they don't know if they're in a multi-threaded environment. I'd rather them not depend on /proc
either, since I'd like to use them in environments without /proc
if possible.
I've seen various pull requests and issues about this option: approximately a third of the open issues are related to this. I think those options are okay, but what I would really like to see is a function that makes no statements about mode at all and defers to the operating environment of the program:
pub fn create_named_from_umask(path: &Path, open_options: &mut OpenOptions) -> io::Result<File> {
open_options.read(true).write(true).create_new(true);
open_options.open(path)
}
Is this something that would be interesting?