Skip to content

FileUtils.chmod failes when called on symlink under linux #5547

@kschoelhorn

Description

@kschoelhorn

Calling FileUtils.chmod on a symlink tries to call File.lchmod if available. It checks this by calling File.lchmod with an empty file list. This succeeds, because POSIX.lchmod is not actually called in this case:

@JRubyMethod(required = 1, rest = true, meta = true)
public static IRubyObject lchmod(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
Ruby runtime = context.runtime;
int count = 0;
RubyInteger mode = args[0].convertToInteger();
for (int i = 1; i < args.length; i++) {
JRubyFile file = file(args[i]);
if (0 != runtime.getPosix().lchmod(file.toString(), (int) mode.getLongValue())) {
throw runtime.newErrnoFromLastPOSIXErrno();
} else {
count++;
}
}
return runtime.newFixnum(count);
}

This leads to the following error, because lchmod does not exist under linux:

NotImplementedError: No message available
  lchmod at org/jruby/RubyFile.java:1019
   chmod at /data/software/jruby/9.2.5.0/lib/ruby/stdlib/fileutils.rb:1320
   chmod at /data/software/jruby/9.2.5.0/lib/ruby/stdlib/fileutils.rb:1000
    each at org/jruby/RubyArray.java:1792
   chmod at /data/software/jruby/9.2.5.0/lib/ruby/stdlib/fileutils.rb:999
  <main> at ./test.rb:15

This is the test program I used, which includes a workaround that makes sure File.lchmod is not considered available:

#!/usr/bin/env jruby

require 'fileutils'

# uncomment to fix NotImplementedError
# class FileUtils::Entry_
#   def have_lchmod?
#     false
#   end
# end

FileUtils.rm_f "link"
FileUtils.ln_s "target", "link"

FileUtils.chmod 400, "link"

I am using jruby 9.2.5.0 under RHEL 7.4.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions