|
36 | 36 | import java.io.File;
|
37 | 37 | import java.io.IOException;
|
38 | 38 | import java.io.InputStream;
|
| 39 | +import java.nio.file.Files; |
| 40 | +import java.nio.file.LinkOption; |
| 41 | +import java.nio.file.Path; |
| 42 | +import java.nio.file.attribute.BasicFileAttributes; |
39 | 43 |
|
40 | 44 | import jnr.posix.FileStat;
|
41 | 45 | import org.jruby.anno.JRubyMethod;
|
@@ -278,21 +282,23 @@ public static IRubyObject sticky_p(IRubyObject recv, IRubyObject filename) {
|
278 | 282 | @JRubyMethod(name = "symlink?", required = 1, module = true)
|
279 | 283 | public static RubyBoolean symlink_p(IRubyObject recv, IRubyObject filename) {
|
280 | 284 | Ruby runtime = recv.getRuntime();
|
281 |
| - IRubyObject oldExc = runtime.getGlobalVariables().get("$!"); // Save $! |
282 |
| - |
| 285 | + |
| 286 | + Path p = new File(filename.toString()).toPath(); |
| 287 | + |
283 | 288 | try {
|
284 | 289 | // Note: We can't use file.exists() to check whether the symlink
|
285 | 290 | // exists or not, because that method returns false for existing
|
286 | 291 | // but broken symlink. So, we try without the existence check,
|
287 | 292 | // but in the try-catch block.
|
288 | 293 | // MRI behavior: symlink? on broken symlink should return true.
|
289 |
| - FileStat stat = fileResource(filename).lstat(); |
| 294 | + BasicFileAttributes attrs = Files.readAttributes(p, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); |
290 | 295 |
|
291 |
| - return runtime.newBoolean(stat != null && stat.isSymlink()); |
292 |
| - } catch (SecurityException re) { |
| 296 | + return runtime.newBoolean(attrs != null && attrs.isSymbolicLink()); |
| 297 | + } catch (SecurityException se) { |
| 298 | + return runtime.getFalse(); |
| 299 | + } catch (IOException ie) { |
293 | 300 | return runtime.getFalse();
|
294 |
| - } catch (RaiseException re) { |
295 |
| - runtime.getGlobalVariables().set("$!", oldExc); // Restore $! |
| 301 | + } catch (UnsupportedOperationException uoe) { |
296 | 302 | return runtime.getFalse();
|
297 | 303 | }
|
298 | 304 | }
|
|
0 commit comments