|
67 | 67 | import static org.jruby.RubyEnumerator.enumeratorize; |
68 | 68 | import static org.jruby.RubyFile.filePathConvert; |
69 | 69 | import static org.jruby.RubyString.UTF8; |
| 70 | +import static org.jruby.api.Check.checkEmbeddedNulls; |
70 | 71 | import static org.jruby.api.Convert.asBoolean; |
71 | 72 | import static org.jruby.api.Convert.asFixnum; |
72 | 73 | import static org.jruby.api.Create.newArray; |
@@ -152,24 +153,21 @@ public IRubyObject initialize(ThreadContext context, IRubyObject path, IRubyObje |
152 | 153 | } |
153 | 154 |
|
154 | 155 | private RubyDir initializeCommon(ThreadContext context, IRubyObject pathArg, IRubyObject encOpts) { |
155 | | - Ruby runtime = context.runtime; |
156 | | - |
157 | 156 | Encoding encoding = null; |
158 | 157 |
|
159 | 158 | if (!encOpts.isNil()) { |
160 | 159 | RubyHash opts = encOpts.convertToHash(); |
161 | 160 | IRubyObject encodingArg = ArgsUtil.extractKeywordArg(context, opts, "encoding"); |
162 | 161 | if (encodingArg != null && !encodingArg.isNil()) { |
163 | | - encoding = runtime.getEncodingService().getEncodingFromObject(encodingArg); |
| 162 | + encoding = context.runtime.getEncodingService().getEncodingFromObject(encodingArg); |
164 | 163 | } |
165 | 164 | } |
166 | 165 |
|
167 | | - if (encoding == null) encoding = runtime.getEncodingService().getFileSystemEncoding(); |
| 166 | + if (encoding == null) encoding = context.runtime.getEncodingService().getFileSystemEncoding(); |
168 | 167 |
|
169 | | - RubyString newPath = StringSupport.checkEmbeddedNulls(runtime, RubyFile.get_path(context, pathArg)); |
| 168 | + RubyString newPath = checkEmbeddedNulls(context, RubyFile.get_path(context, pathArg)); |
170 | 169 | this.path = newPath; |
171 | 170 | this.pos = 0; |
172 | | - |
173 | 171 | this.encoding = encoding; |
174 | 172 |
|
175 | 173 | String adjustedPath = RubyFile.getAdjustedPath(context, newPath); |
@@ -365,19 +363,14 @@ public RubyArray entries() { |
365 | 363 | */ |
366 | 364 | @JRubyMethod(name = "entries", meta = true) |
367 | 365 | public static RubyArray entries(ThreadContext context, IRubyObject recv, IRubyObject arg) { |
368 | | - Ruby runtime = context.runtime; |
369 | | - |
370 | | - RubyString path = StringSupport.checkEmbeddedNulls(runtime, RubyFile.get_path(context, arg)); |
| 366 | + RubyString path = checkEmbeddedNulls(context, RubyFile.get_path(context, arg)); |
371 | 367 |
|
372 | | - return entriesCommon(context, path, runtime.getDefaultFilesystemEncoding(), false); |
| 368 | + return entriesCommon(context, path, context.runtime.getDefaultFilesystemEncoding(), false); |
373 | 369 | } |
374 | 370 |
|
375 | 371 | @JRubyMethod(name = "entries", meta = true) |
376 | 372 | public static RubyArray entries(ThreadContext context, IRubyObject recv, IRubyObject arg, IRubyObject opts) { |
377 | | - Ruby runtime = context.runtime; |
378 | | - |
379 | | - RubyString path = StringSupport.checkEmbeddedNulls(runtime, RubyFile.get_path(context, arg)); |
380 | | - |
| 373 | + RubyString path = checkEmbeddedNulls(context, RubyFile.get_path(context, arg)); |
381 | 374 | Encoding encoding = getEncodingFromOpts(context, opts); |
382 | 375 |
|
383 | 376 | return entriesCommon(context, path, encoding, false); |
@@ -434,10 +427,8 @@ private static void checkDirIsTwoSlashesOnWindows(Ruby runtime, String path) { |
434 | 427 |
|
435 | 428 | /** Changes the current directory to <code>path</code> */ |
436 | 429 | @JRubyMethod(meta = true) |
437 | | - public static IRubyObject chdir(ThreadContext context, IRubyObject recv, IRubyObject _path, Block block) { |
438 | | - RubyString path = StringSupport.checkEmbeddedNulls(context.runtime, RubyFile.get_path(context, _path)); |
439 | | - |
440 | | - return chdirCommon(context, block, path); |
| 430 | + public static IRubyObject chdir(ThreadContext context, IRubyObject recv, IRubyObject path, Block block) { |
| 431 | + return chdirCommon(context, block, checkEmbeddedNulls(context, RubyFile.get_path(context, path))); |
441 | 432 | } |
442 | 433 |
|
443 | 434 | /** Changes the current directory to <code>path</code> */ |
@@ -535,20 +526,18 @@ public static IRubyObject rmdir19(IRubyObject recv, IRubyObject path) { |
535 | 526 | */ |
536 | 527 | @JRubyMethod(name = {"rmdir", "unlink", "delete"}, meta = true) |
537 | 528 | public static IRubyObject rmdir(ThreadContext context, IRubyObject recv, IRubyObject path) { |
538 | | - Ruby runtime = context.runtime; |
539 | | - RubyString cleanPath = StringSupport.checkEmbeddedNulls(runtime, RubyFile.get_path(context, path)); |
540 | | - return rmdirCommon(runtime, cleanPath.asJavaString()); |
| 529 | + return rmdirCommon(context, checkEmbeddedNulls(context, RubyFile.get_path(context, path)).asJavaString()); |
541 | 530 | } |
542 | 531 |
|
543 | | - private static RubyFixnum rmdirCommon(Ruby runtime, String path) { |
544 | | - JRubyFile directory = getDirForRmdir(runtime, path); |
| 532 | + private static RubyFixnum rmdirCommon(ThreadContext context, String path) { |
| 533 | + JRubyFile directory = getDirForRmdir(context.runtime, path); |
545 | 534 |
|
546 | 535 | // at this point, only thing preventing delete should be non-emptiness |
547 | | - if (runtime.getPosix().rmdir(directory.toString()) < 0) { |
548 | | - throw runtime.newErrnoENOTEMPTYError(path); |
| 536 | + if (context.runtime.getPosix().rmdir(directory.toString()) < 0) { |
| 537 | + throw context.runtime.newErrnoENOTEMPTYError(path); |
549 | 538 | } |
550 | 539 |
|
551 | | - return runtime.newFixnum(0); |
| 540 | + return asFixnum(context, 0); |
552 | 541 | } |
553 | 542 |
|
554 | 543 | /** |
@@ -678,47 +667,45 @@ public static IRubyObject home(ThreadContext context, IRubyObject recv, IRubyObj |
678 | 667 | @JRubyMethod(name = "mkdir", required = 1, optional = 1, checkArity = false, meta = true) |
679 | 668 | public static IRubyObject mkdir(ThreadContext context, IRubyObject recv, IRubyObject... args) { |
680 | 669 | Arity.checkArgumentCount(context, args, 1, 2); |
681 | | - Ruby runtime = context.runtime; |
682 | | - RubyString path = StringSupport.checkEmbeddedNulls(runtime, RubyFile.get_path(context, args[0])); |
683 | | - return mkdirCommon(runtime, path.asJavaString(), args); |
| 670 | + return mkdirCommon(context, RubyFile.get_path(context, args[0]).asJavaString(), args); |
684 | 671 | } |
685 | 672 |
|
686 | 673 | @Deprecated |
687 | 674 | public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) { |
688 | 675 | return mkdir(recv.getRuntime().getCurrentContext(), recv, args); |
689 | 676 | } |
690 | 677 |
|
691 | | - private static IRubyObject mkdirCommon(Ruby runtime, String path, IRubyObject[] args) { |
692 | | - if (path.startsWith("uri:")) throw runtime.newErrnoEACCESError(path); |
| 678 | + private static IRubyObject mkdirCommon(ThreadContext context, String path, IRubyObject[] args) { |
| 679 | + if (path.startsWith("uri:")) throw context.runtime.newErrnoEACCESError(path); |
693 | 680 |
|
694 | | - path = dirFromPath(path, runtime); |
695 | | - FileResource res = JRubyFile.createResource(runtime, path); |
696 | | - if (res.exists()) throw runtime.newErrnoEEXISTError(path); |
| 681 | + path = dirFromPath(path, context.runtime); |
| 682 | + FileResource res = JRubyFile.createResource(context.runtime, path); |
| 683 | + if (res.exists()) throw context.runtime.newErrnoEEXISTError(path); |
697 | 684 |
|
698 | 685 | String name = path.replace('\\', '/'); |
699 | 686 | boolean startsWithDriveLetterOnWindows = RubyFile.startsWithDriveLetterOnWindows(name); |
700 | 687 |
|
701 | 688 | // don't attempt to create a dir for drive letters |
702 | 689 | if (startsWithDriveLetterOnWindows) { |
703 | 690 | // path is just drive letter plus : |
704 | | - if (path.length() == 2) return RubyFixnum.zero(runtime); |
| 691 | + if (path.length() == 2) return asFixnum(context, 0); |
705 | 692 | // path is drive letter plus : plus leading or trailing / |
706 | | - if (path.length() == 3 && (path.charAt(0) == '/' || path.charAt(2) == '/')) return RubyFixnum.zero(runtime); |
| 693 | + if (path.length() == 3 && (path.charAt(0) == '/' || path.charAt(2) == '/')) return asFixnum(context, 0); |
707 | 694 | // path is drive letter plus : plus leading and trailing / |
708 | | - if (path.length() == 4 && (path.charAt(0) == '/' && path.charAt(3) == '/')) return RubyFixnum.zero(runtime); |
| 695 | + if (path.length() == 4 && (path.charAt(0) == '/' && path.charAt(3) == '/')) return asFixnum(context, 0); |
709 | 696 | } |
710 | 697 |
|
711 | 698 | File newDir = res.unwrap(File.class); |
712 | 699 | if (File.separatorChar == '\\') newDir = new File(newDir.getPath()); |
713 | 700 |
|
714 | 701 | int mode = args.length == 2 ? ((int) args[1].convertToInteger().getLongValue()) : 0777; |
715 | 702 |
|
716 | | - if (runtime.getPosix().mkdir(newDir.getAbsolutePath(), mode) < 0) { |
| 703 | + if (context.runtime.getPosix().mkdir(newDir.getAbsolutePath(), mode) < 0) { |
717 | 704 | // FIXME: This is a system error based on errno |
718 | | - throw runtime.newSystemCallError("mkdir failed"); |
| 705 | + throw context.runtime.newSystemCallError("mkdir failed"); |
719 | 706 | } |
720 | 707 |
|
721 | | - return RubyFixnum.zero(runtime); |
| 708 | + return asFixnum(context, 0); |
722 | 709 | } |
723 | 710 |
|
724 | 711 | /** |
@@ -914,25 +901,23 @@ public IRubyObject rewind() { |
914 | 901 | } |
915 | 902 |
|
916 | 903 | @JRubyMethod(name = "empty?", meta = true) |
917 | | - public static IRubyObject empty_p(ThreadContext context, IRubyObject recv, IRubyObject arg) { |
918 | | - RubyString path = StringSupport.checkEmbeddedNulls(context.runtime, RubyFile.get_path(context, arg)); |
919 | | - RubyFileStat fileStat = context.runtime.newFileStat(path.asJavaString(), false); |
| 904 | + public static IRubyObject empty_p(ThreadContext context, IRubyObject recv, IRubyObject path) { |
| 905 | + RubyFileStat fileStat = context.runtime.newFileStat(RubyFile.get_path(context, path).asJavaString(), false); |
920 | 906 | boolean isDirectory = fileStat.directory_p(context).isTrue(); |
921 | | - return asBoolean(context, isDirectory && entries(context, recv, arg).getLength() <= 2); |
| 907 | + return asBoolean(context, isDirectory && entries(context, recv, path).getLength() <= 2); |
922 | 908 | } |
923 | 909 |
|
924 | 910 | @JRubyMethod(name = "exist?", meta = true) |
925 | 911 | public static IRubyObject exist(ThreadContext context, IRubyObject recv, IRubyObject arg) { |
926 | | - Ruby runtime = context.runtime; |
927 | 912 | // Capture previous exception if any. |
928 | | - IRubyObject exception = runtime.getGlobalVariables().get("$!"); |
929 | | - RubyString path = StringSupport.checkEmbeddedNulls(runtime, RubyFile.get_path(context, arg)); |
| 913 | + IRubyObject exception = context.runtime.getGlobalVariables().get("$!"); |
| 914 | + RubyString path = RubyFile.get_path(context, arg); |
930 | 915 |
|
931 | 916 | try { |
932 | | - return runtime.newFileStat(path.asJavaString(), false).directory_p(context); |
| 917 | + return context.runtime.newFileStat(path.asJavaString(), false).directory_p(context); |
933 | 918 | } catch (Exception e) { |
934 | 919 | // Restore $! |
935 | | - runtime.getGlobalVariables().set("$!", exception); |
| 920 | + context.runtime.getGlobalVariables().set("$!", exception); |
936 | 921 | return context.fals; |
937 | 922 | } |
938 | 923 | } |
|
0 commit comments