TarArchiveReader.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.IO;
  29. //using System.Reflection;
  30. using System.Text;
  31. //using log4net;
  32. namespace OpenSim.Region.Environment.Modules.World.Archiver
  33. {
  34. /// <summary>
  35. /// Temporary code to do the bare minimum required to read a tar archive for our purposes
  36. /// </summary>
  37. public class TarArchiveReader
  38. {
  39. //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. protected static ASCIIEncoding m_asciiEncoding = new ASCIIEncoding();
  41. /// <summary>
  42. /// Binary reader for the underlying stream
  43. /// </summary>
  44. protected BinaryReader m_br;
  45. /// <summary>
  46. /// Used to trim off null chars
  47. /// </summary>
  48. protected char[] m_nullCharArray = new char[] { '\0' };
  49. /// <summary>
  50. /// Generate a tar reader which reads from the given stream.
  51. /// </summary>
  52. /// <param name="s"></param>
  53. public TarArchiveReader(Stream s)
  54. {
  55. m_br = new BinaryReader(s);
  56. }
  57. /// <summary>
  58. /// Read the next entry in the tar file.
  59. /// </summary>
  60. /// <param name="filePath"></param>
  61. /// <returns>the data for the entry. Returns null if there are no more entries</returns>
  62. public byte[] ReadEntry(out string filePath)
  63. {
  64. filePath = String.Empty;
  65. TarHeader header = ReadHeader();
  66. if (null == header)
  67. return null;
  68. filePath = header.FilePath;
  69. byte[] data = m_br.ReadBytes(header.FileSize);
  70. //m_log.DebugFormat("[TAR ARCHIVE READER]: filePath {0}, fileSize {1}", filePath, header.FileSize);
  71. // Read the rest of the empty padding in the 512 byte block
  72. if (header.FileSize % 512 != 0)
  73. {
  74. int paddingLeft = 512 - (header.FileSize % 512);
  75. //m_log.DebugFormat("[TAR ARCHIVE READER]: Reading {0} padding bytes", paddingLeft);
  76. m_br.ReadBytes(paddingLeft);
  77. }
  78. return data;
  79. }
  80. /// <summary>
  81. /// Read the next 512 byte chunk of data as a tar header.
  82. /// </summary>
  83. /// <returns>A tar header struct. null if we have reached the end of the archive.</returns>
  84. protected TarHeader ReadHeader()
  85. {
  86. byte[] header = m_br.ReadBytes(512);
  87. // If we've reached the end of the archive we'll be in null block territory, which means
  88. // the next byte will be 0
  89. if (header[0] == 0)
  90. return null;
  91. TarHeader tarHeader = new TarHeader();
  92. tarHeader.FilePath = m_asciiEncoding.GetString(header, 0, 100);
  93. tarHeader.FilePath = tarHeader.FilePath.Trim(m_nullCharArray);
  94. tarHeader.FileSize = ConvertOctalBytesToDecimal(header, 124, 11);
  95. return tarHeader;
  96. }
  97. public void Close()
  98. {
  99. m_br.Close();
  100. }
  101. /// <summary>
  102. /// Convert octal bytes to a decimal representation
  103. /// </summary>
  104. /// <param name="bytes"></param>
  105. /// <returns></returns>
  106. public static int ConvertOctalBytesToDecimal(byte[] bytes, int startIndex, int count)
  107. {
  108. string oString = m_asciiEncoding.GetString(bytes, startIndex, count);
  109. int d = 0;
  110. foreach (char c in oString)
  111. {
  112. d <<= 3;
  113. d |= c - '0';
  114. }
  115. return d;
  116. }
  117. }
  118. public class TarHeader
  119. {
  120. public string FilePath;
  121. public int FileSize;
  122. }
  123. }