Skip to content

Commit

Permalink
[fix] MRI raises EISDIR on File.delete with dir
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Jun 20, 2018
1 parent d0e2144 commit b43b9d8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
15 changes: 9 additions & 6 deletions core/src/main/java/org/jruby/RubyFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -1279,27 +1279,30 @@ public static IRubyObject delete(ThreadContext context, IRubyObject recv, IRubyO

for (int i = 0; i < args.length; i++) {
RubyString filename = StringSupport.checkEmbeddedNulls(runtime, get_path(context, args[i]));
JRubyFile lToDelete = JRubyFile.create(runtime.getCurrentDirectory(), filename.getUnicodeValue());
JRubyFile file = JRubyFile.create(runtime.getCurrentDirectory(), filename.getUnicodeValue());

boolean isSymlink = RubyFileTest.symlink_p(recv, filename).isTrue();
// Broken symlinks considered by exists() as non-existing,
// so we need to check for symlinks explicitly.
if (!lToDelete.exists() && !isSymlink) {
if (!file.exists() && !isSymlink(context, file)) {
throw runtime.newErrnoENOENTError(filename.getUnicodeValue());
}

if (lToDelete.isDirectory() && !isSymlink) {
throw runtime.newErrnoEPERMError(filename.getUnicodeValue());
if (file.isDirectory() && !isSymlink(context, file)) {
throw runtime.newErrnoEISDirError(filename.getUnicodeValue());
}

if (!lToDelete.delete()) {
if (!file.delete()) {
throw runtime.newErrnoEACCESError(filename.getUnicodeValue());
}
}

return runtime.newFixnum(args.length);
}

private static boolean isSymlink(ThreadContext context, JRubyFile file) {
return FileResource.wrap(context.runtime.getPosix(), file).isSymLink();
}

@JRubyMethod(rest = true, meta = true)
public static IRubyObject unlink(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
Ruby runtime = context.runtime;
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/org/jruby/util/FileResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.InputStream;

import jnr.posix.FileStat;
import jnr.posix.POSIX;
import org.jruby.util.io.ModeFlags;

import java.nio.channels.Channel;
Expand All @@ -13,6 +14,10 @@
*/
public interface FileResource {

static FileResource wrap(POSIX posix, JRubyFile file) {
return new RegularFileResource(posix, file, file.getPathDefault());
}

String absolutePath();
String canonicalPath();

Expand Down
4 changes: 1 addition & 3 deletions test/jruby/test_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1288,9 +1288,7 @@ def test_file_stat_with_missing_path
# JRUBY-4859
def test_file_delete_directory
Dir.mkdir("dir_tmp")
assert_raise(Errno::EPERM) {
File.delete "dir_tmp"
}
assert_raise(Errno::EISDIR) { File.delete "dir_tmp" }
ensure
Dir.rmdir("dir_tmp")
end
Expand Down

0 comments on commit b43b9d8

Please sign in to comment.