-
Notifications
You must be signed in to change notification settings - Fork 103
Treat CAB files as containers #875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bricelam
wants to merge
1
commit into
dotnet:main
Choose a base branch
from
bricelam:cab
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE.txt file in the project root for more information. | ||
|
||
using Microsoft.Extensions.Logging; | ||
using WixToolset.Dtf.Compression; | ||
using WixToolset.Dtf.Compression.Cab; | ||
|
||
namespace Sign.Core | ||
{ | ||
internal class CabContainer : Container | ||
{ | ||
private readonly IDirectoryService _directoryService; | ||
private readonly ILogger _logger; | ||
private readonly FileInfo _cabFile; | ||
|
||
internal CabContainer( | ||
FileInfo cabFile, | ||
IDirectoryService directoryService, | ||
IFileMatcher fileMatcher, | ||
ILogger logger) | ||
: base(fileMatcher) | ||
{ | ||
ArgumentNullException.ThrowIfNull(cabFile, nameof(cabFile)); | ||
ArgumentNullException.ThrowIfNull(directoryService, nameof(directoryService)); | ||
ArgumentNullException.ThrowIfNull(logger, nameof(logger)); | ||
|
||
_directoryService = directoryService; | ||
_logger = logger; | ||
_cabFile = cabFile; | ||
} | ||
|
||
public override ValueTask OpenAsync() | ||
{ | ||
if (TemporaryDirectory is not null) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
TemporaryDirectory = new TemporaryDirectory(_directoryService); | ||
|
||
_logger.LogInformation( | ||
Resources.OpeningContainer, | ||
_cabFile.FullName, | ||
TemporaryDirectory.Directory.FullName); | ||
|
||
new CabInfo(_cabFile.FullName).Unpack(TemporaryDirectory.Directory.FullName); | ||
|
||
return ValueTask.CompletedTask; | ||
} | ||
|
||
public override ValueTask SaveAsync() | ||
{ | ||
if (TemporaryDirectory is null) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
_logger.LogInformation( | ||
Resources.SavingContainer, | ||
_cabFile.FullName, | ||
TemporaryDirectory.Directory.FullName); | ||
|
||
using (TemporaryDirectory temporaryDirectory = new(_directoryService)) | ||
{ | ||
string destinationFilePath = Path.Combine(temporaryDirectory.Directory.FullName, _cabFile.Name); | ||
|
||
new CabInfo(destinationFilePath).Pack( | ||
TemporaryDirectory.Directory.FullName, | ||
includeSubdirectories: true, | ||
CompressionLevel.Max, | ||
progressHandler: null); | ||
|
||
_cabFile.Delete(); | ||
|
||
File.Move(destinationFilePath, _cabFile.FullName, overwrite: true); | ||
|
||
_cabFile.Refresh(); | ||
} | ||
|
||
return ValueTask.CompletedTask; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE.txt file in the project root for more information. | ||
|
||
using System.Text; | ||
using Microsoft.Extensions.Logging; | ||
using Moq; | ||
using WixToolset.Dtf.Compression.Cab; | ||
|
||
namespace Sign.Core.Test | ||
{ | ||
public class CabContainerTests | ||
{ | ||
[Fact] | ||
public async Task OpenAsync_ExtractsCabToDirectory() | ||
{ | ||
string[] expectedFileNames = [".a", "b", "c.d"]; | ||
FileInfo cabFile = CreateCabFile(expectedFileNames); | ||
|
||
using (DirectoryServiceStub directoryService = new()) | ||
using (CabContainer container = new(cabFile, directoryService, Mock.Of<IFileMatcher>(), Mock.Of<ILogger>())) | ||
{ | ||
await container.OpenAsync(); | ||
|
||
FileInfo[] actualFiles = directoryService.Directories[0].GetFiles("*", SearchOption.AllDirectories); | ||
string[] actualFileNames = actualFiles | ||
.Select(file => file.FullName.Substring(directoryService.Directories[0].FullName.Length + 1)) | ||
.ToArray(); | ||
|
||
Assert.Equal(expectedFileNames, actualFileNames); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task SaveAsync_CompressesCabFromDirectory() | ||
{ | ||
string[] fileNames = ["a"]; | ||
FileInfo cabFile = CreateCabFile(fileNames); | ||
|
||
using (DirectoryServiceStub directoryService = new()) | ||
using (CabContainer container = new(cabFile, directoryService, Mock.Of<IFileMatcher>(), Mock.Of<ILogger>())) | ||
{ | ||
await container.OpenAsync(); | ||
|
||
File.WriteAllText(Path.Combine(directoryService.Directories[0].FullName, "b"), "b"); | ||
|
||
await container.SaveAsync(); | ||
} | ||
|
||
var cab = new CabInfo(cabFile.FullName); | ||
var files = cab.GetFiles(); | ||
Assert.Equal(2, files.Count); | ||
Assert.Contains(files, e => e.Name == "a"); | ||
Assert.Contains(files, e => e.Name == "b"); | ||
} | ||
|
||
private static FileInfo CreateCabFile(params string[] entryNames) | ||
{ | ||
FileInfo file = new(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())); | ||
|
||
var cab = new CabInfo(file.FullName); | ||
|
||
var sourceFiles = new List<string>(); | ||
foreach (string entryName in entryNames) | ||
{ | ||
var sourceFile = Path.GetTempFileName(); | ||
File.WriteAllBytes(sourceFile, Encoding.UTF8.GetBytes(entryName)); | ||
|
||
sourceFiles.Add(sourceFile); | ||
} | ||
|
||
cab.PackFiles(sourceDirectory: null, sourceFiles, entryNames); | ||
|
||
return file; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR should not be merged before this has been resolved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are the instructions for mirroring a package. Also be sure to revert this file.