2

I can use os.stat(pathname) to get pathname's perssmions, mtime, atime.... Howerver, I hava a file that has a '+i' attribute, you can see it by: lsattr /tmp/test.py Is there a way to know if the pathname has the '+i' attribute by using python?

7
  • statmode = os.stat("/tmp/test.py").st_modeF_IMMUTABLE(statmode) there is an error: AttributeError: 'posix.stat_result' object has no attribute 'st_modeF_IMMUTABLE'
    – page
    Feb 19, 2016 at 9:14
  • out of curiosity, what Distro/Kernel and filesystem are you using? Is /tmp a nfs share or something similar?
    – Torxed
    Feb 19, 2016 at 9:16
  • centos6.7 ext4,pathname is local file
    – page
    Feb 19, 2016 at 9:30
  • Though it might be a messy way of doing it, one way would be to determine if it is immutable by changing the file in some inconsequential way in a try except block, then reverting the change if it is not immutable. It's definitely not the best way of doing it, though...
    – M.T
    Feb 19, 2016 at 9:40

2 Answers 2

0

This related SO post about why os.chflags doesn't exist on Linux shows a way to work around the absence of lsattr in the os module using fcntl. However, it relies on copying constant definitions from a header file (ext2fs/ext2_fs.h), so it's fragile. A more permanent solution would require writing some C or something like Cython.

Meanwhile, PM 2Ring's answer works, though it should be modified to work with paths that contain the letter "i". Perhaps someone with some reputation could comment or edit that answer (this is my first post)?

import subprocess

def is_immutable(fname):
    p = subprocess.Popen(['lsattr', fname], bufsize=1, stdout=subprocess.PIPE)
    data, _ = p.communicate()
    #print(data)
    return 'i' in data

def is_immutable_safe(file_path):
    """Check if the immutable flag is set on a Linux file path

    Uses the lsattr command, and assumes that the immutable flag
    appears in the first 16 characters of its output.
    """
    return 'i' in subprocess.check_output(['lsattr', file_path])[:16]

# These assertions will pass if the immutable bit is not set on
# /etc/inittab on your system
assert is_immutable('/etc/inittab') is True
assert is_immutable_safe('/etc/inittab') is False
0

There doesn't appear to be an os module function to get these Linux file attributes, but you can use the subprocess module to call the lsattr command in Python.

The code below should work on Python 2 or 3, although it can be made more compact in recent Python versions.

FWIW, I have the i bit set on my fstab file because I got sick of it randomly getting wiped out.

import subprocess

def is_immutable(fname):
    p = subprocess.Popen(['lsattr', fname], bufsize=1, stdout=subprocess.PIPE)
    data, _ = p.communicate()
    #print(data)
    return 'i' in data.split(None, 1)[0]

print(is_immutable("/etc/fstab"))

Update

The previous version of this code had

return 'i' in data.split

However, that will also detect an 'i' if one is present in the file name! The new version only detects an 'i' in the attribute flags. Thanks, sverasch for bring this to my attention.

4
  • 'i' in data may not be a good idea, it's better to use re to match the result p = re.compile(r'-+i-+')
    – page
    Feb 22, 2016 at 3:17
  • @page: Why? On my machine, using in on the result string that lsattr sends to stdout is around 10 times faster than using match with your compiled pattern, and that's not counting the time to import re or compile the pattern; search is even slower, especially when the search fails. If I use a simplified regex r'i', an in test is still faster than a regex search, even if I make the target string 1000 times longer.
    – PM 2Ring
    Feb 22, 2016 at 6:21
  • Thank you very much for your proposed these contrast
    – page
    Feb 23, 2016 at 6:54
  • @sverasch I hastily rejected your edit because I didn't realise why it was needed... until 5 seconds later. :) But I would've fixed it myself if you'd left me a comment...
    – PM 2Ring
    Nov 4, 2016 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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