TarArchiveWriter.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.Collections.Generic;
  29. using System.IO;
  30. using System.Text;
  31. namespace OpenSim.Region.CoreModules.World.Archiver
  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. m_bw.Write(finalZeroPadding);
  99. m_bw.Flush();
  100. m_bw.Close();
  101. }
  102. public static byte[] ConvertDecimalToPaddedOctalBytes(int d, int padding)
  103. {
  104. string oString = "";
  105. while (d > 0)
  106. {
  107. oString = Convert.ToString((byte)'0' + d & 7) + oString;
  108. d >>= 3;
  109. }
  110. while (oString.Length < padding)
  111. {
  112. oString = "0" + oString;
  113. }
  114. byte[] oBytes = m_asciiEncoding.GetBytes(oString);
  115. return oBytes;
  116. }
  117. /// <summary>
  118. /// Write a particular entry
  119. /// </summary>
  120. /// <param name="filePath"></param>
  121. /// <param name="data"></param>
  122. /// <param name="fileType"></param>
  123. protected void WriteEntry(string filePath, byte[] data, char fileType)
  124. {
  125. byte[] header = new byte[512];
  126. // file path field (100)
  127. byte[] nameBytes = m_asciiEncoding.GetBytes(filePath);
  128. int nameSize = (nameBytes.Length >= 100) ? 100 : nameBytes.Length;
  129. Array.Copy(nameBytes, header, nameSize);
  130. // file mode (8)
  131. byte[] modeBytes = m_asciiEncoding.GetBytes("0000777");
  132. Array.Copy(modeBytes, 0, header, 100, 7);
  133. // owner user id (8)
  134. byte[] ownerIdBytes = m_asciiEncoding.GetBytes("0000764");
  135. Array.Copy(ownerIdBytes, 0, header, 108, 7);
  136. // group user id (8)
  137. byte[] groupIdBytes = m_asciiEncoding.GetBytes("0000764");
  138. Array.Copy(groupIdBytes, 0, header, 116, 7);
  139. // file size in bytes (12)
  140. int fileSize = data.Length;
  141. //m_log.DebugFormat("[TAR ARCHIVE WRITER]: File size of {0} is {1}", filePath, fileSize);
  142. byte[] fileSizeBytes = ConvertDecimalToPaddedOctalBytes(fileSize, 11);
  143. Array.Copy(fileSizeBytes, 0, header, 124, 11);
  144. // last modification time (12)
  145. byte[] lastModTimeBytes = m_asciiEncoding.GetBytes("11017037332");
  146. Array.Copy(lastModTimeBytes, 0, header, 136, 11);
  147. // entry type indicator (1)
  148. header[156] = m_asciiEncoding.GetBytes(new char[] { fileType })[0];
  149. Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 329, 7);
  150. Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 337, 7);
  151. // check sum for header block (8) [calculated last]
  152. Array.Copy(m_asciiEncoding.GetBytes(" "), 0, header, 148, 8);
  153. int checksum = 0;
  154. foreach (byte b in header)
  155. {
  156. checksum += b;
  157. }
  158. //m_log.DebugFormat("[TAR ARCHIVE WRITER]: Decimal header checksum is {0}", checksum);
  159. byte[] checkSumBytes = ConvertDecimalToPaddedOctalBytes(checksum, 6);
  160. Array.Copy(checkSumBytes, 0, header, 148, 6);
  161. header[154] = 0;
  162. // Write out header
  163. m_bw.Write(header);
  164. // Write out data
  165. m_bw.Write(data);
  166. if (data.Length % 512 != 0)
  167. {
  168. int paddingRequired = 512 - (data.Length % 512);
  169. //m_log.DebugFormat("[TAR ARCHIVE WRITER]: Padding data with {0} bytes", paddingRequired);
  170. byte[] padding = new byte[paddingRequired];
  171. m_bw.Write(padding);
  172. }
  173. }
  174. }
  175. }