TarArchiveWriter.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. /// <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, Util.UTF8NoBomEncoding.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", Encoding.ASCII.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 = Encoding.ASCII.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. // m_log.DebugFormat(
  130. // "[TAR ARCHIVE WRITER]: Data for {0} is {1} bytes", filePath, (null == data ? "null" : data.Length.ToString()));
  131. byte[] header = new byte[512];
  132. // file path field (100)
  133. byte[] nameBytes = Encoding.ASCII.GetBytes(filePath);
  134. int nameSize = (nameBytes.Length >= 100) ? 100 : nameBytes.Length;
  135. Array.Copy(nameBytes, header, nameSize);
  136. // file mode (8)
  137. byte[] modeBytes = Encoding.ASCII.GetBytes("0000777");
  138. Array.Copy(modeBytes, 0, header, 100, 7);
  139. // owner user id (8)
  140. byte[] ownerIdBytes = Encoding.ASCII.GetBytes("0000764");
  141. Array.Copy(ownerIdBytes, 0, header, 108, 7);
  142. // group user id (8)
  143. byte[] groupIdBytes = Encoding.ASCII.GetBytes("0000764");
  144. Array.Copy(groupIdBytes, 0, header, 116, 7);
  145. // file size in bytes (12)
  146. int fileSize = data.Length;
  147. //m_log.DebugFormat("[TAR ARCHIVE WRITER]: File size of {0} is {1}", filePath, fileSize);
  148. byte[] fileSizeBytes = ConvertDecimalToPaddedOctalBytes(fileSize, 11);
  149. Array.Copy(fileSizeBytes, 0, header, 124, 11);
  150. // last modification time (12)
  151. byte[] lastModTimeBytes = Encoding.ASCII.GetBytes("11017037332");
  152. Array.Copy(lastModTimeBytes, 0, header, 136, 11);
  153. // entry type indicator (1)
  154. header[156] = Encoding.ASCII.GetBytes(new char[] { fileType })[0];
  155. Array.Copy(Encoding.ASCII.GetBytes("0000000"), 0, header, 329, 7);
  156. Array.Copy(Encoding.ASCII.GetBytes("0000000"), 0, header, 337, 7);
  157. // check sum for header block (8) [calculated last]
  158. Array.Copy(Encoding.ASCII.GetBytes(" "), 0, header, 148, 8);
  159. int checksum = 0;
  160. foreach (byte b in header)
  161. {
  162. checksum += b;
  163. }
  164. //m_log.DebugFormat("[TAR ARCHIVE WRITER]: Decimal header checksum is {0}", checksum);
  165. byte[] checkSumBytes = ConvertDecimalToPaddedOctalBytes(checksum, 6);
  166. Array.Copy(checkSumBytes, 0, header, 148, 6);
  167. header[154] = 0;
  168. lock (m_bw)
  169. {
  170. // Write out header
  171. m_bw.Write(header);
  172. // Write out data
  173. // An IOException occurs if we try to write out an empty array in Mono 2.6
  174. if (data.Length > 0)
  175. m_bw.Write(data);
  176. if (data.Length % 512 != 0)
  177. {
  178. int paddingRequired = 512 - (data.Length % 512);
  179. //m_log.DebugFormat("[TAR ARCHIVE WRITER]: Padding data with {0} bytes", paddingRequired);
  180. byte[] padding = new byte[paddingRequired];
  181. m_bw.Write(padding);
  182. }
  183. }
  184. }
  185. }
  186. }