From fb91a1aa49c098d03989a931280eaf668dceef18 Mon Sep 17 00:00:00 2001 From: Justin Clarke Casey Date: Wed, 28 May 2008 17:49:34 +0000 Subject: * Put in stubs for "load-oar" command, including ultra-primitive temporary tar loading code * Currently as a test, this will successfully load only the first file of an opensim archive and do absolutely nothing with it --- .../Modules/World/Archiver/ArchiveReadRequest.cs | 66 +++++++ .../Modules/World/Archiver/ArchiveRequest.cs | 212 --------------------- .../Modules/World/Archiver/ArchiveWriteRequest.cs | 210 ++++++++++++++++++++ .../Modules/World/Archiver/ArchiverModule.cs | 6 +- .../Modules/World/Archiver/TarArchive.cs | 203 -------------------- .../Modules/World/Archiver/TarArchiveReader.cs | 108 +++++++++++ .../Modules/World/Archiver/TarArchiveWriter.cs | 202 ++++++++++++++++++++ 7 files changed, 589 insertions(+), 418 deletions(-) create mode 100644 OpenSim/Region/Environment/Modules/World/Archiver/ArchiveReadRequest.cs delete mode 100644 OpenSim/Region/Environment/Modules/World/Archiver/ArchiveRequest.cs create mode 100644 OpenSim/Region/Environment/Modules/World/Archiver/ArchiveWriteRequest.cs delete mode 100644 OpenSim/Region/Environment/Modules/World/Archiver/TarArchive.cs create mode 100644 OpenSim/Region/Environment/Modules/World/Archiver/TarArchiveReader.cs create mode 100644 OpenSim/Region/Environment/Modules/World/Archiver/TarArchiveWriter.cs (limited to 'OpenSim/Region/Environment/Modules') diff --git a/OpenSim/Region/Environment/Modules/World/Archiver/ArchiveReadRequest.cs b/OpenSim/Region/Environment/Modules/World/Archiver/ArchiveReadRequest.cs new file mode 100644 index 0000000..0a3c4c5 --- /dev/null +++ b/OpenSim/Region/Environment/Modules/World/Archiver/ArchiveReadRequest.cs @@ -0,0 +1,66 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using OpenSim.Region.Environment.Scenes; +using System.Reflection; +using log4net; + +namespace OpenSim.Region.Environment.Modules.World.Archiver +{ + /// + /// Handles an individual archive read request + /// + public class ArchiveReadRequest + { + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + private Scene m_scene; + private string m_loadPath; + + public ArchiveReadRequest(Scene scene, string loadPath) + { + m_scene = scene; + m_loadPath = loadPath; + + DearchiveRegion(); + } + + protected void DearchiveRegion() + { + TarArchiveReader archive = new TarArchiveReader(m_loadPath); + + // Just test for now by reading first file + string filePath = "ERROR"; + + byte[] data = archive.Read(out filePath); + + m_log.DebugFormat("Successfully read {0} ({1} bytes) from archive {2}", filePath, data.Length, m_loadPath); + + archive.Close(); + } + } +} diff --git a/OpenSim/Region/Environment/Modules/World/Archiver/ArchiveRequest.cs b/OpenSim/Region/Environment/Modules/World/Archiver/ArchiveRequest.cs deleted file mode 100644 index 364b31f..0000000 --- a/OpenSim/Region/Environment/Modules/World/Archiver/ArchiveRequest.cs +++ /dev/null @@ -1,212 +0,0 @@ -/* - * Copyright (c) Contributors, http://opensimulator.org/ - * See CONTRIBUTORS.TXT for a full list of copyright holders. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of the OpenSim Project nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -using OpenSim.Framework; -using OpenSim.Framework.Communications.Cache; -using OpenSim.Region.Environment.Interfaces; -using OpenSim.Region.Environment.Modules.World.Serialiser; -using OpenSim.Region.Environment.Scenes; -using System.Collections.Generic; -using System.Reflection; -using libsecondlife; -using log4net; -using Nini.Config; - -namespace OpenSim.Region.Environment -{ - /// - /// Method called when all the necessary assets for an archive request have been received. - /// - public delegate void AssetsRequestCallback(IDictionary assets); - - /// - /// Handles an individual archive request - /// - public class ArchiveRequest - { - private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - - private Scene m_scene; - private string m_savePath; - - private string m_serializedEntities; - - public ArchiveRequest(Scene scene, string savePath) - { - m_scene = scene; - m_savePath = savePath; - - ArchiveRegion(); - } - - protected void ArchiveRegion() - { - m_log.Warn("[ARCHIVER]: Archive region not yet implemented"); - - Dictionary textureUuids = new Dictionary(); - - List entities = m_scene.GetEntities(); - - foreach (EntityBase entity in entities) - { - if (entity is SceneObjectGroup) - { - SceneObjectGroup sceneObject = (SceneObjectGroup)entity; - - foreach (SceneObjectPart part in sceneObject.GetParts()) - { - LLUUID texture = new LLUUID(part.Shape.TextureEntry, 0); - textureUuids[texture] = 1; - } - } - } - - m_serializedEntities = SerializeObjects(entities); - - if (m_serializedEntities != null && m_serializedEntities.Length > 0) - { - m_log.DebugFormat("[ARCHIVER]: Successfully got serialization for {0} entities", entities.Count); - m_log.DebugFormat("[ARCHIVER]: Requiring save of {0} textures", textureUuids.Count); - - // Asynchronously request all the assets required to perform this archive operation - new AssetsRequest(ReceivedAllAssets, m_scene.AssetCache, textureUuids.Keys); - } - } - - protected internal void ReceivedAllAssets(IDictionary assets) - { - m_log.DebugFormat("[ARCHIVER]: Received all {0} textures required", assets.Count); - - // XXX: Shouldn't hijack the asset async callback thread like this - this is only temporary - - TarArchive archive = new TarArchive(); - - archive.AddFile("prims.xml", m_serializedEntities); - - // It appears that gtar, at least, doesn't need the intermediate directory entries in the tar - //archive.AddDir("assets"); - - foreach (LLUUID uuid in assets.Keys) - { - if (assets[uuid] != null) - { - archive.AddFile("assets/" + uuid.ToString() + ".jp2", assets[uuid].Data); - } - else - { - m_log.DebugFormat("[ARCHIVER]: Could not find asset {0} to archive", uuid); - } - } - - archive.WriteTar(m_savePath); - } - - /// - /// Get an xml representation of the given scene objects. - /// - /// - /// - protected static string SerializeObjects(List entities) - { - string serialization = ""; - - List serObjects = new List(); - - foreach (EntityBase ent in entities) - { - if (ent is SceneObjectGroup) - { - serObjects.Add(((SceneObjectGroup) ent).ToXmlString2()); - } - } - - foreach (string serObject in serObjects) - serialization += serObject; - - serialization += ""; - - return serialization; - } - } - - /// - /// Encapsulate the asynchronous requests for the assets required for an archive operation - /// - class AssetsRequest - { - /// - /// Callback used when all the assets requested have been received. - /// - protected AssetsRequestCallback m_assetsRequestCallback; - - /// - /// Assets retrieved in this request - /// - protected Dictionary m_assets = new Dictionary(); - - /// - /// Record the number of asset replies required so we know when we've finished - /// - private int m_repliesRequired; - - /// - /// Asset cache used to request the assets - /// - protected AssetCache m_assetCache; - - protected internal AssetsRequest(AssetsRequestCallback assetsRequestCallback, AssetCache assetCache, ICollection uuids) - { - m_assetsRequestCallback = assetsRequestCallback; - m_assetCache = assetCache; - m_repliesRequired = uuids.Count; - - // We can stop here if there are no assets to fetch - if (m_repliesRequired == 0) - m_assetsRequestCallback(m_assets); - - foreach (LLUUID uuid in uuids) - { - m_assetCache.GetAsset(uuid, AssetRequestCallback, true); - } - } - - /// - /// Called back by the asset cache when it has the asset - /// - /// - /// - public void AssetRequestCallback(LLUUID assetID, AssetBase asset) - { - m_assets[assetID] = asset; - - if (m_assets.Count == m_repliesRequired) - { - m_assetsRequestCallback(m_assets); - } - } - } -} diff --git a/OpenSim/Region/Environment/Modules/World/Archiver/ArchiveWriteRequest.cs b/OpenSim/Region/Environment/Modules/World/Archiver/ArchiveWriteRequest.cs new file mode 100644 index 0000000..b952200 --- /dev/null +++ b/OpenSim/Region/Environment/Modules/World/Archiver/ArchiveWriteRequest.cs @@ -0,0 +1,210 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using OpenSim.Framework; +using OpenSim.Framework.Communications.Cache; +using OpenSim.Region.Environment.Interfaces; +using OpenSim.Region.Environment.Modules.World.Serialiser; +using OpenSim.Region.Environment.Scenes; +using System.Collections.Generic; +using System.Reflection; +using libsecondlife; +using log4net; +using Nini.Config; + +namespace OpenSim.Region.Environment +{ + /// + /// Method called when all the necessary assets for an archive request have been received. + /// + public delegate void AssetsRequestCallback(IDictionary assets); + + /// + /// Handles an individual archive write request + /// + public class ArchiveWriteRequest + { + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + private Scene m_scene; + private string m_savePath; + + private string m_serializedEntities; + + public ArchiveWriteRequest(Scene scene, string savePath) + { + m_scene = scene; + m_savePath = savePath; + + ArchiveRegion(); + } + + protected void ArchiveRegion() + { + Dictionary textureUuids = new Dictionary(); + + List entities = m_scene.GetEntities(); + + foreach (EntityBase entity in entities) + { + if (entity is SceneObjectGroup) + { + SceneObjectGroup sceneObject = (SceneObjectGroup)entity; + + foreach (SceneObjectPart part in sceneObject.GetParts()) + { + LLUUID texture = new LLUUID(part.Shape.TextureEntry, 0); + textureUuids[texture] = 1; + } + } + } + + m_serializedEntities = SerializeObjects(entities); + + if (m_serializedEntities != null && m_serializedEntities.Length > 0) + { + m_log.DebugFormat("[ARCHIVER]: Successfully got serialization for {0} entities", entities.Count); + m_log.DebugFormat("[ARCHIVER]: Requiring save of {0} textures", textureUuids.Count); + + // Asynchronously request all the assets required to perform this archive operation + new AssetsRequest(ReceivedAllAssets, m_scene.AssetCache, textureUuids.Keys); + } + } + + protected internal void ReceivedAllAssets(IDictionary assets) + { + m_log.DebugFormat("[ARCHIVER]: Received all {0} textures required", assets.Count); + + // XXX: Shouldn't hijack the asset async callback thread like this - this is only temporary + + TarArchiveWriter archive = new TarArchiveWriter(); + + archive.AddFile("prims.xml", m_serializedEntities); + + // It appears that gtar, at least, doesn't need the intermediate directory entries in the tar + //archive.AddDir("assets"); + + foreach (LLUUID uuid in assets.Keys) + { + if (assets[uuid] != null) + { + archive.AddFile("assets/" + uuid.ToString() + ".jp2", assets[uuid].Data); + } + else + { + m_log.DebugFormat("[ARCHIVER]: Could not find asset {0} to archive", uuid); + } + } + + archive.WriteTar(m_savePath); + } + + /// + /// Get an xml representation of the given scene objects. + /// + /// + /// + protected static string SerializeObjects(List entities) + { + string serialization = ""; + + List serObjects = new List(); + + foreach (EntityBase ent in entities) + { + if (ent is SceneObjectGroup) + { + serObjects.Add(((SceneObjectGroup) ent).ToXmlString2()); + } + } + + foreach (string serObject in serObjects) + serialization += serObject; + + serialization += ""; + + return serialization; + } + } + + /// + /// Encapsulate the asynchronous requests for the assets required for an archive operation + /// + class AssetsRequest + { + /// + /// Callback used when all the assets requested have been received. + /// + protected AssetsRequestCallback m_assetsRequestCallback; + + /// + /// Assets retrieved in this request + /// + protected Dictionary m_assets = new Dictionary(); + + /// + /// Record the number of asset replies required so we know when we've finished + /// + private int m_repliesRequired; + + /// + /// Asset cache used to request the assets + /// + protected AssetCache m_assetCache; + + protected internal AssetsRequest(AssetsRequestCallback assetsRequestCallback, AssetCache assetCache, ICollection uuids) + { + m_assetsRequestCallback = assetsRequestCallback; + m_assetCache = assetCache; + m_repliesRequired = uuids.Count; + + // We can stop here if there are no assets to fetch + if (m_repliesRequired == 0) + m_assetsRequestCallback(m_assets); + + foreach (LLUUID uuid in uuids) + { + m_assetCache.GetAsset(uuid, AssetRequestCallback, true); + } + } + + /// + /// Called back by the asset cache when it has the asset + /// + /// + /// + public void AssetRequestCallback(LLUUID assetID, AssetBase asset) + { + m_assets[assetID] = asset; + + if (m_assets.Count == m_repliesRequired) + { + m_assetsRequestCallback(m_assets); + } + } + } +} diff --git a/OpenSim/Region/Environment/Modules/World/Archiver/ArchiverModule.cs b/OpenSim/Region/Environment/Modules/World/Archiver/ArchiverModule.cs index cff9235..0e3123e 100644 --- a/OpenSim/Region/Environment/Modules/World/Archiver/ArchiverModule.cs +++ b/OpenSim/Region/Environment/Modules/World/Archiver/ArchiverModule.cs @@ -72,12 +72,12 @@ namespace OpenSim.Region.Environment.Modules.World.Archiver public void ArchiveRegion(string savePath) { - new ArchiveRequest(m_scene, savePath); + new ArchiveWriteRequest(m_scene, savePath); } public void DearchiveRegion(string loadPath) { - m_log.Warn("[ARCHIVER]: Dearchive region not yet implemented"); + new ArchiveReadRequest(m_scene, loadPath); } } -} \ No newline at end of file +} diff --git a/OpenSim/Region/Environment/Modules/World/Archiver/TarArchive.cs b/OpenSim/Region/Environment/Modules/World/Archiver/TarArchive.cs deleted file mode 100644 index c7492fe..0000000 --- a/OpenSim/Region/Environment/Modules/World/Archiver/TarArchive.cs +++ /dev/null @@ -1,203 +0,0 @@ -/* - * Copyright (c) Contributors, http://opensimulator.org/ - * See CONTRIBUTORS.TXT for a full list of copyright holders. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of the OpenSim Project nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - - -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Reflection; -using log4net; - -namespace OpenSim.Region.Environment -{ - /// - /// Temporary code to produce a tar archive in tar v7 format - /// - public class TarArchive - { - private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - - protected Dictionary m_files = new Dictionary(); - - protected static System.Text.ASCIIEncoding m_asciiEncoding = new System.Text.ASCIIEncoding(); - - /// - /// Add a directory to the tar archive. We can only handle one path level right now! - /// - /// - public void AddDir(string dirName) - { - // Directories are signalled by a final / - if (!dirName.EndsWith("/")) - dirName += "/"; - - AddFile(dirName, new byte[0]); - } - - /// - /// Add a file to the tar archive - /// - /// - /// - public void AddFile(string filePath, string data) - { - AddFile(filePath, m_asciiEncoding.GetBytes(data)); - } - - /// - /// Add a file to the tar archive - /// - /// - /// - public void AddFile(string filePath, byte[] data) - { - m_files[filePath] = data; - } - - /// - /// Write the raw tar archive data to a file - /// - /// - public void WriteTar(string archivePath) - { - BinaryWriter bw = new BinaryWriter(new FileStream(archivePath, FileMode.Create)); - - foreach (string filePath in m_files.Keys) - { - byte[] header = new byte[512]; - byte[] data = m_files[filePath]; - - //string filePath = "test.txt"; - //byte[] data = m_asciiEncoding.GetBytes("hello\n"); - - // file path field (100) - byte[] nameBytes = m_asciiEncoding.GetBytes(filePath); - int nameSize = (nameBytes.Length >= 100) ? 100 : nameBytes.Length; - Array.Copy(nameBytes, header, nameSize); - - // file mode (8) - byte[] modeBytes = m_asciiEncoding.GetBytes("0000755"); - Array.Copy(modeBytes, 0, header, 100, 7); - - // owner user id (8) - byte[] ownerIdBytes = m_asciiEncoding.GetBytes("0000764"); - Array.Copy(ownerIdBytes, 0, header, 108, 7); - - // group user id (8) - byte[] groupIdBytes = m_asciiEncoding.GetBytes("0000764"); - Array.Copy(groupIdBytes, 0, header, 116, 7); - - // file size in bytes (12) - int fileSize = data.Length; - m_log.DebugFormat("[TAR ARCHIVE]: File size of {0} is {1}", filePath, fileSize); - - byte[] fileSizeBytes = ConvertDecimalToPaddedOctalBytes(fileSize, 11); - - Array.Copy(fileSizeBytes, 0, header, 124, 11); - - // last modification time (12) - byte[] lastModTimeBytes = m_asciiEncoding.GetBytes("11017037332"); - Array.Copy(lastModTimeBytes, 0, header, 136, 11); - - // link indicator (1) - //header[156] = m_asciiEncoding.GetBytes("0")[0]; - if (filePath.EndsWith("/")) - { - header[156] = m_asciiEncoding.GetBytes("5")[0]; - } - else - { - header[156] = 0; - } - - Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 329, 7); - Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 337, 7); - - // check sum for header block (8) [calculated last] - Array.Copy(m_asciiEncoding.GetBytes(" "), 0, header, 148, 8); - - int checksum = 0; - foreach (byte b in header) - { - checksum += b; - } - - m_log.DebugFormat("[TAR ARCHIVE]: Decimal header checksum is {0}", checksum); - - byte[] checkSumBytes = ConvertDecimalToPaddedOctalBytes(checksum, 6); - //byte[] checkSumBytes = m_asciiEncoding.GetBytes("007520"); - - Array.Copy(checkSumBytes, 0, header, 148, 6); - - header[154] = 0; - - // Write out header - bw.Write(header); - - // Write out data - bw.Write(data); - - if (data.Length % 512 != 0) - { - int paddingRequired = 512 - (data.Length % 512); - - m_log.DebugFormat("Padding data with {0} bytes", paddingRequired); - - byte[] padding = new byte[paddingRequired]; - bw.Write(padding); - } - } - - // Write two consecutive 0 blocks to end the archive - byte[] finalZeroPadding = new byte[1024]; - bw.Write(finalZeroPadding); - - bw.Close(); - } - - public static byte[] ConvertDecimalToPaddedOctalBytes(int d, int padding) - { - string oString = ""; - - while (d > 0) - { - oString = Convert.ToString((byte)'0' + d & 7) + oString; - d >>= 3; - } - - while (oString.Length < padding) - { - oString = "0" + oString; - } - - byte[] oBytes = m_asciiEncoding.GetBytes(oString); - - return oBytes; - } - } -} diff --git a/OpenSim/Region/Environment/Modules/World/Archiver/TarArchiveReader.cs b/OpenSim/Region/Environment/Modules/World/Archiver/TarArchiveReader.cs new file mode 100644 index 0000000..e785c6c --- /dev/null +++ b/OpenSim/Region/Environment/Modules/World/Archiver/TarArchiveReader.cs @@ -0,0 +1,108 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.IO; +//using System.Reflection; +//using log4net; + +namespace OpenSim.Region.Environment.Modules.World.Archiver +{ + /// + /// Temporary code to do the bare minimum required to read a tar archive for our purposes + /// + public class TarArchiveReader + { +// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + protected static System.Text.ASCIIEncoding m_asciiEncoding = new System.Text.ASCIIEncoding(); + + /// + /// Binary reader for the underlying stream + /// + protected BinaryReader m_br; + + public TarArchiveReader(string archivePath) + { + m_br = new BinaryReader(new FileStream(archivePath, FileMode.Open)); + } + + public byte[] Read(out string filePath) + { + TarHeader header = ReadHeader(); + filePath = header.FilePath; + return m_br.ReadBytes(header.FileSize); + } + + /// + /// Read the next 512 byte chunk of data as a tar header. + /// + /// A tar header struct + protected TarHeader ReadHeader() + { + TarHeader tarHeader = new TarHeader(); + + byte[] header = m_br.ReadBytes(512); + + tarHeader.FilePath = m_asciiEncoding.GetString(header, 0, 100); + tarHeader.FileSize = ConvertOctalBytesToDecimal(header, 124, 11); + + return tarHeader; + } + + public void Close() + { + m_br.Close(); + } + + /// + /// Convert octal bytes to a decimal representation + /// + /// + /// + public static int ConvertOctalBytesToDecimal(byte[] bytes, int startIndex, int count) + { + string oString = m_asciiEncoding.GetString(bytes, startIndex, count); + + int d = 0; + + foreach (char c in oString) + { + d <<= 3; + d |= c - '0'; + } + + return d; + } + } + + public struct TarHeader + { + public string FilePath; + public int FileSize; + } +} diff --git a/OpenSim/Region/Environment/Modules/World/Archiver/TarArchiveWriter.cs b/OpenSim/Region/Environment/Modules/World/Archiver/TarArchiveWriter.cs new file mode 100644 index 0000000..e951755 --- /dev/null +++ b/OpenSim/Region/Environment/Modules/World/Archiver/TarArchiveWriter.cs @@ -0,0 +1,202 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Reflection; +using log4net; + +namespace OpenSim.Region.Environment +{ + /// + /// Temporary code to produce a tar archive in tar v7 format + /// + public class TarArchiveWriter + { + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + protected Dictionary m_files = new Dictionary(); + + protected static System.Text.ASCIIEncoding m_asciiEncoding = new System.Text.ASCIIEncoding(); + + /// + /// Add a directory to the tar archive. We can only handle one path level right now! + /// + /// + public void AddDir(string dirName) + { + // Directories are signalled by a final / + if (!dirName.EndsWith("/")) + dirName += "/"; + + AddFile(dirName, new byte[0]); + } + + /// + /// Add a file to the tar archive + /// + /// + /// + public void AddFile(string filePath, string data) + { + AddFile(filePath, m_asciiEncoding.GetBytes(data)); + } + + /// + /// Add a file to the tar archive + /// + /// + /// + public void AddFile(string filePath, byte[] data) + { + m_files[filePath] = data; + } + + /// + /// Write the raw tar archive data to a file + /// + /// + public void WriteTar(string archivePath) + { + BinaryWriter bw = new BinaryWriter(new FileStream(archivePath, FileMode.Create)); + + foreach (string filePath in m_files.Keys) + { + byte[] header = new byte[512]; + byte[] data = m_files[filePath]; + + //string filePath = "test.txt"; + //byte[] data = m_asciiEncoding.GetBytes("hello\n"); + + // file path field (100) + byte[] nameBytes = m_asciiEncoding.GetBytes(filePath); + int nameSize = (nameBytes.Length >= 100) ? 100 : nameBytes.Length; + Array.Copy(nameBytes, header, nameSize); + + // file mode (8) + byte[] modeBytes = m_asciiEncoding.GetBytes("0000755"); + Array.Copy(modeBytes, 0, header, 100, 7); + + // owner user id (8) + byte[] ownerIdBytes = m_asciiEncoding.GetBytes("0000764"); + Array.Copy(ownerIdBytes, 0, header, 108, 7); + + // group user id (8) + byte[] groupIdBytes = m_asciiEncoding.GetBytes("0000764"); + Array.Copy(groupIdBytes, 0, header, 116, 7); + + // file size in bytes (12) + int fileSize = data.Length; + m_log.DebugFormat("[TAR ARCHIVE]: File size of {0} is {1}", filePath, fileSize); + + byte[] fileSizeBytes = ConvertDecimalToPaddedOctalBytes(fileSize, 11); + + Array.Copy(fileSizeBytes, 0, header, 124, 11); + + // last modification time (12) + byte[] lastModTimeBytes = m_asciiEncoding.GetBytes("11017037332"); + Array.Copy(lastModTimeBytes, 0, header, 136, 11); + + // link indicator (1) + //header[156] = m_asciiEncoding.GetBytes("0")[0]; + if (filePath.EndsWith("/")) + { + header[156] = m_asciiEncoding.GetBytes("5")[0]; + } + else + { + header[156] = 0; + } + + Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 329, 7); + Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 337, 7); + + // check sum for header block (8) [calculated last] + Array.Copy(m_asciiEncoding.GetBytes(" "), 0, header, 148, 8); + + int checksum = 0; + foreach (byte b in header) + { + checksum += b; + } + + m_log.DebugFormat("[TAR ARCHIVE]: Decimal header checksum is {0}", checksum); + + byte[] checkSumBytes = ConvertDecimalToPaddedOctalBytes(checksum, 6); + //byte[] checkSumBytes = m_asciiEncoding.GetBytes("007520"); + + Array.Copy(checkSumBytes, 0, header, 148, 6); + + header[154] = 0; + + // Write out header + bw.Write(header); + + // Write out data + bw.Write(data); + + if (data.Length % 512 != 0) + { + int paddingRequired = 512 - (data.Length % 512); + + m_log.DebugFormat("Padding data with {0} bytes", paddingRequired); + + byte[] padding = new byte[paddingRequired]; + bw.Write(padding); + } + } + + // Write two consecutive 0 blocks to end the archive + byte[] finalZeroPadding = new byte[1024]; + bw.Write(finalZeroPadding); + + bw.Close(); + } + + public static byte[] ConvertDecimalToPaddedOctalBytes(int d, int padding) + { + string oString = ""; + + while (d > 0) + { + oString = Convert.ToString((byte)'0' + d & 7) + oString; + d >>= 3; + } + + while (oString.Length < padding) + { + oString = "0" + oString; + } + + byte[] oBytes = m_asciiEncoding.GetBytes(oString); + + return oBytes; + } + } +} -- cgit v1.1