]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Refactor the setting of the mode of output files 829/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 7 Oct 2021 14:22:12 +0000 (16:22 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 7 Oct 2021 14:25:44 +0000 (16:25 +0200)
This doesn't change anything, but is a bit nicer to read.

mkosi/__init__.py

index d40fee4b1d1f804fc829917aef5967fd1cbe49ca..328b7984a5ad2dc2e0cf51605599228b447150e3 100644 (file)
@@ -4167,12 +4167,22 @@ def save_cache(args: CommandLineArguments, root: Path, raw: Optional[str], cache
             shutil.move(cast(str, root), cache_path)  # typing bug, .move() accepts Path
 
 
-def _link_output(args: CommandLineArguments, oldpath: PathString, newpath: PathString) -> None:
+def _link_output(
+        args: CommandLineArguments,
+        oldpath: PathString,
+        newpath: PathString,
+        mode: int = 0o666,
+) -> None:
+
     assert oldpath is not None
     assert newpath is not None
 
-    os.chmod(oldpath, 0o666 & ~args.original_umask)
+    # Temporary files created by tempfile have mode trimmed to the user.
+    # After we are done writing files, adjust the mode to the default specified by umask.
+    os.chmod(oldpath, mode & ~args.original_umask)
+
     os.link(oldpath, newpath)
+
     if args.no_chown:
         return
 
@@ -4261,8 +4271,7 @@ def link_output_sshkey(args: CommandLineArguments, sshkey: Optional[SomeIO]) ->
     if sshkey:
         assert args.output_sshkey
         with complete_step("Linking private ssh key file…", f"Linked {path_relative_to_cwd(args.output_sshkey)}"):
-            _link_output(args, sshkey.name, args.output_sshkey)
-            os.chmod(args.output_sshkey, 0o600)
+            _link_output(args, sshkey.name, args.output_sshkey, mode=0o600)
 
 
 def link_output_split_root(args: CommandLineArguments, split_root: Optional[SomeIO]) -> None: