Example #1
0
        public override void Process(Command command)
        {
            IHashingService            service;
            RandomAccessCommandOptions options;
            Stream stream = null;

            if (Connection.RunLevel is RandomAccessRunLevel)
            {
                IFile file;

                options = (RandomAccessCommandOptions)LoadOptions(typeof(RandomAccessCommandOptions), (TextCommand)command);

                RandomAccessRunLevel randomAccessRunLevel;

                randomAccessRunLevel = ((RandomAccessRunLevel)Connection.RunLevel);

                file = randomAccessRunLevel.FileNode;

                stream  = randomAccessRunLevel.Stream;
                service = (IHashingService)file.GetService(new StreamHashingServiceType(stream, options.Algorithm));
            }
            else
            {
                INode          node;
                CommandOptions cmdOptions;

                options = cmdOptions = (CommandOptions)LoadOptions((TextCommand)command);

                var nodeType = TextNetworkProtocol.GetNodeType(options.NodeType);

                if (nodeType == NodeType.File)
                {
                    node = Connection.FileSystemManager.Resolve(cmdOptions.Uri, nodeType);

                    service = (IHashingService)node.GetService(new FileHashingServiceType(options.Algorithm));
                }
                else if (nodeType == NodeType.Directory)
                {
                    DirectoryHashingServiceType serviceType;

                    node = Connection.FileSystemManager.Resolve(cmdOptions.Uri, nodeType);

                    serviceType = new DirectoryHashingServiceType(cmdOptions.Recursive, options.Algorithm);

                    string[] ss;

                    cmdOptions.DirAttribs = cmdOptions.DirAttribs.Trim();

                    if (cmdOptions.DirAttribs.Length > 0)
                    {
                        ss = cmdOptions.DirAttribs.Split(',');

                        if (ss.Length > 0)
                        {
                            serviceType.IncludedDirectoryAttributes = ss;
                        }
                    }

                    cmdOptions.FileAttribs = cmdOptions.FileAttribs.Trim();

                    if (cmdOptions.FileAttribs.Length > 0)
                    {
                        ss = cmdOptions.FileAttribs.Trim().Split(',');

                        if (ss.Length > 0)
                        {
                            serviceType.IncludedFileAttributes = ss;
                        }
                    }

                    service = (IHashingService)node.GetService(serviceType);
                }
                else
                {
                    throw new NotSupportedException("ComputeHash_NodeType_" + options.NodeType);
                }
            }

            var hashResult = service.ComputeHash(options.Offset, options.Length);

            if (stream != null)
            {
                Connection.WriteOk
                (
                    "hash", options.SerializeAsHex ? hashResult.TextValue : hashResult.Base64TextValue,
                    "offset", hashResult.Offset,
                    "length", hashResult.Length,
                    "stream-position", stream.Position
                );
            }
            else
            {
                Connection.WriteOk
                (
                    "hash", options.SerializeAsHex ? hashResult.TextValue : hashResult.Base64TextValue,
                    "offset", hashResult.Offset,
                    "length", hashResult.Length
                );
            }

            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));
                    }
                }
            }
        }