* Read all files from tar archive

* No reload functionality implemented yet
0.6.0-stable
Justin Clarke Casey 2008-05-30 15:18:40 +00:00
parent 9590e671e6
commit f26eeab3d4
2 changed files with 55 additions and 11 deletions

View File

@ -56,9 +56,13 @@ namespace OpenSim.Region.Environment.Modules.World.Archiver
// Just test for now by reading first file // Just test for now by reading first file
string filePath = "ERROR"; string filePath = "ERROR";
byte[] data = archive.Read(out filePath); byte[] data;
while ((data = archive.ReadEntry(out filePath)) != null)
{
m_log.DebugFormat("[ARCHIVER]: Successfully read {0} ({1} bytes) from archive {2}", filePath, data.Length, m_loadPath);
}
m_log.DebugFormat("[ARCHIVER]: Successfully read {0} ({1} bytes) from archive {2}", filePath, data.Length, m_loadPath); m_log.DebugFormat("[ARCHIVER]: Reached end of archive");
archive.Close(); archive.Close();
} }

View File

@ -27,8 +27,8 @@
using System; using System;
using System.IO; using System.IO;
//using System.Reflection; using System.Reflection;
//using log4net; using log4net;
namespace OpenSim.Region.Environment.Modules.World.Archiver namespace OpenSim.Region.Environment.Modules.World.Archiver
{ {
@ -37,9 +37,9 @@ namespace OpenSim.Region.Environment.Modules.World.Archiver
/// </summary> /// </summary>
public class TarArchiveReader public class TarArchiveReader
{ {
// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
protected static System.Text.ASCIIEncoding m_asciiEncoding = new System.Text.ASCIIEncoding(); protected static System.Text.ASCIIEncoding m_asciiEncoding = new System.Text.ASCIIEncoding();
/// <summary> /// <summary>
/// Binary reader for the underlying stream /// Binary reader for the underlying stream
@ -51,19 +51,59 @@ namespace OpenSim.Region.Environment.Modules.World.Archiver
m_br = new BinaryReader(new FileStream(archivePath, FileMode.Open)); m_br = new BinaryReader(new FileStream(archivePath, FileMode.Open));
} }
public byte[] Read(out string filePath) /// <summary>
/// Are we at the end of the archive?
/// </summary>
/// <returns></returns>
public bool AtEof()
{ {
TarHeader header = ReadHeader(); // If we've reached the end of the archive we'll be in null block territory, which means
// the next byte will be 0
if (m_br.PeekChar() == 0)
return true;
return false;
}
/// <summary>
/// Read the next entry in the tar file.
/// </summary>
/// <param name="filePath"></param>
/// <returns>the data for the entry. Returns null if there are no more entries</returns>
public byte[] ReadEntry(out string filePath)
{
filePath = String.Empty;
if (AtEof())
return null;
TarHeader header = ReadHeader();
filePath = header.FilePath; filePath = header.FilePath;
return m_br.ReadBytes(header.FileSize); byte[] data = m_br.ReadBytes(header.FileSize);
m_log.DebugFormat("[TAR ARCHIVE READER]: filePath {0}, fileSize {1}", filePath, header.FileSize);
// Read the rest of the empty padding in the 512 byte block
if (header.FileSize % 512 != 0)
{
int paddingLeft = 512 - (header.FileSize % 512);
m_log.DebugFormat("[TAR ARCHIVE READER]: Reading {0} padding bytes", paddingLeft);
m_br.ReadBytes(paddingLeft);
}
return data;
} }
/// <summary> /// <summary>
/// Read the next 512 byte chunk of data as a tar header. /// Read the next 512 byte chunk of data as a tar header.
/// This method assumes we are not at the end of the archive
/// </summary> /// </summary>
/// <returns>A tar header struct</returns> /// <returns>A tar header struct.</returns>
protected TarHeader ReadHeader() protected TarHeader ReadHeader()
{ {
TarHeader tarHeader = new TarHeader(); TarHeader tarHeader = new TarHeader();
byte[] header = m_br.ReadBytes(512); byte[] header = m_br.ReadBytes(512);