Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs::File::create() on Unix should use 0666 mode by default. #22174

Closed
rodrigorc opened this issue Feb 11, 2015 · 5 comments
Closed

fs::File::create() on Unix should use 0666 mode by default. #22174

rodrigorc opened this issue Feb 11, 2015 · 5 comments

Comments

@rodrigorc
Copy link

Compare the output of this rust code

fn main() {
    std::fs::File::create("test1.txt");
}

with the C equivalent:

int main() { fopen("test2.txt", "w"); }

or the C++ one:

#include <fstream>
int main() { std::ofstream f("test3.txt"); }

The output of these programs is:

$ (compile & run)
$ ls -l test?.txt
-rw------- 1 rodrigo rodrigo 0 Feb 11 12:00 test1.txt
-rw-r--r-- 1 rodrigo rodrigo 0 Feb 11 12:00 test2.txt
-rw-r--r-- 1 rodrigo rodrigo 0 Feb 11 12:00 test3.txt

The C and C++ versions will create a file with mode 0666 (minus the umask, mine is 0022). But the Rust one is different: 0600.

Given the Principle of Least Surprise, I think that the default mode should be changed from 0600 to 0666.

@GuillaumeGomez
Copy link
Member

There is no direct equivalent in C. However, it doesn't seem a good idea to give a create function without forcing the user to give permission. I think a change on that should be done (I think I'll right a RFC to change that).

@rodrigorc
Copy link
Author

I don't understand why you think my C code is not equivalent. fopen() is the most C-ish way of creating files. Maybe you are thinking of open() but that is the POSIX way, not the C one.

Let me cite "man 3 fopen":

Any created files will have mode S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH (0666), as modified by the process's umask value (see umask(2)).

About forcing the user to set permission (open() does that), but fopen() does just fine without any mechanism to set permissions at all. It could be a good idea anyway, but remember that in Windows the permissions work quite differently, so maybe there is no easy equivalent.

@alexcrichton
Copy link
Member

Yes I think that this change was just carried over from the previous implementation. The relevant line to change would be this one and I think it's fine to make the change.

@GuillaumeGomez
Copy link
Member

Then I change it.

@alexcrichton
Copy link
Member

Fixed in #22186

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants