From 0b1be57e08622149c108cab93cb5c0e77ca39925 Mon Sep 17 00:00:00 2001 From: Jordan Galby <67924449+Jordan474@users.noreply.github.com> Date: Thu, 19 May 2022 17:50:24 +0200 Subject: Fix spack install chgrp on symlinks (#30743) Fixes missing chgrp on symlinks in package installations, and errors on symlinks referencing non-existent or non-writable locations. Note: `os.chown(.., follow_symlinks=False)` is python3 only, but `os.lchown` exists in both versions. --- lib/spack/llnl/util/filesystem.py | 7 +++++-- lib/spack/spack/test/installer.py | 2 +- lib/spack/spack/util/file_permissions.py | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/spack/llnl/util/filesystem.py b/lib/spack/llnl/util/filesystem.py index 753129b78e..286273fc83 100644 --- a/lib/spack/llnl/util/filesystem.py +++ b/lib/spack/llnl/util/filesystem.py @@ -302,13 +302,16 @@ def group_ids(uid=None): return [g.gr_gid for g in grp.getgrall() if user in g.gr_mem] -def chgrp(path, group): +def chgrp(path, group, follow_symlinks=True): """Implement the bash chgrp function on a single path""" if isinstance(group, six.string_types): gid = grp.getgrnam(group).gr_gid else: gid = group - os.chown(path, -1, gid) + if follow_symlinks: + os.chown(path, -1, gid) + else: + os.lchown(path, -1, gid) def chmod_x(entry, perms): diff --git a/lib/spack/spack/test/installer.py b/lib/spack/spack/test/installer.py index 4f7f24dc24..47174ad73f 100644 --- a/lib/spack/spack/test/installer.py +++ b/lib/spack/spack/test/installer.py @@ -834,7 +834,7 @@ def test_setup_install_dir_grp(install_mockery, monkeypatch, capfd): def _get_group(spec): return mock_group - def _chgrp(path, group): + def _chgrp(path, group, follow_symlinks=True): tty.msg(mock_chgrp_msg.format(path, group)) monkeypatch.setattr(prefs, 'get_package_group', _get_group) diff --git a/lib/spack/spack/util/file_permissions.py b/lib/spack/spack/util/file_permissions.py index 9a510ef6c8..b2de56eda9 100644 --- a/lib/spack/spack/util/file_permissions.py +++ b/lib/spack/spack/util/file_permissions.py @@ -44,7 +44,7 @@ def set_permissions(path, perms, group=None): fs.chmod_x(path, perms) if group: - fs.chgrp(path, group) + fs.chgrp(path, group, follow_symlinks=False) class InvalidPermissionsError(SpackError): -- cgit v1.2.3-70-g09d2