Skip to content

Commit b975a7b

Browse files
Reapply "[lit] Implement builtin umask (#94621)" (#155850)
This reverts commit faa4e35. This was originally reverted because it was using a Python 3.9 feature (umask in subprocess.Popen) when LLVM only requires Python 3.8. This patch uses os.umask instead, which has been around for longer.
1 parent a44ce1b commit b975a7b

File tree

9 files changed

+66
-7
lines changed

9 files changed

+66
-7
lines changed

llvm/test/tools/llvm-dwarfutil/ELF/X86/mirror-permissions-unix.test

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
## Setting the umask to 0 ensures deterministic permissions across
44
## test environments.
55
# UNSUPPORTED: system-windows
6-
# REQUIRES: shell
76

87
# RUN: touch %t
98
# RUN: chmod 0777 %t

llvm/test/tools/llvm-objcopy/ELF/mirror-permissions-unix.test

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
## Setting the umask to 0 ensures deterministic permissions across
77
## test environments.
88
# UNSUPPORTED: system-windows
9-
# REQUIRES: shell
109

1110
# RUN: touch %t
1211
# RUN: chmod 0777 %t

llvm/test/tools/llvm-objcopy/ELF/respect-umask.test

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
## This tests that the umask is respected when
22
## assigning permissions of output files.
33

4-
## Windows has no umask so this test makes no sense, nor would
5-
## it work because there is no umask(1) in a Windows environment
4+
## Windows has no umask so this test makes no sense.
65
# UNSUPPORTED: system-windows
7-
# REQUIRES: shell
86

97
# RUN: rm -f %t
108
# RUN: touch %t

llvm/utils/lit/lit/TestRunner.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ class ShellEnvironment(object):
9898
we maintain a dir stack for pushd/popd.
9999
"""
100100

101-
def __init__(self, cwd, env):
101+
def __init__(self, cwd, env, umask=-1):
102102
self.cwd = cwd
103103
self.env = dict(env)
104+
self.umask = umask
104105
self.dirStack = []
105106

106107
def change_dir(self, newdir):
@@ -582,6 +583,20 @@ class SHFILEOPSTRUCTW(Structure):
582583
return ShellCommandResult(cmd, "", stderr.getvalue(), exitCode, False)
583584

584585

586+
def executeBuiltinUmask(cmd, shenv):
587+
"""executeBuiltinUmask - Change the current umask."""
588+
if os.name != "posix":
589+
raise InternalShellError(cmd, "'umask' not supported on this system")
590+
if len(cmd.args) != 2:
591+
raise InternalShellError(cmd, "'umask' supports only one argument")
592+
try:
593+
# Update the umask in the parent environment.
594+
shenv.umask = int(cmd.args[1], 8)
595+
except ValueError as err:
596+
raise InternalShellError(cmd, "Error: 'umask': %s" % str(err))
597+
return ShellCommandResult(cmd, "", "", 0, False)
598+
599+
585600
def executeBuiltinColon(cmd, cmd_shenv):
586601
"""executeBuiltinColon - Discard arguments and exit with status 0."""
587602
return ShellCommandResult(cmd, "", "", 0, False)
@@ -736,6 +751,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
736751
"popd": executeBuiltinPopd,
737752
"pushd": executeBuiltinPushd,
738753
"rm": executeBuiltinRm,
754+
"umask": executeBuiltinUmask,
739755
":": executeBuiltinColon,
740756
}
741757
# To avoid deadlock, we use a single stderr stream for piped
@@ -757,7 +773,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
757773
# env FOO=1 llc < %s | env BAR=2 llvm-mc | FileCheck %s
758774
# env FOO=1 %{another_env_plus_cmd} | FileCheck %s
759775
if cmd_shenv is shenv:
760-
cmd_shenv = ShellEnvironment(shenv.cwd, shenv.env)
776+
cmd_shenv = ShellEnvironment(shenv.cwd, shenv.env, shenv.umask)
761777
args = updateEnv(cmd_shenv, args)
762778
if not args:
763779
# Return the environment variables if no argument is provided.
@@ -902,6 +918,13 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
902918
args = quote_windows_command(args)
903919

904920
try:
921+
# TODO(boomanaiden154): We currently wrap the subprocess.Popen with
922+
# os.umask as the umask argument in subprocess.Popen is not
923+
# available before Python 3.9. Once LLVM requires at least Python
924+
# 3.9, this code should be updated to use umask argument.
925+
old_umask = -1
926+
if cmd_shenv.umask != -1:
927+
old_umask = os.umask(cmd_shenv.umask)
905928
procs.append(
906929
subprocess.Popen(
907930
args,
@@ -916,6 +939,8 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
916939
errors="replace",
917940
)
918941
)
942+
if old_umask != -1:
943+
os.umask(old_umask)
919944
proc_not_counts.append(not_count)
920945
# Let the helper know about this process
921946
timeoutHelper.addProcess(procs[-1])
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import lit.formats
2+
3+
config.name = "shtest-umask"
4+
config.suffixes = [".txt"]
5+
config.test_format = lit.formats.ShTest(execute_external=False)
6+
if sys.platform.startswith("win") or sys.platform.startswith("cygwin"):
7+
config.available_features.add("system-windows")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# RUN: umask bad
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## Windows has no umask so this test makes no sense.
2+
# UNSUPPORTED: system-windows
3+
4+
# RUN: touch %t
5+
# RUN: chmod 644 %t && ls -l %t | cut -f 1 -d ' ' > %t.644
6+
# RUN: chmod 600 %t && ls -l %t | cut -f 1 -d ' ' > %t.600
7+
# RUN: chmod 666 %t && ls -l %t | cut -f 1 -d ' ' > %t.666
8+
9+
# RUN: umask 022 && rm %t && touch %t && ls -l %t | cut -f 1 -d ' ' | cmp - %t.644
10+
# RUN: umask 177 && rm %t && touch %t && ls -l %t | cut -f 1 -d ' ' | cmp - %t.600
11+
# RUN: umask 000 && rm %t && touch %t && ls -l %t | cut -f 1 -d ' ' | cmp - %t.666
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# RUN: umask 0 0
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Check the umask command
2+
3+
# RUN: not %{lit} -a -v %{inputs}/shtest-umask | FileCheck -match-full-lines %s
4+
5+
# CHECK: -- Testing: 3 tests{{.*}}
6+
7+
# CHECK-LABEL: FAIL: shtest-umask :: umask-bad-arg.txt ({{[^)]*}})
8+
# CHECK: umask bad
9+
# CHECK: # | Error: 'umask': invalid literal {{.*}}
10+
11+
# CHECK-LABEL: FAIL: shtest-umask :: umask-too-many-args.txt ({{[^)]*}})
12+
# CHECK: umask 0 0
13+
# CHECK: # | 'umask' supports only one argument
14+
15+
# CHECK: Total Discovered Tests: 3
16+
# CHECK: {{Passed|Unsupported}}: 1 {{\([0-9]*\.[0-9]*%\)}}
17+
# CHECK: Failed{{ *}}: 2 {{\([0-9]*\.[0-9]*%\)}}
18+
# CHECK-NOT: {{.}}

0 commit comments

Comments
 (0)