# Changing permission on a file

**URL:** <https://www.ruby-forum.com/t/changing-permission-on-a-file/76261>\
**Category:** Ruby\
**Created:** [November 17, 2006, 2:19am UTC](https://www.ruby-forum.com/t/changing-permission-on-a-file/76261 "2006-11-17T02:19:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![apparna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/48db29/32.png) [@apparna](https://www.ruby-forum.com/u/apparna)\
**Post date:** [November 17, 2006, 2:19am UTC](https://www.ruby-forum.com/t/changing-permission-on-a-file/76261/1 "2006-11-17T02:19:00Z")

</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:

# take away read permission

new\_permission = File.lstat(“testfile”).mode ^ 0004  
File.chmod(new\_permission, “testfile” )  
File.readable?(“testfile”) #=\> true

or

new\_permission = File.lstat(“testfile”).mode ^ File::O\_R  
File.chmod(new\_permission, “testfile” )

What am I doing wrong?

Thanks  
Apparna

---

<div class="post-metadata">

**Author:** ![apparna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/48db29/32.png) [@apparna](https://www.ruby-forum.com/u/apparna)\
**Post date:** [November 17, 2006, 6:21am UTC](https://www.ruby-forum.com/t/changing-permission-on-a-file/76261/2 "2006-11-17T06:21: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

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

> here is the code:
> 
> # take away read permission
> 
> 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.

> File.chmod(new\_permission, “testfile” )  
> File.readable?(“testfile”) #=\> true
> 
> or
> 
> new\_permission = File.lstat(“testfile”).mode ^ File::O\_R

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

> File.chmod(new\_permission, “testfile” )
> 
> What am I doing wrong?

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

---

<div class="post-metadata">

**Author:** ![apparna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/48db29/32.png) [@apparna](https://www.ruby-forum.com/u/apparna)\
**Post date:** [November 17, 2006, 6:37am UTC](https://www.ruby-forum.com/t/changing-permission-on-a-file/76261/3 "2006-11-17T06:37:11Z")

</div>

Paul L. wrote:

> > # take away read permission
> > 
> > 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.

---

<div class="post-metadata">

**Author:** ![apparna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/48db29/32.png) [@apparna](https://www.ruby-forum.com/u/apparna)\
**Post date:** [November 17, 2006, 9:37am UTC](https://www.ruby-forum.com/t/changing-permission-on-a-file/76261/4 "2006-11-17T09:37:25Z")

</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:** ![apparna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/48db29/32.png) [@apparna](https://www.ruby-forum.com/u/apparna)\
**Post date:** [November 17, 2006, 12:01pm UTC](https://www.ruby-forum.com/t/changing-permission-on-a-file/76261/5 "2006-11-17T12:01:39Z")

</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 C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–  
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.”

---

<div class="post-metadata">

**Author:** ![apparna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/48db29/32.png) [@apparna](https://www.ruby-forum.com/u/apparna)\
**Post date:** [November 17, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/changing-permission-on-a-file/76261/6 "2006-11-17T14:46:07Z")

</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:
> 
> # take away read permission
> 
> new\_permission = File.lstat(“testfile”).mode ^ 0004  
> File.chmod(new\_permission, “testfile” )  
> 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.
