Skip to content

Commit

Permalink
Merge pull request #5168 from jruby/fu_mv_exdev
Browse files Browse the repository at this point in the history
Remove hack for FileUtils.mv due to incorrect error raised from File.rename
headius authored May 13, 2018

Verified

This commit was signed with the committer’s verified signature.
headius Charles Oliver Nutter
2 parents 30e9fa4 + da420c9 commit be4a079
Showing 4 changed files with 20 additions and 11 deletions.
4 changes: 4 additions & 0 deletions core/src/main/java/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
@@ -3814,6 +3814,10 @@ public RaiseException newErrnoEMSGSIZEError() {
return newRaiseException(getErrno().getClass("EMSGSIZE"), null);
}

public RaiseException newErrnoEXDEVError(String message) {
return newRaiseException(getErrno().getClass("EXDEV"), message);
}

public RaiseException newIndexError(String message) {
return newRaiseException(getIndexError(), message);
}
14 changes: 14 additions & 0 deletions core/src/main/java/org/jruby/RubyFile.java
Original file line number Diff line number Diff line change
@@ -65,6 +65,7 @@
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
@@ -1083,6 +1084,19 @@ public static IRubyObject rename(ThreadContext context, IRubyObject recv, IRubyO
newFile.delete();
}

// Check if source file and dest parent are on same filesystem or raise EXDEV
Path oldPath = Paths.get(oldFile.toURI());
Path destPath = Paths.get(dest.getAbsolutePath());
try {
FileStore oldStore = Files.getFileStore(oldPath);
FileStore destStore = Files.getFileStore(destPath.getParent());
if (!oldStore.equals(destStore)) {
throw runtime.newErrnoEXDEVError("(" + oldFile + ", " + dest + ")");
}
} catch (IOException ioe) {
throw Helpers.newIOErrorFromException(runtime, ioe);
}

if (oldFile.renameTo(dest)) { // try to rename one more time
return RubyFixnum.zero(runtime);
}
Original file line number Diff line number Diff line change
@@ -402,7 +402,7 @@ protected static String printBacktraceJRuby(RubyException exception, boolean con
String message;
try {
message = exception.callMethod(context, "message").toString();
} catch (org.jruby.exceptions.Exception _) {
} catch (org.jruby.exceptions.Exception unused) {
message = exception.message(context).toString();
}
if (exception.getMetaClass() == runtime.getRuntimeError() && message.length() == 0) {
11 changes: 1 addition & 10 deletions lib/ruby/stdlib/fileutils.rb
Original file line number Diff line number Diff line change
@@ -469,7 +469,7 @@ def mv(src, dest, force: nil, noop: nil, verbose: nil, secure: nil)
end
begin
File.rename s, d
rescue *MV_RESCUES
rescue EXDEV
copy_entry s, d, true
if secure
remove_entry_secure s, force
@@ -484,15 +484,6 @@ def mv(src, dest, force: nil, noop: nil, verbose: nil, secure: nil)
end
module_function :mv

# JRuby raises EACCES because JDK reports errors differently
MV_RESCUES = begin
if RUBY_ENGINE == 'jruby'
[Errno::EXDEV, Errno::EACCES]
else
[Errno::EXDEV]
end
end

alias move mv
module_function :move

0 comments on commit be4a079

Please sign in to comment.