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