* Add gnu tar format long file name support to tar reading and writing.

* Not actually tested yet though existing code which doesn't require long file names looks fine
0.6.4-rc1
Justin Clarke Casey 2009-03-04 18:33:05 +00:00
parent b2135c2029
commit b57497fd41
3 changed files with 126 additions and 85 deletions

View File

@ -111,8 +111,8 @@ namespace OpenSim.Region.CoreModules.World.Archiver
if (TarArchiveReader.TarEntryType.TYPE_DIRECTORY == entryType)
{
m_log.WarnFormat("[ARCHIVER]: Ignoring directory entry {0}",
filePath);
m_log.WarnFormat(
"[ARCHIVER]: Ignoring directory entry {0}", filePath);
}
else if (filePath.StartsWith(ArchiveConstants.OBJECTS_PATH))
{

View File

@ -27,7 +27,9 @@
using System;
using System.IO;
using System.Reflection;
using System.Text;
using log4net;
namespace OpenSim.Region.CoreModules.World.Archiver
{
@ -36,7 +38,7 @@ namespace OpenSim.Region.CoreModules.World.Archiver
/// </summary>
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);
public enum TarEntryType
{
@ -120,8 +122,20 @@ namespace OpenSim.Region.CoreModules.World.Archiver
TarHeader tarHeader = new TarHeader();
// If we're looking at a GNU tar long link then extract the long name and pull up the next header
if (header[156] == (byte)'L')
{
int longNameLength = ConvertOctalBytesToDecimal(header, 124, 11);
tarHeader.FilePath = m_asciiEncoding.GetString(m_br.ReadBytes(longNameLength));
m_log.DebugFormat("[TAR ARCHIVE READER]: Got long file name {0}", tarHeader.FilePath);
header = m_br.ReadBytes(512);
}
else
{
tarHeader.FilePath = m_asciiEncoding.GetString(header, 0, 100);
tarHeader.FilePath = tarHeader.FilePath.Trim(m_nullCharArray);
}
tarHeader.FileSize = ConvertOctalBytesToDecimal(header, 124, 11);
switch (header[156])

View File

@ -86,9 +86,74 @@ namespace OpenSim.Region.CoreModules.World.Archiver
BinaryWriter bw = new BinaryWriter(s);
foreach (string filePath in m_files.Keys)
{
WriteFile(bw, filePath, m_files[filePath]);
}
//m_log.Debug("[TAR ARCHIVE WRITER]: Writing final consecutive 0 blocks");
// Write two consecutive 0 blocks to end the archive
byte[] finalZeroPadding = new byte[1024];
bw.Write(finalZeroPadding);
bw.Flush();
bw.Close();
}
public static byte[] ConvertDecimalToPaddedOctalBytes(int d, int padding)
{
string oString = "";
while (d > 0)
{
oString = Convert.ToString((byte)'0' + d & 7) + oString;
d >>= 3;
}
while (oString.Length < padding)
{
oString = "0" + oString;
}
byte[] oBytes = m_asciiEncoding.GetBytes(oString);
return oBytes;
}
/// <summary>
/// Write a particular file of data
/// </summary>
/// <param name="filePath"></param>
/// <param name="data"></param>
protected void WriteFile(BinaryWriter bw, string filePath, byte[] data)
{
if (filePath.Length > 100)
WriteEntry(bw, "././@LongLink", m_asciiEncoding.GetBytes(filePath), 'L');
char fileType;
if (filePath.EndsWith("/"))
{
fileType = '5';
}
else
{
fileType = '0';
}
WriteEntry(bw, filePath, data, fileType);
}
/// <summary>
/// Write a particular file of data
/// </summary>
/// <param name="bw"></param>
/// <param name="filePath"></param>
/// <param name="data"></param>
/// <param name="fileType"></param>
protected void WriteEntry(BinaryWriter bw, string filePath, byte[] data, char fileType)
{
byte[] header = new byte[512];
byte[] data = m_files[filePath];
// file path field (100)
byte[] nameBytes = m_asciiEncoding.GetBytes(filePath);
@ -119,16 +184,8 @@ namespace OpenSim.Region.CoreModules.World.Archiver
byte[] lastModTimeBytes = m_asciiEncoding.GetBytes("11017037332");
Array.Copy(lastModTimeBytes, 0, header, 136, 11);
// link indicator (1)
//header[156] = m_asciiEncoding.GetBytes("0")[0];
if (filePath.EndsWith("/"))
{
header[156] = m_asciiEncoding.GetBytes("5")[0];
}
else
{
header[156] = 0;
}
// entry type indicator (1)
header[156] = m_asciiEncoding.GetBytes(new char[] { fileType })[0];
Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 329, 7);
Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 337, 7);
@ -166,35 +223,5 @@ namespace OpenSim.Region.CoreModules.World.Archiver
bw.Write(padding);
}
}
//m_log.Debug("[TAR ARCHIVE WRITER]: Writing final consecutive 0 blocks");
// Write two consecutive 0 blocks to end the archive
byte[] finalZeroPadding = new byte[1024];
bw.Write(finalZeroPadding);
bw.Flush();
bw.Close();
}
public static byte[] ConvertDecimalToPaddedOctalBytes(int d, int padding)
{
string oString = "";
while (d > 0)
{
oString = Convert.ToString((byte)'0' + d & 7) + oString;
d >>= 3;
}
while (oString.Length < padding)
{
oString = "0" + oString;
}
byte[] oBytes = m_asciiEncoding.GetBytes(oString);
return oBytes;
}
}
}