Package org.jruby.util

Examples of org.jruby.util.JRubyFile


        ZipEntry entry = file_in_archive(filename);
        if (entry != null) {
            return runtime.newFixnum(entry.getSize());
        }

        JRubyFile file = file(filename);

        if (!file.exists()) {
            noFileError(filename);
        }

        return runtime.newFixnum(file.length());
    }
View Full Code Here


            } else {
                return runtime.getNil();
            }
        }

        JRubyFile file = file(filename);

        if (!file.exists()) {
            return runtime.getNil();
        }

        long length = file.length();
        if (length > 0) {
            return runtime.newFixnum(length);
        } else {
            return runtime.getNil();
        }
View Full Code Here

    }

    @JRubyMethod(name = "socket?", required = 1, module = true)
    public static IRubyObject socket_p(IRubyObject recv, IRubyObject filename) {
        Ruby runtime = recv.getRuntime();
        JRubyFile file = file(filename);

        return runtime.newBoolean(file.exists() && runtime.getPosix().stat(file.getAbsolutePath()).isSocket());
    }
View Full Code Here

    }

    @JRubyMethod(name = "sticky?", required = 1, module = true)
    public static IRubyObject sticky_p(IRubyObject recv, IRubyObject filename) {
        Ruby runtime = recv.getRuntime();
        JRubyFile file = file(filename);

        return runtime.newBoolean(file.exists() && runtime.getPosix().stat(file.getAbsolutePath()).isSticky());
    }
View Full Code Here

    }

    @JRubyMethod(name = "symlink?", required = 1, module = true)
    public static RubyBoolean symlink_p(IRubyObject recv, IRubyObject filename) {
        Ruby runtime = recv.getRuntime();
        JRubyFile file = file(filename);

        try {
            // Note: We can't use file.exists() to check whether the symlink
            // exists or not, because that method returns false for existing
            // but broken symlink. So, we try without the existence check,
            // but in the try-catch block.
            // MRI behavior: symlink? on broken symlink should return true.
            return runtime.newBoolean(runtime.getPosix().lstat(file.getAbsolutePath()).isSymlink());
        } catch (SecurityException re) {
            return runtime.getFalse();
        } catch (RaiseException re) {
            return runtime.getFalse();
        }
View Full Code Here

        ZipEntry entry = file_in_archive(filename);
        if (entry != null) {
            return runtime.newBoolean(entry.getSize() == 0L);
        }

        JRubyFile file = file(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();
        }
    }
View Full Code Here

        Ruby runtime = context.getRuntime();

        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);
        }
View Full Code Here

            path = path.substring("classpath:/".length());
            InputStream is = classLoader.getResourceAsStream(path);
            // FIXME: don't use RubyIO for this
            return new ChannelDescriptor(Channels.newChannel(is), flags);
        } else {
            JRubyFile theFile = JRubyFile.create(cwd,path);

            if (theFile.isDirectory() && flags.isWritable()) {
                throw new DirectoryAsFileException();
            }

            if (flags.isCreate()) {
                if (theFile.exists() && flags.isExclusive()) {
                    throw new FileExistsException(path);
                }
                try {
                    fileCreated = theFile.createNewFile();
                } catch (IOException ioe) {
                    // See JRUBY-4380.
                    // MRI behavior: raise Errno::ENOENT in case
                    // when the directory for the file doesn't exist.
                    // Java in such cases just throws IOException.
                    File parent = theFile.getParentFile();
                    if (parent != null && parent != theFile && !parent.exists()) {
                        throw new FileNotFoundException(path);
                    } else if (!theFile.canWrite()) {
                        throw new PermissionDeniedException(path);
                    } else {
                        // for all other IO errors, just re-throw the original exception
                        throw ioe;
                    }
                }
            } else {
                if (!theFile.exists()) {
                    throw new FileNotFoundException(path);
                }
            }

            // We always open this rw since we can only open it r or rw.
            RandomAccessFile file = new RandomAccessFile(theFile, flags.toJavaModeString());

            // call chmod after we created the RandomAccesFile
            // because otherwise, the file could be read-only
            if (fileCreated) {
                // attempt to set the permissions, if we have been passed a POSIX instance,
                // and only if the file was created in this call.
                if (posix != null && perm != -1) {
                    posix.chmod(theFile.getPath(), perm);
                }
            }

            if (flags.isTruncate()) file.setLength(0L);
View Full Code Here

        return entriesIntoADirectory(runtime, path);
    }

    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;
View Full Code Here

        Ruby runtime = context.getRuntime();
        RubyString path = args.length == 1 ?
            RubyFile.get_path(context, args[0]) : getHomeDirectoryPath(context);
        String adjustedPath = RubyFile.adjustRootPathOnWindows(runtime, path.getUnicodeValue(), null);
        checkDirIsTwoSlashesOnWindows(runtime, adjustedPath);
        JRubyFile dir = getDir(runtime, adjustedPath, true);
        String realPath = null;
        String oldCwd = runtime.getCurrentDirectory();

        // We get canonical path to try and flatten the path out.
        // a dir '/subdir/..' should return as '/'
        // cnutter: Do we want to flatten path out?
        try {
            realPath = dir.getCanonicalPath();
        } catch (IOException e) {
            realPath = dir.getAbsolutePath();
        }

        IRubyObject result = null;
        if (block.isGiven()) {
            // FIXME: Don't allow multiple threads to do this at once
View Full Code Here

TOP

Related Classes of org.jruby.util.JRubyFile

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.