aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/CoreModules/World/Archiver/TarArchiveReader.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/CoreModules/World/Archiver/TarArchiveReader.cs')
-rw-r--r--OpenSim/Region/CoreModules/World/Archiver/TarArchiveReader.cs218
1 files changed, 0 insertions, 218 deletions
diff --git a/OpenSim/Region/CoreModules/World/Archiver/TarArchiveReader.cs b/OpenSim/Region/CoreModules/World/Archiver/TarArchiveReader.cs
deleted file mode 100644
index 070f597..0000000
--- a/OpenSim/Region/CoreModules/World/Archiver/TarArchiveReader.cs
+++ /dev/null
@@ -1,218 +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.IO;
30using System.Reflection;
31using System.Text;
32using log4net;
33
34namespace OpenSim.Region.CoreModules.World.Archiver
35{
36 /// <summary>
37 /// Temporary code to do the bare minimum required to read a tar archive for our purposes
38 /// </summary>
39 public class TarArchiveReader
40 {
41 //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
42
43 public enum TarEntryType
44 {
45 TYPE_UNKNOWN = 0,
46 TYPE_NORMAL_FILE = 1,
47 TYPE_HARD_LINK = 2,
48 TYPE_SYMBOLIC_LINK = 3,
49 TYPE_CHAR_SPECIAL = 4,
50 TYPE_BLOCK_SPECIAL = 5,
51 TYPE_DIRECTORY = 6,
52 TYPE_FIFO = 7,
53 TYPE_CONTIGUOUS_FILE = 8,
54 }
55
56 protected static ASCIIEncoding m_asciiEncoding = new ASCIIEncoding();
57
58 /// <summary>
59 /// Binary reader for the underlying stream
60 /// </summary>
61 protected BinaryReader m_br;
62
63 /// <summary>
64 /// Used to trim off null chars
65 /// </summary>
66 protected char[] m_nullCharArray = new char[] { '\0' };
67
68 /// <summary>
69 /// Generate a tar reader which reads from the given stream.
70 /// </summary>
71 /// <param name="s"></param>
72 public TarArchiveReader(Stream s)
73 {
74 m_br = new BinaryReader(s);
75 }
76
77 /// <summary>
78 /// Read the next entry in the tar file.
79 /// </summary>
80 /// <param name="filePath"></param>
81 /// <returns>the data for the entry. Returns null if there are no more entries</returns>
82 public byte[] ReadEntry(out string filePath, out TarEntryType entryType)
83 {
84 filePath = String.Empty;
85 entryType = TarEntryType.TYPE_UNKNOWN;
86 TarHeader header = ReadHeader();
87
88 if (null == header)
89 return null;
90
91 entryType = header.EntryType;
92 filePath = header.FilePath;
93 return ReadData(header.FileSize);
94 }
95
96 /// <summary>
97 /// Read the next 512 byte chunk of data as a tar header.
98 /// </summary>
99 /// <returns>A tar header struct. null if we have reached the end of the archive.</returns>
100 protected TarHeader ReadHeader()
101 {
102 byte[] header = m_br.ReadBytes(512);
103
104 // If we've reached the end of the archive we'll be in null block territory, which means
105 // the next byte will be 0
106 if (header[0] == 0)
107 return null;
108
109 TarHeader tarHeader = new TarHeader();
110
111 // If we're looking at a GNU tar long link then extract the long name and pull up the next header
112 if (header[156] == (byte)'L')
113 {
114 int longNameLength = ConvertOctalBytesToDecimal(header, 124, 11);
115 tarHeader.FilePath = m_asciiEncoding.GetString(ReadData(longNameLength));
116 //m_log.DebugFormat("[TAR ARCHIVE READER]: Got long file name {0}", tarHeader.FilePath);
117 header = m_br.ReadBytes(512);
118 }
119 else
120 {
121 tarHeader.FilePath = m_asciiEncoding.GetString(header, 0, 100);
122 tarHeader.FilePath = tarHeader.FilePath.Trim(m_nullCharArray);
123 //m_log.DebugFormat("[TAR ARCHIVE READER]: Got short file name {0}", tarHeader.FilePath);
124 }
125
126 tarHeader.FileSize = ConvertOctalBytesToDecimal(header, 124, 11);
127
128 switch (header[156])
129 {
130 case 0:
131 tarHeader.EntryType = TarEntryType.TYPE_NORMAL_FILE;
132 break;
133 case (byte)'0':
134 tarHeader.EntryType = TarEntryType.TYPE_NORMAL_FILE;
135 break;
136 case (byte)'1':
137 tarHeader.EntryType = TarEntryType.TYPE_HARD_LINK;
138 break;
139 case (byte)'2':
140 tarHeader.EntryType = TarEntryType.TYPE_SYMBOLIC_LINK;
141 break;
142 case (byte)'3':
143 tarHeader.EntryType = TarEntryType.TYPE_CHAR_SPECIAL;
144 break;
145 case (byte)'4':
146 tarHeader.EntryType = TarEntryType.TYPE_BLOCK_SPECIAL;
147 break;
148 case (byte)'5':
149 tarHeader.EntryType = TarEntryType.TYPE_DIRECTORY;
150 break;
151 case (byte)'6':
152 tarHeader.EntryType = TarEntryType.TYPE_FIFO;
153 break;
154 case (byte)'7':
155 tarHeader.EntryType = TarEntryType.TYPE_CONTIGUOUS_FILE;
156 break;
157 }
158
159 return tarHeader;
160 }
161
162 /// <summary>
163 /// Read data following a header
164 /// </summary>
165 /// <param name="fileSize"></param>
166 /// <returns></returns>
167 protected byte[] ReadData(int fileSize)
168 {
169 byte[] data = m_br.ReadBytes(fileSize);
170
171 //m_log.DebugFormat("[TAR ARCHIVE READER]: fileSize {0}", fileSize);
172
173 // Read the rest of the empty padding in the 512 byte block
174 if (fileSize % 512 != 0)
175 {
176 int paddingLeft = 512 - (fileSize % 512);
177
178 //m_log.DebugFormat("[TAR ARCHIVE READER]: Reading {0} padding bytes", paddingLeft);
179
180 m_br.ReadBytes(paddingLeft);
181 }
182
183 return data;
184 }
185
186 public void Close()
187 {
188 m_br.Close();
189 }
190
191 /// <summary>
192 /// Convert octal bytes to a decimal representation
193 /// </summary>
194 /// <param name="bytes"></param>
195 /// <returns></returns>
196 public static int ConvertOctalBytesToDecimal(byte[] bytes, int startIndex, int count)
197 {
198 string oString = m_asciiEncoding.GetString(bytes, startIndex, count);
199
200 int d = 0;
201
202 foreach (char c in oString)
203 {
204 d <<= 3;
205 d |= c - '0';
206 }
207
208 return d;
209 }
210 }
211
212 public class TarHeader
213 {
214 public string FilePath;
215 public int FileSize;
216 public TarArchiveReader.TarEntryType EntryType;
217 }
218}