// // Copyright (c) 2008-2011, Kenneth Bell // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. // namespace DiscUtils.Ntfs { using System; using System.Collections.Generic; using System.IO; internal class NtfsStream { private File _file; private NtfsAttribute _attr; public NtfsStream(File file, NtfsAttribute attr) { _file = file; _attr = attr; } public NtfsAttribute Attribute { get { return _attr; } } public AttributeType AttributeType { get { return _attr.Type; } } public string Name { get { return _attr.Name; } } /// /// Gets the content of a stream. /// /// The stream's content structure. /// The content. public T GetContent() where T : IByteArraySerializable, IDiagnosticTraceable, new() { byte[] buffer; using (Stream s = Open(FileAccess.Read)) { buffer = Utilities.ReadFully(s, (int)s.Length); } T value = new T(); value.ReadFrom(buffer, 0); return value; } /// /// Sets the content of a stream. /// /// The stream's content structure. /// The new value for the stream. public void SetContent(T value) where T : IByteArraySerializable, IDiagnosticTraceable, new() { byte[] buffer = new byte[value.Size]; value.WriteTo(buffer, 0); using (Stream s = Open(FileAccess.Write)) { s.Write(buffer, 0, buffer.Length); s.SetLength(buffer.Length); } } public SparseStream Open(FileAccess access) { return _attr.Open(access); } internal Range[] GetClusters() { return _attr.GetClusters(); } internal StreamExtent[] GetAbsoluteExtents() { List result = new List(); long clusterSize = _file.Context.BiosParameterBlock.BytesPerCluster; if (_attr.IsNonResident) { Range[] clusters = _attr.GetClusters(); foreach (var clusterRange in clusters) { result.Add(new StreamExtent(clusterRange.Offset * clusterSize, clusterRange.Count * clusterSize)); } } else { result.Add(new StreamExtent(_attr.OffsetToAbsolutePos(0), _attr.Length)); } return result.ToArray(); } } }