File.new('foo', 0600 , 'wb')


(Tobi Reif) #1

Hi,

I want to create an image file foo with mode 0600.

File.new(‘foo’, 0600 , ‘wb’)
doesn’t work. Do I have to use this ugly uppercase syntax?

Tobi

···


http://www.pinkjuice.com/


(Dossy) #2

According to the documentation, you should be doing:

File.new('foo', 'wb', 0600)

This is gotten from:

http://www.rubycentral.com/book/ref_c_file.html#File.new

File.new( fileName [, aModeNum [ aPermNum ] ] ) -> file 

HTH, HAND,

– Dossy

···

On 2002.06.16, Tobias Reif tobiasreif@pinkjuice.com wrote:

Hi,

I want to create an image file foo with mode 0600.

File.new(‘foo’, 0600 , ‘wb’)
doesn’t work. Do I have to use this ugly uppercase syntax?


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)


(Tobi Reif) #3

Dossy wrote:

According to the documentation, you should be doing:

File.new('foo', 'wb', 0600)

Yes, but unless I made a typo, that didn’t work. I’ll try again as soon
as I’m back on Linux.

thx,

Tobi

···


http://www.pinkjuice.com/


(Tobi Reif) #4

Dossy wrote:

According to the documentation, you should be doing:

File.new('foo', 'wb', 0600)

This is gotten from:

http://www.rubycentral.com/book/ref_c_file.html#File.new

File.new( fileName [, aModeNum [ aPermNum ] ] ) -> file 

That’s what I did first.

$ ruby -e “File.new(‘foozer’,‘wb’,0600)”
$ stat foozer
File: "foozer"
Size: 0 Blocks: 0 Regular File
Device: 306h/774d Inode: 374159 Links: 1
Access: (0644/-rw-r–r--) Uid: ( 501/ tobi) Gid: ( 501/ tobi)
Access: Sat Jun 15 20:11:15 2002
Modify: Sat Jun 15 20:11:15 2002
Change: Sat Jun 15 20:11:15 2002

Tobi

···


http://www.pinkjuice.com/


(Dossy) #5

Neat. I just tried on 1.6.7 and I got the same thing you did.

This works as a workaround:

File.open(“foo”, “w+”) { |file| file.chmod 0600 }’

But, this isn’t suitable where security is important because someone
could grab a filehandle between the time the file is created and the
perms are changed and read the file as it’s written.

This bug seems to have been fixed in 1.7.2:

$ umask 022
$ rm -f foo
$ ls -l foo
/bin/ls: foo: No such file or directory
$ ruby -ve 'File.new “foo”, “w”, 0600’
ruby 1.6.7 (2002-03-19) [i386-linux]
$ ls -l foo
-rw-r–r-- 1 dossy users 0 Jun 15 14:43 foo

$ umask 022
$ rm -f foo
$ ls -l foo
/bin/ls: foo: No such file or directory
$ ruby-1.7.2 -ve 'File.new “foo”, “w”, 0600’
ruby 1.7.2 (2002-05-30) [i686-linux]
$ ls -l foo
-rw------- 1 dossy users 0 Jun 15 14:43 foo

– Dossy

···

On 2002.06.16, Tobias Reif tobiasreif@pinkjuice.com wrote:

Dossy wrote:

According to the documentation, you should be doing:

File.new(‘foo’, ‘wb’, 0600)

This is gotten from:

http://www.rubycentral.com/book/ref_c_file.html#File.new

File.new( fileName [, aModeNum [ aPermNum ] ] ) -> file

That’s what I did first.

$ ruby -e “File.new(‘foozer’,‘wb’,0600)”
$ stat foozer
File: "foozer"
Size: 0 Blocks: 0 Regular File
Device: 306h/774d Inode: 374159 Links: 1
Access: (0644/-rw-r–r--) Uid: ( 501/ tobi) Gid: ( 501/ tobi)
Access: Sat Jun 15 20:11:15 2002
Modify: Sat Jun 15 20:11:15 2002
Change: Sat Jun 15 20:11:15 2002


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)


(Tobi Reif) #6

Dossy wrote:

This works as a workaround:

File.open(“foo”, “w+”) { |file| file.chmod 0600 }’

sure; but we don’t want workarounds, do we? :slight_smile:

This bug seems to have been fixed in 1.7.2:

$ ruby-1.7.2 -ve 'File.new “foo”, “w”, 0600’
ruby 1.7.2 (2002-05-30) [i686-linux]
$ ls -l foo
-rw------- 1 dossy users 0 Jun 15 14:43 foo

Thanks :slight_smile:

Tobi

···


http://www.pinkjuice.com/


(Tobi Reif) #7

Dossy wrote:

This bug seems to have been fixed in 1.7.2:

What’s the oldest or most stable version where this is fixed?
If it’s 1.7.2;
Where to get 1.7.2 for Linux; is it
ftp://ftp.ruby-lang.org/pub/ruby/stable-snapshot.tar.gz
?

Tobi

···


http://www.pinkjuice.com/


(YANAGAWA Kazuhisa) #8

This bug seems to have been fixed in 1.7.2:

$ umask 022
$ rm -f foo
$ ls -l foo
/bin/ls: foo: No such file or directory
$ ruby -ve 'File.new “foo”, “w”, 0600’
ruby 1.6.7 (2002-03-19) [i386-linux]
$ ls -l foo
-rw-r–r-- 1 dossy users 0 Jun 15 14:43 foo

$ umask 022
$ rm -f foo
$ ls -l foo
/bin/ls: foo: No such file or directory
$ ruby-1.7.2 -ve 'File.new “foo”, “w”, 0600’
ruby 1.7.2 (2002-05-30) [i686-linux]
$ ls -l foo
-rw------- 1 dossy users 0 Jun 15 14:43 foo

That’s not a bug but a new feature implemented in 1.7.x, IMO. The
description of THE BOOK means following case:

>ruby -e 'File.new("/tmp/foo", File::CREAT, 0600)
>ls -l /tmp/foo
-rw-------  1 kjana  wheel  0   6/16 12:10 /tmp/foo

namely open(2) system call like form. This form should work on both
1.6.x and 1.7.x.

Of course you can specify arbitrary mode allowed on your system.

eg. File.new("/tmp/foo", File::CREAT|File::TRUNC|File::RDWR, 0600)

The feature recently requested has been adopted and implemented, but
not merged to the stable branch?

···

In message 20020615184356.GE29645@panoptic.com dossy@panoptic.com writes:


kjana@dm4lab.to June 16, 2002
Of two evils choose the lesser.


(Tobi Reif) #9

I wrote:

What’s the oldest or most stable version where this is fixed?

err … implemented.
(I knew it :wink: )

Tobi

···


http://www.pinkjuice.com/


(Dossy) #10

I just tested on 1.6.7 and you’re right: using File::CREAT instead
of “wb” does what one would expect with regard to the file permissions.

– Dossy

···

On 2002.06.16, YANAGAWA Kazuhisa kjana@dm4lab.to wrote:

That’s not a bug but a new feature implemented in 1.7.x, IMO. The
description of THE BOOK means following case:

>ruby -e 'File.new("/tmp/foo", File::CREAT, 0600)
>ls -l /tmp/foo
-rw-------  1 kjana  wheel  0   6/16 12:10 /tmp/foo

namely open(2) system call like form. This form should work on both
1.6.x and 1.7.x.


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)


(Tobi Reif) #11

YANAGAWA Kazuhisa wrote:

Of course you can specify arbitrary mode allowed on your system.

eg. File.new("/tmp/foo", File::CREAT|File::TRUNC|File::RDWR, 0600)

Thanks. How to write the equivalent of

File.new “foo”, “wb”, 0600

?

I mean, how to specify that it’s a binary (image) file?

Tobi

···


http://www.pinkjuice.com/


(Carlos) #12

Of course you can specify arbitrary mode allowed on your system.

eg. File.new("/tmp/foo", File::CREAT|File::TRUNC|File::RDWR, 0600)

Thanks. How to write the equivalent of

File.new “foo”, “wb”, 0600

?

I mean, how to specify that it’s a binary (image) file?

What is the difference if you are on a Unix system? In any case, if you need
to open the file with “wb”, you can set first File.umask, then open with
File.new “foo”, “wb”, and then restore File.umask to the original value.


(ts) #13

I mean, how to specify that it's a binary (image) file?

  flag |= File::BINARY if File::Constants.const_defined?("BINARY")

with 1.7

  flag |= File::BINARY if File::const_defined?("BINARY")

Guy Decoux


(Tobi Reif) #14

angus wrote:

What is the difference if you are on a Unix system?

The code must be platform independent, so I need both the binary flag
and the permissions flag.

In any case, if you need
to open the file with “wb”, you can set first File.umask, then open with
File.new “foo”, “wb”, and then restore File.umask to the original value.

Hmm…

Tobi

···


http://www.pinkjuice.com/


(Tobi Reif) #15

ts wrote:

I mean, how to specify that it’s a binary (image) file?

flag |= File::BINARY if File::Constants.const_defined?(“BINARY”)

with 1.7

flag |= File::BINARY if File::const_defined?(“BINARY”)

Thx.

With 1.7.2, is
File.new “foo”, “wb”, 0600
OK?

Tobi

···


http://www.pinkjuice.com/


(Carlos) #16

In any case, if you need
to open the file with “wb”, you can set first File.umask, then open with
File.new “foo”, “wb”, and then restore File.umask to the original value.

Hmm…

Forget it. Use ts’s solution.


(Carlos) #17

flag |= File::BINARY if File::Constants.const_defined?(“BINARY”)

with 1.7

flag |= File::BINARY if File::const_defined?(“BINARY”)

Thx.

With 1.7.2, is
File.new “foo”, “wb”, 0600
OK?

Sorry I get in the way again :). I don’t know about 1.7, but in 1.6 (and I
don’t think the interface has changed), you have 2 options to open a file:

File.new , <string_mode>

and

File.new , <num_mode>,

So you cannot specify permissions if you use “wb”, this parameter is
ignored. If you want to specify permissions, you must use numeric mode. Guy
is suggesting that you do:

flag = File::CREAT
flag |= File::BINARY if File::Constants.const_defined?(“BINARY”)
File.new “foo”, flag, 0600


(Carlos) #18

Maybe adding File::WRONLY as well :wink:

···

flag = File::CREAT|File::WRONLY # modified line, sorry :wink:
flag |= File::BINARY if File::Constants.const_defined?(“BINARY”)
File.new “foo”, flag, 0600


(Tobi Reif) #19

Carlos wrote:

So you cannot specify permissions if you use “wb”, this parameter is
ignored. If you want to specify permissions, you must use numeric mode. Guy
is suggesting that you do:

flag = File::CREAT
flag |= File::BINARY if File::Constants.const_defined?(“BINARY”)
File.new “foo”, flag, 0600

I’m not sure if an alternative would be feasible, but I find all this
very quirky. I just want save some image data, with permission 0600; the
code should be very short and simple, and work on all major OSes. I do
not want to write three lines of uppercase double colon mess.

Tobi

···


http://www.pinkjuice.com/


(Yohanes Santoso) #20

Tobias Reif tobiasreif@pinkjuice.com writes:

Carlos wrote:

So you cannot specify permissions if you use “wb”, this parameter is
ignored. If you want to specify permissions, you must use numeric mode. Guy
is suggesting that you do:
flag = File::CREAT
flag |= File::BINARY if File::Constants.const_defined?(“BINARY”)
File.new “foo”, flag, 0600

I’m not sure if an alternative would be feasible, but I find all this
very quirky. I just want save some image data, with permission 0600;
the code should be very short and simple, and work on all major
OSes. I do not want to write three lines of uppercase double colon
mess.

Well, you have two conflicting requirements: permission 0600 and
platform independence. Permission 0600 may produce unexepected
permission/result in Windows or other non-POSIX OSes, depending on the
reliability and conformance of the Unix emulation layer on those OSes,
and File::BINARY is invalid in UNIX.

The code above is as succinct as it can be if you want it to operate
in both Unix and Windows.

YS.