Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-62308: Added dir_fd & follow_symlinks to shutil.chown #118136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Doc/library/shutil.rst
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ Directory and files operations

.. availability:: Unix, Windows.

.. function:: chown(path, user=None, group=None)
.. function:: chown(path, user=None, group=None, *, dir_fd=None, \
follow_symlinks=True)

Change owner *user* and/or *group* of the given *path*.

Expand All @@ -436,6 +437,8 @@ Directory and files operations

.. versionadded:: 3.3

.. versionchanged:: 3.13
Added *dir_fd* and *follow_symlinks* parameters.

.. function:: which(cmd, mode=os.F_OK | os.X_OK, path=None)

Expand Down
13 changes: 11 additions & 2 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1442,11 +1442,19 @@ def disk_usage(path):
return _ntuple_diskusage(total, used, free)


def chown(path, user=None, group=None):
def chown(path, user=None, group=None, *, dir_fd=None, follow_symlinks=True):
"""Change owner user and group of the given path.

user and group can be the uid/gid or the user/group names, and in that case,
they are converted to their respective uid/gid.

If dir_fd is not None, it should be a file descriptor open to a directory;
path will then be relative to that directory.
dir_fd may not be implemented on your platform.
If it is unavailable, using it will raise a NotImplementedError.

If follow_symlinks is not set and path is a symbolic link, the owner of the
symlink will be changed instead of the file it points to.
"""
sys.audit('shutil.chown', path, user, group)

Expand All @@ -1472,7 +1480,8 @@ def chown(path, user=None, group=None):
if _group is None:
raise LookupError("no such group: {!r}".format(group))

os.chown(path, _user, _group)
os.chown(path, _user, _group, dir_fd=dir_fd,
follow_symlinks=follow_symlinks)

def get_terminal_size(fallback=(80, 24)):
"""Get the size of the terminal window.
Expand Down
20 changes: 19 additions & 1 deletion Lib/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -2107,6 +2107,7 @@ def test_disk_usage(self):
def test_chown(self):
dirname = self.mkdtemp()
filename = tempfile.mktemp(dir=dirname)
dirfd = os.open(dirname, os.O_RDONLY)
write_file(filename, 'testing chown function')

with self.assertRaises(ValueError):
Expand All @@ -2128,7 +2129,7 @@ def test_chown(self):
gid = os.getgid()

def check_chown(path, uid=None, gid=None):
s = os.stat(filename)
s = os.stat(path)
if uid is not None:
self.assertEqual(uid, s.st_uid)
if gid is not None:
Expand All @@ -2142,15 +2143,32 @@ def check_chown(path, uid=None, gid=None):
check_chown(filename, uid)
shutil.chown(filename, group=gid)
check_chown(filename, gid=gid)
shutil.chown(os.path.basename(filename), uid, gid, dir_fd=dirfd)
check_chown(filename, uid, gid)
shutil.chown(os.path.basename(filename), user=uid, dir_fd=dirfd)
check_chown(filename, uid)
shutil.chown(os.path.basename(filename), group=gid, dir_fd=dirfd)
check_chown(filename, gid=gid)

with self.assertRaises(FileNotFoundError):
shutil.chown('invalid-file', user=uid, dir_fd=dirfd)
shutil.chown('invalid-file', group=gid, dir_fd=dirfd)
shutil.chown('invalid-file', user=uid, group=gid, dir_fd=dirfd)

shutil.chown(dirname, uid, gid)
check_chown(dirname, uid, gid)
shutil.chown(dirname, uid)
check_chown(dirname, uid)
shutil.chown(dirname, user=uid)
check_chown(dirname, uid)
shutil.chown(dirname, user=uid, follow_symlinks=True)
check_chown(dirname, uid)
shutil.chown(dirname, group=gid)
check_chown(dirname, gid=gid)
shutil.chown(dirname, group=gid, follow_symlinks=True)
check_chown(dirname, gid=gid)
shutil.chown(dirname, user=uid, group=gid, follow_symlinks=True)
check_chown(dirname, uid=uid, gid=gid)

try:
user = pwd.getpwuid(uid)[0]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added the *dir_fd* & *follow_symlinks* parameters to :func:`shutil.chown`.
Loading