6

I'm using Python 2.7.9 under Debian GNU/Linux 8 (jessie) 64-bit. I just tried to change file attributes by calling os.chflags(path, mode). In the Python docs there is an article about the os interface which says that this method is available in Unix, but it doesn't work for Linux. Python always throws:

Traceback (most recent call last):
File "/home/lexer/py/epam/tests/main.py", line 43, in <module>
os.chflags(path_to_file(file_name), stat.SF_NOUNLINK)
AttributeError: 'module' object has no attribute 'chflags'

There is an issue which was already raised for that a long time ago, but I still can't understand why os.chflags() doesn't do the chattr command's job. Could anybody elaborate it?

5
  • The linked bug on launchpad says that it is (apparently) a bug in the Python's OS feature test: "the configure check fails".
    – Dummy00001
    Commented Dec 11, 2015 at 15:44
  • Can you show what os.__file__ says. Commented Dec 13, 2015 at 9:15
  • Did you accidentally define your own os module or something like that? This looks like an import/resolution error of some kind. Try it in the repl and see what happens.
    – pvg
    Commented Dec 13, 2015 at 9:17
  • @pvg That is where my comment is going. Commented Dec 13, 2015 at 9:18
  • @MikeMüller I'm sure you're right, if a bit more cryptic :)
    – pvg
    Commented Dec 13, 2015 at 9:25

1 Answer 1

5

Linux does not provide the chflags syscall, so Python does not provide the wrapper os.chflags().

The chattr command uses the code (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);

to set the extended attributes for a file, so if you port that to Python (and use some C to extract the value for EXT2_IOC_SETFLAGS from ext2fs/ext2_fs.h), you can do something like:

#!/usr/bin/python2

import fcntl
import os
import struct

# Taken from ext2fs/ext2_fs.h.
EXT2_IMMUTABLE_FL = 0x00000010
EXT2_IOC_SETFLAGS = 0x40086602

fd = os.open('/var/tmp/testfile', os.O_RDWR)
f = struct.pack('i', EXT2_IMMUTABLE_FL)
fcntl.ioctl(fd, EXT2_IOC_SETFLAGS, f);
os.close(fd)

Et voilà:

[tim@passepartout ~]$ lsattr /var/tmp/testfile
----i----------- /var/tmp/testfile
[tim@passepartout ~]$

But for all practical purposes it is probably much more prudent to execute chattr(1) as a child process than to turn the proof-of-concept above into something that runs reliably without maintenance.

2
  • My willing was just to set one of these attributes to a file: link and verify it works, but looks like it is impossible Commented Aug 19, 2016 at 20:03
  • For a 32-bit machine (such as armv7, which I'm working on) use 0x40046602 as the op for ioctl instead of 0x40086602.
    – Jellicle
    Commented Mar 29, 2018 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.