SYNOPSIS:
posixlock
use fcntl (vs. flock) based locking for File#flock
USAGE
require ‘posixlock’ # replaces File#flock!
i currently do not have a place to post software - so here’s a little
extension that replaces flock with an fcntl based impl for posix compliant
locking (safe on nfs) - posted to c.l.r for posterity:
extconf.rb
···
================
require ‘mkmf’
create_makefile ‘posixlock’
posixlock.c
#ifdef _WIN32
#include “missing/file.h”
#endif
#include “ruby.h”
#include “rubyio.h”
#include “rubysig.h”
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <errno.h>
extern VALUE rb_cFile;
ifndef LOCK_SH
define LOCK_SH 1
endif
ifndef LOCK_EX
define LOCK_EX 2
endif
ifndef LOCK_NB
define LOCK_NB 4
endif
ifndef LOCK_UN
define LOCK_UN 8
endif
static int
posixlock (fd, operation)
int fd;
int operation;
{
struct flock lock;
switch (operation & ~LOCK_NB)
{
case LOCK_SH:
lock.l_type = F_RDLCK;
break;
case LOCK_EX:
lock.l_type = F_WRLCK;
break;
case LOCK_UN:
lock.l_type = F_UNLCK;
break;
default:
errno = EINVAL;
return -1;
}
lock.l_whence = SEEK_SET;
lock.l_start = lock.l_len = 0L;
return fcntl (fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &lock);
}
static VALUE
rb_file_posixlock (obj, operation)
VALUE obj;
VALUE operation;
{
#ifndef CHECKER
OpenFile *fptr;
int ret;
rb_secure (2);
GetOpenFile (obj, fptr);
if (fptr->mode & FMODE_WRITABLE)
{
fflush (GetWriteFile (fptr));
}
retry:
TRAP_BEG;
ret = posixlock (fileno (fptr->f), NUM2INT (operation));
TRAP_END;
if (ret < 0)
{
switch (errno)
{
case EAGAIN:
case EACCES:
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
return Qfalse;
case EINTR:
#if defined(ERESTART)
case ERESTART:
#endif
goto retry;
}
rb_sys_fail (fptr->path);
}
#endif
return INT2FIX (0);
}
void
Init_posixlock()
{
rb_define_method(rb_cFile, “flock”, rb_file_posixlock, 1);
}
-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’
===============================================================================