TarArchiveWriter.cs 7.2 KB

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