@@ -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+
585600def 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 ])
0 commit comments