Bug? in os.chown()

Erwin S. Andreasen erwin at andreasen.com
Mon Jan 1 08:21:29 EST 2001


On Sun, 31 Dec 2000 17:27:25 -0600, David Lambert wrote:
>I am running Python 2.0 on Mandrake Linux 7.2 and have come across an
>apparent anomaly with os.chown(). Running a process as root, I can
>change any file's ownerships apart from symbolic links. No error is
>given, but the ownership refuses to be changed. Is this a feature or a
>bug? How do I change a symbolic link's owner and group?

Just about any operation on a symbolic link (i.e. open, chdir etc. but not
unlink) will act on the file linked to instead. So you are really changing the
permission on the target file. (Note, however, that while the system call
chown follows the link, the GNU chown program does NOT follow the link and
changes the owner of the link -- at least with 4.0.35 version of fileutils).

There are a number of l* system calls that act on the link rather than the
thing being linked to (lstat, lchown). However, Python doesn't implement
lchown yet (at least not in Python 2.0b1).

So, I suppose you could use system() to run GNU chown on the file (you could
give it the --no-dereference flag to be sure, see man 1 chown) -- it's only
necessary to do it when the target is a symbolic link (you can check using
os.path.islink). Not a very portable solution though.

PS: Ownership of symbolic links isn't very useful though (see man symlink).

-- 
=======================================================================
<erwin at andreasen.com>           Herlev, Denmark       Software Designer
<URL:http://www.andreasen.org/>       <*>         LASAT^WEicon Networks
=======================================================================



More information about the Python-list mailing list