Ruby: missing/flock.c Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

flock.c
1#include "ruby/internal/config.h"
2#include "ruby/ruby.h"
3
4#if defined _WIN32
5#elif defined __wasi__
6#include <errno.h>
7
8int
9flock(int fd, int operation)
10{
11 errno = EINVAL;
12 return -1;
13}
14#elif defined HAVE_FCNTL && defined HAVE_FCNTL_H
15
16/* These are the flock() constants. Since this systems doesn't have
17 flock(), the values of the constants are probably not available.
18*/
19# ifndef LOCK_SH
20# define LOCK_SH 1
21# endif
22# ifndef LOCK_EX
23# define LOCK_EX 2
24# endif
25# ifndef LOCK_NB
26# define LOCK_NB 4
27# endif
28# ifndef LOCK_UN
29# define LOCK_UN 8
30# endif
31
32#include <fcntl.h>
33#include <unistd.h>
34#include <errno.h>
35
36int
37flock(int fd, int operation)
38{
39 struct flock lock;
40
41 switch (operation & ~LOCK_NB) {
42 case LOCK_SH:
43 lock.l_type = F_RDLCK;
44 break;
45 case LOCK_EX:
46 lock.l_type = F_WRLCK;
47 break;
48 case LOCK_UN:
49 lock.l_type = F_UNLCK;
50 break;
51 default:
52 errno = EINVAL;
53 return -1;
54 }
55 lock.l_whence = SEEK_SET;
56 lock.l_start = lock.l_len = 0L;
57
58 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &lock);
59}
60
61#elif defined(HAVE_LOCKF)
62
63#include <unistd.h>
64#include <errno.h>
65
66/* Emulate flock() with lockf() or fcntl(). This is just to increase
67 portability of scripts. The calls might not be completely
68 interchangeable. What's really needed is a good file
69 locking module.
70*/
71
72# ifndef F_ULOCK
73# define F_ULOCK 0 /* Unlock a previously locked region */
74# endif
75# ifndef F_LOCK
76# define F_LOCK 1 /* Lock a region for exclusive use */
77# endif
78# ifndef F_TLOCK
79# define F_TLOCK 2 /* Test and lock a region for exclusive use */
80# endif
81# ifndef F_TEST
82# define F_TEST 3 /* Test a region for other processes locks */
83# endif
84
85/* These are the flock() constants. Since this systems doesn't have
86 flock(), the values of the constants are probably not available.
87*/
88# ifndef LOCK_SH
89# define LOCK_SH 1
90# endif
91# ifndef LOCK_EX
92# define LOCK_EX 2
93# endif
94# ifndef LOCK_NB
95# define LOCK_NB 4
96# endif
97# ifndef LOCK_UN
98# define LOCK_UN 8
99# endif
100
101int
102flock(int fd, int operation)
103{
104 switch (operation) {
105
106 /* LOCK_SH - get a shared lock */
107 case LOCK_SH:
108 rb_notimplement();
109 return -1;
110 /* LOCK_EX - get an exclusive lock */
111 case LOCK_EX:
112 return lockf (fd, F_LOCK, 0);
113
114 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
115 case LOCK_SH|LOCK_NB:
116 rb_notimplement();
117 return -1;
118 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
119 case LOCK_EX|LOCK_NB:
120 return lockf (fd, F_TLOCK, 0);
121
122 /* LOCK_UN - unlock */
123 case LOCK_UN:
124 return lockf (fd, F_ULOCK, 0);
125
126 /* Default - can't decipher operation */
127 default:
128 errno = EINVAL;
129 return -1;
130 }
131}
132#else
133int
134flock(int fd, int operation)
135{
136 rb_notimplement();
137 return -1;
138}
139#endif
rb_notimplement
void rb_notimplement(void)
Definition error.c:3193
ruby.h