TarArchiveWriter.cs 7.8 KB

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