Skip to content

Commit

Permalink
File Format
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed May 19, 2011
1 parent 50ae9d1 commit 33cbc69
Show file tree
Hide file tree
Showing 65 changed files with 13,050 additions and 0 deletions.
20 changes: 20 additions & 0 deletions 文件格式研究/MTF/BackupReader/BackupReader.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BackupReader", "BackupReader\BackupReader.csproj", "{CAAE6D2F-69A2-46E0-A310-BF1DA11B19B9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CAAE6D2F-69A2-46E0-A310-BF1DA11B19B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CAAE6D2F-69A2-46E0-A310-BF1DA11B19B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CAAE6D2F-69A2-46E0-A310-BF1DA11B19B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CAAE6D2F-69A2-46E0-A310-BF1DA11B19B9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Binary file not shown.
102 changes: 102 additions & 0 deletions 文件格式研究/MTF/BackupReader/BackupReader/BackupReader.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{CAAE6D2F-69A2-46E0-A310-BF1DA11B19B9}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>BackupReader</RootNamespace>
<AssemblyName>BackupReader</AssemblyName>
<ApplicationIcon>backup.ico</ApplicationIcon>
<SignManifests>false</SignManifests>
<TargetZone>LocalIntranet</TargetZone>
<GenerateManifests>false</GenerateManifests>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>2.0</OldToolsVersion>
<ApplicationRevision>1</ApplicationRevision>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CBackupReader.cs" />
<Compile Include="CBackupStream.cs" />
<Compile Include="CCatalog.cs" />
<Compile Include="CDataStream.cs" />
<Compile Include="CDescriptorBlock.cs" />
<Compile Include="COSSpecificData.cs" />
<Compile Include="CSupport.cs" />
<Compile Include="frmMain.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="frmMain.Designer.cs">
<DependentUpon>frmMain.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="frmMain.resx">
<SubType>Designer</SubType>
<DependentUpon>frmMain.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<BaseApplicationManifest Include="Properties\app.manifest" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
</ItemGroup>
<ItemGroup>
<Content Include="backup.ico" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishUrlHistory>publish\</PublishUrlHistory>
<InstallUrlHistory>
</InstallUrlHistory>
<SupportUrlHistory>
</SupportUrlHistory>
<UpdateUrlHistory>
</UpdateUrlHistory>
<BootstrapperUrlHistory>
</BootstrapperUrlHistory>
<FallbackCulture>en-US</FallbackCulture>
<VerifyUploadedFiles>false</VerifyUploadedFiles>
</PropertyGroup>
</Project>
Binary file not shown.
197 changes: 197 additions & 0 deletions 文件格式研究/MTF/BackupReader/BackupReader/CBackupReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@

namespace BackupReader
{
/// <summary>
/// Represents a backup file reader.
/// </summary>
class CBackupReader
{
private long mLastPos;
private long mIncrement;
private bool mCancel;
private CBackupStream mStream;

/// <summary>
/// Provides an event handler for the OnProgressChange event.
/// Progress is an integer between 1-100, representing the progress of
/// the catalog read operation.
/// </summary>
public delegate void ProgressChange(int Progress);
/// <summary>
/// Occurs when the catalog read progress changes by 1%.
/// </summary>
public event ProgressChange OnProgressChange;

/// <summary>
/// Returns the underlying stream.
/// </summary>
public CBackupStream Stream
{
get { return mStream; }
}

/// <summary>
/// Reads the entire backup file and returns a root catalog node.
/// The root node contains backup sets/volumes/directories/files
/// as child nodes.
/// </summary>
public CCatalogNode ReadCatalog()
{
// Set to true to cancel reading
mCancel = false;

// Read the media header
CTapeHeaderDescriptorBlock tape = (CTapeHeaderDescriptorBlock)mStream.ReadDBLK();
// Read soft file mark
CSoftFilemarkDescriptorBlock file = (CSoftFilemarkDescriptorBlock)mStream.ReadDBLK();

// Create the root catalog node
CCatalogNode node = new CCatalogNode(tape.MediaName, ENodeType.Root, 0);
CCatalogNode nLastSet = null;
CCatalogNode nLastVolume = null;
CCatalogNode nLastDir = null;

// Get next block type
EBlockType bt = mStream.PeekNextBlockType();
while ((bt != EBlockType.MTF_EOTM) && (bt != 0) && (mCancel == false))
{
// Read next block
CDescriptorBlock block = mStream.ReadDBLK();

// Add to catalog
if (bt == EBlockType.MTF_SSET)
{
CStartOfDataSetDescriptorBlock sset = (CStartOfDataSetDescriptorBlock)block;
CCatalogNode cnode = node.AddSet("Set: " + sset.DataSetNumber + " - " + sset.DataSetName, block.StartPosition);
nLastSet = cnode;
}
else if (bt == EBlockType.MTF_VOLB)
{
CVolumeDescriptorBlock vol = (CVolumeDescriptorBlock)block;
CCatalogNode cnode = nLastSet.AddVolume(vol.DeviceName, block.StartPosition);
nLastVolume = cnode;
}
else if (bt == EBlockType.MTF_DIRB)
{
CDirectoryDescriptorBlock dir = (CDirectoryDescriptorBlock)block;
// Check if the directory name is contained in a data stream
CCatalogNode cnode = null;
if ((dir.DIRBAttributes & EDIRBAttributes.DIRB_PATH_IN_STREAM_BIT) != 0)
{
foreach (CDataStream data in dir.Streams)
{
if (data.Header.StreamID == "PNAM")
{
if (dir.StringType == EStringType.ANSI)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
string str = encoding.GetString(data.Data);
str = str.Substring(0, str.Length - 1);
cnode = nLastVolume.AddFolder(str, block.StartPosition);
}
else if (dir.StringType == EStringType.Unicode)
{
System.Text.UnicodeEncoding encoding = new System.Text.UnicodeEncoding();
string str = encoding.GetString(data.Data);
str = str.Substring(0, str.Length - 1);
cnode = nLastVolume.AddFolder(str, block.StartPosition);
}

}
}
}
else
cnode = nLastVolume.AddFolder(dir.DirectoryName.Substring(0, dir.DirectoryName.Length - 1), block.StartPosition);

if (cnode != null) nLastDir = cnode;
}
else if (bt == EBlockType.MTF_FILE)
{
CFileDescriptorBlock fil = (CFileDescriptorBlock)block;
// Check if the file name is contained in a data stream
CCatalogNode cnode = null;
if ((fil.FileAttributes & EFileAttributes.FILE_NAME_IN_STREAM_BIT) != 0)
{
foreach (CDataStream data in fil.Streams)
{
if (data.Header.StreamID == "FNAM")
{
if (fil.StringType == EStringType.ANSI)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
string str = encoding.GetString(data.Data);
cnode = nLastDir.AddFile(str, block.StartPosition);
}
else if (fil.StringType == EStringType.Unicode)
{
System.Text.UnicodeEncoding encoding = new System.Text.UnicodeEncoding();
string str = encoding.GetString(data.Data);
cnode = nLastDir.AddFile(str, block.StartPosition);
}

}
}
}
else
cnode = nLastDir.AddFile(fil.FileName, block.StartPosition);
}


// Get next block type
bt = mStream.PeekNextBlockType();

// Check progress
if (mStream.BaseStream.Position > mLastPos + mIncrement)
{
mLastPos = mStream.BaseStream.Position;
OnProgressChange((int)((float)mLastPos / (float)mStream.BaseStream.Length * 100.0f));
}
}

return node;
}

/// <summary>
/// Stops reading the catalog. The nodes that has already been read will still be available.
/// </summary>
public void CancelRead()
{
mCancel = true;
}

/// <summary>
/// Opens a backup file.
/// </summary>
public void Open(string Filename)
{
mStream = new CBackupStream(Filename);
mIncrement = mStream.BaseStream.Length / 100;
mLastPos = 0;
mCancel = false;
}

/// <summary>
/// Closes the backup file.
/// </summary>
public void Close()
{
mStream.Close();
}

public CBackupReader()
{

}

public CBackupReader(string Filename)
{
Open(Filename);
}

~CBackupReader()
{
Close();
}
}

}
Loading

0 comments on commit 33cbc69

Please sign in to comment.