From: Jeremy Bopp Date: 2012-02-08T15:46:39+09:00 Subject: Re: Make a file read-only? On 02/07/2012 04:20 PM, Robert Klemme wrote: > On Mon, Feb 6, 2012 at 9:14 PM, Jeremy Bopp wrote: >> On 02/06/2012 01:38 PM, Intransition wrote: >> More specifically, then, you want to remove the write permission from >> the file, leaving it whatever other permissions were already set >> (possibly none!). In other words, you want to /mask out/ the write bits >> from whatever the current file mode is. Try the following: >> >> mode = 0100664 >> mask = mode ^ 0222 >> new_mode = mode & mask > > Interesting approach. I usually do it like this: > > irb(main):038:0> mode = 0100664 > => 33204 > irb(main):039:0> mask = 0222 > => 146 > irb(main):040:0> new_mode = (mode | mask) ^ mask > => 33060 > irb(main):041:0> "%07o" % new_mode > => "0100444" As far as I can tell, these approaches are functionally equivalent in both result and efficiency. Folding the method I used all together using your settings for mode and mask makes mine as follows: new_mode = mode & (mode ^ mask) I suspect that someone who more recently completed their college philosophy of logic class than I might even remember how to derive one of our solutions from the other using tidy boolean algebra. I'm too rusty for it at the moment unfortunately, but I did give it a try. :-( Maybe I'll pick at it more tomorrow... -Jeremy