diff options
Diffstat (limited to 'OpenSim/Region/CoreModules/Avatar/Inventory/Archiver/InventoryArchiveReadRequest.cs')
-rw-r--r-- | OpenSim/Region/CoreModules/Avatar/Inventory/Archiver/InventoryArchiveReadRequest.cs | 279 |
1 files changed, 279 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/Avatar/Inventory/Archiver/InventoryArchiveReadRequest.cs b/OpenSim/Region/CoreModules/Avatar/Inventory/Archiver/InventoryArchiveReadRequest.cs new file mode 100644 index 0000000..49006a2 --- /dev/null +++ b/OpenSim/Region/CoreModules/Avatar/Inventory/Archiver/InventoryArchiveReadRequest.cs | |||
@@ -0,0 +1,279 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.IO; | ||
31 | using System.IO.Compression; | ||
32 | using System.Reflection; | ||
33 | using System.Xml; | ||
34 | using OpenMetaverse; | ||
35 | using OpenSim.Region.Framework.Scenes; | ||
36 | using OpenSim.Region.CoreModules.World.Archiver; | ||
37 | using OpenSim.Region.CoreModules.World.Serialiser; | ||
38 | using OpenSim.Framework; | ||
39 | using OpenSim.Framework.Communications; | ||
40 | using OpenSim.Framework.Communications.Cache; | ||
41 | using log4net; | ||
42 | |||
43 | |||
44 | namespace OpenSim.Region.CoreModules.Avatar.Inventory.Archiver | ||
45 | { | ||
46 | public class InventoryArchiveReadRequest | ||
47 | { | ||
48 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
49 | |||
50 | protected Scene scene; | ||
51 | protected TarArchiveReader archive; | ||
52 | private static System.Text.ASCIIEncoding m_asciiEncoding = new System.Text.ASCIIEncoding(); | ||
53 | |||
54 | CommunicationsManager commsManager; | ||
55 | |||
56 | public InventoryArchiveReadRequest(Scene currentScene, CommunicationsManager commsManager) | ||
57 | { | ||
58 | //List<string> serialisedObjects = new List<string>(); | ||
59 | scene = currentScene; | ||
60 | this.commsManager = commsManager; | ||
61 | } | ||
62 | |||
63 | protected InventoryItemBase loadInvItem(string path, string contents) | ||
64 | { | ||
65 | InventoryItemBase item = new InventoryItemBase(); | ||
66 | StringReader sr = new StringReader(contents); | ||
67 | XmlTextReader reader = new XmlTextReader(sr); | ||
68 | |||
69 | if (contents.Equals("")) return null; | ||
70 | |||
71 | reader.ReadStartElement("InventoryObject"); | ||
72 | reader.ReadStartElement("Name"); | ||
73 | item.Name = reader.ReadString(); | ||
74 | reader.ReadEndElement(); | ||
75 | reader.ReadStartElement("ID"); | ||
76 | item.ID = UUID.Parse(reader.ReadString()); | ||
77 | reader.ReadEndElement(); | ||
78 | reader.ReadStartElement("InvType"); | ||
79 | item.InvType = System.Convert.ToInt32(reader.ReadString()); | ||
80 | reader.ReadEndElement(); | ||
81 | reader.ReadStartElement("CreatorUUID"); | ||
82 | item.Creator = UUID.Parse(reader.ReadString()); | ||
83 | reader.ReadEndElement(); | ||
84 | reader.ReadStartElement("CreationDate"); | ||
85 | item.CreationDate = System.Convert.ToInt32(reader.ReadString()); | ||
86 | reader.ReadEndElement(); | ||
87 | reader.ReadStartElement("Owner"); | ||
88 | item.Owner = UUID.Parse(reader.ReadString()); | ||
89 | reader.ReadEndElement(); | ||
90 | //No description would kill it | ||
91 | if (reader.IsEmptyElement) | ||
92 | { | ||
93 | reader.ReadStartElement("Description"); | ||
94 | } | ||
95 | else | ||
96 | { | ||
97 | reader.ReadStartElement("Description"); | ||
98 | item.Description = reader.ReadString(); | ||
99 | reader.ReadEndElement(); | ||
100 | } | ||
101 | reader.ReadStartElement("AssetType"); | ||
102 | item.AssetType = System.Convert.ToInt32(reader.ReadString()); | ||
103 | reader.ReadEndElement(); | ||
104 | reader.ReadStartElement("AssetID"); | ||
105 | item.AssetID = UUID.Parse(reader.ReadString()); | ||
106 | reader.ReadEndElement(); | ||
107 | reader.ReadStartElement("SaleType"); | ||
108 | item.SaleType = System.Convert.ToByte(reader.ReadString()); | ||
109 | reader.ReadEndElement(); | ||
110 | reader.ReadStartElement("SalePrice"); | ||
111 | item.SalePrice = System.Convert.ToInt32(reader.ReadString()); | ||
112 | reader.ReadEndElement(); | ||
113 | reader.ReadStartElement("BasePermissions"); | ||
114 | item.BasePermissions = System.Convert.ToUInt32(reader.ReadString()); | ||
115 | reader.ReadEndElement(); | ||
116 | reader.ReadStartElement("CurrentPermissions"); | ||
117 | item.CurrentPermissions = System.Convert.ToUInt32(reader.ReadString()); | ||
118 | reader.ReadEndElement(); | ||
119 | reader.ReadStartElement("EveryOnePermssions"); | ||
120 | item.EveryOnePermissions = System.Convert.ToUInt32(reader.ReadString()); | ||
121 | reader.ReadEndElement(); | ||
122 | reader.ReadStartElement("NextPermissions"); | ||
123 | item.NextPermissions = System.Convert.ToUInt32(reader.ReadString()); | ||
124 | reader.ReadEndElement(); | ||
125 | reader.ReadStartElement("Flags"); | ||
126 | item.Flags = System.Convert.ToUInt32(reader.ReadString()); | ||
127 | reader.ReadEndElement(); | ||
128 | reader.ReadStartElement("GroupID"); | ||
129 | item.GroupID = UUID.Parse(reader.ReadString()); | ||
130 | reader.ReadEndElement(); | ||
131 | reader.ReadStartElement("GroupOwned"); | ||
132 | item.GroupOwned = System.Convert.ToBoolean(reader.ReadString()); | ||
133 | reader.ReadEndElement(); | ||
134 | //reader.ReadStartElement("ParentFolderID"); | ||
135 | //item.Folder = UUID.Parse(reader.ReadString()); | ||
136 | //reader.ReadEndElement(); | ||
137 | //reader.ReadEndElement(); | ||
138 | |||
139 | return item; | ||
140 | } | ||
141 | |||
142 | public void execute(string firstName, string lastName, string invPath, string loadPath) | ||
143 | { | ||
144 | string filePath = "ERROR"; | ||
145 | int successfulAssetRestores = 0; | ||
146 | int failedAssetRestores = 0; | ||
147 | int successfulItemRestores = 0; | ||
148 | |||
149 | UserProfileData userProfile = commsManager.UserService.GetUserProfile(firstName, lastName); | ||
150 | if (null == userProfile) | ||
151 | { | ||
152 | m_log.ErrorFormat("[CONSOLE]: Failed to find user {0} {1}", firstName, lastName); | ||
153 | return; | ||
154 | } | ||
155 | |||
156 | CachedUserInfo userInfo = commsManager.UserProfileCacheService.GetUserDetails(userProfile.ID); | ||
157 | if (null == userInfo) | ||
158 | { | ||
159 | m_log.ErrorFormat( | ||
160 | "[CONSOLE]: Failed to find user info for {0} {1} {2}", | ||
161 | firstName, lastName, userProfile.ID); | ||
162 | |||
163 | return; | ||
164 | } | ||
165 | |||
166 | if (!userInfo.HasReceivedInventory) | ||
167 | { | ||
168 | m_log.ErrorFormat( | ||
169 | "[CONSOLE]: Have not yet received inventory info for user {0} {1} {2}", | ||
170 | firstName, lastName, userProfile.ID); | ||
171 | |||
172 | return; | ||
173 | } | ||
174 | |||
175 | InventoryFolderImpl inventoryFolder = userInfo.RootFolder.FindFolderByPath(invPath); | ||
176 | |||
177 | if (null == inventoryFolder) | ||
178 | { | ||
179 | // TODO: Later on, automatically create this folder if it does not exist | ||
180 | m_log.ErrorFormat("[ARCHIVER]: Inventory path {0} does not exist", invPath); | ||
181 | |||
182 | return; | ||
183 | } | ||
184 | |||
185 | archive | ||
186 | = new TarArchiveReader(new GZipStream( | ||
187 | new FileStream(loadPath, FileMode.Open), CompressionMode.Decompress)); | ||
188 | |||
189 | byte[] data; | ||
190 | TarArchiveReader.TarEntryType entryType; | ||
191 | while ((data = archive.ReadEntry(out filePath, out entryType)) != null) | ||
192 | { | ||
193 | if (entryType == TarArchiveReader.TarEntryType.TYPE_DIRECTORY) { | ||
194 | m_log.WarnFormat("[ARCHIVER]: Ignoring directory entry {0}", filePath); | ||
195 | } else if (filePath.StartsWith(ArchiveConstants.ASSETS_PATH)) | ||
196 | { | ||
197 | if (LoadAsset(filePath, data)) | ||
198 | successfulAssetRestores++; | ||
199 | else | ||
200 | failedAssetRestores++; | ||
201 | } | ||
202 | else | ||
203 | { | ||
204 | InventoryItemBase item = loadInvItem(filePath, m_asciiEncoding.GetString(data)); | ||
205 | |||
206 | if (item != null) | ||
207 | { | ||
208 | item.Creator = userProfile.ID; | ||
209 | item.Owner = userProfile.ID; | ||
210 | |||
211 | // Reset folder ID to the one in which we want to load it | ||
212 | // TODO: Properly restore entire folder structure. At the moment all items are dumped in this | ||
213 | // single folder no matter where in the saved folder structure they are. | ||
214 | item.Folder = inventoryFolder.ID; | ||
215 | |||
216 | userInfo.AddItem(item); | ||
217 | successfulItemRestores++; | ||
218 | } | ||
219 | } | ||
220 | } | ||
221 | |||
222 | archive.Close(); | ||
223 | |||
224 | m_log.DebugFormat("[ARCHIVER]: Restored {0} assets", successfulAssetRestores); | ||
225 | m_log.InfoFormat("[ARCHIVER]: Restored {0} items", successfulItemRestores); | ||
226 | } | ||
227 | |||
228 | /// <summary> | ||
229 | /// Load an asset | ||
230 | /// </summary> | ||
231 | /// <param name="assetFilename"></param> | ||
232 | /// <param name="data"></param> | ||
233 | /// <returns>true if asset was successfully loaded, false otherwise</returns> | ||
234 | private bool LoadAsset(string assetPath, byte[] data) | ||
235 | { | ||
236 | //IRegionSerialiser serialiser = scene.RequestModuleInterface<IRegionSerialiser>(); | ||
237 | // Right now we're nastily obtaining the UUID from the filename | ||
238 | string filename = assetPath.Remove(0, ArchiveConstants.ASSETS_PATH.Length); | ||
239 | int i = filename.LastIndexOf(ArchiveConstants.ASSET_EXTENSION_SEPARATOR); | ||
240 | |||
241 | if (i == -1) | ||
242 | { | ||
243 | m_log.ErrorFormat( | ||
244 | "[ARCHIVER]: Could not find extension information in asset path {0} since it's missing the separator {1}. Skipping", | ||
245 | assetPath, ArchiveConstants.ASSET_EXTENSION_SEPARATOR); | ||
246 | |||
247 | return false; | ||
248 | } | ||
249 | |||
250 | string extension = filename.Substring(i); | ||
251 | string uuid = filename.Remove(filename.Length - extension.Length); | ||
252 | |||
253 | if (ArchiveConstants.EXTENSION_TO_ASSET_TYPE.ContainsKey(extension)) | ||
254 | { | ||
255 | sbyte assetType = ArchiveConstants.EXTENSION_TO_ASSET_TYPE[extension]; | ||
256 | |||
257 | m_log.DebugFormat("[ARCHIVER]: Importing asset {0}, type {1}", uuid, assetType); | ||
258 | |||
259 | AssetBase asset = new AssetBase(new UUID(uuid), "RandomName"); | ||
260 | |||
261 | asset.Metadata.Type = assetType; | ||
262 | asset.Data = data; | ||
263 | |||
264 | scene.AssetCache.AddAsset(asset); | ||
265 | |||
266 | |||
267 | return true; | ||
268 | } | ||
269 | else | ||
270 | { | ||
271 | m_log.ErrorFormat( | ||
272 | "[ARCHIVER]: Tried to dearchive data with path {0} with an unknown type extension {1}", | ||
273 | assetPath, extension); | ||
274 | |||
275 | return false; | ||
276 | } | ||
277 | } | ||
278 | } | ||
279 | } | ||