Package org.jruby.ext.posix

Examples of org.jruby.ext.posix.JavaSecuredFile


    private static JRubyFile createNoUnicodeConversion(String cwd, String pathname) {
        if (pathname == null || pathname.equals("") || Ruby.isSecurityRestricted()) {
            return JRubyNonExistentFile.NOT_EXIST;
        }
        File internal = new JavaSecuredFile(pathname);
        if(!internal.isAbsolute()) {
            internal = new JavaSecuredFile(cwd, pathname);
            if(!internal.isAbsolute()) {
                throw new IllegalArgumentException("Neither current working directory ("+cwd+") nor pathname ("+pathname+") led to an absolute path");
            }
        }
        return new JRubyFile(internal);
    }
View Full Code Here


        }
    }

    private static int addToResultIfExists(String cwd, byte[] bytes, int begin, int end, int flags, GlobFunc func, GlobArgs arg) {
        String fileName = newStringFromUTF8(bytes, begin, end - begin);
        JavaSecuredFile file = cwd != null ? new JavaSecuredFile(cwd, fileName) :
            new JavaSecuredFile(fileName);

        if (file.exists()) {
            // On case-insenstive file systems any case string will 'exists',
            // but what does it display as if you ls/dir it?
            if ((flags & FNM_CASEFOLD) != 0) {
                try {
                    String realName = file.getCanonicalFile().getName();
                    // TODO: This is only being done to the name of the file,
                    // but it should do for all parent directories too...
                    int newEnd = fileName.lastIndexOf('/');
                    if (newEnd != -1) {
                        realName = fileName.substring(0, newEnd + 1) + realName;
View Full Code Here

                ix = i;
                break;
            }
        }

        File file = new JavaSecuredFile(newStringFromUTF8(bytes, begin + 5, ix - 5));
        try {
            String jar = newStringFromUTF8(bytes, begin + ix + 1, end - (ix + 1));
            JarFile jf = new JarFile(file);

            if (jar.startsWith("/")) jar = jar.substring(1);
View Full Code Here

                    boolean recursive = false;
                    String jar = null;
                    JarFile jf = null;

                    if(isAbsolutePath(dir, 0, dir.length)) {
                        st = new JavaSecuredFile(newStringFromUTF8(dir));
                    } else if(isJarFilePath(dir, 0, dir.length)) {
                        int ix = dir.length;
                        for(int i = 0;i<dir.length;i++) {
                            if(dir[i] == '!') {
                                ix = i;
                                break;
                            }
                        }

                        st = new JavaSecuredFile(newStringFromUTF8(dir, 5, ix-5));
                        if(ix<dir.length) {
                            jar = newStringFromUTF8(dir, ix+1, dir.length-(ix+1));
                            try {
                                jf = new JarFile(st);

                                if (jar.startsWith("/")) jar = jar.substring(1);
                                if (jf.getEntry(jar + "/") != null) jar = jar + "/";
                            } catch(Exception e) {
                                jar = null;
                                jf = null;
                            }
                        }
                    } else {
                        st = new JavaSecuredFile(cwd, newStringFromUTF8(dir));
                    }

                    if((jf != null && ("".equals(jar) || (jf.getJarEntry(jar) != null &&
                            jf.getJarEntry(jar).isDirectory()))) || st.isDirectory()) {
                        if(m != -1 && Arrays.equals(magic, DOUBLE_STAR)) {
                            int n = base.length;
                            recursive = true;
                            buf.length(0);
                            buf.append(base);
                            buf.append(bytes, (base.length > 0 ? m : m + 1), end - (base.length > 0 ? m : m + 1));
                            if (jf != null) {
                                buf = fixBytesForJarInUTF8(buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize());
                            }
                            status = glob_helper(cwd, buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize(), n, flags, func, arg);
                            if(status != 0) {
                                break finalize;
                            }
                        }
                    } else {
                        break mainLoop;
                    }

                    if(jar == null) {
                        String[] dirp = files(st);

                        for(int i=0;i<dirp.length;i++) {
                            if(recursive) {
                                byte[] bs = getBytesInUTF8(dirp[i]);
                                if (fnmatch(STAR,0,1,bs,0,bs.length,flags) != 0) {
                                    continue;
                                }
                                buf.length(0);
                                buf.append(base);
                                buf.append( BASE(base) ? SLASH : EMPTY );
                                buf.append(getBytesInUTF8(dirp[i]));
                                if (isAbsolutePath(buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize())) {
                                    st = new JavaSecuredFile(newStringFromUTF8(buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize()));
                                } else {
                                    st = new JavaSecuredFile(cwd, newStringFromUTF8(buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize()));
                                }
                                if(st.isDirectory() && !".".equals(dirp[i]) && !"..".equals(dirp[i])) {
                                    int t = buf.getRealSize();
                                    buf.append(SLASH);
                                    buf.append(DOUBLE_STAR);
                                    buf.append(bytes, m, end - m);
                                    status = glob_helper(cwd, buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize(), t, flags, func, arg);
                                    if(status != 0) {
                                        break;
                                    }
                                }
                                continue;
                            }
                            byte[] bs = getBytesInUTF8(dirp[i]);
                            if(fnmatch(magic,0,magic.length,bs,0, bs.length,flags) == 0) {
                                buf.length(0);
                                buf.append(base);
                                buf.append( BASE(base) ? SLASH : EMPTY );
                                buf.append(getBytesInUTF8(dirp[i]));
                                if(m == -1) {
                                    status = func.call(buf.getUnsafeBytes(),0, buf.getRealSize(),arg);
                                    if(status != 0) {
                                        break;
                                    }
                                    continue;
                                }
                                link.add(new DirGlobber(buf, null));
                                buf = new ByteList(20);
                            }
                        }
                    } else {
                        try {
                            List<JarEntry> dirp = new ArrayList<JarEntry>();
                            for(Enumeration<JarEntry> eje = jf.entries(); eje.hasMoreElements(); ) {
                                JarEntry je = eje.nextElement();
                                String name = je.getName();
                                int ix = name.indexOf('/', jar.length());
                                if (ix == -1 || ix == name.length()-1) {
                                    if("/".equals(jar) || (name.startsWith(jar) && name.length()>jar.length())) {
                                        dirp.add(je);
                                    }
                                }
                            }
                            for(JarEntry je : dirp) {
                                String basename = (new File(je.getName())).getName();
                                byte[] bs = getBytesInUTF8(basename);
                                byte[] absoluteName = getBytesInUTF8(je.getName());
                                int len = bs.length;
                                int absoluteLen = absoluteName.length;

                                if(je.isDirectory()) {
                                    len--;
                                    absoluteLen--;
                                }

                                if(recursive) {
                                    if(fnmatch(STAR,0,1,bs,0,len,flags) != 0) {
                                        continue;
                                    }
                                    buf.length(0);
                                    buf.append(base, 0, base.length - jar.length());
                                    buf.append( BASE(base) ? SLASH : EMPTY );
                                    buf.append(absoluteName, 0, absoluteLen);

                                    if(je.isDirectory()) {
                                        int t = buf.getRealSize();
                                        buf.append(SLASH);
                                        buf.append(DOUBLE_STAR);
                                        buf.append(bytes, m, end - m);

                                        buf = fixBytesForJarInUTF8(buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize());

                                        status = glob_helper(cwd, buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize(), t, flags, func, arg);
                                        if(status != 0) {
                                            break;
                                        }
                                    }
                                    continue;
                                }

                                if(fnmatch(magic,0,magic.length,bs,0,len,flags) == 0) {
                                    buf.length(0);
                                    buf.append(base, 0, base.length - jar.length());
                                    buf.append( BASE(base) ? SLASH : EMPTY );
                                    buf.append(absoluteName, 0, absoluteLen);

                                    buf = fixBytesForJarInUTF8(buf.getUnsafeBytes(), 0, buf.getRealSize());
                                    if(m == -1) {
                                        status = func.call(buf.getUnsafeBytes(),0, buf.getRealSize(),arg);
                                        if(status != 0) {
                                            break;
                                        }
                                        continue;
                                    }
                                    link.add(new DirGlobber(buf, je));
                                    buf = new ByteList(20);
                                }
                            }
                        } catch(Exception e) {}
                    }
                } while(false);

                if (link.size() > 0) {
                    for (DirGlobber globber : link) {
                        ByteList b = globber.link;
                        if (status == 0) {
                            if(isAbsolutePath(b.getUnsafeBytes(), b.begin(), b.getRealSize())) {
                                st = new JavaSecuredFile(newStringFromUTF8(b.getUnsafeBytes(), 0, b.getRealSize()));
                            } else {
                                st = new JavaSecuredFile(cwd, newStringFromUTF8(b.getUnsafeBytes(), 0, b.getRealSize()));
                            }
                            if(st.isDirectory() || (globber.jarEntry != null && globber.jarEntry.isDirectory()) ) {
                                int len = b.getRealSize();
                                buf.length(0);
                                buf.append(b);
View Full Code Here

TOP

Related Classes of org.jruby.ext.posix.JavaSecuredFile

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.