TarArchiveWriter.cs 8.1 KB

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