@JRubyMethod(required = 2, meta = true) public static IRubyObject rename(ThreadContext context, IRubyObject recv, IRubyObject oldName, IRubyObject newName) { Ruby runtime = context.runtime; RubyString oldNameString = get_path(context, oldName); RubyString newNameString = get_path(context, newName); String newNameJavaString = newNameString.getUnicodeValue(); String oldNameJavaString = oldNameString.getUnicodeValue(); JRubyFile oldFile = JRubyFile.create(runtime.getCurrentDirectory(), oldNameJavaString); JRubyFile newFile = JRubyFile.create(runtime.getCurrentDirectory(), newNameJavaString); if (!oldFile.exists() || !newFile.getParentFile().exists()) { throw runtime.newErrnoENOENTError(oldNameJavaString + " or " + newNameJavaString); } JRubyFile dest = JRubyFile.create(runtime.getCurrentDirectory(), newNameJavaString); if (oldFile.renameTo(dest)) { // rename is successful return RubyFixnum.zero(runtime); } // rename via Java API call wasn't successful, let's try some tricks, similar to MRI if (newFile.exists()) { runtime.getPosix().chmod(newNameJavaString, 0666); newFile.delete(); } if (oldFile.renameTo(dest)) { // try to rename one more time return RubyFixnum.zero(runtime); } throw runtime.newErrnoEACCESError(oldNameJavaString + " or " + newNameJavaString); }
@JRubyMethod(required = 2, meta = true) public static IRubyObject rename(ThreadContext context, IRubyObject recv, IRubyObject oldName, IRubyObject newName) { Ruby runtime = context.runtime; RubyString oldNameString = get_path(context, oldName); RubyString newNameString = get_path(context, newName); String newNameJavaString = newNameString.getUnicodeValue(); String oldNameJavaString = oldNameString.getUnicodeValue(); JRubyFile oldFile = JRubyFile.create(runtime.getCurrentDirectory(), oldNameJavaString); JRubyFile newFile = JRubyFile.create(runtime.getCurrentDirectory(), newNameJavaString); if (!oldFile.exists() || !newFile.getParentFile().exists()) { throw runtime.newErrnoENOENTError(oldNameJavaString + " or " + newNameJavaString); } JRubyFile dest = JRubyFile.create(runtime.getCurrentDirectory(), newNameJavaString); if (oldFile.renameTo(dest)) { // rename is successful return RubyFixnum.zero(runtime); } // rename via Java API call wasn't successful, let's try some tricks, similar to MRI if (newFile.exists()) { runtime.getPosix().chmod(newNameJavaString, 0666); newFile.delete(); } if (oldFile.renameTo(dest)) { // try to rename one more time return RubyFixnum.zero(runtime); } throw runtime.newErrnoEACCESError(oldNameJavaString + " or " + newNameJavaString); }
if (oldFile.renameTo(dest)) { // rename is successful return RubyFixnum.zero(runtime);
if (oldFile.renameTo(dest)) { // rename is successful return RubyFixnum.zero(runtime);