Skip to content

Commit cf1f63c

Browse files
authored
Implement umask on POSIX (IronLanguages#2108)
1 parent 8ba9450 commit cf1f63c

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎src/core/IronPython.Modules/nt.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,6 +1898,10 @@ public static object urandom(object? n)
18981898
private static readonly object _umaskKey = new object();
18991899

19001900
public static int umask(CodeContext/*!*/ context, int mask) {
1901+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
1902+
return umaskUnix(mask & 0x1FF);
1903+
}
1904+
19011905
mask &= 0x180;
19021906
object oldMask = context.LanguageContext.GetSetModuleState(_umaskKey, mask);
19031907
if (oldMask == null) {

‎src/core/IronPython.Modules/posix.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ private static void chmodUnix(string path, int mode) {
123123
}
124124

125125

126+
[SupportedOSPlatform("linux")]
127+
[SupportedOSPlatform("macos")]
128+
private static int umaskUnix(int mask)
129+
=> (int)NativeConvert.FromFilePermissions(Syscall.umask(NativeConvert.ToFilePermissions((uint)mask)));
130+
131+
126132
[SupportedOSPlatform("linux")]
127133
[SupportedOSPlatform("macos")]
128134
private static void linkUnix(string src, string dst) {

‎tests/suite/modules/system_related/test_os.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# See the LICENSE file in the project root for more information.
44

55
import os
6+
import unittest
67

78
from iptest import IronPythonTestCase, is_osx, is_linux, is_windows, run_test
89

@@ -55,4 +56,21 @@ def test_open_abplus(self):
5556
finally:
5657
os.close(fd)
5758

59+
@unittest.skipIf(is_windows, "umask is emulated on Windows")
60+
def test_umask(self):
61+
old = os.umask(0o022)
62+
try:
63+
self.assertEqual(os.umask(0o077), 0o022)
64+
self.assertEqual(os.umask(0o022), 0o077)
65+
66+
os.umask(0o027)
67+
try:
68+
fd = os.open(self.temp_file, os.O_CREAT | os.O_WRONLY, 0o666)
69+
os.close(fd)
70+
self.assertEqual(os.stat(self.temp_file).st_mode & 0o777, 0o640)
71+
finally:
72+
self.delete_files(self.temp_file)
73+
finally:
74+
os.umask(old)
75+
5876
run_test(__name__)

0 commit comments

Comments
 (0)