AssetsArchiver.cs 5.8 KB

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