RCR - 'struct flock*' wrapper for rb_io_fcntl

SYNOPSIS:

ruby’s fcntl call is incomplete in that one cannot pass a ‘struct flock *’
arg as fcntl’s third arg. because of this, no posix compliant locking is
available from within ruby.

SOLUTIONS:

a) add a class, rb_cFlock, underneath class rb_cIO and a test in rb_io_fcntl to
see whether it’s third argument is of type rb_cFlock and retrieves the
‘struct flock *’ pointer to pass through to other calls. rb_cFlock could
simply be a wrapper on a ‘struct flock *’ with methods type=, whence=, etc.

b) create a module, fcntl, like the present one, but which does more that
simply export the #defines from the header file

i can do either and submit a patch if some preference is expressed.

i’m hoping the usage in ruby would be as in:

require ‘fcntl’ # import constants, perhaps this replaces IO#fcntl…
include Fcntl

f.open ‘foobar’, ‘w’
flock = IO::Flock # defaults: F_WRLCK, SEEK_SET, 0L, 0L

f.fcntl F_SETLKW, flock

comments?

-a

···

ATTN: please update your address books with address below!

===============================================================================

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
STP :: Solar-Terrestrial Physics Data | NCEI
NGDC :: http://www.ngdc.noaa.gov/
NESDIS :: http://www.nesdis.noaa.gov/
NOAA :: http://www.noaa.gov/
US DOC :: http://www.commerce.gov/

The difference between art and science is that science is what we
understand well enough to explain to a computer.
Art is everything else.
– Donald Knuth, “Discover”

/bin/sh -c ‘for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done’
===============================================================================

Hi,

SYNOPSIS:

ruby’s fcntl call is incomplete in that one cannot pass a ‘struct flock *’
arg as fcntl’s third arg. because of this, no posix compliant locking is
available from within ruby.

You can use pack/unpack for the purpose.

require ‘fcntl’ # import constants, perhaps this replaces IO#fcntl…

f = open(‘/tmp/foobar’, ‘w’)
flock = [Fcntl::F_RDLCK, 0, 0, 0, 0].pack(“ssqqi”)

p Fcntl::F_RDLCK, Fcntl::F_GETLK
f.fcntl Fcntl::F_GETLK, flock
p flock.unpack(“ssqqi”)

But still I would not deny your proposal for the sake of readability
and portability. Maybe there can be functions that work similar to
pack_sockaddr_() / unpack_sockaddr_() in the socket extension.
pack_flock() or something.

						matz.
···

In message “RCR - ‘struct flock*’ wrapper for rb_io_fcntl” on 03/12/09, “Ara.T.Howard” ahoward@ngdc.noaa.gov writes:

Hi,

···

At Tue, 9 Dec 2003 13:41:50 +0900, Yukihiro Matsumoto wrote:

But still I would not deny your proposal for the sake of readability
and portability. Maybe there can be functions that work similar to
pack_sockaddr_() / unpack_sockaddr_() in the socket extension.
pack_flock() or something.

Rather, wouldn’t IO#lock/lock? methods be better?


Nobu Nakada

Date: Tue, 9 Dec 2003 13:41:50 +0900
From: Yukihiro Matsumoto matz@ruby-lang.org
Newsgroups: comp.lang.ruby
Subject: Re: RCR - ‘struct flock*’ wrapper for rb_io_fcntl

Hi,

SYNOPSIS:

ruby’s fcntl call is incomplete in that one cannot pass a ‘struct flock *’
arg as fcntl’s third arg. because of this, no posix compliant locking is
available from within ruby.

You can use pack/unpack for the purpose.

require ‘fcntl’ # import constants, perhaps this replaces IO#fcntl…

f = open(‘/tmp/foobar’, ‘w’)
flock = [Fcntl::F_RDLCK, 0, 0, 0, 0].pack(“ssqqi”)

is this guaranteed to work:

are the offsets always the same as a struct flock* as the compiler lays them
out?

if so, i am personally o.k with this. (thanks for the pointer!)

p Fcntl::F_RDLCK, Fcntl::F_GETLK
f.fcntl Fcntl::F_GETLK, flock
p flock.unpack(“ssqqi”)

But still I would not deny your proposal for the sake of readability
and portability. Maybe there can be functions that work similar to
pack_sockaddr_() / unpack_sockaddr_() in the socket extension.
pack_flock() or something.

yes that sounds good if anwser to above is ‘yes’

-a

···

On Tue, 9 Dec 2003, Yukihiro Matsumoto wrote:

In message “RCR - ‘struct flock*’ wrapper for rb_io_fcntl” > on 03/12/09, “Ara.T.Howard” ahoward@ngdc.noaa.gov writes:

ATTN: please update your address books with address below!

===============================================================================

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
STP :: Solar-Terrestrial Physics Data | NCEI
NGDC :: http://www.ngdc.noaa.gov/
NESDIS :: http://www.nesdis.noaa.gov/
NOAA :: http://www.noaa.gov/
US DOC :: http://www.commerce.gov/

The difference between art and science is that science is what we
understand well enough to explain to a computer.
Art is everything else.
– Donald Knuth, “Discover”

/bin/sh -c ‘for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done’
===============================================================================

Hi,

···

In message “Re: RCR - ‘struct flock*’ wrapper for rb_io_fcntl” on 03/12/09, nobu.nokada@softhome.net nobu.nokada@softhome.net writes:

Maybe there can be functions that work similar to
pack_sockaddr_() / unpack_sockaddr_() in the socket extension.
pack_flock() or something.

Rather, wouldn’t IO#lock/lock? methods be better?

I don’t think so. They would be confusing with IO#flock.

						matz.

Hi,

You can use pack/unpack for the purpose.

require ‘fcntl’ # import constants, perhaps this replaces IO#fcntl…

f = open(‘/tmp/foobar’, ‘w’)
flock = [Fcntl::F_RDLCK, 0, 0, 0, 0].pack(“ssqqi”)

is this guaranteed to work:

are the offsets always the same as a struct flock* as the compiler lays them
out?

if so, i am personally o.k with this. (thanks for the pointer!)

It works. Perl people do similar things a lot. But it’s not
portable.

But still I would not deny your proposal for the sake of readability
and portability. Maybe there can be functions that work similar to
pack_sockaddr_() / unpack_sockaddr_() in the socket extension.
pack_flock() or something.

yes that sounds good if anwser to above is ‘yes’

Yes, and preparing pack/unpack function, they can be portable.

						matz.
···

In message “Re: RCR - ‘struct flock*’ wrapper for rb_io_fcntl” on 03/12/09, “Ara.T.Howard” ahoward@ngdc.noaa.gov writes:

Hi,

···

At Tue, 9 Dec 2003 15:00:55 +0900, Yukihiro Matsumoto wrote:

Maybe there can be functions that work similar to
pack_sockaddr_() / unpack_sockaddr_() in the socket extension.
pack_flock() or something.

Rather, wouldn’t IO#lock/lock? methods be better?

I don’t think so. They would be confusing with IO#flock.

Then, how about replacing IO#flock by fcntl()?


Nobu Nakada

Hi,

···

In message “Re: RCR - ‘struct flock*’ wrapper for rb_io_fcntl” on 03/12/09, nobu.nokada@softhome.net nobu.nokada@softhome.net writes:

Rather, wouldn’t IO#lock/lock? methods be better?

I don’t think so. They would be confusing with IO#flock.

Then, how about replacing IO#flock by fcntl()?

Can you be more specific? Removing IO#flock? Or something else?

I think flock cannot be replaced by fcntl, because on most platform,
flock(2) and file lock by fcntl(2) are independent. IO#flock should
be implemented by flock(2) to ensure interoperability.

						matz.