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

Make the archives created on Unix have the read permissions by default #70735

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.IO.Compression
{
internal static partial class ZipArchiveEntryConstants
{
/// <summary>
/// The default external file attributes are used to support zip archives on multiple platforms.
/// </summary>
internal const UnixFileMode DefaultFileEntryPermissions = UnixFileMode.UserRead | UnixFileMode.UserWrite
| UnixFileMode.GroupRead
| UnixFileMode.OtherRead;

/// <summary>
/// The default external directory attributes are used to support zip archives on multiple platforms.
/// Directories on Unix require the execute permissions to get into them.
/// </summary>
internal const UnixFileMode DefaultDirectoryEntryPermissions = UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute
| UnixFileMode.GroupRead | UnixFileMode.GroupExecute
| UnixFileMode.OtherRead | UnixFileMode.OtherExecute;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.IO.Compression
{
internal static partial class ZipArchiveEntryConstants
{
/// <summary>
/// The default external file attributes are used to support zip archives on multiple platforms.
/// Since Windows doesn't use file permissions, there's no default value needed.
/// </summary>
internal const UnixFileMode DefaultFileEntryPermissions = UnixFileMode.None;

/// <summary>
/// The default external directory attributes are used to support zip archives on multiple platforms.
/// Since Windows doesn't use file permissions, there's no default value needed.
/// </summary>
internal const UnixFileMode DefaultDirectoryEntryPermissions = UnixFileMode.None;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == ''">
<Compile Include="System\IO\Compression\ZipFileExtensions.ZipArchive.Create.Unix.cs" />
<Compile Include="System\IO\Compression\ZipFileExtensions.ZipArchiveEntry.Extract.Unix.cs" />
<Compile Include="$(CommonPath)System\IO\Compression\ZipArchiveEntryConstants.Unix.cs" />
<Compile Include="$(CommonPath)Interop\Unix\Interop.IOErrors.cs"
Link="Common\Interop\Unix\Interop.IOErrors.cs" />
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;

using static System.IO.Compression.ZipArchiveEntryConstants;

namespace System.IO.Compression
{
public static partial class ZipFileExtensions
{
static partial void SetExternalAttributes(FileStream fs, ZipArchiveEntry entry)
{
Debug.Assert(!OperatingSystem.IsWindows());

// SetExternalAttributes is only used to overwrite the default values when reading on the unix systems
// This assert ensures that nothing else sets values different to the default before
// overwriting it with the data read from the files
Debug.Assert(entry.ExternalAttributes == ((uint)DefaultFileEntryPermissions << 16));

Interop.Sys.FileStatus status;
Interop.CheckIo(Interop.Sys.FStat(fs.SafeFileHandle, out status), fs.Name);

entry.ExternalAttributes |= status.Mode << 16;
entry.ExternalAttributes = status.Mode << 16;
derwasp marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@
<!-- Windows specific files -->
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'windows'">
<Compile Include="System\IO\Compression\ZipArchiveEntry.Windows.cs" />
<Compile Include="$(CommonPath)System\IO\Compression\ZipArchiveEntryConstants.Windows.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs"
Link="Common\Interop\Windows\Interop.Libraries.cs" />
</ItemGroup>
<!-- Unix specific files -->
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'Unix' or '$(TargetPlatformIdentifier)' == 'Browser'">
<Compile Include="System\IO\Compression\ZipArchiveEntry.Unix.cs" />
<Compile Include="$(CommonPath)System\IO\Compression\ZipArchiveEntryConstants.Unix.cs" />
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
Link="Common\Interop\Unix\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.PathConf.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Threading;
using System.Threading.Tasks;

using static System.IO.Compression.ZipArchiveEntryConstants;

namespace System.IO.Compression
{
// The disposable fields that this class owns get disposed when the ZipArchive it belongs to gets disposed
Expand Down Expand Up @@ -115,7 +117,11 @@ internal ZipArchiveEntry(ZipArchive archive, string entryName)

_compressedSize = 0; // we don't know these yet
_uncompressedSize = 0;
_externalFileAttr = 0;
UnixFileMode defaultEntryPermissions = entryName.EndsWith(Path.DirectorySeparatorChar) || entryName.EndsWith(Path.AltDirectorySeparatorChar)
? DefaultDirectoryEntryPermissions
: DefaultFileEntryPermissions;
_externalFileAttr = (uint)(defaultEntryPermissions) << 16;

_offsetOfLocalHeader = 0;
_storedOffsetOfCompressedData = null;
_crc32 = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
<Compile Include="$(CommonPath)System\Net\MultiArrayBuffer.cs" Link="ProductionCode\Common\System\Net\MultiArrayBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\StreamBuffer.cs" Link="ProductionCode\Common\System\Net\StreamBuffer.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'Unix' or '$(TargetPlatformIdentifier)' == 'Browser'">
<Compile Include="ZipArchive\zip_CreateTests.Unix.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(CommonTestPath)StreamConformanceTests\StreamConformanceTests.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading.Tasks;
using Xunit;

namespace System.IO.Compression.Tests
{
public partial class zip_CreateTests : ZipFileTestBase
{
[Theory]
[InlineData("folder/", 493 << 16)]
[InlineData("folder/file", 420 << 16)]
[InlineData("folder\\file", 420 << 16)]
public static void Verify_Default_Permissions_Are_Applied_For_Entries(string path, int permissions)
{
using var archive = new ZipArchive(new MemoryStream(), ZipArchiveMode.Create, false);
var newEntry = archive.CreateEntry(path);
Assert.Equal(newEntry.ExternalAttributes, permissions);
}
}
}