[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Changing permission on a file

App Ra

11/17/2006 1:19:00 AM

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

4 Answers

Paul Lutus

11/17/2006 5:20:00 AM

0

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

Paul Lutus

11/17/2006 5:33:00 AM

0

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

Pit Capitain

11/17/2006 8:37:00 AM

0

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

Daniel Berger

11/17/2006 1:44:00 PM

0


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.