123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using Utilities;
- namespace DiskAccessLibrary.FileSystems.NTFS
- {
- public class DataRunSequence : List<DataRun>
- {
- public DataRunSequence() : base()
- {
-
- }
-
- public KeyValuePairList<long, int> TranslateToLCN(long startClusterOffset, int clusterCount)
- {
- KeyValuePairList<long, int> result = new KeyValuePairList<long, int>();
- long previousLCN = 0;
- long clusterOffset = startClusterOffset;
- int clustersLeftToTranslate = clusterCount;
- bool translating = false;
- for(int index = 0; index < this.Count; index++)
- {
- DataRun run = this[index];
- long lcn = previousLCN + run.RunOffset;
- if (!translating)
- {
- if (clusterOffset >= run.RunLength)
- {
- clusterOffset -= run.RunLength;
- }
- else
- {
- translating = true;
-
- long startClusterLCN = lcn + clusterOffset;
- long clustersLeftInRun = run.RunLength - clusterOffset;
- int clustersTranslated = (int)Math.Min(clustersLeftToTranslate, clustersLeftInRun);
- result.Add(startClusterLCN, clustersTranslated);
- clustersLeftToTranslate -= clustersTranslated;
- if (clustersLeftToTranslate == 0)
- {
- break;
- }
- }
- }
- else
- {
- int clustersTranslated = (int)Math.Min(clustersLeftToTranslate, run.RunLength);
- result.Add(lcn, clustersTranslated);
- clustersLeftToTranslate -= clustersTranslated;
- if (clustersLeftToTranslate == 0)
- {
- break;
- }
- }
- previousLCN = lcn;
- }
- return result;
- }
- public long GetDataClusterLCN(long clusterVCN)
- {
- long previousLCN = 0;
- long clusterOffset = clusterVCN;
- foreach (DataRun run in this)
- {
- long lcn = previousLCN + run.RunOffset;
-
- if (clusterOffset >= run.RunLength)
- {
- clusterOffset = clusterOffset - run.RunLength;
- }
- else
- {
- return lcn + clusterOffset;
- }
- previousLCN = lcn;
- }
- throw new InvalidDataException("Invalid cluster VCN");
- }
- public override string ToString()
- {
- StringBuilder builder = new StringBuilder();
- long vcn = 0;
- for(int index = 0; index < this.Count; index++)
- {
- DataRun run = this[index];
- long absoluteLCN = GetDataClusterLCN(vcn);
- builder.AppendFormat("Data Run Number {0}: Absolute LCN: {1}, Length: {2}\n", index, absoluteLCN, run.RunLength);
- vcn += run.RunLength;
- }
- builder.AppendFormat("Number of clusters in sequence: {0}\n", DataClusterCount);
- return builder.ToString();
- }
- public long FirstDataRunLCN
- {
- get
- {
- if (this.Count > 0)
- {
- return this[0].RunOffset;
- }
- else
- {
- return -1;
- }
- }
- }
-
-
-
- public long LastDataRunStartLCN
- {
- get
- {
- if (this.Count > 0)
- {
- long clusterIndex = 0;
- for (int index = 0; index < this.Count - 1; index++)
- {
- DataRun run = this[index];
- clusterIndex += run.RunLength;
- }
- return GetDataClusterLCN(clusterIndex);
- }
- else
- {
- return -1;
- }
- }
- }
-
-
-
- public long DataLastLCN
- {
- get
- {
- return GetDataClusterLCN(this.DataClusterCount - 1);
- }
- }
-
-
- public long DataClusterCount
- {
- get
- {
- long result = 0;
- foreach (DataRun run in this)
- {
- result += run.RunLength;
- }
- return result;
- }
- }
- public int RecordLength
- {
- get
- {
- int dataRunRecordSequenceLength = 0;
- foreach (DataRun run in this)
- {
- dataRunRecordSequenceLength += run.RecordLength;
- }
- dataRunRecordSequenceLength += 1;
- return dataRunRecordSequenceLength;
- }
- }
- }
- }
|