public IRubyObject flock(ThreadContext context, IRubyObject lockingConstant) {
Ruby runtime = context.runtime;
// TODO: port exact behavior from MRI, and move most locking logic into ChannelDescriptor
// TODO: for all LOCK_NB cases, return false if they would block
ChannelDescriptor descriptor;
try {
descriptor = openFile.getMainStreamSafe().getDescriptor();
} catch (BadDescriptorException e) {
throw context.runtime.newErrnoEBADFError();
}
// null channel always succeeds for all locking operations
if (descriptor.isNull()) return RubyFixnum.zero(runtime);
if (descriptor.getChannel() instanceof FileChannel) {
FileChannel fileChannel = (FileChannel)descriptor.getChannel();
int lockMode = RubyNumeric.num2int(lockingConstant);
checkSharedExclusive(runtime, openFile, lockMode);
if (!lockStateChanges(currentLock, lockMode)) return RubyFixnum.zero(runtime);