-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Change default umask
from 777
to 022
#22589
Merged
+71
−1
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b7d046d
Set umask to 027
fluiddot a42e9e0
Add umask unit test
fluiddot b0d7dcf
Move `test_umask` to core suite
fluiddot 1ff58dd
Remove `atexit` from test
fluiddot 3d69a91
Fix indentation
fluiddot 48487f1
Update year of copyright
fluiddot 75827a0
Mark `test_umask` as `crossplatform` and to be tested with `wasmfs`
fluiddot 9619d38
Move `test_umask` unit test file to `other`
fluiddot 2891de1
Change default `umask` to `022`
fluiddot 567027a
Merge branch 'main' into update-umask
fluiddot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2024 The Emscripten Authors. All rights reserved. | ||
* Emscripten is available under two separate licenses, the MIT license and the | ||
* University of Illinois/NCSA Open Source License. Both these licenses can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
#include <assert.h> | ||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <signal.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
#include <sys/stat.h> | ||
#include <sys/types.h> | ||
|
||
mode_t get_umask() { | ||
mode_t current = umask(0); // Set umask to 0 and get the old value | ||
umask(current); // Immediately set it back | ||
return current; | ||
} | ||
|
||
void create_file(const char *path, const char *buffer) { | ||
mode_t mode = 0777 - get_umask(); | ||
int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode); | ||
assert(fd >= 0); | ||
|
||
int err = write(fd, buffer, sizeof(char) * strlen(buffer)); | ||
assert(err == (sizeof(char) * strlen(buffer))); | ||
|
||
close(fd); | ||
} | ||
|
||
int main() { | ||
// Get the default umask | ||
mode_t default_umask = get_umask(); | ||
printf("default umask: %o\n", default_umask); | ||
assert(default_umask == 022); | ||
|
||
// Create a new file with default umask | ||
create_file("umask_test_file", "abcdef"); | ||
struct stat st; | ||
stat("umask_test_file", &st); | ||
printf("default_umask - stat: %o\n", st.st_mode); | ||
assert((st.st_mode & 0666) == 0644); | ||
unlink("umask_test_file"); | ||
|
||
// Set new umask | ||
mode_t new_umask = 027; | ||
mode_t old_umask = umask(new_umask); | ||
|
||
// Create a new file with new umask | ||
create_file("umask_test_file", "abcdef"); | ||
stat("umask_test_file", &st); | ||
printf("new_umask - stat: %o\n", st.st_mode); | ||
assert((st.st_mode & 0666) == 0640); | ||
|
||
// Restore the old umask | ||
umask(old_umask); | ||
|
||
puts("success"); | ||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference, here are the definitions of values used here:
emscripten/system/lib/libc/musl/include/sys/stat.h
Lines 60 to 74 in 8eb432e