Esempio n. 1
0
File: Dir.cs Progetto: fronx/ioke
        private static int GlobHelper(string cwd, string pattern, int begin, int end, int sub, int flags, GlobFunc func, GlobArgs arg) {
            int p,m;
            int status = 0;
            StringBuilder newpath = null;
            FileSystemInfo st;
            p = sub != -1 ? sub : begin;
            if (!HasMagic(pattern, p, end, flags)) {
                if (DOSISH || (flags & FNM_NOESCAPE) == 0) {
                    newpath = new StringBuilder();
                    newpath.Append(pattern, 0, end);
                    if (sub != -1) {
                        p = (sub - begin);
                        end = RemoveBackslashes(newpath, p, end);
                        sub = p;
                    } else {
                        end = RemoveBackslashes(newpath, 0, end);
                        pattern = newpath.ToString();
                    }
                }

                if(pattern[begin] == '/' || (DOSISH && begin+2<end && pattern[begin+1] == ':' && IsDirSep(pattern[begin+2]))) {
                    string ss = pattern.Substring(begin, end-begin);
                    if(new FileInfo(ss).Exists || new DirectoryInfo(ss).Exists) {
                        status = func(pattern, begin, end, arg);
                    }
                } else if((end - begin) > 0) { // Length check is a hack.  We should not be reeiving "" as a filename ever. 
                    string ss2 = pattern.Substring(begin, end-begin);
                    if(new FileInfo(ss2).Exists || new DirectoryInfo(ss2).Exists) {
                        status = func(pattern, begin, end - begin, arg);
                    }
                }

                return status;
            }

            string bytes2 = pattern;
            string buf = "";
            var link = new List<string>();
            while(p != -1 && status == 0) {
                if(bytes2[p] == '/') p++;

                m = strchr(bytes2, p, end, '/');
                if(HasMagic(bytes2, p, m == -1 ? end : m, flags)) {
                    do {
                        string _base = ExtractPath(bytes2, begin, p);
                        string dir = begin == p ? "." : _base; 
                        string magic = ExtractElem(bytes2,p,end);
                        bool recursive = false;
                        try {
                            if(dir[0] == '/'  || (DOSISH && 2<dir.Length && dir[1] == ':' && IsDirSep(dir[2]))) {
                                st = new DirectoryInfo(dir);
                            } else {
                                st = new DirectoryInfo(Path.Combine(cwd, dir));
                            }
                        } catch(Exception) {
                            // A clear sign that the file doesn't exist.
                            return status;
                        }

                        if(st.Exists) {
                            if(m != -1 && magic.Equals(DOUBLE_STAR)) {
                                int n = _base.Length;
                                recursive = true;
                                buf = _base + bytes2.Substring((_base.Length > 0 ? m : m + 1), end - (_base.Length > 0 ? m : m + 1));
                                status = GlobHelper(cwd, buf, 0, buf.Length, n, flags, func, arg);
                                if(status != 0) {
                                    goto finalize;
                                }
                            }
                        } else {
                            return status;
                        }

                        string[] dirp = Files((DirectoryInfo)st);

                        for(int i=0;i<dirp.Length;i++) {
                            if(recursive) {
                                string bs = dirp[i];
                                if(FilenameMatch(STAR,0,1,bs,0,bs.Length,flags) != 0) {
                                    continue;
                                }
                                buf = _base + (BASE(_base) ? SLASH : EMPTY);
                                buf += dirp[i];
                                if(buf[0] == '/' || (DOSISH && 2<buf.Length && buf[1] == ':' && IsDirSep(buf[2]))) {
                                    st = new DirectoryInfo(buf);
                                } else {
                                    st = new DirectoryInfo(Path.Combine(cwd, buf));
                                }
                                if(st.Exists && !".".Equals(dirp[i]) && !"..".Equals(dirp[i])) {
                                    int t = buf.Length;
                                    buf += SLASH + DOUBLE_STAR;
                                    buf += bytes2.Substring(m, end-m);
                                    status = GlobHelper(cwd, buf, 0, buf.Length, t, flags, func, arg);
                                    if(status != 0) {
                                        break;
                                    }
                                }
                                continue;
                            }
                            string bsx = dirp[i];
                            if(FilenameMatch(magic,0,magic.Length,bsx,0, bsx.Length,flags) == 0) {
                                buf = _base + (BASE(_base) ? SLASH : EMPTY) + dirp[i];
                                if(m == -1) {
                                    status = func(buf,0,buf.Length,arg);
                                    if(status != 0) {
                                        break;
                                    }
                                    continue;
                                }
                                link.Add(buf);
                                buf = "";
                            }
                        }
                    } while(false);
                    finalize:

                    if(link.Count > 0) {
                        foreach(string b in link) {
                            if(status == 0) {
                                if(b[0] == '/'  || (DOSISH && 2<b.Length && b[1] == ':' && IsDirSep(b[2]))) {
                                    st = new DirectoryInfo(b);
                                } else {
                                    st = new DirectoryInfo(Path.Combine(cwd, b));
                                }

                                if(st.Exists) {
                                    int len = b.Length;
                                    buf = b + bytes2.Substring(m, end - m);
                                    status = GlobHelper(cwd,buf,0,buf.Length,len,flags,func,arg);
                                }
                            }
                        }
                        return status;
                    }
                }
                p = m;
            }

            return status;
        }
Esempio n. 2
0
        private static int GlobHelper(string cwd, string pattern, int begin, int end, int sub, int flags, GlobFunc func, GlobArgs arg)
        {
            int            p, m;
            int            status  = 0;
            StringBuilder  newpath = null;
            FileSystemInfo st;

            p = sub != -1 ? sub : begin;
            if (!HasMagic(pattern, p, end, flags))
            {
                if (DOSISH || (flags & FNM_NOESCAPE) == 0)
                {
                    newpath = new StringBuilder();
                    newpath.Append(pattern, 0, end);
                    if (sub != -1)
                    {
                        p   = (sub - begin);
                        end = RemoveBackslashes(newpath, p, end);
                        sub = p;
                    }
                    else
                    {
                        end     = RemoveBackslashes(newpath, 0, end);
                        pattern = newpath.ToString();
                    }
                }

                if (pattern[begin] == '/' || (DOSISH && begin + 2 < end && pattern[begin + 1] == ':' && IsDirSep(pattern[begin + 2])))
                {
                    string ss = pattern.Substring(begin, end - begin);
                    if (new FileInfo(ss).Exists || new DirectoryInfo(ss).Exists)
                    {
                        status = func(pattern, begin, end, arg);
                    }
                }
                else if ((end - begin) > 0)    // Length check is a hack.  We should not be reeiving "" as a filename ever.
                {
                    string ss2 = pattern.Substring(begin, end - begin);
                    if (new FileInfo(ss2).Exists || new DirectoryInfo(ss2).Exists)
                    {
                        status = func(pattern, begin, end - begin, arg);
                    }
                }

                return(status);
            }

            string bytes2 = pattern;
            string buf    = "";
            var    link   = new List <string>();

            while (p != -1 && status == 0)
            {
                if (bytes2[p] == '/')
                {
                    p++;
                }

                m = strchr(bytes2, p, end, '/');
                if (HasMagic(bytes2, p, m == -1 ? end : m, flags))
                {
                    do
                    {
                        string _base     = ExtractPath(bytes2, begin, p);
                        string dir       = begin == p ? "." : _base;
                        string magic     = ExtractElem(bytes2, p, end);
                        bool   recursive = false;
                        try {
                            if (dir[0] == '/' || (DOSISH && 2 < dir.Length && dir[1] == ':' && IsDirSep(dir[2])))
                            {
                                st = new DirectoryInfo(dir);
                            }
                            else
                            {
                                st = new DirectoryInfo(Path.Combine(cwd, dir));
                            }
                        } catch (Exception) {
                            // A clear sign that the file doesn't exist.
                            return(status);
                        }

                        if (st.Exists)
                        {
                            if (m != -1 && magic.Equals(DOUBLE_STAR))
                            {
                                int n = _base.Length;
                                recursive = true;
                                buf       = _base + bytes2.Substring((_base.Length > 0 ? m : m + 1), end - (_base.Length > 0 ? m : m + 1));
                                status    = GlobHelper(cwd, buf, 0, buf.Length, n, flags, func, arg);
                                if (status != 0)
                                {
                                    goto finalize;
                                }
                            }
                        }
                        else
                        {
                            return(status);
                        }

                        string[] dirp = Files((DirectoryInfo)st);

                        for (int i = 0; i < dirp.Length; i++)
                        {
                            if (recursive)
                            {
                                string bs = dirp[i];
                                if (FilenameMatch(STAR, 0, 1, bs, 0, bs.Length, flags) != 0)
                                {
                                    continue;
                                }
                                buf  = _base + (BASE(_base) ? SLASH : EMPTY);
                                buf += dirp[i];
                                if (buf[0] == '/' || (DOSISH && 2 < buf.Length && buf[1] == ':' && IsDirSep(buf[2])))
                                {
                                    st = new DirectoryInfo(buf);
                                }
                                else
                                {
                                    st = new DirectoryInfo(Path.Combine(cwd, buf));
                                }
                                if (st.Exists && !".".Equals(dirp[i]) && !"..".Equals(dirp[i]))
                                {
                                    int t = buf.Length;
                                    buf   += SLASH + DOUBLE_STAR;
                                    buf   += bytes2.Substring(m, end - m);
                                    status = GlobHelper(cwd, buf, 0, buf.Length, t, flags, func, arg);
                                    if (status != 0)
                                    {
                                        break;
                                    }
                                }
                                continue;
                            }
                            string bsx = dirp[i];
                            if (FilenameMatch(magic, 0, magic.Length, bsx, 0, bsx.Length, flags) == 0)
                            {
                                buf = _base + (BASE(_base) ? SLASH : EMPTY) + dirp[i];
                                if (m == -1)
                                {
                                    status = func(buf, 0, buf.Length, arg);
                                    if (status != 0)
                                    {
                                        break;
                                    }
                                    continue;
                                }
                                link.Add(buf);
                                buf = "";
                            }
                        }
                    } while(false);
finalize:

                    if (link.Count > 0)
                    {
                        foreach (string b in link)
                        {
                            if (status == 0)
                            {
                                if (b[0] == '/' || (DOSISH && 2 < b.Length && b[1] == ':' && IsDirSep(b[2])))
                                {
                                    st = new DirectoryInfo(b);
                                }
                                else
                                {
                                    st = new DirectoryInfo(Path.Combine(cwd, b));
                                }

                                if (st.Exists)
                                {
                                    int len = b.Length;
                                    buf    = b + bytes2.Substring(m, end - m);
                                    status = GlobHelper(cwd, buf, 0, buf.Length, len, flags, func, arg);
                                }
                            }
                        }
                        return(status);
                    }
                }
                p = m;
            }

            return(status);
        }