Changing permission on a file

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

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.

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.

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

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. [email protected] 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.”

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs