1
0

AssetsArchiver.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 libsecondlife;
  32. using log4net;
  33. using OpenSim.Framework;
  34. namespace OpenSim.Region.Environment.Modules.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. /// <summary>
  43. /// Archive assets
  44. /// </summary>
  45. protected IDictionary<LLUUID, AssetBase> m_assets;
  46. public AssetsArchiver(IDictionary<LLUUID, AssetBase> assets)
  47. {
  48. m_assets = assets;
  49. }
  50. /// <summary>
  51. /// Archive the assets given to this archiver to the given archive.
  52. /// </summary>
  53. /// <param name="archive"></param>
  54. public void Archive(TarArchiveWriter archive)
  55. {
  56. //WriteMetadata(archive);
  57. WriteData(archive);
  58. }
  59. /// <summary>
  60. /// Write an assets metadata file to the given archive
  61. /// </summary>
  62. /// <param name="archive"></param>
  63. protected void WriteMetadata(TarArchiveWriter archive)
  64. {
  65. StringWriter sw = new StringWriter();
  66. XmlTextWriter xtw = new XmlTextWriter(sw);
  67. xtw.Formatting = Formatting.Indented;
  68. xtw.WriteStartDocument();
  69. xtw.WriteStartElement("assets");
  70. foreach (LLUUID uuid in m_assets.Keys)
  71. {
  72. AssetBase asset = m_assets[uuid];
  73. if (asset != null)
  74. {
  75. xtw.WriteStartElement("asset");
  76. string extension = string.Empty;
  77. if (ArchiveConstants.ASSET_TYPE_TO_EXTENSION.ContainsKey(asset.Type))
  78. {
  79. extension = ArchiveConstants.ASSET_TYPE_TO_EXTENSION[asset.Type];
  80. }
  81. xtw.WriteElementString("filename", uuid.ToString() + extension);
  82. xtw.WriteElementString("name", asset.Name);
  83. xtw.WriteElementString("description", asset.Description);
  84. xtw.WriteElementString("asset-type", asset.Type.ToString());
  85. xtw.WriteEndElement();
  86. }
  87. }
  88. xtw.WriteEndElement();
  89. xtw.WriteEndDocument();
  90. archive.AddFile("assets.xml", sw.ToString());
  91. }
  92. /// <summary>
  93. /// Write asset data files to the given archive
  94. /// </summary>
  95. /// <param name="archive"></param>
  96. protected void WriteData(TarArchiveWriter archive)
  97. {
  98. // It appears that gtar, at least, doesn't need the intermediate directory entries in the tar
  99. //archive.AddDir("assets");
  100. foreach (LLUUID uuid in m_assets.Keys)
  101. {
  102. AssetBase asset = m_assets[uuid];
  103. if (asset != null)
  104. {
  105. string extension = string.Empty;
  106. if (ArchiveConstants.ASSET_TYPE_TO_EXTENSION.ContainsKey(asset.Type))
  107. {
  108. extension = ArchiveConstants.ASSET_TYPE_TO_EXTENSION[asset.Type];
  109. }
  110. archive.AddFile(
  111. ArchiveConstants.ASSETS_PATH + uuid.ToString() + extension,
  112. asset.Data);
  113. }
  114. else
  115. {
  116. m_log.WarnFormat("[ARCHIVER]: Could not find asset {0} to archive", uuid);
  117. }
  118. }
  119. }
  120. }
  121. }