@Override public File getCanonicalFile() throws IOException { return new JRubyFile(getCanonicalPath()); }
@Override public boolean canRead() { return file.canRead(); }
@Override public boolean canWrite() { return file.canWrite(); }
@Override public String canonicalPath() { // Seems like for Ruby absolute path implies resolving system links, // so canonicalization is in order. try { return file.getCanonicalPath(); } catch (IOException ioError) { // I guess absolute path is next best thing? return file.getAbsolutePath(); } }
@Override public File getAbsoluteFile() { return new JRubyFile(getAbsolutePath()); }
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; }
JRubyFile theFile = JRubyFile.create(cwd,path); if (theFile.isDirectory() && flags.isWritable()) { throw new DirectoryAsFileException(); fileCreated = theFile.createNewFile(); File parent = theFile.getParentFile(); if (parent != null && parent != theFile && !parent.exists()) { throw new FileNotFoundException(path); } else if (!theFile.canWrite()) { throw new PermissionDeniedException(path); } else { if (!theFile.exists()) { throw new FileNotFoundException(path); posix.chmod(theFile.getPath(), perm);
JRubyFile oldFile = JRubyFile.create(runtime.getCurrentDirectory(), oldNameJavaString); JRubyFile newFile = JRubyFile.create(runtime.getCurrentDirectory(), newNameJavaString); if (!(oldFile.exists() || isOldSymlink) || !newFile.getParentFile().exists()) { throw runtime.newErrnoENOENTError(oldNameJavaString + " or " + newNameJavaString); JRubyFile dest = JRubyFile.create(runtime.getCurrentDirectory(), newNameJavaString); if (oldFile.renameTo(dest)) { // rename is successful return RubyFixnum.zero(runtime); if (newFile.exists()) { runtime.getPosix().chmod(newNameJavaString, 0666); newFile.delete(); Path oldPath = Paths.get(oldFile.toURI()); Path destPath = Paths.get(dest.getAbsolutePath()); try { Files.move(oldPath, destPath, StandardCopyOption.ATOMIC_MOVE);
protected LoadServiceResource tryResourceFromLoadPath( String namePlusSuffix,String loadPathEntry) throws RaiseException { LoadServiceResource foundResource = null; try { if (!Ruby.isSecurityRestricted()) { String reportedPath = loadPathEntry + "/" + namePlusSuffix; boolean absolute = true; // we check length == 0 for 'load', which does not use load path if (!new File(reportedPath).isAbsolute()) { absolute = false; // prepend ./ if . is not already there, since we're loading based on CWD if (reportedPath.charAt(0) != '.') { reportedPath = "./" + reportedPath; } loadPathEntry = JRubyFile.create(runtime.getCurrentDirectory(), loadPathEntry).getAbsolutePath(); } JRubyFile actualPath = JRubyFile.create(loadPathEntry, RubyFile.expandUserPath(runtime.getCurrentContext(), namePlusSuffix)); if (RubyInstanceConfig.DEBUG_LOAD_SERVICE) { debugLogTry("resourceFromLoadPath", "'" + actualPath.toString() + "' " + actualPath.isFile() + " " + actualPath.canRead()); } if (actualPath.canRead()) { foundResource = new LoadServiceResource(actualPath, reportedPath, absolute); debugLogFound(foundResource); } } } 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); }
} else { String cwd = runtime.getCurrentDirectory(); JRubyFile theFile = JRubyFile.create(cwd,path); if (theFile.isDirectory() && modes.isWritable()) { throw runtime.newErrnoEISDirError(path); if (theFile.exists() && modes.isExclusive()) { throw runtime.newErrnoEEXISTError("File exists - " + path); theFile.createNewFile(); } else { if (!theFile.exists()) { throw runtime.newErrnoENOENTError("file not found - " + path);
@JRubyMethod(required = 2, meta = true) public static IRubyObject rename(ThreadContext context, IRubyObject recv, IRubyObject oldName, IRubyObject newName) { Ruby runtime = context.runtime; RubyString oldNameString = get_path(context, oldName); RubyString newNameString = get_path(context, newName); String newNameJavaString = newNameString.getUnicodeValue(); String oldNameJavaString = oldNameString.getUnicodeValue(); JRubyFile oldFile = JRubyFile.create(runtime.getCurrentDirectory(), oldNameJavaString); JRubyFile newFile = JRubyFile.create(runtime.getCurrentDirectory(), newNameJavaString); if (!oldFile.exists() || !newFile.getParentFile().exists()) { throw runtime.newErrnoENOENTError(oldNameJavaString + " or " + newNameJavaString); } JRubyFile dest = JRubyFile.create(runtime.getCurrentDirectory(), newNameJavaString); if (oldFile.renameTo(dest)) { // rename is successful return RubyFixnum.zero(runtime); } // rename via Java API call wasn't successful, let's try some tricks, similar to MRI if (newFile.exists()) { runtime.getPosix().chmod(newNameJavaString, 0666); newFile.delete(); } if (oldFile.renameTo(dest)) { // try to rename one more time return RubyFixnum.zero(runtime); } throw runtime.newErrnoEACCESError(oldNameJavaString + " or " + newNameJavaString); }
public static IRubyObject rowned_p(IRubyObject recv, IRubyObject filename) { Ruby runtime = recv.getRuntime(); JRubyFile file = file(filename); return runtime.newBoolean(file.exists() && runtime.getPosix().stat(file.getAbsolutePath()).isROwned()); }
try { if (canonicalize) { classPath = new JRubyFile(before + filename.substring(idx + 1)).getCanonicalPath(); } else { classPath = new JRubyFile(before + filename.substring(idx + 1)).toString(); try { if (canonicalize) { classPath = new JRubyFile(filename).getCanonicalPath(); } else { classPath = new JRubyFile(filename).toString(); try { if (canonicalize) { parentPath = new JRubyFile(parent).getCanonicalPath(); } else { parentPath = new JRubyFile(parent).toString();
@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); }
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; }
@JRubyMethod(name = "identical?", required = 2, module = true) public static IRubyObject identical_p(IRubyObject recv, IRubyObject filename1, IRubyObject filename2) { Ruby runtime = recv.getRuntime(); JRubyFile file1 = file(filename1); JRubyFile file2 = file(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) try { return runtime.newBoolean(file1.exists() && file2.exists() && file1.getCanonicalPath().equals(file2.getCanonicalPath())); } catch (IOException e) { // this is indicative of something really wrong, but for now... return runtime.getFalse(); } } return runtime.newBoolean(file1.exists() && file2.exists() && runtime.getPosix().stat(file1.getAbsolutePath()).isIdentical(runtime.getPosix().stat(file2.getAbsolutePath()))); }
public static JRubyFile create(String cwd, String pathname) { return createNoUnicodeConversion(cwd, pathname); }
@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); }