aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Modules/World/Archiver/TarArchive.cs
diff options
context:
space:
mode:
authorJeff Ames2008-05-28 03:44:49 +0000
committerJeff Ames2008-05-28 03:44:49 +0000
commit5752c1f5c2ee069e2ff5ffc0ff2f186d7454c5c4 (patch)
treeea980f49f1a6bb40ba037a87581a3d741c3d3c56 /OpenSim/Region/Environment/Modules/World/Archiver/TarArchive.cs
parentThank you kindly, Melanie for a patch that: (diff)
downloadopensim-SC_OLD-5752c1f5c2ee069e2ff5ffc0ff2f186d7454c5c4.zip
opensim-SC_OLD-5752c1f5c2ee069e2ff5ffc0ff2f186d7454c5c4.tar.gz
opensim-SC_OLD-5752c1f5c2ee069e2ff5ffc0ff2f186d7454c5c4.tar.bz2
opensim-SC_OLD-5752c1f5c2ee069e2ff5ffc0ff2f186d7454c5c4.tar.xz
Formatting cleanup.
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/Environment/Modules/World/Archiver/TarArchive.cs82
1 files changed, 41 insertions, 41 deletions
diff --git a/OpenSim/Region/Environment/Modules/World/Archiver/TarArchive.cs b/OpenSim/Region/Environment/Modules/World/Archiver/TarArchive.cs
index 5e5be34..3a7518c 100644
--- a/OpenSim/Region/Environment/Modules/World/Archiver/TarArchive.cs
+++ b/OpenSim/Region/Environment/Modules/World/Archiver/TarArchive.cs
@@ -34,16 +34,16 @@ using System.Reflection;
34using log4net; 34using log4net;
35 35
36namespace OpenSim.Region.Environment 36namespace OpenSim.Region.Environment
37{ 37{
38 /// <summary> 38 /// <summary>
39 /// Temporary code to produce a tar archive in tar v7 format 39 /// Temporary code to produce a tar archive in tar v7 format
40 /// </summary> 40 /// </summary>
41 public class TarArchive 41 public class TarArchive
42 { 42 {
43 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 43 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
44 44
45 protected Dictionary<string, byte[]> m_files = new Dictionary<string, byte[]>(); 45 protected Dictionary<string, byte[]> m_files = new Dictionary<string, byte[]>();
46 46
47 protected static System.Text.ASCIIEncoding m_asciiEncoding = new System.Text.ASCIIEncoding(); 47 protected static System.Text.ASCIIEncoding m_asciiEncoding = new System.Text.ASCIIEncoding();
48 48
49 /// <summary> 49 /// <summary>
@@ -55,7 +55,7 @@ namespace OpenSim.Region.Environment
55 { 55 {
56 AddFile(filePath, m_asciiEncoding.GetBytes(data)); 56 AddFile(filePath, m_asciiEncoding.GetBytes(data));
57 } 57 }
58 58
59 /// <summary> 59 /// <summary>
60 /// Add a file to the tar archive 60 /// Add a file to the tar archive
61 /// </summary> 61 /// </summary>
@@ -65,117 +65,117 @@ namespace OpenSim.Region.Environment
65 { 65 {
66 m_files[filePath] = data; 66 m_files[filePath] = data;
67 } 67 }
68 68
69 /// <summary> 69 /// <summary>
70 /// Write the raw tar archive data to a file 70 /// Write the raw tar archive data to a file
71 /// </summary> 71 /// </summary>
72 /// <returns></returns> 72 /// <returns></returns>
73 public void WriteTar(string archivePath) 73 public void WriteTar(string archivePath)
74 { 74 {
75 BinaryWriter bw = new BinaryWriter(new FileStream(archivePath, FileMode.Create)); 75 BinaryWriter bw = new BinaryWriter(new FileStream(archivePath, FileMode.Create));
76 76
77 foreach (string filePath in m_files.Keys) 77 foreach (string filePath in m_files.Keys)
78 { 78 {
79 byte[] header = new byte[512]; 79 byte[] header = new byte[512];
80 byte[] data = m_files[filePath]; 80 byte[] data = m_files[filePath];
81 81
82 //string filePath = "test.txt"; 82 //string filePath = "test.txt";
83 //byte[] data = m_asciiEncoding.GetBytes("hello\n"); 83 //byte[] data = m_asciiEncoding.GetBytes("hello\n");
84 84
85 // file path field (100) 85 // file path field (100)
86 byte[] nameBytes = m_asciiEncoding.GetBytes(filePath); 86 byte[] nameBytes = m_asciiEncoding.GetBytes(filePath);
87 int nameSize = (nameBytes.Length >= 100) ? 100 : nameBytes.Length; 87 int nameSize = (nameBytes.Length >= 100) ? 100 : nameBytes.Length;
88 Array.Copy(nameBytes, header, nameSize); 88 Array.Copy(nameBytes, header, nameSize);
89 89
90 // file mode (8) 90 // file mode (8)
91 byte[] modeBytes = m_asciiEncoding.GetBytes("0000644"); 91 byte[] modeBytes = m_asciiEncoding.GetBytes("0000644");
92 Array.Copy(modeBytes, 0, header, 100, 7); 92 Array.Copy(modeBytes, 0, header, 100, 7);
93 93
94 // owner user id (8) 94 // owner user id (8)
95 byte[] ownerIdBytes = m_asciiEncoding.GetBytes("0000764"); 95 byte[] ownerIdBytes = m_asciiEncoding.GetBytes("0000764");
96 Array.Copy(ownerIdBytes, 0, header, 108, 7); 96 Array.Copy(ownerIdBytes, 0, header, 108, 7);
97 97
98 // group user id (8) 98 // group user id (8)
99 byte[] groupIdBytes = m_asciiEncoding.GetBytes("0000764"); 99 byte[] groupIdBytes = m_asciiEncoding.GetBytes("0000764");
100 Array.Copy(groupIdBytes, 0, header, 116, 7); 100 Array.Copy(groupIdBytes, 0, header, 116, 7);
101 101
102 // file size in bytes (12) 102 // file size in bytes (12)
103 int fileSize = data.Length; 103 int fileSize = data.Length;
104 m_log.DebugFormat("[TAR ARCHIVE]: File size of {0} is {1}", filePath, fileSize); 104 m_log.DebugFormat("[TAR ARCHIVE]: File size of {0} is {1}", filePath, fileSize);
105 105
106 byte[] fileSizeBytes = ConvertDecimalToPaddedOctalBytes(fileSize, 11); 106 byte[] fileSizeBytes = ConvertDecimalToPaddedOctalBytes(fileSize, 11);
107 107
108 Array.Copy(fileSizeBytes, 0, header, 124, 11); 108 Array.Copy(fileSizeBytes, 0, header, 124, 11);
109 109
110 // last modification time (12) 110 // last modification time (12)
111 byte[] lastModTimeBytes = m_asciiEncoding.GetBytes("11017037332"); 111 byte[] lastModTimeBytes = m_asciiEncoding.GetBytes("11017037332");
112 Array.Copy(lastModTimeBytes, 0, header, 136, 11); 112 Array.Copy(lastModTimeBytes, 0, header, 136, 11);
113 113
114 // link indicator (1) 114 // link indicator (1)
115 //header[156] = m_asciiEncoding.GetBytes("0")[0]; 115 //header[156] = m_asciiEncoding.GetBytes("0")[0];
116 header[156] = 0; 116 header[156] = 0;
117 117
118 Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 329, 7); 118 Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 329, 7);
119 Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 337, 7); 119 Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 337, 7);
120 120
121 // check sum for header block (8) [calculated last] 121 // check sum for header block (8) [calculated last]
122 Array.Copy(m_asciiEncoding.GetBytes(" "), 0, header, 148, 8); 122 Array.Copy(m_asciiEncoding.GetBytes(" "), 0, header, 148, 8);
123 123
124 int checksum = 0; 124 int checksum = 0;
125 foreach (byte b in header) 125 foreach (byte b in header)
126 { 126 {
127 checksum += b; 127 checksum += b;
128 } 128 }
129 129
130 m_log.DebugFormat("[TAR ARCHIVE]: Decimal header checksum is {0}", checksum); 130 m_log.DebugFormat("[TAR ARCHIVE]: Decimal header checksum is {0}", checksum);
131 131
132 byte[] checkSumBytes = ConvertDecimalToPaddedOctalBytes(checksum, 6); 132 byte[] checkSumBytes = ConvertDecimalToPaddedOctalBytes(checksum, 6);
133 //byte[] checkSumBytes = m_asciiEncoding.GetBytes("007520"); 133 //byte[] checkSumBytes = m_asciiEncoding.GetBytes("007520");
134 134
135 Array.Copy(checkSumBytes, 0, header, 148, 6); 135 Array.Copy(checkSumBytes, 0, header, 148, 6);
136 136
137 header[154] = 0; 137 header[154] = 0;
138 138
139 // Write out header 139 // Write out header
140 bw.Write(header); 140 bw.Write(header);
141 141
142 // Write out data 142 // Write out data
143 bw.Write(data); 143 bw.Write(data);
144 144
145 int paddingRequired = 512 - (data.Length % 512); 145 int paddingRequired = 512 - (data.Length % 512);
146 if (paddingRequired > 0) 146 if (paddingRequired > 0)
147 { 147 {
148 m_log.DebugFormat("Padding data with {0} bytes", paddingRequired); 148 m_log.DebugFormat("Padding data with {0} bytes", paddingRequired);
149 149
150 byte[] padding = new byte[paddingRequired]; 150 byte[] padding = new byte[paddingRequired];
151 bw.Write(padding); 151 bw.Write(padding);
152 } 152 }
153 } 153 }
154 154
155 // Write two consecutive 0 blocks to end the archive 155 // Write two consecutive 0 blocks to end the archive
156 byte[] finalZeroPadding = new byte[1024]; 156 byte[] finalZeroPadding = new byte[1024];
157 bw.Write(finalZeroPadding); 157 bw.Write(finalZeroPadding);
158 158
159 bw.Close(); 159 bw.Close();
160 } 160 }
161 161
162 public static byte[] ConvertDecimalToPaddedOctalBytes(int d, int padding) 162 public static byte[] ConvertDecimalToPaddedOctalBytes(int d, int padding)
163 { 163 {
164 string oString = ""; 164 string oString = "";
165 165
166 while (d > 0) 166 while (d > 0)
167 { 167 {
168 oString = Convert.ToString((byte)'0' + d & 7) + oString; 168 oString = Convert.ToString((byte)'0' + d & 7) + oString;
169 d >>= 3; 169 d >>= 3;
170 } 170 }
171 171
172 while (oString.Length < padding) 172 while (oString.Length < padding)
173 { 173 {
174 oString = "0" + oString; 174 oString = "0" + oString;
175 } 175 }
176 176
177 byte[] oBytes = m_asciiEncoding.GetBytes(oString); 177 byte[] oBytes = m_asciiEncoding.GetBytes(oString);
178 178
179 return oBytes; 179 return oBytes;
180 } 180 }
181 } 181 }