posixlock 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (8) hide show
  1. data/Makefile +126 -0
  2. data/README +78 -0
  3. data/README.EXT +1069 -0
  4. data/VERSION +1 -0
  5. data/extconf.rb +1 -0
  6. data/posixlock.c +289 -0
  7. data/posixlock.gemspec +19 -0
  8. metadata +46 -0
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1 @@
1
+ require 'mkmf' and create_makefile 'posixlock'
@@ -0,0 +1,289 @@
1
+ #ifdef _WIN32
2
+ #include "missing/file.h"
3
+ #endif
4
+
5
+ #include "ruby.h"
6
+ #include "rubyio.h"
7
+ #include "rubysig.h"
8
+
9
+ #ifdef HAVE_UNISTD_H
10
+ #include <unistd.h>
11
+ #endif
12
+
13
+ #ifdef HAVE_FCNTL_H
14
+ #include <fcntl.h>
15
+ #endif
16
+
17
+ #include <errno.h>
18
+
19
+ extern VALUE rb_cFile;
20
+ static VALUE rb_cFile_F_LOCK;
21
+ static VALUE rb_cFile_F_LOCKR;
22
+ static VALUE rb_cFile_F_LOCKW;
23
+ static VALUE rb_cFile_F_TLOCK;
24
+ static VALUE rb_cFile_F_TLOCKR;
25
+ static VALUE rb_cFile_F_TLOCKW;
26
+ static VALUE rb_cFile_F_ULOCK;
27
+ static VALUE rb_cFile_F_TEST;
28
+ static VALUE rb_cFile_F_TESTR;
29
+ static VALUE rb_cFile_F_TESTW;
30
+
31
+ # ifndef LOCK_SH
32
+ # define LOCK_SH 1
33
+ # endif
34
+ # ifndef LOCK_EX
35
+ # define LOCK_EX 2
36
+ # endif
37
+ # ifndef LOCK_NB
38
+ # define LOCK_NB 4
39
+ # endif
40
+ # ifndef LOCK_UN
41
+ # define LOCK_UN 8
42
+ # endif
43
+
44
+ #ifndef F_LOCK
45
+ #define F_LOCK 1
46
+ #endif
47
+ #ifndef F_TLOCK
48
+ #define F_TLOCK 2
49
+ #endif
50
+ #ifndef F_ULOCK
51
+ #define F_ULOCK 4
52
+ #endif
53
+ #ifndef F_TEST
54
+ #define F_TEST 8
55
+ #endif
56
+ #ifndef F_LOCKR
57
+ #define F_LOCKR 16
58
+ #endif
59
+ #ifndef F_LOCKW
60
+ #define F_LOCKW F_LOCK
61
+ #endif
62
+ #ifndef F_TLOCKR
63
+ #define F_TLOCKR 32
64
+ #endif
65
+ #ifndef F_TLOCKW
66
+ #define F_TLOCKW F_TLOCK
67
+ #endif
68
+ #ifndef F_TESTR
69
+ #define F_TESTR 64
70
+ #endif
71
+ #ifndef F_TESTW
72
+ #define F_TESTW F_TEST
73
+ #endif
74
+
75
+
76
+
77
+ static int
78
+ posixlock (fd, operation)
79
+ int fd;
80
+ int operation;
81
+ {
82
+ struct flock lock;
83
+
84
+ switch (operation & ~LOCK_NB)
85
+ {
86
+ case LOCK_SH:
87
+ lock.l_type = F_RDLCK;
88
+ break;
89
+ case LOCK_EX:
90
+ lock.l_type = F_WRLCK;
91
+ break;
92
+ case LOCK_UN:
93
+ lock.l_type = F_UNLCK;
94
+ break;
95
+ default:
96
+ errno = EINVAL;
97
+ return -1;
98
+ }
99
+ lock.l_whence = SEEK_SET;
100
+ lock.l_start = lock.l_len = 0L;
101
+ return fcntl (fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &lock);
102
+ }
103
+
104
+
105
+ static VALUE
106
+ rb_file_posixlock (obj, operation)
107
+ VALUE obj;
108
+ VALUE operation;
109
+ {
110
+ #ifndef __CHECKER__
111
+ OpenFile *fptr;
112
+ int ret;
113
+
114
+ rb_secure (2);
115
+ GetOpenFile (obj, fptr);
116
+
117
+ if (fptr->mode & FMODE_WRITABLE)
118
+ {
119
+ fflush (GetWriteFile (fptr));
120
+ }
121
+ retry:
122
+ TRAP_BEG;
123
+ ret = posixlock (fileno (fptr->f), NUM2INT (operation));
124
+ TRAP_END;
125
+ if (ret < 0)
126
+ {
127
+ switch (errno)
128
+ {
129
+ case EAGAIN:
130
+ case EACCES:
131
+ #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
132
+ case EWOULDBLOCK:
133
+ #endif
134
+ return Qfalse;
135
+ case EINTR:
136
+ #if defined(ERESTART)
137
+ case ERESTART:
138
+ #endif
139
+ goto retry;
140
+ }
141
+ rb_sys_fail (fptr->path);
142
+ }
143
+ #endif
144
+
145
+ return INT2FIX (0);
146
+ }
147
+
148
+
149
+ static VALUE
150
+ rb_file_lockf (obj, cmd, len)
151
+ VALUE obj;
152
+ VALUE cmd;
153
+ VALUE len;
154
+ {
155
+ #ifndef __CHECKER__
156
+ OpenFile *fptr;
157
+ int ret;
158
+ int pid;
159
+ int f_test;
160
+ struct flock lock;
161
+ char msg[1024];
162
+
163
+
164
+ rb_secure (2);
165
+ GetOpenFile (obj, fptr);
166
+
167
+ snprintf (msg, 1024, "path <%s>", fptr->path);
168
+
169
+ if (fptr->mode & FMODE_WRITABLE)
170
+ {
171
+ fflush (GetWriteFile (fptr));
172
+ }
173
+ retry:
174
+ TRAP_BEG;
175
+ pid = -1;
176
+ f_test = 0;
177
+ lock.l_whence = SEEK_CUR;
178
+ lock.l_start = 0L;
179
+ lock.l_len = NUM2INT (len);
180
+ switch (FIX2INT (cmd))
181
+ {
182
+ case F_LOCK:
183
+ lock.l_type = F_WRLCK;
184
+ ret = fcntl (fileno (fptr->f), F_SETLKW, &lock);
185
+ break;
186
+ case F_LOCKR:
187
+ lock.l_type = F_RDLCK;
188
+ ret = fcntl (fileno (fptr->f), F_SETLKW, &lock);
189
+ break;
190
+ case F_TLOCK:
191
+ lock.l_type = F_WRLCK;
192
+ ret = fcntl (fileno (fptr->f), F_SETLK, &lock);
193
+ break;
194
+ case F_TLOCKR:
195
+ lock.l_type = F_RDLCK;
196
+ ret = fcntl (fileno (fptr->f), F_SETLK, &lock);
197
+ break;
198
+ case F_ULOCK:
199
+ lock.l_type = F_UNLCK;
200
+ ret = fcntl (fileno (fptr->f), F_SETLK, &lock);
201
+ break;
202
+ case F_TEST:
203
+ f_test = 1;
204
+ lock.l_type = F_WRLCK;
205
+ ret = fcntl (fileno (fptr->f), F_GETLK, &lock);
206
+ if (ret == 0 && lock.l_type != F_UNLCK)
207
+ {
208
+ pid = lock.l_pid;
209
+ }
210
+ break;
211
+ case F_TESTR:
212
+ f_test = 1;
213
+ lock.l_type = F_RDLCK;
214
+ ret = fcntl (fileno (fptr->f), F_GETLK, &lock);
215
+ if (ret == 0 && lock.l_type != F_UNLCK)
216
+ {
217
+ pid = lock.l_pid;
218
+ }
219
+ break;
220
+ default:
221
+ errno = EINVAL;
222
+ snprintf (msg, 1024, "invalid cmd <%d>", FIX2INT (cmd));
223
+ ret = -1;
224
+ }
225
+ TRAP_END;
226
+ if (ret < 0)
227
+ {
228
+ switch (errno)
229
+ {
230
+ case EAGAIN:
231
+ case EACCES:
232
+ #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
233
+ case EWOULDBLOCK:
234
+ #endif
235
+ return Qfalse;
236
+ case EINTR:
237
+ #if defined(ERESTART)
238
+ case ERESTART:
239
+ #endif
240
+ goto retry;
241
+ }
242
+ rb_sys_fail (msg);
243
+ }
244
+ #endif
245
+
246
+ if (f_test)
247
+ {
248
+ if (pid != -1)
249
+ {
250
+ return INT2FIX (pid);
251
+ }
252
+ else
253
+ {
254
+ return Qnil;
255
+ }
256
+ }
257
+ else
258
+ {
259
+ return INT2FIX (0);
260
+ }
261
+ }
262
+
263
+
264
+ void
265
+ Init_posixlock ()
266
+ {
267
+ rb_cFile_F_LOCK = INT2FIX (F_LOCK);
268
+ rb_cFile_F_LOCKR = INT2FIX (F_LOCKR);
269
+ rb_cFile_F_LOCKW = INT2FIX (F_LOCKW);
270
+ rb_cFile_F_TLOCK = INT2FIX (F_TLOCK);
271
+ rb_cFile_F_TLOCKR = INT2FIX (F_TLOCKR);
272
+ rb_cFile_F_TLOCKW = INT2FIX (F_TLOCKW);
273
+ rb_cFile_F_ULOCK = INT2FIX (F_ULOCK);
274
+ rb_cFile_F_TEST = INT2FIX (F_TEST);
275
+ rb_cFile_F_TESTR = INT2FIX (F_TESTR);
276
+ rb_cFile_F_TESTW = INT2FIX (F_TESTW);
277
+ rb_define_const (rb_cFile, "F_LOCK", rb_cFile_F_LOCK);
278
+ rb_define_const (rb_cFile, "F_LOCKR", rb_cFile_F_LOCKR);
279
+ rb_define_const (rb_cFile, "F_LOCKW", rb_cFile_F_LOCKW);
280
+ rb_define_const (rb_cFile, "F_TLOCK", rb_cFile_F_TLOCK);
281
+ rb_define_const (rb_cFile, "F_TLOCKR", rb_cFile_F_TLOCKR);
282
+ rb_define_const (rb_cFile, "F_TLOCKW", rb_cFile_F_TLOCKW);
283
+ rb_define_const (rb_cFile, "F_ULOCK", rb_cFile_F_ULOCK);
284
+ rb_define_const (rb_cFile, "F_TEST", rb_cFile_F_TEST);
285
+ rb_define_const (rb_cFile, "F_TESTR", rb_cFile_F_TESTR);
286
+ rb_define_const (rb_cFile, "F_TESTW", rb_cFile_F_TESTW);
287
+ rb_define_method (rb_cFile, "lockf", rb_file_lockf, 2);
288
+ rb_define_method (rb_cFile, "posixlock", rb_file_posixlock, 1);
289
+ }
@@ -0,0 +1,19 @@
1
+ require 'date'
2
+ Gem::Specification.new do |s|
3
+ s.name = %q{posixlock}
4
+ s.version = "0.0.1"
5
+ s.date = Date.today.to_s
6
+ s.summary = %q{Methods to add posix (fcntl based and nfs safe) locking to the builtin File class}
7
+ s.description =<<DESCRIPTION
8
+ Methods to add posix (fcntl based and nfs safe) locking to the builtin File class
9
+ DESCRIPTION
10
+ s.author = %q{-a}
11
+ s.email = %q{ara.t.howard@noaa.gov}
12
+ s.homepage = %q{http://www.codeforpeople.com/lib/ruby/posixlock/}
13
+ s.files = Dir.glob('**/*')
14
+ s.autorequire = %q{posixlock}
15
+ s.has_rdoc = true
16
+ s.rdoc_options = ["--main", "README"]
17
+ s.extra_rdoc_files = ["README"]
18
+ s.extensions = %w{extconf.rb}
19
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: "0.8"
3
+ specification_version: 1
4
+ name: posixlock
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2004-11-01
8
+ summary: Methods to add posix (fcntl based and nfs safe) locking to the builtin File class
9
+ require_paths:
10
+ - lib
11
+ author: "-a"
12
+ email: ara.t.howard@noaa.gov
13
+ homepage: http://www.codeforpeople.com/lib/ruby/posixlock/
14
+ rubyforge_project:
15
+ description: Methods to add posix (fcntl based and nfs safe) locking to the builtin File class
16
+ autorequire: posixlock
17
+ default_executable:
18
+ bindir: bin
19
+ has_rdoc: true
20
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
21
+ requirements:
22
+ -
23
+ - ">"
24
+ - !ruby/object:Gem::Version
25
+ version: 0.0.0
26
+ version:
27
+ platform: ruby
28
+ files:
29
+ - Makefile
30
+ - posixlock.c
31
+ - README.EXT
32
+ - extconf.rb
33
+ - VERSION
34
+ - README
35
+ - posixlock.gemspec
36
+ test_files: []
37
+ rdoc_options:
38
+ - "--main"
39
+ - README
40
+ extra_rdoc_files:
41
+ - README
42
+ executables: []
43
+ extensions:
44
+ - extconf.rb
45
+ requirements: []
46
+ dependencies: []