TarArchiveReader.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. public enum TarEntryType
  41. {
  42. TYPE_UNKNOWN = 0,
  43. TYPE_NORMAL_FILE = 1,
  44. TYPE_HARD_LINK = 2,
  45. TYPE_SYMBOLIC_LINK = 3,
  46. TYPE_CHAR_SPECIAL = 4,
  47. TYPE_BLOCK_SPECIAL = 5,
  48. TYPE_DIRECTORY = 6,
  49. TYPE_FIFO = 7,
  50. TYPE_CONTIGUOUS_FILE = 8,
  51. }
  52. protected static ASCIIEncoding m_asciiEncoding = new ASCIIEncoding();
  53. /// <summary>
  54. /// Binary reader for the underlying stream
  55. /// </summary>
  56. protected BinaryReader m_br;
  57. /// <summary>
  58. /// Used to trim off null chars
  59. /// </summary>
  60. protected char[] m_nullCharArray = new char[] { '\0' };
  61. /// <summary>
  62. /// Generate a tar reader which reads from the given stream.
  63. /// </summary>
  64. /// <param name="s"></param>
  65. public TarArchiveReader(Stream s)
  66. {
  67. m_br = new BinaryReader(s);
  68. }
  69. /// <summary>
  70. /// Read the next entry in the tar file.
  71. /// </summary>
  72. /// <param name="filePath"></param>
  73. /// <returns>the data for the entry. Returns null if there are no more entries</returns>
  74. public byte[] ReadEntry(out string filePath, out TarEntryType entryType)
  75. {
  76. filePath = String.Empty;
  77. entryType = TarEntryType.TYPE_UNKNOWN;
  78. TarHeader header = ReadHeader();
  79. if (null == header)
  80. return null;
  81. entryType = header.EntryType;
  82. filePath = header.FilePath;
  83. byte[] data = m_br.ReadBytes(header.FileSize);
  84. //m_log.DebugFormat("[TAR ARCHIVE READER]: filePath {0}, fileSize {1}", filePath, header.FileSize);
  85. // Read the rest of the empty padding in the 512 byte block
  86. if (header.FileSize % 512 != 0)
  87. {
  88. int paddingLeft = 512 - (header.FileSize % 512);
  89. //m_log.DebugFormat("[TAR ARCHIVE READER]: Reading {0} padding bytes", paddingLeft);
  90. m_br.ReadBytes(paddingLeft);
  91. }
  92. return data;
  93. }
  94. /// <summary>
  95. /// Read the next 512 byte chunk of data as a tar header.
  96. /// </summary>
  97. /// <returns>A tar header struct. null if we have reached the end of the archive.</returns>
  98. protected TarHeader ReadHeader()
  99. {
  100. byte[] header = m_br.ReadBytes(512);
  101. // If we've reached the end of the archive we'll be in null block territory, which means
  102. // the next byte will be 0
  103. if (header[0] == 0)
  104. return null;
  105. TarHeader tarHeader = new TarHeader();
  106. tarHeader.FilePath = m_asciiEncoding.GetString(header, 0, 100);
  107. tarHeader.FilePath = tarHeader.FilePath.Trim(m_nullCharArray);
  108. tarHeader.FileSize = ConvertOctalBytesToDecimal(header, 124, 11);
  109. switch (header[156])
  110. {
  111. case 0:
  112. tarHeader.EntryType = TarEntryType.TYPE_NORMAL_FILE;
  113. break;
  114. case (byte)'0':
  115. tarHeader.EntryType = TarEntryType.TYPE_NORMAL_FILE;
  116. break;
  117. case (byte)'1':
  118. tarHeader.EntryType = TarEntryType.TYPE_HARD_LINK;
  119. break;
  120. case (byte)'2':
  121. tarHeader.EntryType = TarEntryType.TYPE_SYMBOLIC_LINK;
  122. break;
  123. case (byte)'3':
  124. tarHeader.EntryType = TarEntryType.TYPE_CHAR_SPECIAL;
  125. break;
  126. case (byte)'4':
  127. tarHeader.EntryType = TarEntryType.TYPE_BLOCK_SPECIAL;
  128. break;
  129. case (byte)'5':
  130. tarHeader.EntryType = TarEntryType.TYPE_DIRECTORY;
  131. break;
  132. case (byte)'6':
  133. tarHeader.EntryType = TarEntryType.TYPE_FIFO;
  134. break;
  135. case (byte)'7':
  136. tarHeader.EntryType = TarEntryType.TYPE_CONTIGUOUS_FILE;
  137. break;
  138. }
  139. return tarHeader;
  140. }
  141. public void Close()
  142. {
  143. m_br.Close();
  144. }
  145. /// <summary>
  146. /// Convert octal bytes to a decimal representation
  147. /// </summary>
  148. /// <param name="bytes"></param>
  149. /// <returns></returns>
  150. public static int ConvertOctalBytesToDecimal(byte[] bytes, int startIndex, int count)
  151. {
  152. string oString = m_asciiEncoding.GetString(bytes, startIndex, count);
  153. int d = 0;
  154. foreach (char c in oString)
  155. {
  156. d <<= 3;
  157. d |= c - '0';
  158. }
  159. return d;
  160. }
  161. }
  162. public class TarHeader
  163. {
  164. public string FilePath;
  165. public int FileSize;
  166. public TarArchiveReader.TarEntryType EntryType;
  167. }
  168. }