# Changing permission on a file

**URL:** <https://rubytalk.org/t/changing-permission-on-a-file/33012>\
**Category:** ruby-talk\
**Created:** [17 November 2006 01:19 UTC](https://rubytalk.org/t/changing-permission-on-a-file/33012 "2006-11-17T01:19:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![App\_Ra](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@App\_Ra](https://rubytalk.org/u/App_Ra)\
**Post date:** [17 November 2006 01:19 UTC](https://rubytalk.org/t/changing-permission-on-a-file/33012/1 "2006-11-17T01:19:02Z")

</div>

I'm trying to change permission on a file on a windows machine. This  
should work according to the documentation (may be i'm missing something  
here). But it doesn't  
here is the code:

&nbsp;&nbsp;# take away read permission  
&nbsp;&nbsp;new\_permission = File.lstat("testfile").mode ^ 0004  
&nbsp;&nbsp;File.chmod(new\_permission, "testfile" )  
&nbsp;&nbsp;File.readable?("testfile") #=\> true

or

&nbsp;&nbsp;new\_permission = File.lstat("testfile").mode ^ File::O\_R  
&nbsp;&nbsp;File.chmod(new\_permission, "testfile" )

What am I doing wrong?

Thanks  
Apparna

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Paul\_Lutus](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Paul\_Lutus](https://rubytalk.org/u/Paul_Lutus)\
**Post date:** [17 November 2006 05:20 UTC](https://rubytalk.org/t/changing-permission-on-a-file/33012/2 "2006-11-17T05:20:05Z")

</div>

App Ra wrote:

> I'm trying to change permission on a file on a windows machine. This  
> should work according to the documentation (may be i'm missing something  
> here). But it doesn't

Be specific. What did you expect, and what did you get instead? Any error  
messages?

> here is the code:
> 
> &nbsp;&nbsp;# take away read permission  
> &nbsp;&nbsp;new\_permission = File.lstat("testfile").mode ^ 0004

Hoo-boy. Do you know what "^" actually does? Do it this way:

x & 4 ^ 0xffff

Meaning original value "AND" the bit-flip of the desired pattern. That way,  
you actually get a predictable result, regardless of the original value,  
instead of an unpredictable toggle.

> &nbsp;&nbsp;File.chmod(new\_permission, "testfile" )  
> &nbsp;&nbsp;File.readable?("testfile") #=\> true
> 
> or
> 
> &nbsp;&nbsp;new\_permission = File.lstat("testfile").mode ^ File::O\_R

Again, think about what "^" does, and which input arguments produce which  
output arguments.

> &nbsp;&nbsp;File.chmod(new\_permission, "testfile" )
> 
> What am I doing wrong?

Also, be sure to say what happened, how you know something is wrong.

> **···**
>
> --  
> Paul Lutus  
> [http://www.arachnoid.com](http://www.arachnoid.com)

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [17 November 2006 08:37 UTC](https://rubytalk.org/t/changing-permission-on-a-file/33012/3 "2006-11-17T08:37:04Z")

</div>

App Ra schrieb:

> I'm trying to change permission on a file on a windows machine.

The documentation of File.chmod says "Actual effects are platform dependent". Does Windows support taking away read permissions? How would you do this in Windows?

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [17 November 2006 13:45 UTC](https://rubytalk.org/t/changing-permission-on-a-file/33012/4 "2006-11-17T13:45:08Z")

</div>

App Ra wrote:

> I'm trying to change permission on a file on a windows machine. This  
> should work according to the documentation (may be i'm missing something  
> here). But it doesn't  
> here is the code:
> 
> &nbsp;&nbsp;# take away read permission  
> &nbsp;&nbsp;new\_permission = File.lstat("testfile").mode ^ 0004  
> &nbsp;&nbsp;File.chmod(new\_permission, "testfile" )  
> &nbsp;&nbsp;File.readable?("testfile") #=\> true

Windows only supports two modes for File.chmod - 0644 (read/write) or  
0444 (readonly).

You can use advanced attribute and security settings with the  
win32-file package. However, you cannot make a file unreadable on  
Windows except perhaps through ACL.

Regards,

Dan

PS - File.lstat is the same as File.stat on Windows.

---

<div class="post-metadata">

**Author:** ![Paul\_Lutus](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Paul\_Lutus](https://rubytalk.org/u/Paul_Lutus)\
**Post date:** [17 November 2006 05:35 UTC](https://rubytalk.org/t/changing-permission-on-a-file/33012/5 "2006-11-17T05:35:07Z")

</div>

Paul Lutus wrote:

> App Ra wrote:
> 
> > I'm trying to change permission on a file on a windows machine. This  
> > should work according to the documentation (may be i'm missing something  
> > here). But it doesn't
> 
> Be specific. What did you expect, and what did you get instead? Any error  
> messages?
> 
> > here is the code:
> > 
> > &nbsp;&nbsp;# take away read permission  
> > &nbsp;&nbsp;new\_permission = File.lstat("testfile").mode ^ 0004
> 
> Hoo-boy. Do you know what "^" actually does? Do it this way:
> 
> x & 4 ^ 0xffff
> 
> Meaning original value "AND" the bit-flip of the desired pattern. That  
> way, you actually get a predictable result, regardless of the original  
> value, instead of an unpredictable toggle.

Let me add, for clarity, to SET bit 2 (starting at 0 = LSB):

x |= 4

to CLEAR bit 2:

x &= 4 ^ 0xffff

The idea is to act only on the chosen bit and have no effect on any other  
bits.

> **···**
>
> --  
> Paul Lutus  
> [http://www.arachnoid.com](http://www.arachnoid.com)

---

<div class="post-metadata">

**Author:** ![Hung\_Dao](https://avatars.discourse-cdn.com/v4/letter/h/7bcc69/32.png) [@Hung\_Dao](https://rubytalk.org/u/Hung_Dao)\
**Post date:** [17 November 2006 10:59 UTC](https://rubytalk.org/t/changing-permission-on-a-file/33012/6 "2006-11-17T10:59:31Z")

</div>

In Windows there is no read permission , may be you can try to set it as  
hidden , read permission is in \*nix only

> **···**
>
> On 11/17/06, Pit Capitain \<pit@capitain.de\> wrote:
> 
> > App Ra schrieb:  
> > \> I'm trying to change permission on a file on a windows machine.
> > 
> > The documentation of File.chmod says "Actual effects are platform  
> > dependent". Does Windows support taking away read permissions? How would  
> > you do this in Windows?
> > 
> > Regards,  
> > Pit
> 
> --  
> Chỉ có lý trí của bản thân mới soi sáng được sự thật."Cái đẹp đẽ nhất mà  
> chúng ta có thể trải nghiệm được là cái bí ẩn. Đó là cảm thức nền tảng trong  
> cái nôi của nghệ thuật và khoa học chân chính. Kẻ nào không biết đến nó,  
> không còn khả năng ngạc nhiên hay kinh ngạc, kẻ đó coi như đã chết, đã tắt  
> rụi lửa sống trong mắt mình""Das Schönste, was wir erleben können, ist das  
> Geheimnisvolle. Es ist das Grundgefühl, das an der Wiege von wahrer Kunst  
> und Wissenschaft steht. Wer es nicht kennt und sich nicht mehr wundern,  
> nicht mehr staunen kann, der ist sozusagen tot und sein Auge erloschen.""The  
> fairest thing we can experience is the mysterious. It is the fundamental  
> emotion which stands at the cradle of true art and true science. He who  
> knows it not and can no longer wonder, no longer feel amazement, is as good  
> as dead, his eyelight has been extinct."
