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; }
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; }
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 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 static IRubyObject mkdirCommon(Ruby runtime, String path, IRubyObject[] args) { if (path.startsWith("uri:")) throw runtime.newErrnoEACCESError(path); path = dirFromPath(path, runtime); FileResource res = JRubyFile.createResource(runtime, path); if (res.isDirectory()) throw runtime.newErrnoEEXISTError(path); String name = path.replace('\\', '/'); boolean startsWithDriveLetterOnWindows = RubyFile.startsWithDriveLetterOnWindows(name); // don't attempt to create a dir for drive letters if (startsWithDriveLetterOnWindows) { // path is just drive letter plus : if (path.length() == 2) return RubyFixnum.zero(runtime); // path is drive letter plus : plus leading or trailing / if (path.length() == 3 && (path.charAt(0) == '/' || path.charAt(2) == '/')) return RubyFixnum.zero(runtime); // path is drive letter plus : plus leading and trailing / if (path.length() == 4 && (path.charAt(0) == '/' && path.charAt(3) == '/')) return RubyFixnum.zero(runtime); } File newDir = res.unwrap(File.class); if (File.separatorChar == '\\') newDir = new File(newDir.getPath()); int mode = args.length == 2 ? ((int) args[1].convertToInteger().getLongValue()) : 0777; if (runtime.getPosix().mkdir(newDir.getAbsolutePath(), mode) < 0) { // FIXME: This is a system error based on errno throw runtime.newSystemCallError("mkdir failed"); } return RubyFixnum.zero(runtime); }
private static IRubyObject mkdirCommon(Ruby runtime, String path, IRubyObject[] args) { if (path.startsWith("uri:")) throw runtime.newErrnoEACCESError(path); path = dirFromPath(path, runtime); FileResource res = JRubyFile.createResource(runtime, path); if (res.isDirectory()) throw runtime.newErrnoEEXISTError(path); String name = path.replace('\\', '/'); boolean startsWithDriveLetterOnWindows = RubyFile.startsWithDriveLetterOnWindows(name); // don't attempt to create a dir for drive letters if (startsWithDriveLetterOnWindows) { // path is just drive letter plus : if (path.length() == 2) return RubyFixnum.zero(runtime); // path is drive letter plus : plus leading or trailing / if (path.length() == 3 && (path.charAt(0) == '/' || path.charAt(2) == '/')) return RubyFixnum.zero(runtime); // path is drive letter plus : plus leading and trailing / if (path.length() == 4 && (path.charAt(0) == '/' && path.charAt(3) == '/')) return RubyFixnum.zero(runtime); } File newDir = res.unwrap(File.class); if (File.separatorChar == '\\') newDir = new File(newDir.getPath()); int mode = args.length == 2 ? ((int) args[1].convertToInteger().getLongValue()) : 0777; if (runtime.getPosix().mkdir(newDir.getAbsolutePath(), mode) < 0) { // FIXME: This is a system error based on errno throw runtime.newSystemCallError("mkdir failed"); } return RubyFixnum.zero(runtime); }
public static IRubyObject directory_p(ThreadContext context, IRubyObject filename) { if (!(filename instanceof RubyIO) && filename.respondsTo("to_io")) { filename = TypeConverter.convertToType(filename, context.runtime.getIO(), "to_io"); } return context.runtime.newBoolean(fileResource(context, filename).isDirectory()); }
public static IRubyObject directory_p(ThreadContext context, IRubyObject filename) { if (!(filename instanceof RubyIO) && filename.respondsTo("to_io")) { filename = TypeConverter.convertToType(filename, context.runtime.getIO(), "to_io"); } return context.runtime.newBoolean(fileResource(context, filename).isDirectory()); }
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 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; }
public static IRubyObject directory_p(ThreadContext context, IRubyObject filename) { Ruby runtime = context.runtime; if (!(filename instanceof RubyFile || filename instanceof RubyIO)) { if (filename.respondsTo("to_io")) { filename = (RubyIO) TypeConverter.convertToType(filename, context.runtime.getIO(), "to_io"); } else { filename = get_path(context, filename); } } FileResource file = fileResource(filename); return runtime.newBoolean(file.exists() && file.isDirectory()); }
/** Returns a Java <code>File</code> object for the specified path. If * <code>path</code> is not a directory, throws <code>IOError</code>. * * @param path path for which to return the <code>File</code> object. * @param mustExist is true the directory must exist. If false it must not. */ // split out - no longer used protected static FileResource getDir(final Ruby runtime, final String path, final boolean mustExist) { String dir = dirFromPath(path, runtime); FileResource result = JRubyFile.createResource(runtime, dir); if (mustExist && (result == null || !result.exists())) { throw runtime.newErrnoENOENTError(dir); } boolean isDirectory = result.isDirectory(); if (mustExist && !isDirectory) { throw runtime.newErrnoENOTDIRError(path); } if (!mustExist && isDirectory) { throw runtime.newErrnoEEXISTError(dir); } return result; }
/** Returns a Java <code>File</code> object for the specified path. If * <code>path</code> is not a directory, throws <code>IOError</code>. * * @param path path for which to return the <code>File</code> object. * @param mustExist is true the directory must exist. If false it must not. */ // split out - no longer used protected static FileResource getDir(final Ruby runtime, final String path, final boolean mustExist) { String dir = dirFromPath(path, runtime); FileResource result = JRubyFile.createResource(runtime, dir); if (mustExist && (result == null || !result.exists())) { throw runtime.newErrnoENOENTError(dir); } boolean isDirectory = result.isDirectory(); if (mustExist && !isDirectory) { throw runtime.newErrnoENOTDIRError(path); } if (!mustExist && isDirectory) { throw runtime.newErrnoEEXISTError(dir); } return result; }
FileResource resource = JRubyFile.createRestrictedResource(getCurrentDirectory(), getScriptFileName()); if (resource != null && resource.exists()) { if (resource.canRead() && !resource.isDirectory()) { if (isXFlag()) {
@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(); } }
/** * @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); } }
/** * @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); } }
FileResource resource = JRubyFile.createRestrictedResource(getCurrentDirectory(), getScriptFileName()); if (resource != null && resource.exists()) { if (resource.canRead() && !resource.isDirectory()) { if (isXFlag()) {
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);
if (st.isDirectory()) { if(m != -1 && Arrays.equals(magic, DOUBLE_STAR)) { int n = base.length; buf.append(getBytesInUTF8(dirp[i])); st = JRubyFile.createResource(cwd, newStringFromUTF8(buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize())); if(!st.isSymLink() && st.isDirectory() && !".".equals(dirp[i]) && !"..".equals(dirp[i])) { int t = buf.getRealSize(); buf.append(SLASH); if (status == 0) { st = JRubyFile.createResource(cwd, newStringFromUTF8(b.getUnsafeBytes(), 0, b.getRealSize())); if(st.isDirectory()) { int len = b.getRealSize(); buf.length(0);