TarArchiveReader.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 OpenSimulator 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.Framework.Serialization
  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 static char[] m_nullCharArray = new char[] { '\0' };
  61. /// <summary>
  62. /// Used to trim off space chars
  63. /// </summary>
  64. protected static char[] m_spaceCharArray = new char[] { ' ' };
  65. /// <summary>
  66. /// Generate a tar reader which reads from the given stream.
  67. /// </summary>
  68. /// <param name="s"></param>
  69. public TarArchiveReader(Stream s)
  70. {
  71. m_br = new BinaryReader(s);
  72. }
  73. /// <summary>
  74. /// Read the next entry in the tar file.
  75. /// </summary>
  76. /// <param name="filePath"></param>
  77. /// <returns>the data for the entry. Returns null if there are no more entries</returns>
  78. public byte[] ReadEntry(out string filePath, out TarEntryType entryType)
  79. {
  80. filePath = String.Empty;
  81. entryType = TarEntryType.TYPE_UNKNOWN;
  82. TarHeader header = ReadHeader();
  83. if (null == header)
  84. return null;
  85. entryType = header.EntryType;
  86. filePath = header.FilePath;
  87. return ReadData(header.FileSize);
  88. }
  89. /// <summary>
  90. /// Read the next 512 byte chunk of data as a tar header.
  91. /// </summary>
  92. /// <returns>A tar header struct. null if we have reached the end of the archive.</returns>
  93. protected TarHeader ReadHeader()
  94. {
  95. byte[] header = m_br.ReadBytes(512);
  96. // If there are no more bytes in the stream, return null header
  97. if (header.Length == 0)
  98. return null;
  99. // If we've reached the end of the archive we'll be in null block territory, which means
  100. // the next byte will be 0
  101. if (header[0] == 0)
  102. return null;
  103. TarHeader tarHeader = new TarHeader();
  104. // If we're looking at a GNU tar long link then extract the long name and pull up the next header
  105. if (header[156] == (byte)'L')
  106. {
  107. int longNameLength = ConvertOctalBytesToDecimal(header, 124, 11);
  108. tarHeader.FilePath = m_asciiEncoding.GetString(ReadData(longNameLength));
  109. //m_log.DebugFormat("[TAR ARCHIVE READER]: Got long file name {0}", tarHeader.FilePath);
  110. header = m_br.ReadBytes(512);
  111. }
  112. else
  113. {
  114. tarHeader.FilePath = m_asciiEncoding.GetString(header, 0, 100);
  115. tarHeader.FilePath = tarHeader.FilePath.Trim(m_nullCharArray);
  116. //m_log.DebugFormat("[TAR ARCHIVE READER]: Got short file name {0}", tarHeader.FilePath);
  117. }
  118. tarHeader.FileSize = ConvertOctalBytesToDecimal(header, 124, 11);
  119. switch (header[156])
  120. {
  121. case 0:
  122. tarHeader.EntryType = TarEntryType.TYPE_NORMAL_FILE;
  123. break;
  124. case (byte)'0':
  125. tarHeader.EntryType = TarEntryType.TYPE_NORMAL_FILE;
  126. break;
  127. case (byte)'1':
  128. tarHeader.EntryType = TarEntryType.TYPE_HARD_LINK;
  129. break;
  130. case (byte)'2':
  131. tarHeader.EntryType = TarEntryType.TYPE_SYMBOLIC_LINK;
  132. break;
  133. case (byte)'3':
  134. tarHeader.EntryType = TarEntryType.TYPE_CHAR_SPECIAL;
  135. break;
  136. case (byte)'4':
  137. tarHeader.EntryType = TarEntryType.TYPE_BLOCK_SPECIAL;
  138. break;
  139. case (byte)'5':
  140. tarHeader.EntryType = TarEntryType.TYPE_DIRECTORY;
  141. break;
  142. case (byte)'6':
  143. tarHeader.EntryType = TarEntryType.TYPE_FIFO;
  144. break;
  145. case (byte)'7':
  146. tarHeader.EntryType = TarEntryType.TYPE_CONTIGUOUS_FILE;
  147. break;
  148. }
  149. return tarHeader;
  150. }
  151. /// <summary>
  152. /// Read data following a header
  153. /// </summary>
  154. /// <param name="fileSize"></param>
  155. /// <returns></returns>
  156. protected byte[] ReadData(int fileSize)
  157. {
  158. byte[] data = m_br.ReadBytes(fileSize);
  159. //m_log.DebugFormat("[TAR ARCHIVE READER]: fileSize {0}", fileSize);
  160. // Read the rest of the empty padding in the 512 byte block
  161. if (fileSize % 512 != 0)
  162. {
  163. int paddingLeft = 512 - (fileSize % 512);
  164. //m_log.DebugFormat("[TAR ARCHIVE READER]: Reading {0} padding bytes", paddingLeft);
  165. m_br.ReadBytes(paddingLeft);
  166. }
  167. return data;
  168. }
  169. public void Close()
  170. {
  171. m_br.Close();
  172. }
  173. /// <summary>
  174. /// Convert octal bytes to a decimal representation
  175. /// </summary>
  176. /// <param name="bytes"></param>
  177. /// <returns></returns>
  178. public static int ConvertOctalBytesToDecimal(byte[] bytes, int startIndex, int count)
  179. {
  180. // Trim leading white space: ancient tars do that instead
  181. // of leading 0s :-( don't ask. really.
  182. string oString = m_asciiEncoding.GetString(bytes, startIndex, count).TrimStart(m_spaceCharArray);
  183. int d = 0;
  184. foreach (char c in oString)
  185. {
  186. d <<= 3;
  187. d |= c - '0';
  188. }
  189. return d;
  190. }
  191. }
  192. public class TarHeader
  193. {
  194. public string FilePath;
  195. public int FileSize;
  196. public TarArchiveReader.TarEntryType EntryType;
  197. }
  198. }