Python os module lacks the chflags/lchflags methods

Bug #969032 reported by Olivier Cortès
12
This bug affects 2 people
Affects Status Importance Assigned to Milestone
python2.7 (Ubuntu)
Invalid
Undecided
Unassigned

Bug Description

Reading http://docs.python.org/library/os.html#os.stat , I noticed that on Linux the "flags" or "chattr" features have a Python interface since version 2.6.

However, on up-to-date Precise (Python 2.7.3rc2) and Oneiric (Python Python 2.7.2+), the following commands :

import os
os.chflags

report "AttributeError" in a Python shell. Grepping in /usr/lib/python2.7, the only reference to "chflags" is in the `shutil.py` file which just checks if the python method is present or not.

As the system interface is OK (I can successfully use "chattr" and "lsattr" in the shell on both Ubuntu versions, and historically this features has been present since I started using Linux), I think the "flags" part of the Python distribution should be activated on Ubuntu.

Currently I need this feature in my Python application to see if some files have the IMMUTABLE or APPEND flag, but generally speaking I can't see any good reason why this feature is not active while beeing part of the standard Python distribution.

regards,

--
Olivier

Revision history for this message
Matthias Klose (doko) wrote :

this is only available on MacOS and FreeBSD. Please use chattr on Linux instead.

Changed in python2.7 (Ubuntu):
status: New → Invalid
Revision history for this message
Olivier Cortès (olive) wrote :

Yes, I know `chattr` and I already use it on Linux. But referring to http://docs.python.org/library/os.html#os.chflags the "flags" feature IS the chattr on Linux ("Availability: Unix", not just MacOS and FreeBSD).

Reading further http://bugs.python.org/issue1490190 , the flags/chattr seems understood as different by upstream, but it is really the same system call. Though it has not the same name, it does the same thing (both "flags" and "chattr" have "SF_IMMUTABLE", "SF_APPEND", and more in common).

Doing `os.chflags()` in python on Linux should make the `chattr` equivalent syscall on Linux, if I understand the Python documentation correctly. Only some flags values would be different (some missing and some more than on other OSes).

It seems to be a good candidate for an upstream bug, though. I will investigate a little more in this direction.

Revision history for this message
Matthias Klose (doko) wrote :

currently the configure check fails

Revision history for this message
Tim Landscheidt (scfc) wrote :

I ran into this today and thought I'd share what I learned.

The syscall chflags does not exist in Linux, so when compiling Python it notices this and does not provide os.chflags().

chattr(1) on the other hand uses an ioctl() of EXT2_IOC_SETFLAGS to set the attributes (e2fsprogs-1.42.13's lib/e2p/fsetflags.c):

| fd = open (name, OPEN_FLAGS);
| if (fd == -1)
| return -1;
| f = (int) flags;
| r = ioctl (fd, EXT2_IOC_SETFLAGS, &f);
| if (r == -1)
| save_errno = errno;
| close (fd);

So you could (instead of executing chattr(1)) call Python's fcntl.ioctl() to perform that action.

It would probably be very nice if Python's os.chflags() would DWIM (and if someone brings this to Python please report here), but on the other hand I can also understand the paradigm to keep low-level functions "dumb", i. e. a function where the user expects that a simple syscall is made should only do that and nothing more.

Revision history for this message
Tim Landscheidt (scfc) wrote :

I posted a proof-of-concept for how to set extended attributes with Python at http://stackoverflow.com/questions/34176464/why-os-chflags-doesnt-work-under-linux/36274086#36274086.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.