/** * @deprecated * * Opens a new input stream to read the contents of a resource and returns it. * Note that implementations may be allocating native memory for the stream, so * callers need to close this when they are done with it. users of this * method should follow the pattern: close the stream where you open it. * * @return just opened InputStream * @throws ResourceException is the file does not exists or if the resource is a directory */ default InputStream inputStream() throws ResourceException { if (!exists()) { throw new ResourceException.NotFound(absolutePath()); } if (isDirectory()) { throw new ResourceException.FileIsDirectory(absolutePath()); } try { return openInputStream(); } catch (IOException e) { throw new ResourceException.IOError(e); } }
private static String[] getEntries(ThreadContext context, FileResource dir, String path) { if (!dir.isDirectory()) { if (dir.exists()) { throw context.runtime.newErrnoENOTDIRError(path); } throw context.runtime.newErrnoENOENTError(path); } if (!dir.canRead()) throw context.runtime.newErrnoEACCESError(path); String[] list = dir.list(); return list == null ? NO_FILES : list; }
private String resolve(String path, String scriptName) { if (RubyInstanceConfig.DEBUG_SCRIPT_RESOLUTION) { config.getError().println("Trying path: " + path); } try { FileResource fullName = JRubyFile.createRestrictedResource(path, scriptName); if (fullName.exists() && fullName.isFile()) { if (RubyInstanceConfig.DEBUG_SCRIPT_RESOLUTION) { config.getError().println("Found: " + fullName.absolutePath()); } return fullName.absolutePath(); } } catch (Exception e) { // keep going } return null; }
private final void checkDirIgnoreClosed() { testFrozen("Dir"); // update snapshot (if changed) : if (snapshot == null || dir.exists() && dir.lastModified() > lastModified) { lastModified = dir.lastModified(); snapshot = list(dir); } }
static IOException mapFileNotFoundOnGetChannel(final FileResource file, final FileNotFoundException ex) { // Java throws FileNotFoundException both if the file doesn't exist or there were // permission issues, but Ruby needs to disambiguate those two cases return file.exists() ? new ResourceException.PermissionDenied(file.absolutePath()) : new ResourceException.NotFound(file.absolutePath()); }
@JRubyMethod(name = "identical?", required = 2, module = true) public static IRubyObject identical_p(IRubyObject recv, IRubyObject filename1, IRubyObject filename2) { Ruby runtime = recv.getRuntime(); FileResource file1 = fileResource(filename1); FileResource file2 = fileResource(filename2); if (Platform.IS_WINDOWS || !runtime.getPosix().isNative()) { // posix stat uses inodes to determine indentity, and windows has no inodes // (they are always zero), so we use canonical paths instead. (JRUBY-5726) // If we can't load a native POSIX, use this same logic. (JRUBY-6982) if (file1.exists() && file2.exists()) { try { String canon1 = new File(file1.absolutePath()).getCanonicalPath(); String canon2 = new File(file2.absolutePath()).getCanonicalPath(); return runtime.newBoolean(canon1.equals(canon2)); } catch (IOException canonicalizationError) { return runtime.getFalse(); } } else { return runtime.getFalse(); } } return runtime.newBoolean(file1.exists() && file2.exists() && file1.stat(runtime.getPosix()).isIdentical(file2.stat(runtime.getPosix()))); }
final String script = getScriptFileName(); FileResource resource = JRubyFile.createRestrictedResource(getCurrentDirectory(), getScriptFileName()); if (resource != null && resource.exists()) { if (resource.canRead() && !resource.isDirectory()) { if (isXFlag()) { return findScript(resource.openInputStream()); return resource.openInputStream();
private static FileResource getExistingDir(final Ruby runtime, final String path) { FileResource result = JRubyFile.createResource(runtime, path); if (result == null || !result.exists()) { throw runtime.newErrnoENOENTError(path); } if (!result.isDirectory()) { throw runtime.newErrnoENOTDIRError(path); } return result; }
private void setup(String filename, boolean lstat) { if (Platform.IS_WINDOWS && filename.length() == 2 && filename.charAt(1) == ':' && Character.isLetter(filename.charAt(0))) { filename += "/"; } file = JRubyFile.createResource(getRuntime().getCurrentDirectory(), filename); if (!file.exists()) { throw getRuntime().newErrnoENOENTError("No such file or directory - " + filename); } POSIX posix = getRuntime().getPosix(); stat = lstat ? file.lstat(posix) : file.stat(posix); }
@JRubyMethod(name = "zero?", required = 1, module = true) public static RubyBoolean zero_p(ThreadContext context, IRubyObject recv, IRubyObject filename) { Ruby runtime = context.runtime; if (!(filename instanceof RubyFile)) { filename = get_path(context, filename); } FileResource file = fileResource(filename); if (file.exists()) { if (file.isDirectory()) { // MRI behavior, enforced by RubySpecs. return runtime.newBoolean(Platform.IS_WINDOWS); } else { return runtime.newBoolean(file.length() == 0L); } } else { return runtime.getFalse(); } }
private void setup(String filename, boolean lstat) { Ruby runtime = getRuntime(); if (Platform.IS_WINDOWS && filename.length() == 2 && filename.charAt(1) == ':' && Character.isLetter(filename.charAt(0))) { filename += '/'; } file = JRubyFile.createResource(runtime, filename); stat = lstat ? file.lstat() : file.stat(); if (stat == null) throw runtime.newErrnoFromInt(file.errno(), filename); }
private FoundLibrary findFileResourceWithLoadPath(String searchName, String suffix, String loadPath) { String fullPath = loadPath != null ? loadPath + "/" + searchName : searchName; String pathWithSuffix = fullPath + suffix; DebugLog.Resource.logTry(pathWithSuffix); FileResource resource = JRubyFile.createResourceAsFile(runtime, pathWithSuffix); if (resource.exists()) { if (resource.absolutePath() != resource.canonicalPath()) { FileResource expandedResource = JRubyFile.createResourceAsFile(runtime, resource.canonicalPath()); if (expandedResource.exists()){ String scriptName = resolveScriptName(expandedResource, expandedResource.canonicalPath()); String loadName = resolveLoadName(expandedResource, searchName + suffix); return new FoundLibrary(ResourceLibrary.create(searchName, scriptName, resource), loadName); } } DebugLog.Resource.logFound(pathWithSuffix); String scriptName = resolveScriptName(resource, pathWithSuffix); String loadName = resolveLoadName(resource, searchName + suffix); return new FoundLibrary(ResourceLibrary.create(searchName, scriptName, resource), loadName); } return null; }
public ResourceLibrary(String searchName, String scriptName, FileResource resource) { this.searchName = searchName; this.scriptName = scriptName; this.location = resource.absolutePath(); this.resource = resource; }
if ( resource.isDirectory() ) { if ( SLASH_INDEX != -1 && Arrays.equals(magic, DOUBLE_STAR) ) { final int lengthOfBase = base.length; buf.append( getBytesInUTF8(file) ); resource = JRubyFile.createResource(runtime, cwd, new String(buf.unsafeBytes(), buf.begin(), buf.length(), enc.getCharset())); if ( !resource.isSymLink() && resource.isDirectory() && !".".equals(file) && !"..".equals(file) ) { final int len = buf.getRealSize(); buf.append(SLASH); if ( status == 0 ) { resource = JRubyFile.createResource(runtime, cwd, RubyString.byteListToString(link)); if ( resource.isDirectory() ) { final int len = link.getRealSize(); buf.length(0);
private static String[] list(FileResource directory) { final String[] contents = directory.list(); // If an IO exception occurs (something odd, but possible) // A directory may return null. return contents == null ? NO_FILES : contents; }
private static int addToResultIfExists(Ruby runtime, String cwd, byte[] bytes, int begin, int end, Encoding enc, int flags, GlobFunc<GlobArgs> func, GlobArgs arg) { final String fileName = new String(bytes, begin, end - begin, enc.getCharset()); // FIXME: Ultimately JRubyFile.createResource should do this but all 1.7.x is only selectively honoring raw // paths and using system drive make it absolute. MRI does this on many methods we don't. if (Platform.IS_WINDOWS && cwd == null && !fileName.isEmpty() && fileName.charAt(0) == '/') { cwd = System.getenv("SYSTEMDRIVE"); if (cwd == null) cwd = "C:"; cwd = cwd + "/"; } FileResource file = JRubyFile.createResource(runtime, cwd, fileName); if (file.exists()) { return func.call(bytes, begin, end - begin, enc, arg); } return 0; }
@JRubyMethod(name = "size", required = 1, module = true) public static IRubyObject size(ThreadContext context, IRubyObject recv, IRubyObject filename) { Ruby runtime = recv.getRuntime(); if (!(filename instanceof RubyFile)) { if (filename.respondsTo("to_io")) { filename = (RubyIO) TypeConverter.convertToType(filename, runtime.getIO(), "to_io"); } else { filename = get_path(context, filename); } } FileResource file = fileResource(filename); if (!file.exists()) { noFileError(filename); } return runtime.newFixnum(file.length()); }
private static boolean addDirectoryEntries(Set<String> entries, URL url, boolean isDirectory) { switch (url.getProtocol()) { case "jar": // maybe the jar itself contains directory entries (which are actually optional) FileResource jar = JarResource.create(url.toString()); if (jar != null && jar.isDirectory()) { if (!isDirectory) isDirectory = true; entries.addAll(Arrays.asList(jar.list())); } break; case "file": // let's look on the filesystem File file = new File(url.getPath()); if (file.isDirectory()) { if (!isDirectory) isDirectory = true; entries.addAll(Arrays.asList(file.list())); } break; default: } return isDirectory; }