Add copyright headers and cleanup

Added copyright headers to all the 'dotnet' code and cleaned up to match
repo style guidelines.
This commit is contained in:
Eric St. John 2016-06-10 16:18:42 -07:00 committed by Livar Cunha
parent 105e5ab051
commit 2b427bf7db
7 changed files with 82 additions and 104 deletions

View file

@ -1,4 +1,7 @@
using SevenZip; // Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using SevenZip;
using System; using System;
using System.IO; using System.IO;

View file

@ -8,42 +8,43 @@ namespace Microsoft.DotNet.Archive
{ {
public class ConsoleProgressReport : IProgress<ProgressReport> public class ConsoleProgressReport : IProgress<ProgressReport>
{ {
string currentPhase; private string _currentPhase;
int lastLineLength = 0; private int _lastLineLength = 0;
double lastProgress = -1; private double _lastProgress = -1;
Stopwatch stopwatch; private Stopwatch _stopwatch;
private object _stateLock = new object();
public void Report(ProgressReport value) public void Report(ProgressReport value)
{ {
long progress = (long)(100 * ((double)value.Ticks / value.Total)); long progress = (long)(100 * ((double)value.Ticks / value.Total));
if (progress == lastProgress && value.Phase == currentPhase) if (progress == _lastProgress && value.Phase == _currentPhase)
{ {
return; return;
} }
lastProgress = progress; _lastProgress = progress;
lock (this) lock (_stateLock)
{ {
string line = $"{value.Phase} {progress}%"; string line = $"{value.Phase} {progress}%";
if (value.Phase == currentPhase) if (value.Phase == _currentPhase)
{ {
Console.Write(new string('\b', lastLineLength)); Console.Write(new string('\b', _lastLineLength));
Console.Write(line); Console.Write(line);
lastLineLength = line.Length; _lastLineLength = line.Length;
if (progress == 100) if (progress == 100)
{ {
Console.WriteLine($" {stopwatch.ElapsedMilliseconds} ms"); Console.WriteLine($" {_stopwatch.ElapsedMilliseconds} ms");
} }
} }
else else
{ {
Console.Write(line); Console.Write(line);
currentPhase = value.Phase; _currentPhase = value.Phase;
lastLineLength = line.Length; _lastLineLength = line.Length;
stopwatch = Stopwatch.StartNew(); _stopwatch = Stopwatch.StartNew();
} }
} }
} }

View file

@ -1,4 +1,7 @@
using System; // Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
@ -47,24 +50,19 @@ namespace Microsoft.DotNet.Archive
// maps file hash to archve path // maps file hash to archve path
// $ prefix indicates that the file is not in the archive and path is a hash // $ prefix indicates that the file is not in the archive and path is a hash
Dictionary<string, ArchiveFileInfo> archiveFiles = new Dictionary<string, ArchiveFileInfo>(); private Dictionary<string, ArchiveFileInfo> _archiveFiles = new Dictionary<string, ArchiveFileInfo>();
// maps file hash to external path // maps file hash to external path
Dictionary<string, string> externalFiles = new Dictionary<string, string>(); private Dictionary<string, string> _externalFiles = new Dictionary<string, string>();
// lists all extracted files & hashes // lists all extracted files & hashes
List<DestinationFileInfo> destFiles = new List<DestinationFileInfo>(); private List<DestinationFileInfo> _destFiles = new List<DestinationFileInfo>();
bool disposed = false; private bool _disposed = false;
ThreadLocal<SHA256> sha = new ThreadLocal<SHA256>(() => SHA256.Create()); private ThreadLocal<SHA256> _sha = new ThreadLocal<SHA256>(() => SHA256.Create());
public IndexedArchive() public IndexedArchive()
{ { }
}
private static Stream CreateTemporaryStream() private static Stream CreateTemporaryStream()
{ {
// return new MemoryStream();
string temp = Path.GetTempPath(); string temp = Path.GetTempPath();
string tempFile = Path.Combine(temp, Guid.NewGuid().ToString()); string tempFile = Path.Combine(temp, Guid.NewGuid().ToString());
return File.Create(tempFile, 4096, FileOptions.DeleteOnClose); return File.Create(tempFile, 4096, FileOptions.DeleteOnClose);
@ -74,7 +72,6 @@ namespace Microsoft.DotNet.Archive
{ {
string temp = Path.GetTempPath(); string temp = Path.GetTempPath();
string tempFile = Path.Combine(temp, Guid.NewGuid().ToString()); string tempFile = Path.Combine(temp, Guid.NewGuid().ToString());
//return File.Create(tempFile, 4096, FileOptions.DeleteOnClose);
return new FileStream(tempFile, FileMode.Create, FileAccess.ReadWrite, FileShare.Read | FileShare.Delete, 4096, FileOptions.DeleteOnClose); return new FileStream(tempFile, FileMode.Create, FileAccess.ReadWrite, FileShare.Read | FileShare.Delete, 4096, FileOptions.DeleteOnClose);
} }
@ -82,8 +79,7 @@ namespace Microsoft.DotNet.Archive
{ {
CheckDisposed(); CheckDisposed();
//using (var archiveStream = CreateTemporaryStream()) using (var archiveStream = CreateTemporaryStream())
using (var archiveStream = File.Create(archivePath + ".zip"))
{ {
using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create, true)) using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create, true))
{ {
@ -107,10 +103,10 @@ namespace Microsoft.DotNet.Archive
using (var stream = indexEntry.Open()) using (var stream = indexEntry.Open())
using (var textWriter = new StreamWriter(stream)) using (var textWriter = new StreamWriter(stream))
{ {
foreach (var entry in destFiles) foreach (var entry in _destFiles)
{ {
var archiveFile = archiveFiles[entry.Hash]; var archiveFile = _archiveFiles[entry.Hash];
string archivePath = archiveFiles[entry.Hash].ArchivePath; string archivePath = _archiveFiles[entry.Hash].ArchivePath;
if (archiveFile.Stream == null) if (archiveFile.Stream == null)
{ {
archivePath = "$" + archivePath; archivePath = "$" + archivePath;
@ -121,7 +117,7 @@ namespace Microsoft.DotNet.Archive
} }
// sort the files so that similar files are close together // sort the files so that similar files are close together
var filesToArchive = archiveFiles.Values.ToList(); var filesToArchive = _archiveFiles.Values.ToList();
filesToArchive.Sort((f1, f2) => filesToArchive.Sort((f1, f2) =>
{ {
// first sort by extension // first sort by extension
@ -337,7 +333,7 @@ namespace Microsoft.DotNet.Archive
ExtractSource extractSource; ExtractSource extractSource;
if (!sourceCache.TryGetValue(source, out extractSource)) if (!sourceCache.TryGetValue(source, out extractSource))
{ {
sourceCache[source] = extractSource = new ExtractSource(source, externalFiles, tlArchive); sourceCache[source] = extractSource = new ExtractSource(source, _externalFiles, tlArchive);
} }
var zipSeperatorIndex = target.IndexOf("::", StringComparison.OrdinalIgnoreCase); var zipSeperatorIndex = target.IndexOf("::", StringComparison.OrdinalIgnoreCase);
@ -394,8 +390,8 @@ namespace Microsoft.DotNet.Archive
{ {
string hash = GetHash(fs); string hash = GetHash(fs);
// $ prefix indicates that the file is not in the archive and path is relative to an external directory // $ prefix indicates that the file is not in the archive and path is relative to an external directory
archiveFiles[hash] = new ArchiveFileInfo(null, "$" + hash , hash); _archiveFiles[hash] = new ArchiveFileInfo(null, "$" + hash , hash);
externalFiles[hash] = externalFile; _externalFiles[hash] = externalFile;
} }
} }
public void AddDirectory(string sourceDirectory, IProgress<ProgressReport> progress, string destinationDirectory = null) public void AddDirectory(string sourceDirectory, IProgress<ProgressReport> progress, string destinationDirectory = null)
@ -463,24 +459,20 @@ namespace Microsoft.DotNet.Archive
else else
{ {
var copy = CreateTemporaryStream(); var copy = CreateTemporaryStream();
#if NET45
hash = CopyWithHash(stream, copy);
#else
stream.CopyTo(copy); stream.CopyTo(copy);
copy.Seek(0, SeekOrigin.Begin); copy.Seek(0, SeekOrigin.Begin);
hash = GetHash(copy); hash = GetHash(copy);
#endif
stream.Dispose(); stream.Dispose();
stream = copy; stream = copy;
} }
lock (archiveFiles) lock (_archiveFiles)
{ {
destFiles.Add(new DestinationFileInfo(destinationPath, hash)); _destFiles.Add(new DestinationFileInfo(destinationPath, hash));
// see if we already have this file in the archive/external // see if we already have this file in the archive/external
ArchiveFileInfo existing = null; ArchiveFileInfo existing = null;
if (archiveFiles.TryGetValue(hash, out existing)) if (_archiveFiles.TryGetValue(hash, out existing))
{ {
// reduce memory pressure // reduce memory pressure
if (!(stream is MemoryStream) && (existing.Stream is MemoryStream)) if (!(stream is MemoryStream) && (existing.Stream is MemoryStream))
@ -502,39 +494,14 @@ namespace Microsoft.DotNet.Archive
stream.Seek(0, SeekOrigin.Begin); stream.Seek(0, SeekOrigin.Begin);
var archivePath = Path.Combine(hash, Path.GetFileName(destinationPath)); var archivePath = Path.Combine(hash, Path.GetFileName(destinationPath));
archiveFiles.Add(hash, new ArchiveFileInfo(stream, archivePath, hash)); _archiveFiles.Add(hash, new ArchiveFileInfo(stream, archivePath, hash));
} }
} }
} }
#if NET45
/// <summary>
/// Calculates the hash while copying the file to avoid multiple reads
/// </summary>
private const int _DefaultCopyBufferSize = 81920;
public string CopyWithHash(Stream source, Stream destination)
{
byte[] buffer = new byte[_DefaultCopyBufferSize];
int read;
while ((read = source.Read(buffer, 0, buffer.Length)) != 0)
{
sha.Value.TransformBlock(buffer, 0, read, null, 0);
destination.Write(buffer, 0, read);
}
sha.Value.TransformFinalBlock(buffer, 0, 0);
var hash = sha.Value.Hash;
// follow pattern in ComputeHash(stream) where it re-initializes after finishing.
sha.Value.Initialize();
return GetHashString(hash);
}
#endif
public string GetHash(Stream stream) public string GetHash(Stream stream)
{ {
var hashBytes = sha.Value.ComputeHash(stream); var hashBytes = _sha.Value.ComputeHash(stream);
return GetHashString(hashBytes); return GetHashString(hashBytes);
} }
@ -551,11 +518,11 @@ namespace Microsoft.DotNet.Archive
public void Dispose() public void Dispose()
{ {
if (!disposed) if (!_disposed)
{ {
if (archiveFiles != null) if (_archiveFiles != null)
{ {
foreach(var archiveFile in archiveFiles.Values) foreach(var archiveFile in _archiveFiles.Values)
{ {
if (archiveFile.Stream != null) if (archiveFile.Stream != null)
{ {
@ -565,17 +532,17 @@ namespace Microsoft.DotNet.Archive
} }
} }
if (sha != null) if (_sha != null)
{ {
sha.Dispose(); _sha.Dispose();
sha = null; _sha = null;
} }
} }
} }
private void CheckDisposed() private void CheckDisposed()
{ {
if (disposed) if (_disposed)
{ {
throw new ObjectDisposedException(nameof(IndexedArchive)); throw new ObjectDisposedException(nameof(IndexedArchive));
} }

View file

@ -1,24 +1,28 @@
using System; // Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
namespace Microsoft.DotNet.Archive namespace Microsoft.DotNet.Archive
{ {
public struct ProgressReport public struct ProgressReport
{ {
public string Phase; public ProgressReport(string phase, long ticks, long total)
public long Ticks; {
public long Total; Phase = phase;
Ticks = ticks;
Total = total;
}
public string Phase { get; }
public long Ticks { get; }
public long Total { get; }
} }
public static class ProgressReportExtensions public static class ProgressReportExtensions
{ {
public static void Report(this IProgress<ProgressReport> progress, string phase, long ticks, long total) public static void Report(this IProgress<ProgressReport> progress, string phase, long ticks, long total)
{ {
progress.Report(new ProgressReport() progress.Report(new ProgressReport(phase, ticks, total));
{
Phase = phase,
Ticks = ticks,
Total = total
});
} }
} }

View file

@ -1,15 +1,20 @@
using System; // Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Threading; using System.Threading;
namespace Microsoft.DotNet.Archive namespace Microsoft.DotNet.Archive
{ {
// wraps ThreadLocal<ZipArchive> and exposes Dispose semantics that dispose all archives /// <summary>
class ThreadLocalZipArchive : IDisposable /// Wraps ThreadLocal<ZipArchive> and exposes Dispose semantics that dispose all archives
/// </summary>
internal class ThreadLocalZipArchive : IDisposable
{ {
private ThreadLocal<ZipArchive> _archive; private ThreadLocal<ZipArchive> _archive;
private bool disposed = false; private bool _disposed = false;
public ThreadLocalZipArchive(string archivePath, ZipArchive local = null) public ThreadLocalZipArchive(string archivePath, ZipArchive local = null)
{ {
@ -28,7 +33,7 @@ namespace Microsoft.DotNet.Archive
public void Dispose() public void Dispose()
{ {
if (!disposed) if (!_disposed)
{ {
if (_archive != null) if (_archive != null)
{ {

View file

@ -5,15 +5,11 @@
}, },
"description": "Archive and compression types.", "description": "Archive and compression types.",
"dependencies": { "dependencies": {
"NETStandard.Library": "1.6.0-rc3-24201-00" "NETStandard.Library": "1.6.0-rc3-24201-00",
"System.Linq.Parallel": "4.0.1-rc3-24201-00"
}, },
"frameworks": { "frameworks": {
"net45": {}, "netstandard1.3": {}
"netstandard1.3": {
"dependencies": {
"System.Linq.Parallel": "4.0.1-rc3-24201-00"
}
}
}, },
"scripts": {} "scripts": {}
} }

View file

@ -1,4 +1,7 @@
using Microsoft.DotNet.Cli.CommandLine; // Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.DotNet.Cli.CommandLine;
//using Microsoft.DotNet.Cli.Utils; //using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Archive; using Microsoft.DotNet.Archive;
using System; using System;
@ -28,7 +31,6 @@ namespace Microsoft.DotNet.Tools.Archive
var externals = app.Option("--external <external>...", "External files and directories to consider for extraction", CommandOptionType.MultipleValue); var externals = app.Option("--external <external>...", "External files and directories to consider for extraction", CommandOptionType.MultipleValue);
var sources = app.Argument("<sources>...", "Files & directory to include in the archive", multipleValues:true); var sources = app.Argument("<sources>...", "Files & directory to include in the archive", multipleValues:true);
var dotnetNew = new ArchiveCommand();
app.OnExecute(() => { app.OnExecute(() => {
if (extract.HasValue() && sources.Values.Any()) if (extract.HasValue() && sources.Values.Any())
@ -79,7 +81,7 @@ namespace Microsoft.DotNet.Tools.Archive
archive.Save(archiveFile.Value(), progress); archive.Save(archiveFile.Value(), progress);
} }
else // extract.HasValue() else // sources not specified, extract must have been specified
{ {
archive.Extract(archiveFile.Value(), extract.Value(), progress); archive.Extract(archiveFile.Value(), extract.Value(), progress);