LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-05-2013, 06:07 PM   #1
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Rep: Reputation: 17
Python - checking for immutable bit


Hi,

I am writing some Python 2 code to detect if the immutable bit is set on a file or not.

This is as far as I have been able to get.

Code:
statmode = os.stat("/etc/immutable").st_mode
if stat.SF_IMMUTABLE(statmode)
        print "immutable bit is set"
else:
        print "imutable bit not set"
There is UF_IMMUTABLE and SF_IMMUTABLE but I don't understand the difference between them.

Python keeps on returning "SyntaxError: invalid syntax" on line 2.

I am following the documentation here, but something in my syntax and I suspect code, also is not correct.

http://docs.python.org/dev/library/s...t.UF_IMMUTABLE

Last edited by dazdaz; 04-05-2013 at 06:08 PM.
 
Old 04-06-2013, 10:30 AM   #2
tc60045
LQ Newbie
 
Registered: Apr 2013
Location: Chicagoland
Distribution: Mostly OS X 10.8, some RasPi Wheezy / Raspbian
Posts: 4

Rep: Reputation: Disabled
Quote:
Originally Posted by dazdaz View Post
Hi,

I am writing some Python 2 code to detect if the immutable bit is set on a file or not.

This is as far as I have been able to get.

Code:
statmode = os.stat("/etc/immutable").st_mode
if stat.SF_IMMUTABLE(statmode)
        print "immutable bit is set"
else:
        print "imutable bit not set"
There is UF_IMMUTABLE and SF_IMMUTABLE but I don't understand the difference between them.

Python keeps on returning "SyntaxError: invalid syntax" on line 2.

I am following the documentation here, but something in my syntax and I suspect code, also is not correct.

http://docs.python.org/dev/library/s...t.UF_IMMUTABLE
Is /etc/immutable a file or a directory on your machine? It appears to me from the (awful) os.stat documentation that the flags are returned for files; I've yet to see an example performed on directories.

Have you tried both UF_Imm... and SF_Imm? I cannot tell which you should be using, either.

Also: isn't an "immutable" bit really just a test of the write permissions of the file for the user you are running the program as? There isn't a canonical "immutable" bit in linux if you're running the process as root and the file is 777, right? In that case, root can do what root wants on that file. However, if you're running the script as a user and the file permissions are 600, then it is "immutable" as far as that user is concerned.

Just a few thoughts to try to help you out.
 
Old 04-06-2013, 10:37 AM   #3
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Original Poster
Rep: Reputation: 17
A test file with the "immutable" attribute set can easily be created, like such.
Code:
# touch /etc/immutable
# chattr +i /etc/immutable
# lsattr /etc/immutable
The immutable bit has nothing to do with file permissions, it's related to a file's attribute. When the immutable bit has been set, it prevents a file from being removed/changed, think of it like a "write protect" feature.

I've tried a number of variations with the code to no avail. And, yes, the script is run as root.

Last edited by dazdaz; 04-06-2013 at 10:45 AM.
 
Old 04-06-2013, 01:04 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by dazdaz View Post
Code:
statmode = os.stat("/etc/immutable").st_mode
if stat.SF_IMMUTABLE(statmode)
        print "immutable bit is set"
else:
        print "imutable bit not set"
You are using SF_IMMUTABLE as a function but it is a bitwise flag (an integer), also you forgot the ":" after the if condition. Use bitwise and to check flags:
Code:
if statmode & stat.SF_IMMUTABLE:
Quote:
There is UF_IMMUTABLE and SF_IMMUTABLE but I don't understand the difference between them.
http://docs.python.org/dev/library/s...t.UF_IMMUTABLE

Quote:
The following flags can be used in the flags argument of os.chflags():
...
See the *BSD or Mac OS systems man page chflags(2) for more information.
chflags(2)
Quote:
The UF_IMMUTABLE and UF_APPEND flags may be set or unset by either the owner of a file or the superuser.

The SF_ARCHIVED, SF_IMMUTABLE and SF_APPEND flags may only be set or unset by the superuser.
 
1 members found this post helpful.
Old 04-06-2013, 02:50 PM   #5
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Original Poster
Rep: Reputation: 17
Hi

Python now parses the code, however regardless of if a file has the immutable flag set or not, the code always returns, "immutable bit is not set".

Did I misunderstand something ?

Code:
#!/usr/bin/python

statmode = os.stat("/etc/immutable").st_mode
if statmode & stat.SF_IMMUTABLE:
        print "immutable bit is set"
else:
        print "imutable bit is not set"

Last edited by dazdaz; 04-06-2013 at 04:06 PM.
 
Old 04-06-2013, 05:24 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by dazdaz View Post
A test file with the "immutable" attribute set can easily be created, like such.
Code:
# touch /etc/immutable
# chattr +i /etc/immutable
# lsattr /etc/immutable
It looks like you are on Linux.

Quote:
the code always returns, "immutable bit is not set".

Did I misunderstand something ?
I think the (S|U)F_* flags are specific to BSD based systems and you need to be checking the st_flags field instead of st_mode (the guy here seems to think st_flags should have the lsattr info on Linux, but the version of python I have installed here doesn't give me such a field).
 
Old 04-07-2013, 06:02 PM   #7
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Original Poster
Rep: Reputation: 17
I presume that by "st_flags - user defined flags for file", they mean a file attributes which includes the immutable bit.

I cannot find any information on the structure of st_flags on docs.python.org. I also looked at my header files on CentOS 6.4 (I have python 2.6.6 installed) and could'nt find any further definition for it in there.

If my system did support st_flags, how would I parse data in st_flags, is it also a bitwise flag.

I found the following code, which returns false on my system. Is'nt this telling us that this functionality does not exist ?

Code:
if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
        os.chflags(dst, st.st_flags)
Tbh, I'm completely lost right now and am not sure what's left to try. If anyone can figure out how to check for a file's attribute on Linux, then i'd be very gretful. I can't be the first person to do this :-)

Last edited by dazdaz; 04-07-2013 at 06:04 PM.
 
Old 04-08-2013, 12:41 PM   #8
tc60045
LQ Newbie
 
Registered: Apr 2013
Location: Chicagoland
Distribution: Mostly OS X 10.8, some RasPi Wheezy / Raspbian
Posts: 4

Rep: Reputation: Disabled
Dazdaz,

You can dismiss or hate this answer, but..... back in the day when I started programming professionally in COBOL and JCL, file reads / writes were a hella big deal. DB2 was a beast that you'd feed with changes from flat files, but only after considerable vetting. Production files were copied to working files, which you had free reign to parse and work with. Proposed changes to a db2 record or production file were managed by a select team, who really owned those files. Imagine all parts ever produced for all cars made for a top 3 US Auto OEM and you get a sense of what we worked with.

So, I have to ask: is the only way to accomplish what you want to accomplish? Must you have an immutable flag? I've never used file attributes because of my (possibly arcane, definitely old-school) reliance on creating working copies, owned by ME, of any file that could be in contention. Could you do the same thing?

I'm never one to tell a fellow professional "you're doing it wrong" -- that is obnoxious. I'm just asking if there is another way to skin this cat. Hate to watch another man suffer, needlessly, when a change in approach might offer up a solution. Tell us more and we'll try to help.

Best,

TC
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bit Rate Checking iqbal mohd Red Hat 5 06-23-2010 09:54 AM
Finding files with immutable bit set SlowCoder Linux - General 2 02-24-2009 01:57 PM
Checking directory or file exsistance in Python xgreen Programming 10 07-07-2005 10:40 AM
Checking passwords and system clock in Python 1337 Twinkie Programming 9 10-31-2004 06:06 PM
checking bit of an address blackzone Programming 3 09-20-2004 09:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration