Ejemplo n.º 1
0
        /// <summary>
        /// Crawl subdirectories recursively. permparent is the last parent folder that had new permissions set
        /// </summary>
        /// <param name="path"></param>
        /// <param name="permparent"></param>
        public void Crawl()
        {
            bool   connected     = false;
            string newpermparent = this.PermParent;

            try
            {
                Folder f = this.QueryFolder(new DirectoryInfo(this.Path), this.PermParent, this.IsRoot);
                if ((f.PermissionCount > 0) || this.IsRoot)
                {
                    newpermparent = this.Path;
                    this._parent.Writer.UpdateFolder(f, this._parent.Driver);
                }

                connected = true;
            }
            catch (Exception e)
            {
                ConsoleWriter.WriteError("Error connecting to " + this.Path + ": " + e.Message);
                Folder f = new Folder()
                {
                    Blocked             = true,
                    Path                = this.Path,
                    Name                = this.Path,
                    PermParent          = this.PermParent,
                    InheritanceDisabled = true,
                    Depth               = this.Depth
                };
                this._parent.Writer.UpdateFolder(f, this._parent.Driver);
            }

            if (connected == true)
            {
                try
                {
                    foreach (string subdirpath in Directory.EnumerateDirectories(this.Path))
                    {
                        CrawlerThreadWrapper subwrapper = new CrawlerThreadWrapper(this._parent, this.Depth + 1);
                        subwrapper.Path       = subdirpath;
                        subwrapper.PermParent = newpermparent;
                        subwrapper.IsRoot     = false;

                        int threadnum = ThreadCounter.RequestThread();

                        if (threadnum != -1)
                        {
                            subwrapper.ThreadNumber = threadnum;
                            subwrapper.IsNewThread  = true;
                            ThreadPool.QueueUserWorkItem(subwrapper.Crawl);
                        }
                        else
                        {
                            subwrapper.ThreadNumber = this.ThreadNumber;
                            subwrapper.Crawl();
                        }
                    }
                }
                catch (Exception e)
                {
                    ConsoleWriter.WriteError("Error encountered while enumerating directories in " + this.Path + ": " + e.Message);
                }
            }

            if (this.IsNewThread == true)
            {
                ThreadCounter.Release(this.ThreadNumber);
                ConsoleWriter.WriteProgress(this.ThreadNumber + " | " + "Released thread", this.ThreadNumber);
            }
        }