diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..22433c6 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,129 @@ +root = true +[*.cs] +file_header_template = Copyright (c). All rights reserved.\n\nLicensed under the MIT license. + +#### Naming styles #### + +# Naming rules + +dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion +dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface +dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i + +dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion +dotnet_naming_rule.types_should_be_pascal_case.symbols = types +dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case + +dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion +dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members +dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case + +# Symbol specifications + +dotnet_naming_symbols.interface.applicable_kinds = interface +dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.interface.required_modifiers = + +dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum +dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.types.required_modifiers = + +dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method +dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.non_field_members.required_modifiers = + +# Naming styles + +dotnet_naming_style.begins_with_i.required_prefix = I +dotnet_naming_style.begins_with_i.required_suffix = +dotnet_naming_style.begins_with_i.word_separator = +dotnet_naming_style.begins_with_i.capitalization = pascal_case + +dotnet_naming_style.pascal_case.required_prefix = +dotnet_naming_style.pascal_case.required_suffix = +dotnet_naming_style.pascal_case.word_separator = +dotnet_naming_style.pascal_case.capitalization = pascal_case + +dotnet_naming_style.pascal_case.required_prefix = +dotnet_naming_style.pascal_case.required_suffix = +dotnet_naming_style.pascal_case.word_separator = +dotnet_naming_style.pascal_case.capitalization = pascal_case +dotnet_style_coalesce_expression = true:suggestion +dotnet_style_null_propagation = true:suggestion +dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion +dotnet_style_prefer_auto_properties = true:silent +dotnet_style_object_initializer = true:suggestion +dotnet_style_collection_initializer = true:suggestion +dotnet_style_prefer_simplified_boolean_expressions = true:suggestion +dotnet_style_prefer_conditional_expression_over_assignment = true:silent +dotnet_style_prefer_conditional_expression_over_return = true:silent +dotnet_style_explicit_tuple_names = true:suggestion +dotnet_style_prefer_inferred_tuple_names = true:suggestion +dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion +dotnet_style_prefer_compound_assignment = true:suggestion +dotnet_style_prefer_simplified_interpolation = true:suggestion +dotnet_style_prefer_collection_expression = when_types_loosely_match:suggestion +dotnet_style_namespace_match_folder = true:suggestion +dotnet_style_operator_placement_when_wrapping = beginning_of_line +tab_width = 4 +indent_size = 4 +end_of_line = crlf + +csharp_using_directive_placement = outside_namespace:silent +csharp_prefer_simple_using_statement = true:error +csharp_prefer_braces = true:silent +csharp_style_namespace_declarations = block_scoped:silent +csharp_style_prefer_method_group_conversion = true:silent +csharp_style_prefer_top_level_statements = true:silent +csharp_style_prefer_primary_constructors = true:suggestion +csharp_prefer_system_threading_lock = true:suggestion +csharp_style_prefer_simple_property_accessors = true:suggestion +csharp_style_expression_bodied_methods = false:silent +csharp_style_expression_bodied_constructors = false:silent +csharp_style_expression_bodied_operators = false:silent +csharp_style_expression_bodied_properties = true:silent +csharp_style_expression_bodied_indexers = true:silent +csharp_style_expression_bodied_accessors = true:silent +csharp_style_expression_bodied_lambdas = true:silent +csharp_style_expression_bodied_local_functions = false:silent +csharp_style_throw_expression = true:suggestion +csharp_style_prefer_null_check_over_type_check = true:suggestion +csharp_prefer_simple_default_expression = true:suggestion +csharp_style_prefer_local_over_anonymous_function = true:suggestion +csharp_style_prefer_index_operator = true:suggestion +csharp_style_prefer_range_operator = true:suggestion +csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion +csharp_style_prefer_implicitly_typed_lambda_expression = true:suggestion +csharp_style_prefer_tuple_swap = true:suggestion +csharp_style_prefer_unbound_generic_type_in_nameof = true:suggestion +csharp_style_prefer_utf8_string_literals = true:suggestion +csharp_style_inlined_variable_declaration = true:suggestion +csharp_style_unused_value_assignment_preference = discard_variable:suggestion +csharp_style_deconstructed_variable_declaration = true:suggestion +csharp_style_unused_value_expression_statement_preference = discard_variable:silent +csharp_prefer_static_local_function = true:suggestion +[*.{cs,vb}] +dotnet_style_operator_placement_when_wrapping = beginning_of_line +tab_width = 4 +indent_size = 4 +end_of_line = crlf +dotnet_style_coalesce_expression = true:suggestion +dotnet_style_null_propagation = true:suggestion +dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion +dotnet_style_prefer_auto_properties = true:silent +dotnet_style_object_initializer = true:suggestion +dotnet_style_collection_initializer = true:suggestion +dotnet_style_prefer_simplified_boolean_expressions = true:suggestion +dotnet_style_prefer_conditional_expression_over_assignment = true:silent +dotnet_style_prefer_conditional_expression_over_return = true:silent +dotnet_style_explicit_tuple_names = true:suggestion +dotnet_style_prefer_inferred_tuple_names = true:suggestion +dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion +dotnet_style_prefer_compound_assignment = true:suggestion +dotnet_style_prefer_simplified_interpolation = true:suggestion +dotnet_style_prefer_collection_expression = when_types_loosely_match:suggestion +dotnet_style_namespace_match_folder = true:suggestion +dotnet_style_readonly_field = true:suggestion +dotnet_style_predefined_type_for_member_access = true:silent +dotnet_style_predefined_type_for_locals_parameters_members = true:silent +dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent \ No newline at end of file diff --git a/.github/dependabot.yml b/.github/dependabot.yml index dcde8dd..d78ebc9 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -13,3 +13,12 @@ updates: - "jeffkl" labels: - "maintenance" + + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "monthly" + assignees: + - "jeffkl" + labels: + - "maintenance" \ No newline at end of file diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml new file mode 100644 index 0000000..ff7b23b --- /dev/null +++ b/.github/workflows/CI.yml @@ -0,0 +1,52 @@ +name: CI + +on: + workflow_dispatch: + push: + branches: + - main + - rel/* + pull_request: + branches: + - main + - rel/* + +env: + ArtifactsDirectoryName: 'artifacts' + BuildConfiguration: 'Debug' + BuildPlatform: 'Any CPU' + ContinuousIntegrationBuild: 'true' + DotNet10Version: '10.x' + +jobs: + BuildAndTest: + name: Build and Test + runs-on: windows-latest + + steps: + - name: Checkout + uses: actions/checkout@v5 + with: + fetch-depth: 0 + + - name: Install .NET ${{ env.DotNet10Version }} + uses: actions/setup-dotnet@v5 + with: + dotnet-version: | + ${{ env.DotNet10Version }} + + - name: Build Solution + run: dotnet build "/Property:Platform=${{ env.BuildPlatform }};Configuration=${{ env.BuildConfiguration }}" "/BinaryLogger:${{ env.ArtifactsDirectoryName }}/build.binlog" + + - name: Run Unit Tests (.NET Framework) + run: dotnet test --no-restore --no-build --framework net472 "/Property:Platform=${{ env.BuildPlatform }};Configuration=${{ env.BuildConfiguration }}" "/BinaryLogger:${{ env.ArtifactsDirectoryName }}/test-net472.binlog" + + - name: Run Unit Tests (.NET 10) + run: dotnet test --no-restore --no-build --framework net10.0 "/Property:Platform=${{ env.BuildPlatform }};Configuration=${{ env.BuildConfiguration }}" "/BinaryLogger:${{ env.ArtifactsDirectoryName }}/test-net10.0.binlog" + + - name: Upload Artifacts + uses: actions/upload-artifact@v4 + if: success() + with: + name: artifacts + path: ${{ env.ArtifactsDirectoryName }} \ No newline at end of file diff --git a/.github/workflows/Official.yml b/.github/workflows/Official.yml new file mode 100644 index 0000000..f9e11dc --- /dev/null +++ b/.github/workflows/Official.yml @@ -0,0 +1,49 @@ +name: Official Build + +on: + workflow_dispatch: + push: + branches: + - main + - rel/* + tags: + - v*.*.* + +env: + ArtifactsDirectoryName: 'artifacts' + BuildConfiguration: 'Release' + BuildPlatform: 'Any CPU' + ContinuousIntegrationBuild: 'true' + DotNet10Version: '10.x' + IsOfficialBuild: 'true' + +jobs: + build: + runs-on: windows-latest + environment: Production + + steps: + - name: Checkout + uses: actions/checkout@v5 + with: + fetch-depth: 0 + + - name: Install .NET ${{ env.DotNet10Version }} + uses: actions/setup-dotnet@v5 + with: + dotnet-version: | + ${{ env.DotNet10Version }} + + - name: Build Solution + run: dotnet build "/Property:Platform=${{ env.BuildPlatform }};Configuration=${{ env.BuildConfiguration }}" "/BinaryLogger:${{ env.ArtifactsDirectoryName }}\build.binlog" + + - name: Upload Artifacts + uses: actions/upload-artifact@v4 + if: success() + with: + name: ${{ env.ArtifactsDirectoryName }} + path: ${{ env.ArtifactsDirectoryName }} + + - name: Push Packages + run: dotnet nuget push --skip-duplicate --api-key ${{ secrets.NuGetApiKey }} ${{ env.ArtifactsDirectoryName }}\**\*.nupkg + if: ${{ startsWith(github.ref, 'refs/tags/v') }} \ No newline at end of file diff --git a/Directory.Build.targets b/Directory.Build.targets index def4e3f..a5e59fc 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -16,5 +16,7 @@ Pack="true" PackagePath="/" Visible="false" /> + \ No newline at end of file diff --git a/Directory.Packages.props b/Directory.Packages.props index d56f936..3bac004 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -3,18 +3,14 @@ true - + - + - + - - - - \ No newline at end of file diff --git a/Microsoft.Wim.sln b/Microsoft.Wim.sln index 828e827..684eada 100644 --- a/Microsoft.Wim.sln +++ b/Microsoft.Wim.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.1.32421.90 +# Visual Studio Version 18 +VisualStudioVersion = 18.3.11301.21 main MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Wim", "src\Microsoft.Wim\Microsoft.Wim.csproj", "{2D098368-E3DE-4C16-B5BD-6223A6476483}" EndProject @@ -9,6 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Wim.Tests", "src\ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{891ED303-4B28-422E-8D68-4A64CB5F5827}" ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig .gitignore = .gitignore azure-pipelines.yml = azure-pipelines.yml CONTRIBUTING.md = CONTRIBUTING.md @@ -21,11 +22,21 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution LICENSE = LICENSE NuGet.config = NuGet.config README.md = README.md - stylecop.json = stylecop.json version.json = version.json wimgapi.h = wimgapi.h EndProjectSection EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{34EB6BB4-DE96-4167-A4A0-46A25303E224}" + ProjectSection(SolutionItems) = preProject + .github\dependabot.yml = .github\dependabot.yml + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{85084096-A738-4A8D-A87B-5BCC651E19DC}" + ProjectSection(SolutionItems) = preProject + .github\workflows\CI.yml = .github\workflows\CI.yml + .github\workflows\Official.yml = .github\workflows\Official.yml + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -44,6 +55,10 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {34EB6BB4-DE96-4167-A4A0-46A25303E224} = {891ED303-4B28-422E-8D68-4A64CB5F5827} + {85084096-A738-4A8D-A87B-5BCC651E19DC} = {34EB6BB4-DE96-4167-A4A0-46A25303E224} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A5781377-D2DB-4477-BC26-001AA4D65A21} EndGlobalSection diff --git a/azure-pipelines.yml b/azure-pipelines.yml deleted file mode 100644 index 8fe5a7e..0000000 --- a/azure-pipelines.yml +++ /dev/null @@ -1,90 +0,0 @@ -resources: -- repo: self - -variables: - ArtifactsDirectoryName: 'artifacts' - BuildConfiguration: 'Debug' - BuildPlatform: 'Any CPU' - DotNet7Version: '7.x' - DotNet8Version: '8.x' - ContinuousIntegrationBuild: 'true' - MSBuildArguments: '"/BinaryLogger:$(Build.SourcesDirectory)\$(ArtifactsDirectoryName)\msbuild.binlog"' - CommonTestArguments: '/noautorsp --no-restore --no-build --configuration:$(BuildConfiguration)' - DOTNET_CLI_TELEMETRY_OPTOUT: 'true' - DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 'true' - DOTNET_MULTILEVEL_LOOKUP: 'false' - -trigger: - batch: 'true' - branches: - include: - - 'main' - - 'refs/tags/*' - paths: - exclude: - - '*.md' -pr: - branches: - include: - - 'main' - paths: - exclude: - - '*.md' - -jobs: -- job: BuildAndTest - displayName: 'Build and Test' - pool: - vmImage: windows-latest - steps: - - script: 'echo ##vso[task.setvariable variable=BuildConfiguration;]Release' - displayName: 'Set BuildConfiguration to Release for tagged commits' - condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/v')) - - - task: UseDotNet@2 - displayName: 'Install .NET $(DotNet7Version)' - inputs: - version: '$(DotNet7Version)' - - - task: UseDotNet@2 - displayName: 'Install .NET $(DotNet8Version)' - inputs: - version: '$(DotNet8Version)' - - - task: MSBuild@1 - displayName: 'Build Solution' - inputs: - platform: '$(BuildPlatform)' - configuration: '$(BuildConfiguration)' - msbuildArguments: '$(MSBuildArguments)' - - - task: DotNetCoreCLI@2 - displayName: 'Run Unit Tests (.NET Framework)' - inputs: - command: 'test' - arguments: '$(CommonTestArguments) --framework net472' - testRunTitle: '.NET Framework v4.7.2' - condition: succeededOrFailed() - - - task: DotNetCoreCLI@2 - displayName: 'Run Unit Tests (.NET 7.0)' - inputs: - command: 'test' - arguments: '$(CommonTestArguments) --framework net7.0' - testRunTitle: '.NET 7.0' - condition: succeededOrFailed() - - - task: DotNetCoreCLI@2 - displayName: 'Run Unit Tests (.NET 8.0)' - inputs: - command: 'test' - arguments: '$(CommonTestArguments) --framework net8.0' - testRunTitle: '.NET 8.0' - condition: succeededOrFailed() - - - task: PublishBuildArtifacts@1 - displayName: 'Publish Artifacts' - inputs: - PathtoPublish: '$(ArtifactsDirectoryName)' - ArtifactName: $(ArtifactsDirectoryName) - condition: always() \ No newline at end of file diff --git a/src/GlobalSuppressions.cs b/src/GlobalSuppressions.cs deleted file mode 100644 index af9b3ec..0000000 Binary files a/src/GlobalSuppressions.cs and /dev/null differ diff --git a/src/Microsoft.Wim.Tests/ApplyImageTests.cs b/src/Microsoft.Wim.Tests/ApplyImageTests.cs index 539b993..b75fa78 100644 --- a/src/Microsoft.Wim.Tests/ApplyImageTests.cs +++ b/src/Microsoft.Wim.Tests/ApplyImageTests.cs @@ -26,10 +26,8 @@ public void ApplyImageTest() { WimgApi.SetTemporaryPath(wimHandle, TempPath); - using (WimHandle imageHandle = WimgApi.LoadImage(wimHandle, 1)) - { - WimgApi.ApplyImage(imageHandle, ApplyPath, WimApplyImageOptions.Index | WimApplyImageOptions.DisableDirectoryAcl | WimApplyImageOptions.DisableFileAcl | WimApplyImageOptions.DisableRPFix); - } + using WimHandle imageHandle = WimgApi.LoadImage(wimHandle, 1); + WimgApi.ApplyImage(imageHandle, ApplyPath, WimApplyImageOptions.Index | WimApplyImageOptions.DisableDirectoryAcl | WimApplyImageOptions.DisableFileAcl | WimApplyImageOptions.DisableRPFix); } Directory.EnumerateFiles(ApplyPath).Count().ShouldBe(TestWimTemplate.FileCount); @@ -38,28 +36,24 @@ public void ApplyImageTest() [Fact] public void ApplyImageTest_Abort() { - using (WimHandle wimHandle = WimgApi.CreateFile(TestWimPath, WimFileAccess.Read, WimCreationDisposition.OpenExisting, WimCreateFileOptions.None, WimCompressionType.None)) - { - WimgApi.SetTemporaryPath(wimHandle, TempPath); + using WimHandle wimHandle = WimgApi.CreateFile(TestWimPath, WimFileAccess.Read, WimCreationDisposition.OpenExisting, WimCreateFileOptions.None, WimCompressionType.None); + WimgApi.SetTemporaryPath(wimHandle, TempPath); - WimMessageResult MessageCallback(WimMessageType messageType, object message, object? userData) => messageType == WimMessageType.Process ? WimMessageResult.Abort : WimMessageResult.Done; + WimMessageResult MessageCallback(WimMessageType messageType, object message, object? userData) => messageType == WimMessageType.Process ? WimMessageResult.Abort : WimMessageResult.Done; - WimgApi.RegisterMessageCallback(wimHandle, MessageCallback); + WimgApi.RegisterMessageCallback(wimHandle, MessageCallback); - try - { - using (WimHandle imageHandle = WimgApi.LoadImage(wimHandle, 1)) - { - WimHandle imageHandleCopy = imageHandle; + try + { + using WimHandle imageHandle = WimgApi.LoadImage(wimHandle, 1); + WimHandle imageHandleCopy = imageHandle; - Should.Throw(() => - WimgApi.ApplyImage(imageHandleCopy, ApplyPath, WimApplyImageOptions.NoApply)); - } - } - finally - { - WimgApi.UnregisterMessageCallback(wimHandle, MessageCallback); - } + Should.Throw(() => + WimgApi.ApplyImage(imageHandleCopy, ApplyPath, WimApplyImageOptions.NoApply)); + } + finally + { + WimgApi.UnregisterMessageCallback(wimHandle, MessageCallback); } } @@ -84,10 +78,8 @@ WimMessageResult MessageCallback(WimMessageType messageType, object message, obj try { - using (WimHandle imageHandle = WimgApi.LoadImage(wimHandle, 1)) - { - WimgApi.ApplyImage(imageHandle, ApplyPath, WimApplyImageOptions.NoApply); - } + using WimHandle imageHandle = WimgApi.LoadImage(wimHandle, 1); + WimgApi.ApplyImage(imageHandle, ApplyPath, WimApplyImageOptions.NoApply); } finally { diff --git a/src/Microsoft.Wim.Tests/CaptureImageTests.cs b/src/Microsoft.Wim.Tests/CaptureImageTests.cs index 3c745fc..0918437 100644 --- a/src/Microsoft.Wim.Tests/CaptureImageTests.cs +++ b/src/Microsoft.Wim.Tests/CaptureImageTests.cs @@ -22,17 +22,13 @@ public CaptureImageTests(TestWimTemplate template) [Fact] public void CaptureImageTest() { - using (WimHandle wimHandle = WimgApi.CreateFile(CaptureWimPath, WimFileAccess.Write, WimCreationDisposition.CreateAlways, WimCreateFileOptions.None, WimCompressionType.Xpress)) - { - WimgApi.SetTemporaryPath(wimHandle, TempPath); + using WimHandle wimHandle = WimgApi.CreateFile(CaptureWimPath, WimFileAccess.Write, WimCreationDisposition.CreateAlways, WimCreateFileOptions.None, WimCompressionType.Xpress); + WimgApi.SetTemporaryPath(wimHandle, TempPath); - using (WimHandle imageHandle = WimgApi.CaptureImage(wimHandle, CapturePath, WimCaptureImageOptions.None)) - { - int imageCount = WimgApi.GetImageCount(wimHandle); + using WimHandle imageHandle = WimgApi.CaptureImage(wimHandle, CapturePath, WimCaptureImageOptions.None); + int imageCount = WimgApi.GetImageCount(wimHandle); - imageCount.ShouldBe(1); - } - } + imageCount.ShouldBe(1); } [Fact] @@ -68,9 +64,7 @@ public void CaptureImageWithCallbackTest() WimgApi.RegisterMessageCallback(wimHandle, CaptureImageWithCallbackTestCallback, userData); try { - using (WimHandle imageHandle = WimgApi.CaptureImage(wimHandle, CapturePath, WimCaptureImageOptions.None)) - { - } + using WimHandle imageHandle = WimgApi.CaptureImage(wimHandle, CapturePath, WimCaptureImageOptions.None); } finally { diff --git a/src/Microsoft.Wim.Tests/CommitImageHandleTests.cs b/src/Microsoft.Wim.Tests/CommitImageHandleTests.cs index f859d1d..7f52548 100644 --- a/src/Microsoft.Wim.Tests/CommitImageHandleTests.cs +++ b/src/Microsoft.Wim.Tests/CommitImageHandleTests.cs @@ -18,43 +18,39 @@ public CommitImageHandleTests(TestWimTemplate template) [Fact] public void CommitImageHandleTest() { - using (WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) - { - WimgApi.MountImage(imageHandle, MountPath, WimMountImageOptions.Fast | WimMountImageOptions.DisableDirectoryAcl | WimMountImageOptions.DisableFileAcl | WimMountImageOptions.DisableRPFix); + using WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1); + WimgApi.MountImage(imageHandle, MountPath, WimMountImageOptions.Fast | WimMountImageOptions.DisableDirectoryAcl | WimMountImageOptions.DisableFileAcl | WimMountImageOptions.DisableRPFix); - try - { - using (WimgApi.CommitImageHandle(imageHandle, false, WimCommitImageOptions.DisableDirectoryAcl | WimCommitImageOptions.DisableFileAcl | WimCommitImageOptions.DisableRPFix)) - { - WimgApi.GetImageCount(TestWimHandle).ShouldBe(TestWimTemplate.ImageCount); - } - } - finally + try + { + using (WimgApi.CommitImageHandle(imageHandle, false, WimCommitImageOptions.DisableDirectoryAcl | WimCommitImageOptions.DisableFileAcl | WimCommitImageOptions.DisableRPFix)) { - WimgApi.UnmountImage(imageHandle); + WimgApi.GetImageCount(TestWimHandle).ShouldBe(TestWimTemplate.ImageCount); } } + finally + { + WimgApi.UnmountImage(imageHandle); + } } [Fact] public void CommitImageHandleTest_Append() { - using (WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) - { - WimgApi.MountImage(imageHandle, MountPath, WimMountImageOptions.Fast | WimMountImageOptions.DisableDirectoryAcl | WimMountImageOptions.DisableFileAcl | WimMountImageOptions.DisableRPFix); + using WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1); + WimgApi.MountImage(imageHandle, MountPath, WimMountImageOptions.Fast | WimMountImageOptions.DisableDirectoryAcl | WimMountImageOptions.DisableFileAcl | WimMountImageOptions.DisableRPFix); - try - { - using (WimgApi.CommitImageHandle(imageHandle, true, WimCommitImageOptions.DisableDirectoryAcl | WimCommitImageOptions.DisableFileAcl | WimCommitImageOptions.DisableRPFix)) - { - WimgApi.GetImageCount(TestWimHandle).ShouldBe(TestWimTemplate.ImageCount + 1); - } - } - finally + try + { + using (WimgApi.CommitImageHandle(imageHandle, true, WimCommitImageOptions.DisableDirectoryAcl | WimCommitImageOptions.DisableFileAcl | WimCommitImageOptions.DisableRPFix)) { - WimgApi.UnmountImage(imageHandle); + WimgApi.GetImageCount(TestWimHandle).ShouldBe(TestWimTemplate.ImageCount + 1); } } + finally + { + WimgApi.UnmountImage(imageHandle); + } } [Fact] diff --git a/src/Microsoft.Wim.Tests/CreateFileTests.cs b/src/Microsoft.Wim.Tests/CreateFileTests.cs index c93aca7..7ef2508 100644 --- a/src/Microsoft.Wim.Tests/CreateFileTests.cs +++ b/src/Microsoft.Wim.Tests/CreateFileTests.cs @@ -24,9 +24,7 @@ public CreateFileTests(TestWimTemplate template) [InlineData(WimCompressionType.Xpress)] public void CreateFileTest(WimCompressionType compressionType) { - using (WimHandle wimHandle = WimgApi.CreateFile(CreateWimPath, WimFileAccess.Write, WimCreationDisposition.CreateAlways, WimCreateFileOptions.None, compressionType)) - { - } + using WimHandle wimHandle = WimgApi.CreateFile(CreateWimPath, WimFileAccess.Write, WimCreationDisposition.CreateAlways, WimCreateFileOptions.None, compressionType); } [Fact] diff --git a/src/Microsoft.Wim.Tests/ExportImageTests.cs b/src/Microsoft.Wim.Tests/ExportImageTests.cs index 640854a..abf2378 100644 --- a/src/Microsoft.Wim.Tests/ExportImageTests.cs +++ b/src/Microsoft.Wim.Tests/ExportImageTests.cs @@ -23,12 +23,10 @@ public void ExportEsdFromWimImageTest() using (WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) { - using (WimHandle wimHandle = WimgApi.CreateFile(exportWimPath, WimFileAccess.Write, WimCreationDisposition.CreateAlways, WimCreateFileOptions.Chunked, WimCompressionType.Lzms)) - { - WimgApi.SetTemporaryPath(wimHandle, TempPath); + using WimHandle wimHandle = WimgApi.CreateFile(exportWimPath, WimFileAccess.Write, WimCreationDisposition.CreateAlways, WimCreateFileOptions.Chunked, WimCompressionType.Lzms); + WimgApi.SetTemporaryPath(wimHandle, TempPath); - WimgApi.ExportImage(imageHandle, wimHandle, WimExportImageOptions.None); - } + WimgApi.ExportImage(imageHandle, wimHandle, WimExportImageOptions.None); } File.Exists(exportWimPath).ShouldBeTrue(); @@ -53,12 +51,10 @@ public void ExportImageTest() using (WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) { - using (WimHandle wimHandle = WimgApi.CreateFile(exportWimPath, WimFileAccess.Write, WimCreationDisposition.CreateAlways, WimCreateFileOptions.None, WimCompressionType.Lzx)) - { - WimgApi.SetTemporaryPath(wimHandle, TempPath); + using WimHandle wimHandle = WimgApi.CreateFile(exportWimPath, WimFileAccess.Write, WimCreationDisposition.CreateAlways, WimCreateFileOptions.None, WimCompressionType.Lzx); + WimgApi.SetTemporaryPath(wimHandle, TempPath); - WimgApi.ExportImage(imageHandle, wimHandle, WimExportImageOptions.None); - } + WimgApi.ExportImage(imageHandle, wimHandle, WimExportImageOptions.None); } File.Exists(exportWimPath).ShouldBeTrue(); @@ -76,13 +72,11 @@ public void ExportImageTest_ThrowsArgumentNullException_imageHandle() [Fact] public void ExportImageTest_ThrowsArgumentNullException_wimHandle() { - using (WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) - { - WimHandle imageHandleCopy = imageHandle; + using WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1); + WimHandle imageHandleCopy = imageHandle; - ShouldThrow("wimHandle", () => - WimgApi.ExportImage(imageHandleCopy, wimHandle: null!, WimExportImageOptions.None)); - } + ShouldThrow("wimHandle", () => + WimgApi.ExportImage(imageHandleCopy, wimHandle: null!, WimExportImageOptions.None)); } [Fact] @@ -92,12 +86,10 @@ public void ExportWimFromEsdImageTest() using (WimHandle imageHandle = WimgApi.LoadImage(TestEsdHandle, 1)) { - using (WimHandle wimHandle = WimgApi.CreateFile(exportWimPath, WimFileAccess.Write, WimCreationDisposition.CreateAlways, WimCreateFileOptions.None, WimCompressionType.None)) - { - WimgApi.SetTemporaryPath(wimHandle, TempPath); + using WimHandle wimHandle = WimgApi.CreateFile(exportWimPath, WimFileAccess.Write, WimCreationDisposition.CreateAlways, WimCreateFileOptions.None, WimCompressionType.None); + WimgApi.SetTemporaryPath(wimHandle, TempPath); - WimgApi.ExportImage(imageHandle, wimHandle, WimExportImageOptions.None); - } + WimgApi.ExportImage(imageHandle, wimHandle, WimExportImageOptions.None); } File.Exists(exportWimPath).ShouldBeTrue(); diff --git a/src/Microsoft.Wim.Tests/ExtractImagePathTests.cs b/src/Microsoft.Wim.Tests/ExtractImagePathTests.cs index 2fec523..ca25920 100644 --- a/src/Microsoft.Wim.Tests/ExtractImagePathTests.cs +++ b/src/Microsoft.Wim.Tests/ExtractImagePathTests.cs @@ -22,34 +22,30 @@ public ExtractImagePathTests(TestWimTemplate template) [Fact] public void ExtractImagePathTest() { - using (WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) + using WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1); + foreach (string file in GetImageFiles(imageHandle).Take(10)) { - foreach (string file in GetImageFiles(imageHandle).Take(10)) - { - string fileName = Path.GetFileName(file); + string fileName = Path.GetFileName(file); - fileName.ShouldNotBeNull(); + fileName.ShouldNotBeNull(); - // ReSharper disable once AssignNullToNotNullAttribute - string destinationPath = Path.Combine(TestDirectory, fileName); + // ReSharper disable once AssignNullToNotNullAttribute + string destinationPath = Path.Combine(TestDirectory, fileName); - WimgApi.ExtractImagePath(imageHandle, file, destinationPath); + WimgApi.ExtractImagePath(imageHandle, file, destinationPath); - File.Exists(destinationPath).ShouldBeTrue(); - } + File.Exists(destinationPath).ShouldBeTrue(); } } [Fact] public void ExtractImagePathTest_ThrowsArgumentNullException_destinationFile() { - using (WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) - { - WimHandle imageHandleCopy = imageHandle; + using WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1); + WimHandle imageHandleCopy = imageHandle; - ShouldThrow("destinationFile", () => - WimgApi.ExtractImagePath(imageHandleCopy, string.Empty, destinationFile: null!)); - } + ShouldThrow("destinationFile", () => + WimgApi.ExtractImagePath(imageHandleCopy, string.Empty, destinationFile: null!)); } [Fact] @@ -62,13 +58,11 @@ public void ExtractImagePathTest_ThrowsArgumentNullException_imageHandle() [Fact] public void ExtractImagePathTest_ThrowsArgumentNullException_sourceFile() { - using (WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) - { - WimHandle imageHandleCopy = imageHandle; + using WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1); + WimHandle imageHandleCopy = imageHandle; - ShouldThrow("sourceFile", () => - WimgApi.ExtractImagePath(imageHandleCopy, sourceFile: null!, string.Empty)); - } + ShouldThrow("sourceFile", () => + WimgApi.ExtractImagePath(imageHandleCopy, sourceFile: null!, string.Empty)); } private IEnumerable GetImageFiles(WimHandle imageHandle) diff --git a/src/Microsoft.Wim.Tests/LoadImageTests.cs b/src/Microsoft.Wim.Tests/LoadImageTests.cs index 53aee31..d63f598 100644 --- a/src/Microsoft.Wim.Tests/LoadImageTests.cs +++ b/src/Microsoft.Wim.Tests/LoadImageTests.cs @@ -51,14 +51,12 @@ public void DeleteImageTest_ThrowsIndexOutOfRangeException_indexZero() [Fact] public void LoadImageTest() { - using (WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) - { - imageHandle.ShouldNotBeNull(); + using WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1); + imageHandle.ShouldNotBeNull(); - imageHandle.IsInvalid.ShouldBeFalse(); + imageHandle.IsInvalid.ShouldBeFalse(); - imageHandle.IsClosed.ShouldBeFalse(); - } + imageHandle.IsClosed.ShouldBeFalse(); } [Fact] diff --git a/src/Microsoft.Wim.Tests/Microsoft.Wim.Tests.csproj b/src/Microsoft.Wim.Tests/Microsoft.Wim.Tests.csproj index 96d0293..37c3f17 100644 --- a/src/Microsoft.Wim.Tests/Microsoft.Wim.Tests.csproj +++ b/src/Microsoft.Wim.Tests/Microsoft.Wim.Tests.csproj @@ -1,6 +1,6 @@  - net472;net7.0;net8.0 + net472;net10.0 $(NoWarn);SA0001;RS0016;SA1600 diff --git a/src/Microsoft.Wim.Tests/MountImageTests.cs b/src/Microsoft.Wim.Tests/MountImageTests.cs index 791a2b8..482eccf 100644 --- a/src/Microsoft.Wim.Tests/MountImageTests.cs +++ b/src/Microsoft.Wim.Tests/MountImageTests.cs @@ -25,28 +25,26 @@ public void GetMountedImageHandleTest_ReadOnly() { ExecuteAgainstMountedImage((wimHandle, imageHandle) => { - using (WimHandle actualWimHandle = WimgApi.GetMountedImageHandle(MountPath, true, out WimHandle actualImageHandle)) + using WimHandle actualWimHandle = WimgApi.GetMountedImageHandle(MountPath, true, out WimHandle actualImageHandle); + try { - try - { - actualWimHandle.ShouldNotBeNull(); - - actualImageHandle.ShouldNotBeNull(); - - WimMountInfo wimMountInfo = WimgApi.GetMountedImageInfoFromHandle(actualImageHandle); - - wimMountInfo.ShouldNotBeNull(); - - wimMountInfo.ImageIndex.ShouldBe(1); - wimMountInfo.MountPath.ShouldBe(MountPath, StringCompareShould.IgnoreCase); - wimMountInfo.Path.ShouldBe(TestWimPath); - wimMountInfo.ReadOnly.ShouldBeTrue(); - wimMountInfo.State.ShouldBe(WimMountPointState.Mounted); - } - finally - { - actualImageHandle.Dispose(); - } + actualWimHandle.ShouldNotBeNull(); + + actualImageHandle.ShouldNotBeNull(); + + WimMountInfo wimMountInfo = WimgApi.GetMountedImageInfoFromHandle(actualImageHandle); + + wimMountInfo.ShouldNotBeNull(); + + wimMountInfo.ImageIndex.ShouldBe(1); + wimMountInfo.MountPath.ShouldBe(MountPath, StringCompareShould.IgnoreCase); + wimMountInfo.Path.ShouldBe(TestWimPath); + wimMountInfo.ReadOnly.ShouldBeTrue(); + wimMountInfo.State.ShouldBe(WimMountPointState.Mounted); + } + finally + { + actualImageHandle.Dispose(); } }); } @@ -56,28 +54,26 @@ public void GetMountedImageHandleTest_ReadWrite() { ExecuteAgainstMountedImage((wimHandle, imageHandle) => { - using (WimHandle actualWimHandle = WimgApi.GetMountedImageHandle(MountPath, false, out WimHandle actualImageHandle)) + using WimHandle actualWimHandle = WimgApi.GetMountedImageHandle(MountPath, false, out WimHandle actualImageHandle); + try { - try - { - actualWimHandle.ShouldNotBeNull(); - - actualImageHandle.ShouldNotBeNull(); - - WimMountInfo wimMountInfo = WimgApi.GetMountedImageInfoFromHandle(actualImageHandle); - - wimMountInfo.ShouldNotBeNull(); - - wimMountInfo.ImageIndex.ShouldBe(1); - wimMountInfo.MountPath.ShouldBe(MountPath, StringCompareShould.IgnoreCase); - wimMountInfo.Path.ShouldBe(TestWimPath); - wimMountInfo.ReadOnly.ShouldBeTrue(); - wimMountInfo.State.ShouldBe(WimMountPointState.Mounted); - } - finally - { - actualImageHandle.Dispose(); - } + actualWimHandle.ShouldNotBeNull(); + + actualImageHandle.ShouldNotBeNull(); + + WimMountInfo wimMountInfo = WimgApi.GetMountedImageInfoFromHandle(actualImageHandle); + + wimMountInfo.ShouldNotBeNull(); + + wimMountInfo.ImageIndex.ShouldBe(1); + wimMountInfo.MountPath.ShouldBe(MountPath, StringCompareShould.IgnoreCase); + wimMountInfo.Path.ShouldBe(TestWimPath); + wimMountInfo.ReadOnly.ShouldBeTrue(); + wimMountInfo.State.ShouldBe(WimMountPointState.Mounted); + } + finally + { + actualImageHandle.Dispose(); } }); } @@ -187,25 +183,21 @@ public void MountImageHandleTest_ThrowsArgumentNullException_imageHandle() [Fact] public void MountImageHandleTest_ThrowsArgumentNullException_mountPath() { - using (WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) - { - WimHandle imageHandleCopy = imageHandle; + using WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1); + WimHandle imageHandleCopy = imageHandle; - ShouldThrow("mountPath", () => - WimgApi.MountImage(imageHandleCopy, mountPath: null!, WimMountImageOptions.None)); - } + ShouldThrow("mountPath", () => + WimgApi.MountImage(imageHandleCopy, mountPath: null!, WimMountImageOptions.None)); } [Fact] public void MountImageHandleTest_ThrowsDirectoryNotFoundException_mountPath() { - using (WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) - { - WimHandle imageHandleCopy = imageHandle; + using WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1); + WimHandle imageHandleCopy = imageHandle; - Should.Throw(() => - WimgApi.MountImage(imageHandleCopy, Path.Combine(TestDirectory, Guid.NewGuid().ToString()), WimMountImageOptions.None)); - } + Should.Throw(() => + WimgApi.MountImage(imageHandleCopy, Path.Combine(TestDirectory, Guid.NewGuid().ToString()), WimMountImageOptions.None)); } [Fact] @@ -331,30 +323,26 @@ internal static void ExecuteAgainstMountedImage(string imagePath, string mountPa internal static void ExecuteAgainstMountedImage(string imagePath, string mountPath, string tempPath, bool readOnly, Action action) { - using (WimHandle wimHandle = WimgApi.CreateFile(imagePath, WimFileAccess.Read | WimFileAccess.Write | WimFileAccess.Mount, WimCreationDisposition.OpenExisting, WimCreateFileOptions.None, WimCompressionType.None)) + using WimHandle wimHandle = WimgApi.CreateFile(imagePath, WimFileAccess.Read | WimFileAccess.Write | WimFileAccess.Mount, WimCreationDisposition.OpenExisting, WimCreateFileOptions.None, WimCompressionType.None); + WimgApi.SetTemporaryPath(wimHandle, tempPath); + + using WimHandle imageHandle = WimgApi.LoadImage(wimHandle, 1); + WimMountImageOptions flags = WimMountImageOptions.Fast | WimMountImageOptions.DisableDirectoryAcl | WimMountImageOptions.DisableFileAcl | WimMountImageOptions.DisableRPFix; + + if (readOnly) { - WimgApi.SetTemporaryPath(wimHandle, tempPath); + flags |= WimMountImageOptions.ReadOnly; + } - using (WimHandle imageHandle = WimgApi.LoadImage(wimHandle, 1)) - { - WimMountImageOptions flags = WimMountImageOptions.Fast | WimMountImageOptions.DisableDirectoryAcl | WimMountImageOptions.DisableFileAcl | WimMountImageOptions.DisableRPFix; - - if (readOnly) - { - flags |= WimMountImageOptions.ReadOnly; - } - - WimgApi.MountImage(imageHandle, mountPath, flags); - - try - { - action?.Invoke(wimHandle, imageHandle); - } - finally - { - WimgApi.UnmountImage(imageHandle); - } - } + WimgApi.MountImage(imageHandle, mountPath, flags); + + try + { + action?.Invoke(wimHandle, imageHandle); + } + finally + { + WimgApi.UnmountImage(imageHandle); } } diff --git a/src/Microsoft.Wim.Tests/RemountImageTests.cs b/src/Microsoft.Wim.Tests/RemountImageTests.cs index f163543..011dff8 100644 --- a/src/Microsoft.Wim.Tests/RemountImageTests.cs +++ b/src/Microsoft.Wim.Tests/RemountImageTests.cs @@ -22,26 +22,24 @@ public RemountImageTests(TestWimTemplate template) [Fact] public void RemountImageTest() { - using (WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1)) - { - WimgApi.MountImage(imageHandle, MountPath, WimMountImageOptions.Fast | WimMountImageOptions.DisableDirectoryAcl | WimMountImageOptions.DisableFileAcl | WimMountImageOptions.DisableRPFix | WimMountImageOptions.ReadOnly); + using WimHandle imageHandle = WimgApi.LoadImage(TestWimHandle, 1); + WimgApi.MountImage(imageHandle, MountPath, WimMountImageOptions.Fast | WimMountImageOptions.DisableDirectoryAcl | WimMountImageOptions.DisableFileAcl | WimMountImageOptions.DisableRPFix | WimMountImageOptions.ReadOnly); - try - { - VerifyMountState(imageHandle, WimMountPointState.Mounted); + try + { + VerifyMountState(imageHandle, WimMountPointState.Mounted); - KillWimServ(); + KillWimServ(); - VerifyMountState(imageHandle, WimMountPointState.Remountable); + VerifyMountState(imageHandle, WimMountPointState.Remountable); - WimgApi.RemountImage(MountPath); + WimgApi.RemountImage(MountPath); - VerifyMountState(imageHandle, WimMountPointState.Mounted); - } - finally - { - WimgApi.UnmountImage(imageHandle); - } + VerifyMountState(imageHandle, WimMountPointState.Mounted); + } + finally + { + WimgApi.UnmountImage(imageHandle); } } diff --git a/src/Microsoft.Wim.Tests/SplitFileTests.cs b/src/Microsoft.Wim.Tests/SplitFileTests.cs index dddc37d..d6ef7c3 100644 --- a/src/Microsoft.Wim.Tests/SplitFileTests.cs +++ b/src/Microsoft.Wim.Tests/SplitFileTests.cs @@ -23,14 +23,12 @@ public void SetReferenceFileTest() { string[] splitWims = CreateSplitWim().ToArray(); - using (WimHandle wimHandle = WimgApi.CreateFile(splitWims[0], WimFileAccess.Read, WimCreationDisposition.OpenExisting, WimCreateFileOptions.None, WimCompressionType.None)) - { - WimgApi.SetTemporaryPath(wimHandle, TempPath); + using WimHandle wimHandle = WimgApi.CreateFile(splitWims[0], WimFileAccess.Read, WimCreationDisposition.OpenExisting, WimCreateFileOptions.None, WimCompressionType.None); + WimgApi.SetTemporaryPath(wimHandle, TempPath); - foreach (string referenceFile in splitWims.Skip(1)) - { - WimgApi.SetReferenceFile(wimHandle, referenceFile, WimSetReferenceMode.Append, WimSetReferenceOptions.None); - } + foreach (string referenceFile in splitWims.Skip(1)) + { + WimgApi.SetReferenceFile(wimHandle, referenceFile, WimSetReferenceMode.Append, WimSetReferenceOptions.None); } } diff --git a/src/Microsoft.Wim.Tests/TestWimTemplate.cs b/src/Microsoft.Wim.Tests/TestWimTemplate.cs index 1859a71..0921a1b 100644 --- a/src/Microsoft.Wim.Tests/TestWimTemplate.cs +++ b/src/Microsoft.Wim.Tests/TestWimTemplate.cs @@ -39,12 +39,10 @@ public static void CreateTestFiles(string path, int fileCount = FileCount, int l { string filePath = Path.Combine(path, $"TestFile{Guid.NewGuid()}.txt"); - using (StreamWriter fs = File.CreateText(filePath)) + using StreamWriter fs = File.CreateText(filePath); + for (int x = 0; x < lineCount; x++) { - for (int x = 0; x < lineCount; x++) - { - fs.WriteLine(Guid.NewGuid().ToString()); - } + fs.WriteLine(Guid.NewGuid().ToString()); } } } @@ -70,9 +68,7 @@ private string CaptureTemplateImage(string filename, string capturePath, WimCrea for (int i = 0; i < ImageCount; i++) { // ReSharper disable once UnusedVariable - using (WimHandle imageHandle = WimgApi.CaptureImage(wimHandle, capturePath, WimCaptureImageOptions.DisableDirectoryAcl | WimCaptureImageOptions.DisableFileAcl | WimCaptureImageOptions.DisableRPFix)) - { - } + using WimHandle imageHandle = WimgApi.CaptureImage(wimHandle, capturePath, WimCaptureImageOptions.DisableDirectoryAcl | WimCaptureImageOptions.DisableFileAcl | WimCaptureImageOptions.DisableRPFix); } XmlDocument? xmlDocument = WimgApi.GetImageInformationAsXmlDocument(wimHandle); diff --git a/src/Microsoft.Wim/ExtensionMethods.cs b/src/Microsoft.Wim/ExtensionMethods.cs new file mode 100644 index 0000000..842e849 --- /dev/null +++ b/src/Microsoft.Wim/ExtensionMethods.cs @@ -0,0 +1,45 @@ +// Copyright (c). All rights reserved. +// +// Licensed under the MIT license. + +using System; +#if NET5_0_OR_GREATER +using System.Diagnostics.CodeAnalysis; +#endif +using System.Runtime.InteropServices; + +namespace Microsoft.Wim +{ + internal static class ExtensionMethods + { + /// + /// Marshals data from an unmanaged block of memory to a newly allocated managed object of the specified type. + /// + /// The System.Type of object to be created. This type object must represent a formatted class or a structure. + /// A pointer to an unmanaged block of memory. + /// A managed object containing the data pointed to by the ptr parameter. + /// The T parameter layout is not sequential or explicit. + /// -or- + /// The T parameter is a generic type. +#if NET5_0_OR_GREATER + public static T? ToStructure<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] T>(this IntPtr ptr) +#else + + public static T? ToStructure(this IntPtr ptr) +#endif + { +#if NET5_0_OR_GREATER + return Marshal.PtrToStructure(ptr); +#else + object? result = Marshal.PtrToStructure(ptr, typeof(T)); + + if (result is null) + { + return default; + } + + return (T)result; +#endif + } + } +} diff --git a/src/Microsoft.Wim/Microsoft.Wim.csproj b/src/Microsoft.Wim/Microsoft.Wim.csproj index ec0ac32..1399551 100644 --- a/src/Microsoft.Wim/Microsoft.Wim.csproj +++ b/src/Microsoft.Wim/Microsoft.Wim.csproj @@ -1,9 +1,17 @@  - net40;netstandard2.0 + net40;netstandard2.0;net8.0 true true ..\..\artifacts\$(MSBuildProjectName) Managed Windows Imaging API (WIMGAPI) + README.md + true + + + + + + \ No newline at end of file diff --git a/src/Microsoft.Wim/WimFileInfo.cs b/src/Microsoft.Wim/WimFileInfo.cs index 8e2d732..1ace189 100644 --- a/src/Microsoft.Wim/WimFileInfo.cs +++ b/src/Microsoft.Wim/WimFileInfo.cs @@ -42,7 +42,7 @@ internal WimFileInfo(string fullPath, WimgApi.WIN32_FIND_DATA findData) /// The full path to the file or directory. /// A pointer to a containing information about the file or directory. internal WimFileInfo(string fullPath, IntPtr findDataPtr) - : this(fullPath, (WimgApi.WIN32_FIND_DATA)Marshal.PtrToStructure(findDataPtr, typeof(WimgApi.WIN32_FIND_DATA))) + : this(fullPath, findDataPtr.ToStructure()) { } @@ -64,7 +64,7 @@ internal WimFileInfo(string fullPath, IntPtr findDataPtr) /// /// Gets a string representing the directory's full path. /// - public string DirectoryName => Path.GetDirectoryName(FullName); + public string DirectoryName => Path.GetDirectoryName(FullName)!; /// /// Gets the string representing the extension part of the file. diff --git a/src/Microsoft.Wim/WimHandle.cs b/src/Microsoft.Wim/WimHandle.cs index d679c4b..f97a004 100644 --- a/src/Microsoft.Wim/WimHandle.cs +++ b/src/Microsoft.Wim/WimHandle.cs @@ -79,7 +79,6 @@ internal WimHandle() } /// - [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] protected override bool ReleaseHandle() { return !IsInvalid && WimgApi.CloseHandle(handle); diff --git a/src/Microsoft.Wim/WimInfo.cs b/src/Microsoft.Wim/WimInfo.cs index 9061dd6..3d63e7e 100644 --- a/src/Microsoft.Wim/WimInfo.cs +++ b/src/Microsoft.Wim/WimInfo.cs @@ -64,7 +64,7 @@ public sealed class WimInfo /// /// A pointer to a native struct. internal WimInfo(IntPtr wimInfoPtr) - : this((WimgApi.WIM_INFO)Marshal.PtrToStructure(wimInfoPtr, typeof(WimgApi.WIM_INFO))) + : this(wimInfoPtr.ToStructure()) { } diff --git a/src/Microsoft.Wim/WimMountInfo.cs b/src/Microsoft.Wim/WimMountInfo.cs index ed2f43f..77960e6 100644 --- a/src/Microsoft.Wim/WimMountInfo.cs +++ b/src/Microsoft.Wim/WimMountInfo.cs @@ -76,7 +76,7 @@ public sealed class WimMountInfo /// /// A pointer to a native struct. internal WimMountInfo(IntPtr wimMountInfoPtr) - : this((WimgApi.WIM_MOUNT_INFO_LEVEL1)Marshal.PtrToStructure(wimMountInfoPtr, typeof(WimgApi.WIM_MOUNT_INFO_LEVEL1))) + : this(wimMountInfoPtr.ToStructure()) { } @@ -128,13 +128,10 @@ public static WimMountInfo GetMountInfo(string mountPath) try { // Get a mounted image handle - // - // ReSharper disable once UnusedVariable - using (WimHandle wimHandle = WimgApi.GetMountedImageHandle(mountPath, true, out imageHandle)) - { - // Return the mounted image info from the handle - return WimgApi.GetMountedImageInfoFromHandle(imageHandle); - } + using WimHandle wimHandle = WimgApi.GetMountedImageHandle(mountPath, true, out imageHandle); + + // Return the mounted image info from the handle + return WimgApi.GetMountedImageInfoFromHandle(imageHandle); } finally { diff --git a/src/Microsoft.Wim/WimgApi.GetAttributes.cs b/src/Microsoft.Wim/WimgApi.GetAttributes.cs index 8f1f244..0007275 100644 --- a/src/Microsoft.Wim/WimgApi.GetAttributes.cs +++ b/src/Microsoft.Wim/WimgApi.GetAttributes.cs @@ -28,7 +28,11 @@ public static WimInfo GetAttributes(WimHandle wimHandle) } // Calculate the size of the buffer needed +#if NET5_0_OR_GREATER + uint wimInfoSize = (DWORD)Marshal.SizeOf(); +#else uint wimInfoSize = (DWORD)Marshal.SizeOf(typeof(WimgApi.WIM_INFO)); +#endif // Allocate a buffer to receive the native struct IntPtr wimInfoPtr = Marshal.AllocHGlobal((int)wimInfoSize); diff --git a/src/Microsoft.Wim/WimgApi.GetImageInformation.cs b/src/Microsoft.Wim/WimgApi.GetImageInformation.cs index a82e7bd..02d44be 100644 --- a/src/Microsoft.Wim/WimgApi.GetImageInformation.cs +++ b/src/Microsoft.Wim/WimgApi.GetImageInformation.cs @@ -37,13 +37,9 @@ public static partial class WimgApi DtdProcessing = DtdProcessing.Prohibit, }; - using (StringReader stringReader = new StringReader(xml)) - { - using (XmlReader xmlReader = XmlReader.Create(stringReader, xmlReaderSettings)) - { - return new XPathDocument(xmlReader); - } - } + using StringReader stringReader = new StringReader(xml); + using XmlReader xmlReader = XmlReader.Create(stringReader, xmlReaderSettings); + return new XPathDocument(xmlReader); } /// @@ -120,13 +116,11 @@ public static partial class WimgApi using (StringReader stringReader = new StringReader(xml)) { - using (XmlReader xmlReader = new XmlTextReader(stringReader) + using XmlReader xmlReader = new XmlTextReader(stringReader) { DtdProcessing = DtdProcessing.Prohibit, - }) - { - xmlDocument.Load(xmlReader); - } + }; + xmlDocument.Load(xmlReader); } return xmlDocument; diff --git a/src/Microsoft.Wim/WimgApi.GetMountedImageInfoFromHandle.cs b/src/Microsoft.Wim/WimgApi.GetMountedImageInfoFromHandle.cs index 725b877..af94283 100644 --- a/src/Microsoft.Wim/WimgApi.GetMountedImageInfoFromHandle.cs +++ b/src/Microsoft.Wim/WimgApi.GetMountedImageInfoFromHandle.cs @@ -28,7 +28,11 @@ public static WimMountInfo GetMountedImageInfoFromHandle(WimHandle imageHandle) } // Calculate the size of the buffer needed +#if NET5_0_OR_GREATER + int mountInfoSize = Marshal.SizeOf(); +#else int mountInfoSize = Marshal.SizeOf(typeof(WimgApi.WIM_MOUNT_INFO_LEVEL1)); +#endif // Allocate a buffer for the native function IntPtr mountInfoPtr = Marshal.AllocHGlobal(mountInfoSize); diff --git a/src/Microsoft.Wim/WimgApi.SetImageInformation.cs b/src/Microsoft.Wim/WimgApi.SetImageInformation.cs index 6945bae..b5b7cbc 100644 --- a/src/Microsoft.Wim/WimgApi.SetImageInformation.cs +++ b/src/Microsoft.Wim/WimgApi.SetImageInformation.cs @@ -33,7 +33,7 @@ public static void SetImageInformation(WimHandle wimHandle, IXPathNavigable imag throw new ArgumentNullException(nameof(imageInfoXml)); } - SetImageInformation(wimHandle, imageInfoXml.CreateNavigator().OuterXml); + SetImageInformation(wimHandle, imageInfoXml.CreateNavigator()!.OuterXml); } /// diff --git a/src/Microsoft.Wim/WimgApi.SplitFile.cs b/src/Microsoft.Wim/WimgApi.SplitFile.cs index 9d49e01..1e10345 100644 --- a/src/Microsoft.Wim/WimgApi.SplitFile.cs +++ b/src/Microsoft.Wim/WimgApi.SplitFile.cs @@ -37,8 +37,6 @@ public static void SplitFile(WimHandle wimHandle, string partPath, long partSize } // See if the directory of partPath does not exist - // - // ReSharper disable once AssignNullToNotNullAttribute if (!Directory.Exists(Path.GetDirectoryName(partPath))) { throw new DirectoryNotFoundException($"Could not find part of the path '{Path.GetDirectoryName(partPath)}'"); diff --git a/stylecop.json b/stylecop.json deleted file mode 100644 index 5f5b9d0..0000000 --- a/stylecop.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", - "settings": { - "orderingRules": { - "usingDirectivesPlacement": "outsideNamespace", - "systemUsingDirectivesFirst": false, - "blankLinesBetweenUsingGroups": "require" - }, - "documentationRules": { - "companyName": "", - "copyrightText": "Copyright (c). All rights reserved.\n\nLicensed under the MIT license.", - "xmlHeader": false - } - } -} \ No newline at end of file diff --git a/version.json b/version.json index d482d18..cb21b94 100644 --- a/version.json +++ b/version.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", - "version": "1.3", + "version": "2.0", "assemblyVersion": "1.0", "publicReleaseRefSpec": [ "^refs/heads/main",