AssetsArchiver.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. using System.Collections.Generic;
  28. using System.IO;
  29. using System.Reflection;
  30. using System.Xml;
  31. using log4net;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Serialization;
  35. namespace OpenSim.Region.CoreModules.World.Archiver
  36. {
  37. /// <summary>
  38. /// Archives assets
  39. /// </summary>
  40. public class AssetsArchiver
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. /// <value>
  44. /// Post a message to the log every x assets as a progress bar
  45. /// </value>
  46. protected static int LOG_ASSET_LOAD_NOTIFICATION_INTERVAL = 100;
  47. /// <value>
  48. /// Keep a count of the number of assets written so that we can provide status updates
  49. /// </value>
  50. protected int m_assetsWritten;
  51. protected TarArchiveWriter m_archiveWriter;
  52. public AssetsArchiver(TarArchiveWriter archiveWriter)
  53. {
  54. m_archiveWriter = archiveWriter;
  55. }
  56. /// <summary>
  57. /// Archive the assets given to this archiver to the given archive.
  58. /// </summary>
  59. /// <param name="archive"></param>
  60. public void WriteAsset(AssetBase asset)
  61. {
  62. //WriteMetadata(archive);
  63. WriteData(asset);
  64. }
  65. /// <summary>
  66. /// Write an assets metadata file to the given archive
  67. /// </summary>
  68. /// <param name="archive"></param>
  69. // protected void WriteMetadata(TarArchiveWriter archive)
  70. // {
  71. // StringWriter sw = new StringWriter();
  72. // XmlTextWriter xtw = new XmlTextWriter(sw);
  73. //
  74. // xtw.Formatting = Formatting.Indented;
  75. // xtw.WriteStartDocument();
  76. //
  77. // xtw.WriteStartElement("assets");
  78. //
  79. // foreach (UUID uuid in m_assets.Keys)
  80. // {
  81. // AssetBase asset = m_assets[uuid];
  82. //
  83. // if (asset != null)
  84. // {
  85. // xtw.WriteStartElement("asset");
  86. //
  87. // string extension = string.Empty;
  88. //
  89. // if (ArchiveConstants.ASSET_TYPE_TO_EXTENSION.ContainsKey(asset.Type))
  90. // {
  91. // extension = ArchiveConstants.ASSET_TYPE_TO_EXTENSION[asset.Type];
  92. // }
  93. //
  94. // xtw.WriteElementString("filename", uuid.ToString() + extension);
  95. //
  96. // xtw.WriteElementString("name", asset.Name);
  97. // xtw.WriteElementString("description", asset.Description);
  98. // xtw.WriteElementString("asset-type", asset.Type.ToString());
  99. //
  100. // xtw.WriteEndElement();
  101. // }
  102. // }
  103. //
  104. // xtw.WriteEndElement();
  105. //
  106. // xtw.WriteEndDocument();
  107. //
  108. // archive.WriteFile("assets.xml", sw.ToString());
  109. // }
  110. /// <summary>
  111. /// Write asset data files to the given archive
  112. /// </summary>
  113. /// <param name="asset"></param>
  114. protected void WriteData(AssetBase asset)
  115. {
  116. // It appears that gtar, at least, doesn't need the intermediate directory entries in the tar
  117. //archive.AddDir("assets");
  118. string extension = string.Empty;
  119. if (ArchiveConstants.ASSET_TYPE_TO_EXTENSION.ContainsKey(asset.Type))
  120. {
  121. extension = ArchiveConstants.ASSET_TYPE_TO_EXTENSION[asset.Type];
  122. }
  123. else
  124. {
  125. m_log.ErrorFormat(
  126. "[ARCHIVER]: Unrecognized asset type {0} with uuid {1}. This asset will be saved but not reloaded",
  127. asset.Type, asset.ID);
  128. }
  129. m_archiveWriter.WriteFile(
  130. ArchiveConstants.ASSETS_PATH + asset.FullID.ToString() + extension,
  131. asset.Data);
  132. m_assetsWritten++;
  133. //m_log.DebugFormat("[ARCHIVER]: Added asset {0}", m_assetsWritten);
  134. if (m_assetsWritten % LOG_ASSET_LOAD_NOTIFICATION_INTERVAL == 0)
  135. m_log.InfoFormat("[ARCHIVER]: Added {0} assets to archive", m_assetsWritten);
  136. }
  137. }
  138. }