* 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) if (TarArchiveReader.TarEntryType.TYPE_DIRECTORY == entryType)
{ {
m_log.WarnFormat("[ARCHIVER]: Ignoring directory entry {0}", m_log.WarnFormat(
filePath); "[ARCHIVER]: Ignoring directory entry {0}", filePath);
} }
else if (filePath.StartsWith(ArchiveConstants.OBJECTS_PATH)) else if (filePath.StartsWith(ArchiveConstants.OBJECTS_PATH))
{ {

View File

@ -27,7 +27,9 @@
using System; using System;
using System.IO; using System.IO;
using System.Reflection;
using System.Text; using System.Text;
using log4net;
namespace OpenSim.Region.CoreModules.World.Archiver namespace OpenSim.Region.CoreModules.World.Archiver
{ {
@ -36,7 +38,7 @@ namespace OpenSim.Region.CoreModules.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);
public enum TarEntryType public enum TarEntryType
{ {
@ -120,8 +122,20 @@ namespace OpenSim.Region.CoreModules.World.Archiver
TarHeader tarHeader = new TarHeader(); TarHeader tarHeader = new TarHeader();
tarHeader.FilePath = m_asciiEncoding.GetString(header, 0, 100); // If we're looking at a GNU tar long link then extract the long name and pull up the next header
tarHeader.FilePath = tarHeader.FilePath.Trim(m_nullCharArray); 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); tarHeader.FileSize = ConvertOctalBytesToDecimal(header, 124, 11);
switch (header[156]) switch (header[156])

View File

@ -87,84 +87,7 @@ namespace OpenSim.Region.CoreModules.World.Archiver
foreach (string filePath in m_files.Keys) foreach (string filePath in m_files.Keys)
{ {
byte[] header = new byte[512]; WriteFile(bw, filePath, m_files[filePath]);
byte[] data = m_files[filePath];
// file path field (100)
byte[] nameBytes = m_asciiEncoding.GetBytes(filePath);
int nameSize = (nameBytes.Length >= 100) ? 100 : nameBytes.Length;
Array.Copy(nameBytes, header, nameSize);
// file mode (8)
byte[] modeBytes = m_asciiEncoding.GetBytes("0000777");
Array.Copy(modeBytes, 0, header, 100, 7);
// owner user id (8)
byte[] ownerIdBytes = m_asciiEncoding.GetBytes("0000764");
Array.Copy(ownerIdBytes, 0, header, 108, 7);
// group user id (8)
byte[] groupIdBytes = m_asciiEncoding.GetBytes("0000764");
Array.Copy(groupIdBytes, 0, header, 116, 7);
// file size in bytes (12)
int fileSize = data.Length;
//m_log.DebugFormat("[TAR ARCHIVE WRITER]: File size of {0} is {1}", filePath, fileSize);
byte[] fileSizeBytes = ConvertDecimalToPaddedOctalBytes(fileSize, 11);
Array.Copy(fileSizeBytes, 0, header, 124, 11);
// last modification time (12)
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;
}
Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 329, 7);
Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 337, 7);
// check sum for header block (8) [calculated last]
Array.Copy(m_asciiEncoding.GetBytes(" "), 0, header, 148, 8);
int checksum = 0;
foreach (byte b in header)
{
checksum += b;
}
//m_log.DebugFormat("[TAR ARCHIVE WRITER]: Decimal header checksum is {0}", checksum);
byte[] checkSumBytes = ConvertDecimalToPaddedOctalBytes(checksum, 6);
Array.Copy(checkSumBytes, 0, header, 148, 6);
header[154] = 0;
// Write out header
bw.Write(header);
// Write out data
bw.Write(data);
if (data.Length % 512 != 0)
{
int paddingRequired = 512 - (data.Length % 512);
//m_log.DebugFormat("[TAR ARCHIVE WRITER]: Padding data with {0} bytes", paddingRequired);
byte[] padding = new byte[paddingRequired];
bw.Write(padding);
}
} }
//m_log.Debug("[TAR ARCHIVE WRITER]: Writing final consecutive 0 blocks"); //m_log.Debug("[TAR ARCHIVE WRITER]: Writing final consecutive 0 blocks");
@ -196,5 +119,109 @@ namespace OpenSim.Region.CoreModules.World.Archiver
return oBytes; 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];
// file path field (100)
byte[] nameBytes = m_asciiEncoding.GetBytes(filePath);
int nameSize = (nameBytes.Length >= 100) ? 100 : nameBytes.Length;
Array.Copy(nameBytes, header, nameSize);
// file mode (8)
byte[] modeBytes = m_asciiEncoding.GetBytes("0000777");
Array.Copy(modeBytes, 0, header, 100, 7);
// owner user id (8)
byte[] ownerIdBytes = m_asciiEncoding.GetBytes("0000764");
Array.Copy(ownerIdBytes, 0, header, 108, 7);
// group user id (8)
byte[] groupIdBytes = m_asciiEncoding.GetBytes("0000764");
Array.Copy(groupIdBytes, 0, header, 116, 7);
// file size in bytes (12)
int fileSize = data.Length;
//m_log.DebugFormat("[TAR ARCHIVE WRITER]: File size of {0} is {1}", filePath, fileSize);
byte[] fileSizeBytes = ConvertDecimalToPaddedOctalBytes(fileSize, 11);
Array.Copy(fileSizeBytes, 0, header, 124, 11);
// last modification time (12)
byte[] lastModTimeBytes = m_asciiEncoding.GetBytes("11017037332");
Array.Copy(lastModTimeBytes, 0, header, 136, 11);
// 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);
// check sum for header block (8) [calculated last]
Array.Copy(m_asciiEncoding.GetBytes(" "), 0, header, 148, 8);
int checksum = 0;
foreach (byte b in header)
{
checksum += b;
}
//m_log.DebugFormat("[TAR ARCHIVE WRITER]: Decimal header checksum is {0}", checksum);
byte[] checkSumBytes = ConvertDecimalToPaddedOctalBytes(checksum, 6);
Array.Copy(checkSumBytes, 0, header, 148, 6);
header[154] = 0;
// Write out header
bw.Write(header);
// Write out data
bw.Write(data);
if (data.Length % 512 != 0)
{
int paddingRequired = 512 - (data.Length % 512);
//m_log.DebugFormat("[TAR ARCHIVE WRITER]: Padding data with {0} bytes", paddingRequired);
byte[] padding = new byte[paddingRequired];
bw.Write(padding);
}
}
} }
} }