public override void SetAttributes(string uri, NodeType nodeType, IEnumerable <Pair <string, object> > attributes) { using (this.AcquireCommandContext()) { var attributesList = attributes.Where(x => !x.Left.EqualsIgnoreCase("length") && !x.Left.EqualsIgnoreCase("exists")).ToList(); SendCommandWithoutResponse(@"setattributes -t={0} ""{1}""", TextNetworkProtocol.GetNodeTypeName(nodeType), uri); ReadResponse().ProcessError(); try { foreach (var attribute in attributesList) { WriteTextBlock(@"{0}=""{1}:{2}""", attribute.Name, ProtocolTypes.GetTypeName(attribute.Value.GetType()), ProtocolTypes.ToEscapedString(attribute.Value)); } } finally { WriteTextBlock(ResponseCodes.READY); } ReadResponse().ProcessError(); } }
public override void Process(Command command) { Exception e = null; var options = (CommandOptions)this.LoadOptions((TextCommand)command); var nodeType = NodeType.FromName(options.NodeType); var node = this.Connection.FileSystemManager.Resolve(options.Uri, nodeType); node.Refresh(); if (!node.Exists) { throw new NodeNotFoundException(node.Address); } if (!NodeTypeSupported(node.NodeType)) { Connection.WriteError(ErrorCodes.INVALID_PARAM); return; } Connection.WriteOk(); Connection.Flush(); using (node.Attributes.AquireUpdateContext()) { var lastLineRead = new ValueBox <string>(); foreach (var attribute in TextNetworkProtocol.ReadAttributes(Connection.ReadTextBlock, lastLineRead)) { var attributeName = attribute.Name; var attributeValue = attribute.Value; if (!(attributeName.EqualsIgnoreCase("exists") || attributeName.EqualsIgnoreCase("length"))) { e = ActionUtils.IgnoreExceptions(delegate { node.Attributes[attributeName] = attributeValue; }); } } } if (e != null) { throw e; } this.Connection.WriteOk(); }
public override void Delete(string uri, NodeType nodeType, bool recursive) { using (this.AcquireCommandContext()) { try { SendCommand(DefaultTryCount, "delete -t={0} {1} \"{2}\"", TextNetworkProtocol.GetNodeTypeName(nodeType), recursive ? "-r" : "", uri).ProcessError(); } catch (Exception) { this.connected = false; throw; } } }
public override void Create(string uri, NodeType nodeType, bool createParent) { using (this.AcquireCommandContext()) { try { SendCommand(DefaultTryCount, "create -t={0} {1} \"{2}\"", TextNetworkProtocol.GetNodeTypeName(nodeType), createParent && nodeType.IsLikeDirectory ? "-p" : "", uri).ProcessError(); } catch (TextNetworkProtocolException) { this.connected = false; throw; } catch (IOException) { this.connected = false; throw; } } }
public override IEnumerable <Pair <string, object> > GetAttributes(string uri, NodeType nodeType) { using (this.AcquireCommandContext(false)) { var lastReadLine = new ValueBox <string>(this.lastLineRead); try { this.SendCommand(DefaultTryCount, @"getattributes -t={0} ""{1}""", TextNetworkProtocol.GetNodeTypeName(nodeType), uri).ProcessError(); } catch { ReadReady(); throw; } foreach (var attribute in TextNetworkProtocol.ReadAttributes(this.ReadNextLine, lastReadLine)) { yield return(attribute); } } }
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 HashValue ComputeHash(string uri, NodeType nodeType, string algorithm, bool recursive, long offset, long length, IEnumerable <string> fileAttributes, IEnumerable <string> dirAttributes) { StringBuilder dirAttributesString = null; StringBuilder fileAttributesString = null; using (this.AcquireCommandContext()) { try { if (dirAttributes != null) { dirAttributesString = new StringBuilder(); foreach (string s in dirAttributes) { dirAttributesString.Append(s); dirAttributesString.Append(','); } if (dirAttributesString.Length > 0) { dirAttributesString.Length--; } else { dirAttributesString = null; } } if (fileAttributes != null) { fileAttributesString = new StringBuilder(); foreach (string s in fileAttributes) { fileAttributesString.Append(s); fileAttributesString.Append(','); } if (fileAttributesString.Length > 0) { fileAttributesString.Length--; } else { fileAttributesString = null; } } StringBuilder commandText = new StringBuilder(128); commandText.Append("computehash -hex"); commandText.Append(" -t=\"").Append(TextNetworkProtocol.GetNodeTypeName(nodeType)).Append('\"'); if (offset != 0) { commandText.Append(" -o=\"").Append(offset).Append('\"'); } if (recursive) { commandText.Append(" -r"); } if (length != -1) { commandText.Append(" -l=\"").Append(length).Append('\"'); } if (algorithm != "md5") { commandText.Append(" -a=\"").Append(algorithm).Append('\"'); } if (fileAttributesString != null) { commandText.Append(" -fileattribs=\"").Append(fileAttributesString).Append('\"'); } if (dirAttributesString != null) { commandText.Append(" -dirattribs=\"").Append(dirAttributesString).Append('\"'); } commandText.Append(" \"").Append(uri).Append('\"'); var response = SendCommand(DefaultTryCount, commandText.ToString()).ProcessError(); return(new HashValue ( TextConversion.FromHexString(response.ResponseTuples["hash"]), algorithm, 0, length )); } catch (TextNetworkProtocolException) { this.connected = false; throw; } catch (IOException) { this.connected = false; throw; } } }
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(); } } }
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)); } } } }
public override void Copy(string srcUri, string desUri, NodeType nodeType, bool overwrite) { using (this.AcquireCommandContext()) { try { SendCommand(DefaultTryCount, "copy -t={0} -o={1} \"{2}\" \"{3}\"", TextNetworkProtocol.GetNodeTypeName(nodeType), overwrite, srcUri, desUri).ProcessError(); } catch (TextNetworkProtocolException) { this.connected = false; throw; } catch (IOException) { this.connected = false; throw; } } }