Skip to content

Commit fd89ac3

Browse files
authored
Merge pull request #8472 from enebo/api12
Add new Check#checkEmbeddedNulls to replace StringSupport method
2 parents f09d13e + 0779860 commit fd89ac3

File tree

14 files changed

+222
-289
lines changed

14 files changed

+222
-289
lines changed

core/src/main/java/org/jruby/RubyDir.java

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import static org.jruby.RubyEnumerator.enumeratorize;
6868
import static org.jruby.RubyFile.filePathConvert;
6969
import static org.jruby.RubyString.UTF8;
70+
import static org.jruby.api.Check.checkEmbeddedNulls;
7071
import static org.jruby.api.Convert.asBoolean;
7172
import static org.jruby.api.Convert.asFixnum;
7273
import static org.jruby.api.Create.newArray;
@@ -152,24 +153,21 @@ public IRubyObject initialize(ThreadContext context, IRubyObject path, IRubyObje
152153
}
153154

154155
private RubyDir initializeCommon(ThreadContext context, IRubyObject pathArg, IRubyObject encOpts) {
155-
Ruby runtime = context.runtime;
156-
157156
Encoding encoding = null;
158157

159158
if (!encOpts.isNil()) {
160159
RubyHash opts = encOpts.convertToHash();
161160
IRubyObject encodingArg = ArgsUtil.extractKeywordArg(context, opts, "encoding");
162161
if (encodingArg != null && !encodingArg.isNil()) {
163-
encoding = runtime.getEncodingService().getEncodingFromObject(encodingArg);
162+
encoding = context.runtime.getEncodingService().getEncodingFromObject(encodingArg);
164163
}
165164
}
166165

167-
if (encoding == null) encoding = runtime.getEncodingService().getFileSystemEncoding();
166+
if (encoding == null) encoding = context.runtime.getEncodingService().getFileSystemEncoding();
168167

169-
RubyString newPath = StringSupport.checkEmbeddedNulls(runtime, RubyFile.get_path(context, pathArg));
168+
RubyString newPath = checkEmbeddedNulls(context, RubyFile.get_path(context, pathArg));
170169
this.path = newPath;
171170
this.pos = 0;
172-
173171
this.encoding = encoding;
174172

175173
String adjustedPath = RubyFile.getAdjustedPath(context, newPath);
@@ -365,19 +363,14 @@ public RubyArray entries() {
365363
*/
366364
@JRubyMethod(name = "entries", meta = true)
367365
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));
371367

372-
return entriesCommon(context, path, runtime.getDefaultFilesystemEncoding(), false);
368+
return entriesCommon(context, path, context.runtime.getDefaultFilesystemEncoding(), false);
373369
}
374370

375371
@JRubyMethod(name = "entries", meta = true)
376372
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));
381374
Encoding encoding = getEncodingFromOpts(context, opts);
382375

383376
return entriesCommon(context, path, encoding, false);
@@ -434,10 +427,8 @@ private static void checkDirIsTwoSlashesOnWindows(Ruby runtime, String path) {
434427

435428
/** Changes the current directory to <code>path</code> */
436429
@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)));
441432
}
442433

443434
/** Changes the current directory to <code>path</code> */
@@ -535,20 +526,18 @@ public static IRubyObject rmdir19(IRubyObject recv, IRubyObject path) {
535526
*/
536527
@JRubyMethod(name = {"rmdir", "unlink", "delete"}, meta = true)
537528
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());
541530
}
542531

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);
545534

546535
// 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);
549538
}
550539

551-
return runtime.newFixnum(0);
540+
return asFixnum(context, 0);
552541
}
553542

554543
/**
@@ -678,47 +667,45 @@ public static IRubyObject home(ThreadContext context, IRubyObject recv, IRubyObj
678667
@JRubyMethod(name = "mkdir", required = 1, optional = 1, checkArity = false, meta = true)
679668
public static IRubyObject mkdir(ThreadContext context, IRubyObject recv, IRubyObject... args) {
680669
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);
684671
}
685672

686673
@Deprecated
687674
public static IRubyObject mkdir(IRubyObject recv, IRubyObject[] args) {
688675
return mkdir(recv.getRuntime().getCurrentContext(), recv, args);
689676
}
690677

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);
693680

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);
697684

698685
String name = path.replace('\\', '/');
699686
boolean startsWithDriveLetterOnWindows = RubyFile.startsWithDriveLetterOnWindows(name);
700687

701688
// don't attempt to create a dir for drive letters
702689
if (startsWithDriveLetterOnWindows) {
703690
// path is just drive letter plus :
704-
if (path.length() == 2) return RubyFixnum.zero(runtime);
691+
if (path.length() == 2) return asFixnum(context, 0);
705692
// 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);
707694
// 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);
709696
}
710697

711698
File newDir = res.unwrap(File.class);
712699
if (File.separatorChar == '\\') newDir = new File(newDir.getPath());
713700

714701
int mode = args.length == 2 ? ((int) args[1].convertToInteger().getLongValue()) : 0777;
715702

716-
if (runtime.getPosix().mkdir(newDir.getAbsolutePath(), mode) < 0) {
703+
if (context.runtime.getPosix().mkdir(newDir.getAbsolutePath(), mode) < 0) {
717704
// FIXME: This is a system error based on errno
718-
throw runtime.newSystemCallError("mkdir failed");
705+
throw context.runtime.newSystemCallError("mkdir failed");
719706
}
720707

721-
return RubyFixnum.zero(runtime);
708+
return asFixnum(context, 0);
722709
}
723710

724711
/**
@@ -914,25 +901,23 @@ public IRubyObject rewind() {
914901
}
915902

916903
@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);
920906
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);
922908
}
923909

924910
@JRubyMethod(name = "exist?", meta = true)
925911
public static IRubyObject exist(ThreadContext context, IRubyObject recv, IRubyObject arg) {
926-
Ruby runtime = context.runtime;
927912
// 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);
930915

931916
try {
932-
return runtime.newFileStat(path.asJavaString(), false).directory_p(context);
917+
return context.runtime.newFileStat(path.asJavaString(), false).directory_p(context);
933918
} catch (Exception e) {
934919
// Restore $!
935-
runtime.getGlobalVariables().set("$!", exception);
920+
context.runtime.getGlobalVariables().set("$!", exception);
936921
return context.fals;
937922
}
938923
}

0 commit comments

Comments
 (0)