public static FileResource createResource(String cwd, String pathname) { if (pathname.indexOf('!') > 0) { return JarFileResource.load(pathname); } else { // Regular file is fine return create(cwd, pathname); } }
private static List<String> entriesIntoADirectory(Ruby runtime, String path) { final JRubyFile directory = JRubyFile.create(runtime.getCurrentDirectory(), path); List<String> fileList = getContents(directory); fileList.add(0, "."); fileList.add(1, ".."); return fileList; }
private static List<String> entriesIntoADirectory(Ruby runtime, String path) { final JRubyFile directory = JRubyFile.create(runtime.getCurrentDirectory(), path); List<String> fileList = getContents(directory); fileList.add(0, "."); fileList.add(1, ".."); return fileList; }
public static FileResource createResource(String cwd, String pathname) { if (pathname == null) { // null pathnames do not exist return new RegularFileResource(JRubyNonExistentFile.NOT_EXIST); } // Try as a jar resource first FileResource jarResource = JarResource.create(pathname); if (jarResource != null) { return jarResource; } return new RegularFileResource(create(cwd, pathname)); }
private static RubyFileStat getFileStat(ThreadContext context, IRubyObject filename) { Ruby runtime = context.runtime; RubyFileStat stat = null; if (!(filename instanceof RubyFile)) { RubyString path = get_path(context, filename); JRubyFile file = JRubyFile.create(runtime.getCurrentDirectory(), path.getUnicodeValue()); if (file.exists()) { stat = runtime.newFileStat(file.getPath(), false); } } else { stat = (RubyFileStat) ((RubyFile) filename).stat(context); } return stat; }
/** * Creates a new <code>Dir</code>. This method takes a snapshot of the * contents of the directory at creation time, so changes to the contents * of the directory will not be reflected during the lifetime of the * <code>Dir</code> object returned, so a new <code>Dir</code> instance * must be created to reflect changes to the underlying file system. */ @JRubyMethod(compat = RUBY1_8) public IRubyObject initialize(IRubyObject arg) { RubyString newPath = arg.convertToString(); path = newPath; pos = 0; String adjustedPath = RubyFile.adjustRootPathOnWindows(getRuntime(), newPath.toString(), null); checkDirIsTwoSlashesOnWindows(getRuntime(), adjustedPath); dir = JRubyFile.create(getRuntime().getCurrentDirectory(), adjustedPath); List<String> snapshotList = RubyDir.getEntries(getRuntime(), adjustedPath); snapshot = (String[]) snapshotList.toArray(new String[snapshotList.size()]); return this; }
/** * Creates a new <code>Dir</code>. This method takes a snapshot of the * contents of the directory at creation time, so changes to the contents * of the directory will not be reflected during the lifetime of the * <code>Dir</code> object returned, so a new <code>Dir</code> instance * must be created to reflect changes to the underlying file system. */ @JRubyMethod(compat = RUBY1_8) public IRubyObject initialize(IRubyObject arg) { RubyString newPath = arg.convertToString(); path = newPath; pos = 0; String adjustedPath = RubyFile.adjustRootPathOnWindows(getRuntime(), newPath.toString(), null); checkDirIsTwoSlashesOnWindows(getRuntime(), adjustedPath); dir = JRubyFile.create(getRuntime().getCurrentDirectory(), adjustedPath); List<String> snapshotList = RubyDir.getEntries(getRuntime(), adjustedPath); snapshot = (String[]) snapshotList.toArray(new String[snapshotList.size()]); return this; }
/** 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. * @throws IOError if <code>path</code> is not a directory. */ protected static JRubyFile getDir(final Ruby runtime, final String path, final boolean mustExist) { String dir = dirFromPath(path, runtime); JRubyFile result = JRubyFile.create(runtime.getCurrentDirectory(), dir); if (mustExist && !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. * @throws IOError if <code>path</code> is not a directory. */ protected static JRubyFile getDir(final Ruby runtime, final String path, final boolean mustExist) { String dir = dirFromPath(path, runtime); JRubyFile result = JRubyFile.create(runtime.getCurrentDirectory(), dir); if (mustExist && !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; }
@JRubyMethod(required = 2, meta = true) public static IRubyObject symlink(ThreadContext context, IRubyObject recv, IRubyObject from, IRubyObject to) { Ruby runtime = context.runtime; RubyString fromStr = get_path(context, from); RubyString toStr = get_path(context, to); String tovalue = toStr.getUnicodeValue(); tovalue = JRubyFile.create(runtime.getCurrentDirectory(), tovalue).getAbsolutePath(); try { if (runtime.getPosix().symlink(fromStr.getUnicodeValue(), tovalue) == -1) { if (runtime.getPosix().isNative()) { throw runtime.newErrnoFromInt(runtime.getPosix().errno(), String.format("(%s, %s)", fromStr, toStr)); } else { throw runtime.newErrnoEEXISTError(String.format("(%s, %s)", fromStr, toStr)); } } } catch (java.lang.UnsatisfiedLinkError ule) { throw runtime.newNotImplementedError("symlink() function is unimplemented on this machine"); } return RubyFixnum.zero(runtime); }
@JRubyMethod(required = 2, meta = true) public static IRubyObject symlink(ThreadContext context, IRubyObject recv, IRubyObject from, IRubyObject to) { Ruby runtime = context.runtime; RubyString fromStr = get_path(context, from); RubyString toStr = get_path(context, to); String tovalue = toStr.getUnicodeValue(); tovalue = JRubyFile.create(runtime.getCurrentDirectory(), tovalue).getAbsolutePath(); try { if (runtime.getPosix().symlink(fromStr.getUnicodeValue(), tovalue) == -1) { if (runtime.getPosix().isNative()) { throw runtime.newErrnoFromInt(runtime.getPosix().errno(), String.format("(%s, %s)", fromStr, toStr)); } else { throw runtime.newErrnoEEXISTError(String.format("(%s, %s)", fromStr, toStr)); } } } catch (java.lang.UnsatisfiedLinkError ule) { throw runtime.newNotImplementedError("symlink() function is unimplemented on this machine"); } return RubyFixnum.zero(runtime); }
@Deprecated protected LoadServiceResource tryResourceFromCWD(SearchState state, String baseName,SuffixType suffixType) throws RaiseException { LoadServiceResource foundResource = null; for (String suffix : suffixType.getSuffixes()) { String namePlusSuffix = baseName + suffix; // check current directory; if file exists, retrieve URL and return resource try { JRubyFile file = JRubyFile.create(runtime.getCurrentDirectory(), RubyFile.expandUserPath(runtime.getCurrentContext(), namePlusSuffix)); debugLogTry("resourceFromCWD", file.toString()); if (file.isFile() && file.isAbsolute() && file.canRead()) { boolean absolute = true; foundResource = new LoadServiceResource(file, getFileName(file, namePlusSuffix), absolute); debugLogFound(foundResource); state.setLoadName(resolveLoadName(foundResource, namePlusSuffix)); break; } } catch (IllegalArgumentException illArgEx) { } catch (SecurityException secEx) { } } return foundResource; }
@JRubyMethod(required = 2, meta = true) public static IRubyObject symlink(ThreadContext context, IRubyObject recv, IRubyObject from, IRubyObject to) { Ruby runtime = context.runtime; RubyString fromStr = StringSupport.checkEmbeddedNulls(runtime, get_path(context, from)); RubyString toStr = StringSupport.checkEmbeddedNulls(runtime, get_path(context, to)); String tovalue = toStr.getUnicodeValue(); tovalue = JRubyFile.create(runtime.getCurrentDirectory(), tovalue).getAbsolutePath(); try { if (runtime.getPosix().symlink(fromStr.getUnicodeValue(), tovalue) == -1) { if (runtime.getPosix().isNative()) { throw runtime.newErrnoFromInt(runtime.getPosix().errno(), String.format("(%s, %s)", fromStr, toStr)); } else { throw runtime.newErrnoEEXISTError(String.format("(%s, %s)", fromStr, toStr)); } } } catch (java.lang.UnsatisfiedLinkError ule) { throw runtime.newNotImplementedError("symlink() function is unimplemented on this machine"); } return RubyFixnum.zero(runtime); }
@JRubyMethod(required = 2, meta = true) public static IRubyObject symlink(ThreadContext context, IRubyObject recv, IRubyObject from, IRubyObject to) { Ruby runtime = context.runtime; RubyString fromStr = StringSupport.checkEmbeddedNulls(runtime, get_path(context, from)); RubyString toStr = StringSupport.checkEmbeddedNulls(runtime, get_path(context, to)); String tovalue = toStr.getUnicodeValue(); tovalue = JRubyFile.create(runtime.getCurrentDirectory(), tovalue).getAbsolutePath(); try { if (runtime.getPosix().symlink(fromStr.getUnicodeValue(), tovalue) == -1) { if (runtime.getPosix().isNative()) { throw runtime.newErrnoFromInt(runtime.getPosix().errno(), String.format("(%s, %s)", fromStr, toStr)); } else { throw runtime.newErrnoEEXISTError(String.format("(%s, %s)", fromStr, toStr)); } } } catch (java.lang.UnsatisfiedLinkError ule) { throw runtime.newNotImplementedError("symlink() function is unimplemented on this machine"); } return RubyFixnum.zero(runtime); }
protected LoadServiceResource tryResourceFromCWD(SearchState state, String baseName,SuffixType suffixType) throws RaiseException { LoadServiceResource foundResource = null; for (String suffix : suffixType.getSuffixes()) { String namePlusSuffix = baseName + suffix; // check current directory; if file exists, retrieve URL and return resource try { JRubyFile file = JRubyFile.create(runtime.getCurrentDirectory(), RubyFile.expandUserPath(runtime.getCurrentContext(), namePlusSuffix)); debugLogTry("resourceFromCWD", file.toString()); if (file.isFile() && file.isAbsolute() && file.canRead()) { boolean absolute = true; foundResource = new LoadServiceResource(file, getFileName(file, namePlusSuffix), absolute); debugLogFound(foundResource); state.loadName = resolveLoadName(foundResource, namePlusSuffix); break; } } catch (IllegalArgumentException illArgEx) { } catch (SecurityException secEx) { } } return foundResource; }
@JRubyMethod(name = {"unlink", "delete"}, rest = true, meta = true) public static IRubyObject unlink(ThreadContext context, IRubyObject recv, IRubyObject[] args) { Ruby runtime = context.runtime; for (int i = 0; i < args.length; i++) { RubyString filename = get_path(context, args[i]); JRubyFile lToDelete = JRubyFile.create(runtime.getCurrentDirectory(), filename.getUnicodeValue()); boolean isSymlink = RubyFileTest.symlink_p(recv, filename).isTrue(); // Broken symlinks considered by exists() as non-existing, // so we need to check for symlinks explicitly. if (!lToDelete.exists() && !isSymlink) { throw runtime.newErrnoENOENTError(filename.getUnicodeValue()); } if (lToDelete.isDirectory() && !isSymlink) { throw runtime.newErrnoEPERMError(filename.getUnicodeValue()); } if (!lToDelete.delete()) { throw runtime.newErrnoEACCESError(filename.getUnicodeValue()); } } return runtime.newFixnum(args.length); }
@JRubyMethod(rest = true, meta = true) public static IRubyObject delete(ThreadContext context, IRubyObject recv, IRubyObject[] args) { Ruby runtime = context.runtime; for (int i = 0; i < args.length; i++) { RubyString filename = StringSupport.checkEmbeddedNulls(runtime, get_path(context, args[i])); JRubyFile file = JRubyFile.create(runtime.getCurrentDirectory(), filename.getUnicodeValue()); // Broken symlinks considered by exists() as non-existing, // so we need to check for symlinks explicitly. if (!file.exists() && !isSymlink(context, file)) { throw runtime.newErrnoENOENTError(filename.getUnicodeValue()); } if (file.isDirectory() && !isSymlink(context, file)) { throw runtime.newErrnoEISDirError(filename.getUnicodeValue()); } if (!file.delete()) { throw runtime.newErrnoEACCESError(filename.getUnicodeValue()); } } return runtime.newFixnum(args.length); }
@JRubyMethod(rest = true, meta = true) public static IRubyObject delete(ThreadContext context, IRubyObject recv, IRubyObject[] args) { Ruby runtime = context.runtime; for (int i = 0; i < args.length; i++) { RubyString filename = StringSupport.checkEmbeddedNulls(runtime, get_path(context, args[i])); JRubyFile file = JRubyFile.create(runtime.getCurrentDirectory(), filename.getUnicodeValue()); // Broken symlinks considered by exists() as non-existing, // so we need to check for symlinks explicitly. if (!file.exists() && !isSymlink(context, file)) { throw runtime.newErrnoENOENTError(filename.getUnicodeValue()); } if (file.isDirectory() && !isSymlink(context, file)) { throw runtime.newErrnoEISDirError(filename.getUnicodeValue()); } if (!file.delete()) { throw runtime.newErrnoEACCESError(filename.getUnicodeValue()); } } return runtime.newFixnum(args.length); }
@JRubyMethod(required = 2, rest = true, meta = true) public static IRubyObject lutime(ThreadContext context, IRubyObject recv, IRubyObject[] args) { Ruby runtime = context.runtime; long[] atimeval = null; long[] mtimeval = null; if (args[0] != context.nil || args[1] != context.nil) { atimeval = extractTimespec(context, args[0]); mtimeval = extractTimespec(context, args[1]); } for (int i = 2, j = args.length; i < j; i++) { RubyString filename = StringSupport.checkEmbeddedNulls(runtime, get_path(context, args[i])); JRubyFile fileToTouch = JRubyFile.create(runtime.getCurrentDirectory(), filename.getUnicodeValue()); if (!fileToTouch.exists()) { throw runtime.newErrnoENOENTError(filename.toString()); } int result = runtime.getPosix().lutimes(fileToTouch.getAbsolutePath(), atimeval, mtimeval); if (result == -1) { throw runtime.newErrnoFromInt(runtime.getPosix().errno()); } } return runtime.newFixnum(args.length - 2); }
@JRubyMethod(required = 2, rest = true, meta = true) public static IRubyObject utime(ThreadContext context, IRubyObject recv, IRubyObject[] args) { Ruby runtime = context.runtime; long[] atimeval = null; long[] mtimeval = null; if (args[0] != runtime.getNil() || args[1] != runtime.getNil()) { atimeval = extractTimeval(runtime, args[0]); mtimeval = extractTimeval(runtime, args[1]); } for (int i = 2, j = args.length; i < j; i++) { RubyString filename = get_path(context, args[i]); JRubyFile fileToTouch = JRubyFile.create(runtime.getCurrentDirectory(),filename.getUnicodeValue()); if (!fileToTouch.exists()) { throw runtime.newErrnoENOENTError(filename.toString()); } int result = runtime.getPosix().utimes(fileToTouch.getAbsolutePath(), atimeval, mtimeval); if (result == -1) { throw runtime.newErrnoFromInt(runtime.getPosix().errno()); } } return runtime.newFixnum(args.length - 2); }