Ejemplo n.º 1
0
        /// <summary>
        /// Extracts a log archive to a local temporary directory.  Collects timing data & sets the root log location.
        /// </summary>
        /// <returns>Location where archive was unpacked.</returns>
        protected string UnpackLogset()
        {
            string rootUnpackDirectory = Path.Combine(GetUnpackTempDirectory(), request.RunId);

            ICollection <string> archivesToUnpack = GetArchivesToUnpack(rootUnpackDirectory);

            if (!archivesToUnpack.Any())
            {
                request.Target.ProcessedSize = DiskSpaceHelper.GetDirectorySize(rootUnpackDirectory);
                return(rootUnpackDirectory);
            }

            var unpackTimer = request.RunContext.CreateTimer("Unpack Logset");

            try
            {
                UnpackArchives(archivesToUnpack, rootUnpackDirectory);
            }
            finally
            {
                unpackTimer.Stop();
            }

            request.Target.ProcessedSize = DiskSpaceHelper.GetDirectorySize(rootUnpackDirectory);
            Log.InfoFormat("Finished extracting required files from logset! Unpacked {0} out of {1}. [{2}]", request.Target.ProcessedSize.Value.ToPrettySize(), request.Target.UncompressedSize.ToPrettySize(), unpackTimer.Elapsed.Print());

            return(rootUnpackDirectory);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Extracts the target logset.
        /// </summary>
        /// <returns>The root path where files were extracted.</returns>
        public ExtractionResult Extract(string target, string destination)
        {
            // Unpack files.
            try
            {
                using (var unpackTimer = new LogsharkTimer("Unpack Archives", GlobalEventTimingData.Add))
                {
                    ICollection <string> archivesToUnpack = GetArchivesToUnpack(target, destination);
                    var unpackResults = UnpackArchives(archivesToUnpack, destination, PathHelper.IsDirectory(target));

                    if (unpackResults.Any())
                    {
                        long inputSize     = DiskSpaceHelper.GetSize(target);
                        long extractedSize = DiskSpaceHelper.GetDirectorySize(destination);
                        Log.InfoFormat("Finished extracting required files from logset! Unpacked {0} out of {1}. [{2}]", extractedSize.ToPrettySize(), inputSize.ToPrettySize(), unpackTimer.Elapsed.Print());
                    }

                    return(new ExtractionResult(destination, unpackResults));
                }
            }
            catch (ZipException ex)
            {
                throw new InvalidLogsetException(String.Format("Cannot read logset archive: {0}", ex.Message));
            }
            catch (InsufficientDiskSpaceException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new ExtractionException(ex.Message, ex);
            }
        }
Ejemplo n.º 3
0
        protected long GetRequiredDiskSpace(string target)
        {
            if (PathHelper.IsDirectory(target))
            {
                return(GetWhitelistedFilesInDirectory(target).Sum(file => DiskSpaceHelper.GetSize(file)));
            }

            return(DiskSpaceHelper.GetSize(target));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Validates that sufficient disk space exists to unpack a target archive. Throws exception if space is insufficient.
        /// </summary>
        protected void ValidateSufficientDiskSpaceToUnpack(string archive, string destinationDirectory)
        {
            long availableBytes = DiskSpaceHelper.GetAvailableFreeSpace(destinationDirectory);
            long requiredBytes  = GetRequiredUnpackedSize(archive, destinationDirectory);

            if (availableBytes < requiredBytes)
            {
                throw new InsufficientDiskSpaceException(String.Format("Not enough free disk space available to unpack '{0}'! ({1} available, {2} required)",
                                                                       Path.GetFileName(archive), availableBytes.ToPrettySize(), requiredBytes.ToPrettySize()));
            }
        }
Ejemplo n.º 5
0
        protected void ValidateSufficientDiskSpaceToCopyTarget(string target, string destination)
        {
            long availableDiskSpace = DiskSpaceHelper.GetAvailableFreeSpace(destination);
            long requiredDiskSpace  = GetRequiredDiskSpace(target);

            if (requiredDiskSpace > availableDiskSpace)
            {
                throw new InsufficientDiskSpaceException(String.Format("Failed to copy target '{0}' to local temp directory: Not enough free disk space available! ({1} available, {2} required)",
                                                                       target, availableDiskSpace.ToPrettySize(), requiredDiskSpace.ToPrettySize()));
            }
        }
Ejemplo n.º 6
0
        internal LogsharkRequestTarget(string requestedTarget)
        {
            if (String.IsNullOrWhiteSpace(requestedTarget))
            {
                throw new LogsharkRequestInitializationException("Invalid request: No logset target specified!");
            }

            // A target can either be an MD5 logset hash or a file/UNC path.
            if (PathHelper.IsPathToExistingResource(requestedTarget))
            {
                // If target is a file or directory, we want to use the absolute path and trim any trailing
                // directory separator characters to establish consistency.
                string absolutePath = PathHelper.GetAbsolutePath(requestedTarget);
                if (PathHelper.IsDirectory(absolutePath))
                {
                    IsDirectory      = true;
                    UncompressedSize = DiskSpaceHelper.GetDirectorySize(absolutePath);
                }
                else
                {
                    IsFile           = true;
                    UncompressedSize = 0;
                    CompressedSize   = DiskSpaceHelper.GetFileSize(absolutePath);
                }
                Target = absolutePath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
            }
            else if (requestedTarget.IsValidMD5())
            {
                // Since this isn't a path to a file or directory and it looks like an MD5, we assume it is.
                IsHashId = true;
                Target   = requestedTarget;
            }
            else
            {
                throw new LogsharkRequestInitializationException("Target must be a valid file, directory or MD5 hash!");
            }

            OriginalTarget = Target;
        }
Ejemplo n.º 7
0
        private static void ValidateSufficientDiskSpaceToCopyTargetLocally(LogsharkRequest request, ISet<Regex> whitelistPatterns)
        {
            long availableDiskSpace = DiskSpaceHelper.GetAvailableFreeSpace(GetUnpackTempDirectory());

            long requiredDiskSpace = 0;
            if (request.Target.IsDirectory)
            {
                foreach (string requiredFile in GetRequiredFilesInDirectory(request, whitelistPatterns))
                {
                    requiredDiskSpace += DiskSpaceHelper.GetSize(requiredFile);
                }
            }
            else if (request.Target.IsFile)
            {
                requiredDiskSpace = DiskSpaceHelper.GetSize(request.Target);
            }

            if (requiredDiskSpace > availableDiskSpace)
            {
                throw new InsufficientDiskSpaceException(String.Format("Failed to copy target '{0}' to local temp directory: Not enough free disk space available! ({1} available, {2} required)",
                                                                        request.Target, availableDiskSpace.ToPrettySize(), requiredDiskSpace.ToPrettySize()));
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Extracts a target archive to a target directory.
        /// </summary>
        /// <param name="archive">The path to the archive.</param>
        /// <param name="destinationDirectory">The directory to extract the archive to.</param>
        /// <param name="deleteOnFinish">Indicates whether this archive should be deleted after unzipping it.</param>
        public UnzipResult Unzip(string archive, string destinationDirectory, bool deleteOnFinish = false)
        {
            // Validate archive exists.
            if (!File.Exists(archive))
            {
                throw new ArgumentException(String.Format("Archive '{0}' does not exist!", archive));
            }

            Directory.CreateDirectory(destinationDirectory);
            ValidateSufficientDiskSpaceToUnpack(archive, destinationDirectory);

            Log.InfoFormat("Extracting contents of archive '{0}' to '{1}'.. ({2})", Path.GetFileName(archive), destinationDirectory, DiskSpaceHelper.GetSize(archive).ToPrettySize(decimalPlaces: 1));
            return(ExtractArchive(archive, destinationDirectory, deleteOnFinish));
        }