aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Serialization/TarArchiveWriter.cs
diff options
context:
space:
mode:
authorMike Mazur2009-03-16 00:12:25 +0000
committerMike Mazur2009-03-16 00:12:25 +0000
commitf28886e26d32eaf97180d91dcd42fa045990bdd0 (patch)
tree69f36a36c809c9961758897d43c85413b1d4dd77 /OpenSim/Framework/Serialization/TarArchiveWriter.cs
parentThis patch improves MXP connect and disconnect functionality. (diff)
downloadopensim-SC_OLD-f28886e26d32eaf97180d91dcd42fa045990bdd0.zip
opensim-SC_OLD-f28886e26d32eaf97180d91dcd42fa045990bdd0.tar.gz
opensim-SC_OLD-f28886e26d32eaf97180d91dcd42fa045990bdd0.tar.bz2
opensim-SC_OLD-f28886e26d32eaf97180d91dcd42fa045990bdd0.tar.xz
Rename OpenSim.Framework.Archive to OpenSim.Framework.Serialization
Update using statements and prebuild.xml. Also trim trailing whitespace.
Diffstat (limited to 'OpenSim/Framework/Serialization/TarArchiveWriter.cs')
-rw-r--r--OpenSim/Framework/Serialization/TarArchiveWriter.cs217
1 files changed, 217 insertions, 0 deletions
diff --git a/OpenSim/Framework/Serialization/TarArchiveWriter.cs b/OpenSim/Framework/Serialization/TarArchiveWriter.cs
new file mode 100644
index 0000000..d319b0b
--- /dev/null
+++ b/OpenSim/Framework/Serialization/TarArchiveWriter.cs
@@ -0,0 +1,217 @@
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
28using System;
29using System.Collections.Generic;
30using System.IO;
31using System.Text;
32
33namespace 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
42 protected static ASCIIEncoding m_asciiEncoding = new ASCIIEncoding();
43
44 /// <summary>
45 /// Binary writer for the underlying stream
46 /// </summary>
47 protected BinaryWriter m_bw;
48
49 public TarArchiveWriter(Stream s)
50 {
51 m_bw = new BinaryWriter(s);
52 }
53
54 /// <summary>
55 /// Write a directory entry to the tar archive. We can only handle one path level right now!
56 /// </summary>
57 /// <param name="dirName"></param>
58 public void WriteDir(string dirName)
59 {
60 // Directories are signalled by a final /
61 if (!dirName.EndsWith("/"))
62 dirName += "/";
63
64 WriteFile(dirName, new byte[0]);
65 }
66
67 /// <summary>
68 /// Write a file to the tar archive
69 /// </summary>
70 /// <param name="filePath"></param>
71 /// <param name="data"></param>
72 public void WriteFile(string filePath, string data)
73 {
74 WriteFile(filePath, m_asciiEncoding.GetBytes(data));
75 }
76
77 /// <summary>
78 /// Write a file to the tar archive
79 /// </summary>
80 /// <param name="filePath"></param>
81 /// <param name="data"></param>
82 public void WriteFile(string filePath, byte[] data)
83 {
84 if (filePath.Length > 100)
85 WriteEntry("././@LongLink", m_asciiEncoding.GetBytes(filePath), 'L');
86
87 char fileType;
88
89 if (filePath.EndsWith("/"))
90 {
91 fileType = '5';
92 }
93 else
94 {
95 fileType = '0';
96 }
97
98 WriteEntry(filePath, data, fileType);
99 }
100
101 /// <summary>
102 /// Finish writing the raw tar archive data to a stream. The stream will be closed on completion.
103 /// </summary>
104 /// <param name="s">Stream to which to write the data</param>
105 /// <returns></returns>
106 public void Close()
107 {
108 //m_log.Debug("[TAR ARCHIVE WRITER]: Writing final consecutive 0 blocks");
109
110 // Write two consecutive 0 blocks to end the archive
111 byte[] finalZeroPadding = new byte[1024];
112 m_bw.Write(finalZeroPadding);
113
114 m_bw.Flush();
115 m_bw.Close();
116 }
117
118 public static byte[] ConvertDecimalToPaddedOctalBytes(int d, int padding)
119 {
120 string oString = "";
121
122 while (d > 0)
123 {
124 oString = Convert.ToString((byte)'0' + d & 7) + oString;
125 d >>= 3;
126 }
127
128 while (oString.Length < padding)
129 {
130 oString = "0" + oString;
131 }
132
133 byte[] oBytes = m_asciiEncoding.GetBytes(oString);
134
135 return oBytes;
136 }
137
138 /// <summary>
139 /// Write a particular entry
140 /// </summary>
141 /// <param name="filePath"></param>
142 /// <param name="data"></param>
143 /// <param name="fileType"></param>
144 protected void WriteEntry(string filePath, byte[] data, char fileType)
145 {
146 byte[] header = new byte[512];
147
148 // file path field (100)
149 byte[] nameBytes = m_asciiEncoding.GetBytes(filePath);
150 int nameSize = (nameBytes.Length >= 100) ? 100 : nameBytes.Length;
151 Array.Copy(nameBytes, header, nameSize);
152
153 // file mode (8)
154 byte[] modeBytes = m_asciiEncoding.GetBytes("0000777");
155 Array.Copy(modeBytes, 0, header, 100, 7);
156
157 // owner user id (8)
158 byte[] ownerIdBytes = m_asciiEncoding.GetBytes("0000764");
159 Array.Copy(ownerIdBytes, 0, header, 108, 7);
160
161 // group user id (8)
162 byte[] groupIdBytes = m_asciiEncoding.GetBytes("0000764");
163 Array.Copy(groupIdBytes, 0, header, 116, 7);
164
165 // file size in bytes (12)
166 int fileSize = data.Length;
167 //m_log.DebugFormat("[TAR ARCHIVE WRITER]: File size of {0} is {1}", filePath, fileSize);
168
169 byte[] fileSizeBytes = ConvertDecimalToPaddedOctalBytes(fileSize, 11);
170
171 Array.Copy(fileSizeBytes, 0, header, 124, 11);
172
173 // last modification time (12)
174 byte[] lastModTimeBytes = m_asciiEncoding.GetBytes("11017037332");
175 Array.Copy(lastModTimeBytes, 0, header, 136, 11);
176
177 // entry type indicator (1)
178 header[156] = m_asciiEncoding.GetBytes(new char[] { fileType })[0];
179
180 Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 329, 7);
181 Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 337, 7);
182
183 // check sum for header block (8) [calculated last]
184 Array.Copy(m_asciiEncoding.GetBytes(" "), 0, header, 148, 8);
185
186 int checksum = 0;
187 foreach (byte b in header)
188 {
189 checksum += b;
190 }
191
192 //m_log.DebugFormat("[TAR ARCHIVE WRITER]: Decimal header checksum is {0}", checksum);
193
194 byte[] checkSumBytes = ConvertDecimalToPaddedOctalBytes(checksum, 6);
195
196 Array.Copy(checkSumBytes, 0, header, 148, 6);
197
198 header[154] = 0;
199
200 // Write out header
201 m_bw.Write(header);
202
203 // Write out data
204 m_bw.Write(data);
205
206 if (data.Length % 512 != 0)
207 {
208 int paddingRequired = 512 - (data.Length % 512);
209
210 //m_log.DebugFormat("[TAR ARCHIVE WRITER]: Padding data with {0} bytes", paddingRequired);
211
212 byte[] padding = new byte[paddingRequired];
213 m_bw.Write(padding);
214 }
215 }
216 }
217}