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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
04-05-2013, 07:07 PM
|
#1
|
Member
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333
Rep:
|
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 07:08 PM.
|
|
|
04-06-2013, 11:30 AM
|
#2
|
LQ Newbie
Registered: Apr 2013
Location: Chicagoland
Distribution: Mostly OS X 10.8, some RasPi Wheezy / Raspbian
Posts: 4
Rep:
|
Quote:
Originally Posted by dazdaz
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.
|
|
|
04-06-2013, 11:37 AM
|
#3
|
Member
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333
Original Poster
Rep:
|
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 11:45 AM.
|
|
|
04-06-2013, 02:04 PM
|
#4
|
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,808
|
Quote:
Originally Posted by dazdaz
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.
|
04-06-2013, 03:50 PM
|
#5
|
Member
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333
Original Poster
Rep:
|
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 05:06 PM.
|
|
|
04-06-2013, 06:24 PM
|
#6
|
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,808
|
Quote:
Originally Posted by dazdaz
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).
|
|
|
04-07-2013, 07:02 PM
|
#7
|
Member
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333
Original Poster
Rep:
|
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 07:04 PM.
|
|
|
04-08-2013, 01:41 PM
|
#8
|
LQ Newbie
Registered: Apr 2013
Location: Chicagoland
Distribution: Mostly OS X 10.8, some RasPi Wheezy / Raspbian
Posts: 4
Rep:
|
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
|
|
|
All times are GMT -5. The time now is 02:52 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|