TarArchiveReader.cs 8.1 KB

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