OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
AssetsArchiver.cs
Go to the documentation of this file.
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 OpenSimulator 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.Collections.Generic;
29 using System.IO;
30 using System.Reflection;
31 using System.Xml;
32 using log4net;
33 using OpenMetaverse;
34 using OpenSim.Framework;
35 using OpenSim.Framework.Serialization;
36 
37 namespace OpenSim.Region.CoreModules.World.Archiver
38 {
42  public class AssetsArchiver
43  {
44  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
45 
49  protected static int LOG_ASSET_LOAD_NOTIFICATION_INTERVAL = 50;
50 
54  protected int m_assetsWritten;
55 
57 
58  public AssetsArchiver(TarArchiveWriter archiveWriter)
59  {
60  m_archiveWriter = archiveWriter;
61  }
62 
67  public void WriteAsset(AssetBase asset)
68  {
69  //WriteMetadata(archive);
70  WriteData(asset);
71  }
72 
77 // protected void WriteMetadata(TarArchiveWriter archive)
78 // {
79 // StringWriter sw = new StringWriter();
80 // XmlTextWriter xtw = new XmlTextWriter(sw);
81 //
82 // xtw.Formatting = Formatting.Indented;
83 // xtw.WriteStartDocument();
84 //
85 // xtw.WriteStartElement("assets");
86 //
87 // foreach (UUID uuid in m_assets.Keys)
88 // {
89 // AssetBase asset = m_assets[uuid];
90 //
91 // if (asset != null)
92 // {
93 // xtw.WriteStartElement("asset");
94 //
95 // string extension = string.Empty;
96 //
97 // if (ArchiveConstants.ASSET_TYPE_TO_EXTENSION.ContainsKey(asset.Type))
98 // {
99 // extension = ArchiveConstants.ASSET_TYPE_TO_EXTENSION[asset.Type];
100 // }
101 //
102 // xtw.WriteElementString("filename", uuid.ToString() + extension);
103 //
104 // xtw.WriteElementString("name", asset.Name);
105 // xtw.WriteElementString("description", asset.Description);
106 // xtw.WriteElementString("asset-type", asset.Type.ToString());
107 //
108 // xtw.WriteEndElement();
109 // }
110 // }
111 //
112 // xtw.WriteEndElement();
113 //
114 // xtw.WriteEndDocument();
115 //
116 // archive.WriteFile("assets.xml", sw.ToString());
117 // }
118 
123  protected void WriteData(AssetBase asset)
124  {
125  // It appears that gtar, at least, doesn't need the intermediate directory entries in the tar
126  //archive.AddDir("assets");
127 
128  string extension = string.Empty;
129 
130  if (ArchiveConstants.ASSET_TYPE_TO_EXTENSION.ContainsKey(asset.Type))
131  {
132  extension = ArchiveConstants.ASSET_TYPE_TO_EXTENSION[asset.Type];
133  }
134  else
135  {
136  m_log.ErrorFormat(
137  "[ARCHIVER]: Unrecognized asset type {0} with uuid {1}. This asset will be saved but not reloaded",
138  asset.Type, asset.ID);
139  }
140 
141  m_archiveWriter.WriteFile(
142  ArchiveConstants.ASSETS_PATH + asset.FullID.ToString() + extension,
143  asset.Data);
144 
145  m_assetsWritten++;
146 
147  //m_log.DebugFormat("[ARCHIVER]: Added asset {0}", m_assetsWritten);
148 
149  if (m_assetsWritten % LOG_ASSET_LOAD_NOTIFICATION_INTERVAL == 0)
150  m_log.InfoFormat("[ARCHIVER]: Added {0} assets to archive", m_assetsWritten);
151  }
152 
153  }
154 }
static readonly IDictionary< sbyte, string > ASSET_TYPE_TO_EXTENSION
Constants for the archiving module
sbyte Type
(sbyte) AssetType enum
Definition: AssetBase.cs:198
Temporary code to produce a tar archive in tar v7 format
Asset class. All Assets are reference by this class or a class derived from this class ...
Definition: AssetBase.cs:49
void WriteAsset(AssetBase asset)
Archive the assets given to this archiver to the given archive.
void WriteData(AssetBase asset)
Write an assets metadata file to the given archive