TarArchiveReader.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.CoreModules.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. return ReadData(header.FileSize);
  84. }
  85. /// <summary>
  86. /// Read the next 512 byte chunk of data as a tar header.
  87. /// </summary>
  88. /// <returns>A tar header struct. null if we have reached the end of the archive.</returns>
  89. protected TarHeader ReadHeader()
  90. {
  91. byte[] header = m_br.ReadBytes(512);
  92. // If we've reached the end of the archive we'll be in null block territory, which means
  93. // the next byte will be 0
  94. if (header[0] == 0)
  95. return null;
  96. TarHeader tarHeader = new TarHeader();
  97. // If we're looking at a GNU tar long link then extract the long name and pull up the next header
  98. if (header[156] == (byte)'L')
  99. {
  100. int longNameLength = ConvertOctalBytesToDecimal(header, 124, 11);
  101. tarHeader.FilePath = m_asciiEncoding.GetString(ReadData(longNameLength));
  102. //m_log.DebugFormat("[TAR ARCHIVE READER]: Got long file name {0}", tarHeader.FilePath);
  103. header = m_br.ReadBytes(512);
  104. }
  105. else
  106. {
  107. tarHeader.FilePath = m_asciiEncoding.GetString(header, 0, 100);
  108. tarHeader.FilePath = tarHeader.FilePath.Trim(m_nullCharArray);
  109. //m_log.DebugFormat("[TAR ARCHIVE READER]: Got short file name {0}", tarHeader.FilePath);
  110. }
  111. tarHeader.FileSize = ConvertOctalBytesToDecimal(header, 124, 11);
  112. switch (header[156])
  113. {
  114. case 0:
  115. tarHeader.EntryType = TarEntryType.TYPE_NORMAL_FILE;
  116. break;
  117. case (byte)'0':
  118. tarHeader.EntryType = TarEntryType.TYPE_NORMAL_FILE;
  119. break;
  120. case (byte)'1':
  121. tarHeader.EntryType = TarEntryType.TYPE_HARD_LINK;
  122. break;
  123. case (byte)'2':
  124. tarHeader.EntryType = TarEntryType.TYPE_SYMBOLIC_LINK;
  125. break;
  126. case (byte)'3':
  127. tarHeader.EntryType = TarEntryType.TYPE_CHAR_SPECIAL;
  128. break;
  129. case (byte)'4':
  130. tarHeader.EntryType = TarEntryType.TYPE_BLOCK_SPECIAL;
  131. break;
  132. case (byte)'5':
  133. tarHeader.EntryType = TarEntryType.TYPE_DIRECTORY;
  134. break;
  135. case (byte)'6':
  136. tarHeader.EntryType = TarEntryType.TYPE_FIFO;
  137. break;
  138. case (byte)'7':
  139. tarHeader.EntryType = TarEntryType.TYPE_CONTIGUOUS_FILE;
  140. break;
  141. }
  142. return tarHeader;
  143. }
  144. /// <summary>
  145. /// Read data following a header
  146. /// </summary>
  147. /// <param name="fileSize"></param>
  148. /// <returns></returns>
  149. protected byte[] ReadData(int fileSize)
  150. {
  151. byte[] data = m_br.ReadBytes(fileSize);
  152. //m_log.DebugFormat("[TAR ARCHIVE READER]: fileSize {0}", fileSize);
  153. // Read the rest of the empty padding in the 512 byte block
  154. if (fileSize % 512 != 0)
  155. {
  156. int paddingLeft = 512 - (fileSize % 512);
  157. //m_log.DebugFormat("[TAR ARCHIVE READER]: Reading {0} padding bytes", paddingLeft);
  158. m_br.ReadBytes(paddingLeft);
  159. }
  160. return data;
  161. }
  162. public void Close()
  163. {
  164. m_br.Close();
  165. }
  166. /// <summary>
  167. /// Convert octal bytes to a decimal representation
  168. /// </summary>
  169. /// <param name="bytes"></param>
  170. /// <returns></returns>
  171. public static int ConvertOctalBytesToDecimal(byte[] bytes, int startIndex, int count)
  172. {
  173. string oString = m_asciiEncoding.GetString(bytes, startIndex, count);
  174. int d = 0;
  175. foreach (char c in oString)
  176. {
  177. d <<= 3;
  178. d |= c - '0';
  179. }
  180. return d;
  181. }
  182. }
  183. public class TarHeader
  184. {
  185. public string FilePath;
  186. public int FileSize;
  187. public TarArchiveReader.TarEntryType EntryType;
  188. }
  189. }