public override IEnumerable <NetworkFileSystemEntry> ListAttributes(string uri, string regex)
        {
            Predicate <string> acceptName = null;

            using (this.AcquireCommandContext(false))
            {
                try
                {
                    try
                    {
                        if (!String.IsNullOrEmpty(regex))
                        {
                            try
                            {
                                SendCommand(DefaultTryCount, @"list -a -regex=""{0}"" ""{1}""", TextConversion.ToEscapedHexString(regex), uri).ProcessError();
                            }
                            catch (TextNetworkProtocolErrorResponseException)
                            {
                                ReadReady();

                                SendCommand(DefaultTryCount, @"list -a ""{1}""", regex, uri).ProcessError();

                                acceptName = PredicateUtils.NewRegex(regex);
                            }
                        }
                        else
                        {
                            SendCommand(DefaultTryCount, @"list -a ""{0}""", uri).ProcessError();
                        }
                    }
                    catch
                    {
                        ReadReady();

                        throw;
                    }
                }
                catch (TextNetworkProtocolException)
                {
                    this.connected = false;

                    throw;
                }
                catch (IOException)
                {
                    this.connected = false;

                    throw;
                }

                var enumerator = TextNetworkProtocol.ReadEntries(this.ReadNextLine).GetEnumerator();

                try
                {
                    for (; ;)
                    {
                        try
                        {
                            if (!enumerator.MoveNext())
                            {
                                break;
                            }
                        }
                        catch (TextNetworkProtocolException)
                        {
                            this.connected = false;

                            throw;
                        }
                        catch (IOException)
                        {
                            this.connected = false;

                            throw;
                        }

                        if (enumerator.Current.Right != null)
                        {
                            CommandResponse response;

                            response = ParseResponse(enumerator.Current.Right);

                            response.ProcessError();
                        }

                        if (acceptName != null)
                        {
                            if (acceptName(enumerator.Current.Left.Name))
                            {
                                yield return(enumerator.Current.Left);
                            }
                        }
                        else
                        {
                            yield return(enumerator.Current.Left);
                        }
                    }
                }
                finally
                {
                    enumerator.Dispose();
                }
            }
        }
コード例 #2
0
        public override void Process(Command command)
        {
            var count   = 0;
            var options = this.LoadOptions <CommandOptions>((TextCommand)command);
            var dir     = this.Connection.FileSystemManager.ResolveDirectory(options.Uri);

            dir.Refresh();

            if (options.Regex != null)
            {
                options.Regex = TextConversion.FromEscapedHexString(options.Regex);
            }

            if (!dir.Exists)
            {
                throw new DirectoryNodeNotFoundException(dir.Address);
            }

            Connection.WriteOk();

            if (options.IncludeAttributes)
            {
                Exception       exception = null;
                InvocationQueue queue;

                if (t_InvocationQueue == null)
                {
                    t_InvocationQueue = new InvocationQueue();
                }

                queue = t_InvocationQueue;

                queue.TaskAsynchronisity = TaskAsynchronisity.AsyncWithSystemPoolThread;

                queue.Start();

                try
                {
                    IEnumerable <INode> children = null;

                    if (String.IsNullOrEmpty(options.Regex))
                    {
                        children = dir.GetChildren();
                    }
                    else
                    {
                        children = dir.GetChildren(RegexBasedNodePredicateHelper.New(options.Regex));
                    }

                    foreach (var node in children)
                    {
                        var enclosedNode = node;

                        if (queue.TaskState == TaskState.Stopped)
                        {
                            break;
                        }

                        queue.Enqueue
                        (
                            delegate
                        {
                            try
                            {
                                if (enclosedNode.NodeType == NodeType.Directory)
                                {
                                    Connection.WriteTextPartialBlock("d:");
                                }
                                else
                                {
                                    Connection.WriteTextPartialBlock("f:");
                                }

                                Connection.WriteTextBlock(TextConversion.ToEscapedHexString(enclosedNode.Name, TextConversion.IsStandardUrlEscapedChar));

                                AttributesCommands.PrintAttributes(this.Connection, enclosedNode);

                                count++;

                                if (count % 15 == 0)
                                {
                                    Connection.Flush();
                                }
                            }
                            catch (Exception e)
                            {
                                queue.Stop();

                                exception = e;
                            }
                        }
                        );
                    }

                    if (queue.TaskState == TaskState.Stopped)
                    {
                        throw exception;
                    }
                }
                finally
                {
                    queue.Enqueue(queue.Stop);

                    queue.WaitForAnyTaskState(value => value != TaskState.Running);

                    queue.Reset();

                    Connection.Flush();
                }
            }
            else
            {
                IEnumerable <string> children;

                if (string.IsNullOrEmpty(options.Regex))
                {
                    children = dir.GetChildNames(NodeType.Directory);
                }
                else
                {
                    children = dir.GetChildNames(NodeType.Directory, PredicateUtils.NewRegex(options.Regex));
                }

                foreach (var name in children)
                {
                    Connection.WriteTextPartialBlock("d:");
                    Connection.WriteTextBlock(TextConversion.ToEscapedHexString(name, TextConversion.IsStandardUrlEscapedChar));

                    count++;

                    if (count % 15 == 0)
                    {
                        Connection.Flush();
                    }
                }

                count = 0;

                if (string.IsNullOrEmpty(options.Regex))
                {
                    children = dir.GetChildNames(NodeType.File);
                }
                else
                {
                    children = dir.GetChildNames(NodeType.File, PredicateUtils.NewRegex(options.Regex));
                }

                foreach (var name in children)
                {
                    Connection.WriteTextPartialBlock("f:");
                    Connection.WriteTextBlock(TextConversion.ToEscapedHexString(name));

                    count++;

                    if (count % 15 == 0)
                    {
                        Connection.Flush();
                    }
                }
            }

            Connection.Flush();
        }
        public override IEnumerable <Pair <string, NodeType> > List(string uri, string regex)
        {
            Predicate <string> acceptName = null;

            using (this.AcquireCommandContext(false))
            {
                try
                {
                    try
                    {
                        if (!String.IsNullOrEmpty(regex))
                        {
                            SendCommand(DefaultTryCount, @"list -regex=""{0}"" ""{1}""", TextConversion.ToEscapedHexString(regex), uri).ProcessError();
                        }
                        else
                        {
                            SendCommand(DefaultTryCount, @"list ""{0}""", uri).ProcessError();

                            acceptName = PredicateUtils.NewRegex(regex);
                        }
                    }
                    catch
                    {
                        ReadReady();

                        throw;
                    }
                }
                catch (TextNetworkProtocolException)
                {
                    this.connected = false;

                    throw;
                }
                catch (IOException)
                {
                    this.connected = false;

                    throw;
                }

                for (; ;)
                {
                    string   line;
                    NodeType currentNodeType;
                    Pair <string, string> currentFile;

                    try
                    {
                        line = TextConversion.FromEscapedHexString(ReadNextLine());
                    }
                    catch (TextNetworkProtocolException)
                    {
                        this.connected = false;

                        throw;
                    }
                    catch (IOException)
                    {
                        this.connected = false;

                        throw;
                    }

                    if (line.EqualsIgnoreCase(ResponseCodes.READY))
                    {
                        break;
                    }

                    currentFile = line.SplitAroundFirstCharFromLeft(':');

                    currentFile.Right = TextConversion.FromEscapedHexString(currentFile.Right);

                    currentNodeType = TextNetworkProtocol.GetNodeType(currentFile.Left);

                    if (currentNodeType == null || currentFile.Right.Length == 0)
                    {
                        continue;
                    }

                    if (acceptName != null)
                    {
                        if (acceptName(currentFile.Right))
                        {
                            yield return(new Pair <string, NodeType>(currentFile.Right, currentNodeType));
                        }
                    }
                    else
                    {
                        yield return(new Pair <string, NodeType>(currentFile.Right, currentNodeType));
                    }
                }
            }
        }