ProgrammingThis 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.
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.
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.
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.
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.
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"
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).
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 :-)
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.