RCR - 'struct flock*' wrapper for rb_io_fcntl

A

Ara.T.Howard

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 :: http://www.ngdc.noaa.gov/stp/
| 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'
===============================================================================
 
Y

Yukihiro Matsumoto

Hi,

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

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

Ara.T.Howard

Date: Tue, 9 Dec 2003 13:41:50 +0900
From: Yukihiro Matsumoto <[email protected]>
Newsgroups: comp.lang.ruby
Subject: Re: RCR - 'struct flock*' wrapper for rb_io_fcntl

Hi,

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

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

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 :: http://www.ngdc.noaa.gov/stp/
| 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'
===============================================================================
 
N

nobu.nokada

Hi,

At Tue, 9 Dec 2003 13:41:50 +0900,
Yukihiro said:
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?
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: RCR - 'struct flock*' wrapper for rb_io_fcntl"

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

Yukihiro Matsumoto

Hi,

In message "Re: RCR - 'struct flock*' wrapper for rb_io_fcntl"

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

nobu.nokada

Hi,

At Tue, 9 Dec 2003 15:00:55 +0900,
Yukihiro said:
|> 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()?
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: RCR - 'struct flock*' wrapper for rb_io_fcntl"

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

flock vs fcntl and nfs 0
[RCR] Kernel#hostname 0
make MISSING=flock.o 0
pstore and nfs 0
tk file dialog and directories 3
puts(derived_from_array) 0
install.rb (modified for bin programs) 0
[RCR] Numeric#of 47

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top