TarArchiveWriter.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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.Collections.Generic;
  29. using System.IO;
  30. using System.Text;
  31. namespace OpenSim.Framework.Serialization
  32. {
  33. /// <summary>
  34. /// Temporary code to produce a tar archive in tar v7 format
  35. /// </summary>
  36. public class TarArchiveWriter
  37. {
  38. //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  39. protected static ASCIIEncoding m_asciiEncoding = new ASCIIEncoding();
  40. /// <summary>
  41. /// Binary writer for the underlying stream
  42. /// </summary>
  43. protected BinaryWriter m_bw;
  44. public TarArchiveWriter(Stream s)
  45. {
  46. m_bw = new BinaryWriter(s);
  47. }
  48. /// <summary>
  49. /// Write a directory entry to the tar archive. We can only handle one path level right now!
  50. /// </summary>
  51. /// <param name="dirName"></param>
  52. public void WriteDir(string dirName)
  53. {
  54. // Directories are signalled by a final /
  55. if (!dirName.EndsWith("/"))
  56. dirName += "/";
  57. WriteFile(dirName, new byte[0]);
  58. }
  59. /// <summary>
  60. /// Write a file to the tar archive
  61. /// </summary>
  62. /// <param name="filePath"></param>
  63. /// <param name="data"></param>
  64. public void WriteFile(string filePath, string data)
  65. {
  66. WriteFile(filePath, m_asciiEncoding.GetBytes(data));
  67. }
  68. /// <summary>
  69. /// Write a file to the tar archive
  70. /// </summary>
  71. /// <param name="filePath"></param>
  72. /// <param name="data"></param>
  73. public void WriteFile(string filePath, byte[] data)
  74. {
  75. if (filePath.Length > 100)
  76. WriteEntry("././@LongLink", m_asciiEncoding.GetBytes(filePath), 'L');
  77. char fileType;
  78. if (filePath.EndsWith("/"))
  79. {
  80. fileType = '5';
  81. }
  82. else
  83. {
  84. fileType = '0';
  85. }
  86. WriteEntry(filePath, data, fileType);
  87. }
  88. /// <summary>
  89. /// Finish writing the raw tar archive data to a stream. The stream will be closed on completion.
  90. /// </summary>
  91. /// <param name="s">Stream to which to write the data</param>
  92. /// <returns></returns>
  93. public void Close()
  94. {
  95. //m_log.Debug("[TAR ARCHIVE WRITER]: Writing final consecutive 0 blocks");
  96. // Write two consecutive 0 blocks to end the archive
  97. byte[] finalZeroPadding = new byte[1024];
  98. lock (m_bw)
  99. {
  100. m_bw.Write(finalZeroPadding);
  101. m_bw.Flush();
  102. m_bw.Close();
  103. }
  104. }
  105. public static byte[] ConvertDecimalToPaddedOctalBytes(int d, int padding)
  106. {
  107. string oString = "";
  108. while (d > 0)
  109. {
  110. oString = Convert.ToString((byte)'0' + d & 7) + oString;
  111. d >>= 3;
  112. }
  113. while (oString.Length < padding)
  114. {
  115. oString = "0" + oString;
  116. }
  117. byte[] oBytes = m_asciiEncoding.GetBytes(oString);
  118. return oBytes;
  119. }
  120. /// <summary>
  121. /// Write a particular entry
  122. /// </summary>
  123. /// <param name="filePath"></param>
  124. /// <param name="data"></param>
  125. /// <param name="fileType"></param>
  126. protected void WriteEntry(string filePath, byte[] data, char fileType)
  127. {
  128. byte[] header = new byte[512];
  129. // file path field (100)
  130. byte[] nameBytes = m_asciiEncoding.GetBytes(filePath);
  131. int nameSize = (nameBytes.Length >= 100) ? 100 : nameBytes.Length;
  132. Array.Copy(nameBytes, header, nameSize);
  133. // file mode (8)
  134. byte[] modeBytes = m_asciiEncoding.GetBytes("0000777");
  135. Array.Copy(modeBytes, 0, header, 100, 7);
  136. // owner user id (8)
  137. byte[] ownerIdBytes = m_asciiEncoding.GetBytes("0000764");
  138. Array.Copy(ownerIdBytes, 0, header, 108, 7);
  139. // group user id (8)
  140. byte[] groupIdBytes = m_asciiEncoding.GetBytes("0000764");
  141. Array.Copy(groupIdBytes, 0, header, 116, 7);
  142. // file size in bytes (12)
  143. int fileSize = data.Length;
  144. //m_log.DebugFormat("[TAR ARCHIVE WRITER]: File size of {0} is {1}", filePath, fileSize);
  145. byte[] fileSizeBytes = ConvertDecimalToPaddedOctalBytes(fileSize, 11);
  146. Array.Copy(fileSizeBytes, 0, header, 124, 11);
  147. // last modification time (12)
  148. byte[] lastModTimeBytes = m_asciiEncoding.GetBytes("11017037332");
  149. Array.Copy(lastModTimeBytes, 0, header, 136, 11);
  150. // entry type indicator (1)
  151. header[156] = m_asciiEncoding.GetBytes(new char[] { fileType })[0];
  152. Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 329, 7);
  153. Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 337, 7);
  154. // check sum for header block (8) [calculated last]
  155. Array.Copy(m_asciiEncoding.GetBytes(" "), 0, header, 148, 8);
  156. int checksum = 0;
  157. foreach (byte b in header)
  158. {
  159. checksum += b;
  160. }
  161. //m_log.DebugFormat("[TAR ARCHIVE WRITER]: Decimal header checksum is {0}", checksum);
  162. byte[] checkSumBytes = ConvertDecimalToPaddedOctalBytes(checksum, 6);
  163. Array.Copy(checkSumBytes, 0, header, 148, 6);
  164. header[154] = 0;
  165. lock (m_bw)
  166. {
  167. // Write out header
  168. m_bw.Write(header);
  169. // Write out data
  170. m_bw.Write(data);
  171. if (data.Length % 512 != 0)
  172. {
  173. int paddingRequired = 512 - (data.Length % 512);
  174. //m_log.DebugFormat("[TAR ARCHIVE WRITER]: Padding data with {0} bytes", paddingRequired);
  175. byte[] padding = new byte[paddingRequired];
  176. m_bw.Write(padding);
  177. }
  178. }
  179. }
  180. }
  181. }