Skip to content
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

Loss of Executable Bit When Extracting Archive with Unzip Task #9949

Open
pepone opened this issue Mar 28, 2024 · 4 comments
Open

Loss of Executable Bit When Extracting Archive with Unzip Task #9949

pepone opened this issue Mar 28, 2024 · 4 comments
Assignees
Labels
author-responded Author responded, needs response from dev team.

Comments

@pepone
Copy link

pepone commented Mar 28, 2024

I encountered an issue while using the Unzip task in the .NET MSBuild repository. I attempted to extract an archive containing executables for macOS and Linux, intending to use these executables in a subsequent task. However, the execution failed due to the loss of the executable bit during the extraction process.

Upon extracting the archive using the Unzip task, I observed that the permissions of the extracted files were not preserved correctly. Comparing the permissions of the extracted files with those extracted using the unzip -x command revealed a discrepancy:

  • Files extracted with unzip -x retained the executable bit (-r-xr-xr-x permissions).
  • Files extracted with the Unzip MSBuild task had incorrect permissions (-rw-r--r--).

I conducted further testing using .NET 8, specifically System.IO.Compression.ZipFile.ExtractToDirectory, which correctly preserved the executable bit.

I found a related fix in the .NET repository that addresses this issue: dotnet/runtime#55531.

@AR-May
Copy link
Member

AR-May commented Apr 2, 2024

Team triage: What version of the dotnet sdk are you using (you can use dotnet --version to see the version)?

@AR-May AR-May added the needs-more-info Issues that need more info to continue investigation. label Apr 2, 2024
@pepone
Copy link
Author

pepone commented Apr 2, 2024

I tested with 8.0.101

@dotnet-policy-service dotnet-policy-service bot added author-responded Author responded, needs response from dev team. and removed triaged needs-more-info Issues that need more info to continue investigation. labels Apr 2, 2024
@AR-May
Copy link
Member

AR-May commented Apr 2, 2024

@JaynieBai can you please take a look at this, try to repro/investigate this?

@KalleOlaviNiemitalo
Copy link

The Unzip task does not use ZipFile.ExtractToDirectory; instead it enumerates the zip entries and creates the output files by calling File.Open:

msbuild/src/Tasks/Unzip.cs

Lines 231 to 234 in e857a4e

using (Stream destination = File.Open(destinationPath.FullName, FileMode.Create, FileAccess.Write, FileShare.None))
using (Stream stream = zipArchiveEntry.Open())
{
stream.CopyToAsync(destination, _DefaultCopyBufferSize, _cancellationToken.Token)

I presume this is to allow the task to be canceled, and to let the task log each extracted file to MSBuild.

System.IO.Compression.ZipFileExtensions.ExtractToFile uses the FileStream(string, FileStreamOptions) constructor that lets it specify FileStreamOptions.UnixCreateMode: https://github.com/dotnet/runtime/blob/5535e31a712343a63f5d7d796cd874e563e5ac14/src/libraries/System.IO.Compression.ZipFile/src/System/IO/Compression/ZipFileExtensions.ZipArchiveEntry.Extract.cs#L81-L91

As ZipFileExtensions.ExtractToFile is a public API, I imagine the Unzip task could switch to using that. It's available on .NET Framework too. Because .NET Runtime does not restore execution permissions on Windows, it's OK that .NET Framework won't do that either. However, because there is no ZipFileExtensions.ExtractToFileAsync, this change would lose the ability to cancel the Unzip task during the extraction of a large file. Cancellation would still work between files.

Perhaps then, it's better to start using FileStream(string, FileStreamOptions) in the Unzip task and add #if to skip that on .NET Framework.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author-responded Author responded, needs response from dev team.
Projects
None yet
Development

No branches or pull requests

4 participants