From fcbf63e62c627deae76c1b8cb8c0876c536ed811 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Mon, 16 Mar 2020 18:49:26 +0900 Subject: Fresh start --- jni/ruby/missing/flock.c | 130 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 jni/ruby/missing/flock.c (limited to 'jni/ruby/missing/flock.c') diff --git a/jni/ruby/missing/flock.c b/jni/ruby/missing/flock.c new file mode 100644 index 0000000..829f431 --- /dev/null +++ b/jni/ruby/missing/flock.c @@ -0,0 +1,130 @@ +#include "ruby/config.h" +#include "ruby/ruby.h" + +#if defined _WIN32 +#elif defined HAVE_FCNTL && defined HAVE_FCNTL_H && !defined(__native_client__) + +/* These are the flock() constants. Since this systems doesn't have + flock(), the values of the constants are probably not available. +*/ +# 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 + +#include +#include +#include + +int +flock(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); +} + +#elif defined(HAVE_LOCKF) + +#include +#include + +/* Emulate flock() with lockf() or fcntl(). This is just to increase + portability of scripts. The calls might not be completely + interchangeable. What's really needed is a good file + locking module. +*/ + +# ifndef F_ULOCK +# define F_ULOCK 0 /* Unlock a previously locked region */ +# endif +# ifndef F_LOCK +# define F_LOCK 1 /* Lock a region for exclusive use */ +# endif +# ifndef F_TLOCK +# define F_TLOCK 2 /* Test and lock a region for exclusive use */ +# endif +# ifndef F_TEST +# define F_TEST 3 /* Test a region for other processes locks */ +# endif + +/* These are the flock() constants. Since this systems doesn't have + flock(), the values of the constants are probably not available. +*/ +# 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 + +int +flock(int fd, int operation) +{ + switch (operation) { + + /* LOCK_SH - get a shared lock */ + case LOCK_SH: + rb_notimplement(); + return -1; + /* LOCK_EX - get an exclusive lock */ + case LOCK_EX: + return lockf (fd, F_LOCK, 0); + + /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */ + case LOCK_SH|LOCK_NB: + rb_notimplement(); + return -1; + /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */ + case LOCK_EX|LOCK_NB: + return lockf (fd, F_TLOCK, 0); + + /* LOCK_UN - unlock */ + case LOCK_UN: + return lockf (fd, F_ULOCK, 0); + + /* Default - can't decipher operation */ + default: + errno = EINVAL; + return -1; + } +} +#else +int +flock(int fd, int operation) +{ + rb_notimplement(); + return -1; +} +#endif -- cgit v1.2.3