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

···

--
Posted via http://www.ruby-forum.com/.

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 Lutus
http://www.arachnoid.com

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

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.

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:

  # 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.

···

--
Paul Lutus
http://www.arachnoid.com

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."